raphinesse commented on a change in pull request #448: CB-13685 android: 
Adaptive Icon Support
URL: https://github.com/apache/cordova-android/pull/448#discussion_r196332233
 
 

 ##########
 File path: spec/unit/prepare.spec.js
 ##########
 @@ -0,0 +1,935 @@
+/**
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you under the Apache License, Version 2.0 (the
+    'License'); you may not use this file except in compliance
+    with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing,
+    software distributed under the License is distributed on an
+    'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+    KIND, either express or implied.  See the License for the
+    specific language governing permissions and limitations
+    under the License.
+*/
+
+var rewire = require('rewire');
+var path = require('path');
+var CordovaError = require('cordova-common').CordovaError;
+
+const PATH_RESOURCE = path.join('platforms', 'android', 'app', 'src', 'main', 
'res');
+
+/**
+ * Creates blank resource map object, used for testing.
+ *
+ * @param {String} target specific resource item
+ */
+function createResourceMap (target) {
+    let resources = {};
+
+    [
+        'mipmap-ldpi',
+        'mipmap-mdpi',
+        'mipmap-hdpi',
+        'mipmap-xhdpi',
+        'mipmap-xxhdpi',
+        'mipmap-xxxhdpi',
+        'mipmap-ldpi-v26',
+        'mipmap-mdpi-v26',
+        'mipmap-hdpi-v26',
+        'mipmap-xhdpi-v26',
+        'mipmap-xxhdpi-v26',
+        'mipmap-xxxhdpi-v26'
+    ].forEach((mipmap) => {
+        if (!target || target === 'ic_launcher.png') 
resources[path.join(PATH_RESOURCE, mipmap, 'ic_launcher.png')] = null;
+        if (!target || target === 'ic_launcher_foreground.png') 
resources[path.join(PATH_RESOURCE, mipmap, 'ic_launcher_foreground.png')] = 
null;
+        if (!target || target === 'ic_launcher_background.png') 
resources[path.join(PATH_RESOURCE, mipmap, 'ic_launcher_background.png')] = 
null;
+        if (!target || target === 'ic_launcher_foreground.xml') 
resources[path.join(PATH_RESOURCE, mipmap, 'ic_launcher_foreground.xml')] = 
null;
+        if (!target || target === 'ic_launcher_background.xml') 
resources[path.join(PATH_RESOURCE, mipmap, 'ic_launcher_background.xml')] = 
null;
+
+        if (
+            !mipmap.includes('-v26') &&
+            (!target || target === 'ic_launcher.xml')
+        ) {
+            resources[path.join(PATH_RESOURCE, mipmap, 'ic_launcher.xml')] = 
null;
+        }
+    });
+
+    return resources;
+}
+
+describe('updateIcons method', function () {
+    // Rewire
+    let prepare;
+
+    // Spies
+    let updateIconResourceForAdaptiveSpy;
+    let updateIconResourceForLegacySpy;
+    let emitSpy;
+    let updatePathsSpy;
+
+    // Mock Data
+    let cordovaProject;
+    let platformResourcesDir;
+
+    beforeEach(function () {
+        prepare = rewire('../../bin/templates/cordova/lib/prepare');
+
+        cordovaProject = {
+            root: '/mock',
+            projectConfig: {
+                path: '/mock/config.xml',
+                cdvNamespacePrefix: 'cdv'
+            },
+            locations: {
+                plugins: '/mock/plugins',
+                www: '/mock/www'
+            }
+        };
+        platformResourcesDir = PATH_RESOURCE;
+
+        emitSpy = jasmine.createSpy('emit');
+        prepare.__set__('events', {
+            emit: emitSpy
+        });
+
+        updatePathsSpy = jasmine.createSpy('updatePaths');
+        prepare.__set__('FileUpdater', {
+            updatePaths: updatePathsSpy
+        });
+
+        // mocking initial responses for mapImageResources
+        prepare.__set__('mapImageResources', function (rootDir, subDir, type, 
resourceName) {
+            if (resourceName.includes('ic_launcher.png')) {
+                return createResourceMap('ic_launcher.png');
+            } else if (resourceName.includes('ic_launcher_foreground.png')) {
+                return createResourceMap('ic_launcher_foreground.png');
+            } else if (resourceName.includes('ic_launcher_background.png')) {
+                return createResourceMap('ic_launcher_background.png');
+            } else if (resourceName.includes('ic_launcher_foreground.xml')) {
+                return createResourceMap('ic_launcher_foreground.xml');
+            } else if (resourceName.includes('ic_launcher_background.xml')) {
+                return createResourceMap('ic_launcher_background.xml');
+            } else if (resourceName.includes('ic_launcher.xml')) {
+                return createResourceMap('ic_launcher.xml');
+            }
+        });
+    });
+
+    it('Test#001 : Should detect no defined icons.', function (done) {
+        const updateIcons = prepare.__get__('updateIcons');
+
+        // mock data.
+        cordovaProject.projectConfig.getIcons = function () {
+            return [];
+        };
+
+        updateIcons(cordovaProject, platformResourcesDir);
+
+        // The emit was called
+        expect(emitSpy).toHaveBeenCalled();
+
+        // The emit message was.
+        let actual = emitSpy.calls.argsFor(0)[1];
+        let expected = 'This app does not have launcher icons defined';
+        expect(actual).toEqual(expected);
+
+        done();
+    });
+
+    it('Test#002 : Should detech incorrect configrations for adaptive icon and 
throws error.', function (done) {
+        const updateIcons = prepare.__get__('updateIcons');
+
+        // mock data.
+        cordovaProject.projectConfig.getIcons = function () {
+            return [{
+                src: undefined,
+                target: undefined,
+                density: 'mdpi',
+                platform: 'android',
+                width: undefined,
+                height: undefined,
+                background: 'res/icon/android/mdpi-background.png',
+                foreground: undefined
+            }];
 
 Review comment:
   You have something similar to this in almost every test. Might be nice to 
factor this out e.g as a function that fills in missing props as undefined. 
This could help to slim down this 1000 lines spec.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to