2018-06-05 12:43:05 +08:00
/ * *
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 .
* /
2022-04-18 09:39:54 +08:00
const rewire = require ( 'rewire' ) ;
const path = require ( 'path' ) ;
const CordovaError = require ( 'cordova-common' ) . CordovaError ;
2021-07-13 17:01:50 +08:00
const GradlePropertiesParser = require ( '../../lib/config/GradlePropertiesParser' ) ;
2022-05-18 22:11:31 +08:00
const utils = require ( '../../lib/utils' ) ;
const et = require ( 'elementtree' ) ;
2023-04-12 13:39:47 +08:00
const MockCordovaGradleConfigParser = require ( './mocks/config/MockCordovaGradleConfigParser' ) ;
const CordovaGradleConfigParserFactory = require ( '../../lib/config/CordovaGradleConfigParserFactory' ) ;
2018-06-05 12:43:05 +08:00
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 ) {
2020-01-31 21:02:48 +08:00
const resources = { } ;
2018-06-05 12:43:05 +08:00
[
'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 ;
2023-04-10 07:41:38 +08:00
if ( ! target || target === 'ic_launcher_background.png' ) resources [ path . join ( PATH _RESOURCE , mipmap , 'ic_launcher_monochrome.png' ) ] = null ;
2018-06-05 12:43:05 +08:00
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 ;
2023-04-10 07:41:38 +08:00
if ( ! target || target === 'ic_launcher_background.xml' ) resources [ path . join ( PATH _RESOURCE , mipmap , 'ic_launcher_monochrome.xml' ) ] = null ;
2018-06-05 12:43:05 +08:00
if (
! mipmap . includes ( '-v26' ) &&
( ! target || target === 'ic_launcher.xml' )
) {
resources [ path . join ( PATH _RESOURCE , mipmap , 'ic_launcher.xml' ) ] = null ;
}
} ) ;
return resources ;
}
/ * *
* Create a mock item from the getIcon collection with the supplied updated data .
*
* @ param { Object } data Changes to apply to the mock getIcon item
* /
function mockGetIconItem ( data ) {
return Object . assign ( { } , {
src : undefined ,
target : undefined ,
density : undefined ,
platform : 'android' ,
width : undefined ,
height : undefined ,
background : undefined ,
foreground : undefined
} , data ) ;
}
2020-10-18 05:20:37 +08:00
describe ( 'prepare' , ( ) => {
2018-06-05 12:43:05 +08:00
// Rewire
2021-07-06 19:01:37 +08:00
let prepare ;
// Spies
let emitSpy ;
let updatePathsSpy ;
2023-04-12 13:39:47 +08:00
const PROJECT _DIR = 'platforms/android' ;
beforeAll ( ( ) => {
spyOn ( CordovaGradleConfigParserFactory , 'create' ) . and . returnValue ( new MockCordovaGradleConfigParser ( PROJECT _DIR ) ) ;
} ) ;
2021-07-06 19:01:37 +08:00
beforeEach ( ( ) => {
2021-07-13 17:01:50 +08:00
prepare = rewire ( '../../lib/prepare' ) ;
2020-10-18 05:20:37 +08:00
2021-07-06 19:01:37 +08:00
emitSpy = jasmine . createSpy ( 'emit' ) ;
prepare . _ _set _ _ ( 'events' , {
emit : emitSpy
} ) ;
updatePathsSpy = jasmine . createSpy ( 'updatePaths' ) ;
prepare . _ _set _ _ ( 'FileUpdater' , {
updatePaths : updatePathsSpy
} ) ;
} ) ;
describe ( 'updateIcons method' , function ( ) {
2020-10-18 05:20:37 +08:00
// Spies
let updateIconResourceForAdaptiveSpy ;
let updateIconResourceForLegacySpy ;
// Mock Data
let cordovaProject ;
let platformResourcesDir ;
beforeEach ( function ( ) {
cordovaProject = {
root : '/mock' ,
projectConfig : {
path : '/mock/config.xml' ,
cdvNamespacePrefix : 'cdv'
} ,
locations : {
plugins : '/mock/plugins' ,
www : '/mock/www'
}
} ;
platformResourcesDir = PATH _RESOURCE ;
// 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' ) ;
2023-04-10 07:41:38 +08:00
} else if ( resourceName . includes ( 'ic_launcher_monochrome.png' ) ) {
return createResourceMap ( 'ic_launcher_monochrome.png' ) ;
2020-10-18 05:20:37 +08:00
} 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' ) ;
2023-04-10 07:41:38 +08:00
} else if ( resourceName . includes ( 'ic_launcher_monochrome.xml' ) ) {
return createResourceMap ( 'ic_launcher_monochrome.xml' ) ;
2020-10-18 05:20:37 +08:00
} else if ( resourceName . includes ( 'ic_launcher.xml' ) ) {
return createResourceMap ( 'ic_launcher.xml' ) ;
}
} ) ;
2018-06-05 12:43:05 +08:00
} ) ;
2020-10-18 05:20:37 +08:00
it ( 'Test#001 : Should detect no defined icons.' , function ( ) {
const updateIcons = prepare . _ _get _ _ ( 'updateIcons' ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// mock data.
cordovaProject . projectConfig . getIcons = function ( ) {
return [ ] ;
} ;
2018-06-05 12:43:05 +08:00
updateIcons ( cordovaProject , platformResourcesDir ) ;
2020-10-18 05:20:37 +08:00
// The emit was called
expect ( emitSpy ) . toHaveBeenCalled ( ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// The emit message was.
const actual = emitSpy . calls . argsFor ( 0 ) [ 1 ] ;
const expected = 'This app does not have launcher icons defined' ;
expect ( actual ) . toEqual ( expected ) ;
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
it ( 'Test#002 : Should detech incorrect configrations for adaptive icon and throws error.' , function ( ) {
const updateIcons = prepare . _ _get _ _ ( 'updateIcons' ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// mock data.
cordovaProject . projectConfig . getIcons = function ( ) {
return [ mockGetIconItem ( {
density : 'mdpi' ,
background : 'res/icon/android/mdpi-background.png'
} ) ] ;
} ;
expect ( function ( ) {
updateIcons ( cordovaProject , platformResourcesDir ) ;
} ) . toThrow (
new CordovaError ( 'One of the following attributes are set but missing the other for the density type: mdpi. Please ensure that all require attributes are defined.' )
) ;
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
it ( 'Test#003 : Should detech incorrect configrations (missing foreground) for adaptive icon and throw an error.' , function ( ) {
const updateIcons = prepare . _ _get _ _ ( 'updateIcons' ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// mock data.
cordovaProject . projectConfig . getIcons = function ( ) {
return [ mockGetIconItem ( {
density : 'mdpi' ,
background : 'res/icon/android/mdpi-background.png'
} ) ] ;
} ;
expect ( function ( ) {
updateIcons ( cordovaProject , platformResourcesDir ) ;
} ) . toThrow (
new CordovaError ( 'One of the following attributes are set but missing the other for the density type: mdpi. Please ensure that all require attributes are defined.' )
) ;
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
it ( 'Test#004 : Should detech incorrect configrations (missing background) for adaptive icon and throw an error.' , function ( ) {
const updateIcons = prepare . _ _get _ _ ( 'updateIcons' ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// mock data.
cordovaProject . projectConfig . getIcons = function ( ) {
return [ mockGetIconItem ( {
density : 'mdpi' ,
foreground : 'res/icon/android/mdpi-foreground.png'
} ) ] ;
} ;
expect ( function ( ) {
updateIcons ( cordovaProject , platformResourcesDir ) ;
} ) . toThrow (
new CordovaError ( 'One of the following attributes are set but missing the other for the density type: mdpi. Please ensure that all require attributes are defined.' )
) ;
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
it ( 'Test#005 : Should detech incorrect configrations and throw an error.' , function ( ) {
const updateIcons = prepare . _ _get _ _ ( 'updateIcons' ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// mock data.
cordovaProject . projectConfig . getIcons = function ( ) {
return [ mockGetIconItem ( { density : 'mdpi' } ) ] ;
} ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
expect ( function ( ) {
updateIcons ( cordovaProject , platformResourcesDir ) ;
} ) . toThrow (
new CordovaError ( 'One of the following attributes are set but missing the other for the density type: mdpi. Please ensure that all require attributes are defined.' )
) ;
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
it ( 'Test#006 : Should display incorrect configuration with density in message.' , function ( ) {
const updateIcons = prepare . _ _get _ _ ( 'updateIcons' ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// mock data.
cordovaProject . projectConfig . getIcons = function ( ) {
return [ mockGetIconItem ( { density : 'mdpi' } ) ] ;
} ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
expect ( function ( ) {
updateIcons ( cordovaProject , platformResourcesDir ) ;
} ) . toThrow (
new CordovaError ( 'One of the following attributes are set but missing the other for the density type: mdpi. Please ensure that all require attributes are defined.' )
) ;
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
it ( 'Test#007 : Should display incorrect configuration with size in message from height.' , function ( ) {
const updateIcons = prepare . _ _get _ _ ( 'updateIcons' ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// mock data.
cordovaProject . projectConfig . getIcons = function ( ) {
return [ mockGetIconItem ( { height : '192' } ) ] ;
} ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
expect ( function ( ) {
updateIcons ( cordovaProject , platformResourcesDir ) ;
} ) . toThrow (
new CordovaError ( 'One of the following attributes are set but missing the other for the density type: size=192. Please ensure that all require attributes are defined.' )
) ;
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
it ( 'Test#008 : Should display incorrect configuration with size in message from width.' , function ( ) {
const updateIcons = prepare . _ _get _ _ ( 'updateIcons' ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// mock data.
cordovaProject . projectConfig . getIcons = function ( ) {
return [ mockGetIconItem ( { width : '192' } ) ] ;
} ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
expect ( function ( ) {
updateIcons ( cordovaProject , platformResourcesDir ) ;
} ) . toThrow (
new CordovaError ( 'One of the following attributes are set but missing the other for the density type: size=192. Please ensure that all require attributes are defined.' )
) ;
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
it ( 'Test#009 : Should detech incorrect configrations (missing background) for adaptive icon and throw an error.' , function ( ) {
const updateIcons = prepare . _ _get _ _ ( 'updateIcons' ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// mock data.
cordovaProject . projectConfig . getIcons = function ( ) {
return [ mockGetIconItem ( {
density : 'mdpi' ,
foreground : 'res/icon/android/mdpi-foreground.png'
} ) ] ;
} ;
expect ( function ( ) {
updateIcons ( cordovaProject , platformResourcesDir ) ;
} ) . toThrow (
new CordovaError ( 'One of the following attributes are set but missing the other for the density type: mdpi. Please ensure that all require attributes are defined.' )
) ;
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
it ( 'Test#010 : Should detech adaptive icon with vector foreground and throws error for missing backwards compatability settings.' , function ( ) {
const updateIcons = prepare . _ _get _ _ ( 'updateIcons' ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// mock data.
cordovaProject . projectConfig . getIcons = function ( ) {
return [ mockGetIconItem ( {
density : 'mdpi' ,
background : 'res/icon/android/mdpi-background.png' ,
2023-04-10 07:41:38 +08:00
foreground : 'res/icon/android/mdpi-foreground.xml' ,
monochrome : 'res/icon/android/mdpi-monochrome.png'
2020-10-18 05:20:37 +08:00
} ) ] ;
} ;
expect ( function ( ) {
updateIcons ( cordovaProject , platformResourcesDir ) ;
} ) . toThrow (
new CordovaError ( 'For the following icons with the density of: mdpi, adaptive foreground with a defined color or vector can not be used as a standard fallback icon for older Android devices. To support older Android environments, please provide a value for the src attribute.' )
) ;
2018-06-05 12:43:05 +08:00
} ) ;
2020-10-18 05:20:37 +08:00
it ( 'Test#011 : Should detech adaptive icon with color foreground and throws error for missing backwards compatability settings.' , function ( ) {
const updateIcons = prepare . _ _get _ _ ( 'updateIcons' ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// mock data.
cordovaProject . projectConfig . getIcons = function ( ) {
return [ mockGetIconItem ( {
density : 'mdpi' ,
background : 'res/icon/android/mdpi-background.png' ,
foreground : '@color/background'
} ) ] ;
} ;
expect ( function ( ) {
updateIcons ( cordovaProject , platformResourcesDir ) ;
} ) . toThrow (
new CordovaError ( 'For the following icons with the density of: mdpi, adaptive foreground with a defined color or vector can not be used as a standard fallback icon for older Android devices. To support older Android environments, please provide a value for the src attribute.' )
) ;
2018-06-05 12:43:05 +08:00
} ) ;
2020-10-18 05:20:37 +08:00
it ( 'Test#012 : Should update paths with adaptive and standard icons. Standard icon comes from adaptive foreground' , function ( ) {
const updateIcons = prepare . _ _get _ _ ( 'updateIcons' ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// mock data.
cordovaProject . projectConfig . getIcons = function ( ) {
return [ mockGetIconItem ( {
density : 'mdpi' ,
background : 'res/icon/android/mdpi-background.png' ,
2023-04-10 07:41:38 +08:00
foreground : 'res/icon/android/mdpi-foreground.png' ,
monochrome : 'res/icon/android/mdpi-monochrome.png'
2020-10-18 05:20:37 +08:00
} ) ] ;
} ;
// Creating Spies
const resourceMap = createResourceMap ( ) ;
const phaseOneModification = { } ;
phaseOneModification [ path . join ( PATH _RESOURCE , 'mipmap-mdpi-v26' , 'ic_launcher_foreground.png' ) ] = 'res/icon/android/mdpi-foreground.png' ;
phaseOneModification [ path . join ( PATH _RESOURCE , 'mipmap-mdpi-v26' , 'ic_launcher_background.png' ) ] = 'res/icon/android/mdpi-background.png' ;
2023-04-10 07:41:38 +08:00
phaseOneModification [ path . join ( PATH _RESOURCE , 'mipmap-mdpi-v26' , 'ic_launcher_monochrome.png' ) ] = 'res/icon/android/mdpi-monochrome.png' ;
2020-10-18 05:20:37 +08:00
const phaseOneUpdatedIconsForAdaptive = Object . assign ( { } , resourceMap , phaseOneModification ) ;
updateIconResourceForAdaptiveSpy = jasmine . createSpy ( 'updateIconResourceForAdaptiveSpy' ) ;
prepare . _ _set _ _ ( 'updateIconResourceForAdaptive' , function ( preparedIcons , resourceMap , platformResourcesDir ) {
updateIconResourceForAdaptiveSpy ( ) ;
return phaseOneUpdatedIconsForAdaptive ;
} ) ;
const phaseTwoModification = { } ;
phaseTwoModification [ path . join ( PATH _RESOURCE , 'mipmap-mdpi' , 'ic_launcher.png' ) ] = 'res/icon/android/mdpi-foreground.png' ;
phaseTwoModification [ path . join ( PATH _RESOURCE , 'mipmap-mdpi-v26' , 'ic_launcher_background.png' ) ] = 'res/icon/android/mdpi-background.png' ;
2023-04-10 07:41:38 +08:00
phaseTwoModification [ path . join ( PATH _RESOURCE , 'mipmap-mdpi-v26' , 'ic_launcher_monochrome.png' ) ] = 'res/icon/android/mdpi-monochrome.png' ;
2020-10-18 05:20:37 +08:00
const phaseTwoUpdatedIconsForLegacy = Object . assign ( { } , phaseOneUpdatedIconsForAdaptive , phaseTwoModification ) ;
updateIconResourceForLegacySpy = jasmine . createSpy ( 'updateIconResourceForLegacySpy' ) ;
prepare . _ _set _ _ ( 'updateIconResourceForLegacy' , function ( preparedIcons , resourceMap , platformResourcesDir ) {
updateIconResourceForLegacySpy ( ) ;
return phaseTwoUpdatedIconsForLegacy ;
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
updateIcons ( cordovaProject , platformResourcesDir ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// The emit was called
expect ( emitSpy ) . toHaveBeenCalled ( ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// The emit message was.
const actual = emitSpy . calls . argsFor ( 0 ) [ 1 ] ;
const expected = 'Updating icons at ' + PATH _RESOURCE ;
expect ( actual ) . toEqual ( expected ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// Expected to be called.
expect ( updatePathsSpy ) . toHaveBeenCalled ( ) ;
expect ( updateIconResourceForAdaptiveSpy ) . toHaveBeenCalled ( ) ;
expect ( updateIconResourceForLegacySpy ) . toHaveBeenCalled ( ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
const actualResourceMap = updatePathsSpy . calls . argsFor ( 0 ) [ 0 ] ;
const expectedResourceMap = phaseTwoUpdatedIconsForLegacy ;
expect ( actualResourceMap ) . toEqual ( expectedResourceMap ) ;
2018-06-05 12:43:05 +08:00
} ) ;
2020-10-18 05:20:37 +08:00
it ( 'Test#013 : Should update paths with adaptive and standard icons.' , function ( ) {
const updateIcons = prepare . _ _get _ _ ( 'updateIcons' ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// mock data.
cordovaProject . projectConfig . getIcons = function ( ) {
return [ mockGetIconItem ( {
density : 'mdpi' ,
src : 'res/icon/android/mdpi-icon.png' ,
background : 'res/icon/android/mdpi-background.png' ,
2023-04-10 07:41:38 +08:00
foreground : 'res/icon/android/mdpi-foreground.png' ,
monochrome : 'res/icon/android/mdpi-monochrome.png'
2020-10-18 05:20:37 +08:00
} ) ] ;
} ;
// Creating Spies
const resourceMap = createResourceMap ( ) ;
const phaseOneModification = { } ;
phaseOneModification [ path . join ( PATH _RESOURCE , 'mipmap-mdpi-v26' , 'ic_launcher_foreground.png' ) ] = 'res/icon/android/mdpi-foreground.png' ;
phaseOneModification [ path . join ( PATH _RESOURCE , 'mipmap-mdpi-v26' , 'ic_launcher_background.png' ) ] = 'res/icon/android/mdpi-background.png' ;
2023-04-10 07:41:38 +08:00
phaseOneModification [ path . join ( PATH _RESOURCE , 'mipmap-mdpi-v26' , 'ic_launcher_monochrome.png' ) ] = 'res/icon/android/mdpi-monochrome.png' ;
2020-10-18 05:20:37 +08:00
const phaseOneUpdatedIconsForAdaptive = Object . assign ( { } , resourceMap , phaseOneModification ) ;
updateIconResourceForAdaptiveSpy = jasmine . createSpy ( 'updateIconResourceForAdaptiveSpy' ) ;
prepare . _ _set _ _ ( 'updateIconResourceForAdaptive' , function ( preparedIcons , resourceMap , platformResourcesDir ) {
updateIconResourceForAdaptiveSpy ( ) ;
return phaseOneUpdatedIconsForAdaptive ;
} ) ;
const phaseTwoModification = { } ;
phaseTwoModification [ path . join ( PATH _RESOURCE , 'mipmap-mdpi' , 'ic_launcher.png' ) ] = 'res/icon/android/mdpi-foreground.png' ;
phaseTwoModification [ path . join ( PATH _RESOURCE , 'mipmap-mdpi-v26' , 'ic_launcher_background.png' ) ] = 'res/icon/android/mdpi-background.png' ;
2023-04-10 07:41:38 +08:00
phaseTwoModification [ path . join ( PATH _RESOURCE , 'mipmap-mdpi-v26' , 'ic_launcher_monochrome.png' ) ] = 'res/icon/android/mdpi-monochrome.png' ;
2020-10-18 05:20:37 +08:00
const phaseTwoUpdatedIconsForLegacy = Object . assign ( { } , phaseOneUpdatedIconsForAdaptive , phaseTwoModification ) ;
updateIconResourceForLegacySpy = jasmine . createSpy ( 'updateIconResourceForLegacySpy' ) ;
prepare . _ _set _ _ ( 'updateIconResourceForLegacy' , function ( preparedIcons , resourceMap , platformResourcesDir ) {
updateIconResourceForLegacySpy ( ) ;
return phaseTwoUpdatedIconsForLegacy ;
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
updateIcons ( cordovaProject , platformResourcesDir ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// The emit was called
expect ( emitSpy ) . toHaveBeenCalled ( ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// The emit message was.
const actual = emitSpy . calls . argsFor ( 0 ) [ 1 ] ;
const expected = 'Updating icons at ' + PATH _RESOURCE ;
expect ( actual ) . toEqual ( expected ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// Expected to be called.
expect ( updatePathsSpy ) . toHaveBeenCalled ( ) ;
expect ( updateIconResourceForAdaptiveSpy ) . toHaveBeenCalled ( ) ;
expect ( updateIconResourceForLegacySpy ) . toHaveBeenCalled ( ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
const actualResourceMap = updatePathsSpy . calls . argsFor ( 0 ) [ 0 ] ;
const expectedResourceMap = phaseTwoUpdatedIconsForLegacy ;
expect ( actualResourceMap ) . toEqual ( expectedResourceMap ) ;
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
it ( 'Test#014 : Should update paths with standard icons.' , function ( ) {
const updateIcons = prepare . _ _get _ _ ( 'updateIcons' ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// mock data.
cordovaProject . projectConfig . getIcons = function ( ) {
return [ mockGetIconItem ( {
density : 'mdpi' ,
src : 'res/icon/android/mdpi-icon.png'
} ) ] ;
} ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// Creating Spies
const phaseOneUpdatedIconsForAdaptive = createResourceMap ( ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
updateIconResourceForAdaptiveSpy = jasmine . createSpy ( 'updateIconResourceForAdaptiveSpy' ) ;
prepare . _ _set _ _ ( 'updateIconResourceForAdaptive' , function ( preparedIcons , resourceMap , platformResourcesDir ) {
updateIconResourceForAdaptiveSpy ( ) ;
return phaseOneUpdatedIconsForAdaptive ;
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
const phaseTwoModification = { } ;
phaseTwoModification [ path . join ( PATH _RESOURCE , 'mipmap-mdpi' , 'ic_launcher.png' ) ] = 'res/icon/android/mdpi-icon.png' ;
const phaseTwoUpdatedIconsForLegacy = Object . assign ( { } , phaseOneUpdatedIconsForAdaptive , phaseTwoModification ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
updateIconResourceForLegacySpy = jasmine . createSpy ( 'updateIconResourceForLegacySpy' ) ;
prepare . _ _set _ _ ( 'updateIconResourceForLegacy' , function ( preparedIcons , resourceMap , platformResourcesDir ) {
updateIconResourceForLegacySpy ( ) ;
return phaseTwoUpdatedIconsForLegacy ;
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
updateIcons ( cordovaProject , platformResourcesDir ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// The emit was called
expect ( emitSpy ) . toHaveBeenCalled ( ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// The emit message was.
const actual = emitSpy . calls . argsFor ( 0 ) [ 1 ] ;
const expected = 'Updating icons at ' + PATH _RESOURCE ;
expect ( actual ) . toEqual ( expected ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// Expected to be called.
expect ( updatePathsSpy ) . toHaveBeenCalled ( ) ;
expect ( updateIconResourceForAdaptiveSpy ) . not . toHaveBeenCalled ( ) ;
expect ( updateIconResourceForLegacySpy ) . toHaveBeenCalled ( ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
const actualResourceMap = updatePathsSpy . calls . argsFor ( 0 ) [ 0 ] ;
const expectedResourceMap = phaseTwoUpdatedIconsForLegacy ;
expect ( actualResourceMap ) . toEqual ( expectedResourceMap ) ;
} ) ;
2018-06-05 12:43:05 +08:00
} ) ;
2020-10-18 05:20:37 +08:00
describe ( 'prepareIcons method' , function ( ) {
let prepareIcons ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
beforeEach ( function ( ) {
prepareIcons = prepare . _ _get _ _ ( 'prepareIcons' ) ;
2018-06-05 12:43:05 +08:00
} ) ;
2020-10-18 05:20:37 +08:00
it ( 'Test#001 : should emit extra default icon found for adaptive use case.' , function ( ) {
2018-06-05 12:43:05 +08:00
// mock data.
2020-10-18 05:20:37 +08:00
const ldpi = mockGetIconItem ( {
density : 'ldpi' ,
background : 'res/icon/android/ldpi-background.png' ,
2023-04-10 07:41:38 +08:00
foreground : 'res/icon/android/ldpi-foreground.png' ,
monochrome : 'res/icon/android/ldpi-monochrome.png'
2020-10-18 05:20:37 +08:00
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
const mdpi = mockGetIconItem ( {
density : 'mdpi' ,
background : 'res/icon/android/mdpi-background.png' ,
2023-04-10 07:41:38 +08:00
foreground : 'res/icon/android/mdpi-foreground.png' ,
monochrome : 'res/icon/android/mdpi-monochrome.png'
2020-10-18 05:20:37 +08:00
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
const icons = [ ldpi , mdpi ] ;
const actual = prepareIcons ( icons ) ;
const expected = {
android _icons : { ldpi , mdpi } ,
default _icon : undefined
} ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
expect ( expected ) . toEqual ( actual ) ;
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
it ( 'Test#002 : should emit extra default icon found for legacy use case.' , function ( ) {
2018-06-05 12:43:05 +08:00
// mock data.
2020-10-18 05:20:37 +08:00
const ldpi = mockGetIconItem ( {
src : 'res/icon/android/ldpi-icon.png' ,
density : 'ldpi'
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
const mdpi = mockGetIconItem ( {
src : 'res/icon/android/mdpi-icon.png' ,
density : 'mdpi'
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
const icons = [ ldpi , mdpi ] ;
const actual = prepareIcons ( icons ) ;
const expected = {
android _icons : { ldpi , mdpi } ,
default _icon : undefined
} ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
expect ( expected ) . toEqual ( actual ) ;
} ) ;
2018-06-05 12:43:05 +08:00
} ) ;
2020-10-18 05:20:37 +08:00
describe ( 'updateIconResourceForLegacy method' , function ( ) {
// Spies
let fsWriteFileSyncSpy ;
// Mock Data
let platformResourcesDir ;
let preparedIcons ;
let resourceMap ;
beforeEach ( function ( ) {
// Mocked Data
platformResourcesDir = PATH _RESOURCE ;
preparedIcons = {
android _icons : {
mdpi : mockGetIconItem ( {
src : 'res/icon/android/mdpi-icon.png' ,
density : 'mdpi'
} )
} ,
default _icon : undefined
} ;
resourceMap = createResourceMap ( ) ;
fsWriteFileSyncSpy = jasmine . createSpy ( 'writeFileSync' ) ;
prepare . _ _set _ _ ( 'fs' , {
writeFileSync : fsWriteFileSyncSpy
} ) ;
2018-06-05 12:43:05 +08:00
} ) ;
2020-10-18 05:20:37 +08:00
it ( 'Test#001 : Should update resource map with prepared icons.' , function ( ) {
2018-06-05 12:43:05 +08:00
// Get method for testing
2020-10-18 05:20:37 +08:00
const updateIconResourceForLegacy = prepare . _ _get _ _ ( 'updateIconResourceForLegacy' ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// Run Test
const expectedModification = { } ;
expectedModification [ path . join ( PATH _RESOURCE , 'mipmap-mdpi' , 'ic_launcher.png' ) ] = 'res/icon/android/mdpi-icon.png' ;
const expected = Object . assign ( { } , resourceMap , expectedModification ) ;
const actual = updateIconResourceForLegacy ( preparedIcons , resourceMap , platformResourcesDir ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
expect ( actual ) . toEqual ( expected ) ;
} ) ;
2018-06-05 12:43:05 +08:00
} ) ;
2020-10-18 05:20:37 +08:00
describe ( 'updateIconResourceForAdaptive method' , function ( ) {
// Spies
let fsWriteFileSyncSpy ;
// Mock Data
let platformResourcesDir ;
let preparedIcons ;
let resourceMap ;
2023-07-14 00:18:54 +08:00
describe ( 'without monochrome' , ( ) => {
beforeEach ( function ( ) {
// Mocked Data
platformResourcesDir = PATH _RESOURCE ;
preparedIcons = {
android _icons : {
mdpi : mockGetIconItem ( {
density : 'mdpi' ,
background : 'res/icon/android/mdpi-background.png' ,
foreground : 'res/icon/android/mdpi-foreground.png'
} )
} ,
default _icon : undefined
} ;
resourceMap = createResourceMap ( ) ;
fsWriteFileSyncSpy = jasmine . createSpy ( 'writeFileSync' ) ;
prepare . _ _set _ _ ( 'fs' , {
writeFileSync : fsWriteFileSyncSpy
} ) ;
} ) ;
2020-10-18 05:20:37 +08:00
2023-07-14 00:18:54 +08:00
it ( 'Test#001 : Should update resource map with prepared icons.' , function ( ) {
// Get method for testing
const updateIconResourceForAdaptive = prepare . _ _get _ _ ( 'updateIconResourceForAdaptive' ) ;
2020-10-18 05:20:37 +08:00
2023-07-14 00:18:54 +08:00
// Run Test
const expectedModification = { } ;
expectedModification [ path . join ( PATH _RESOURCE , 'mipmap-mdpi-v26' , 'ic_launcher_background.png' ) ] = 'res/icon/android/mdpi-background.png' ;
expectedModification [ path . join ( PATH _RESOURCE , 'mipmap-mdpi-v26' , 'ic_launcher_foreground.png' ) ] = 'res/icon/android/mdpi-foreground.png' ;
const expected = Object . assign ( { } , resourceMap , expectedModification ) ;
const actual = updateIconResourceForAdaptive ( preparedIcons , resourceMap , platformResourcesDir ) ;
expect ( actual ) . toEqual ( expected ) ;
2020-10-18 05:20:37 +08:00
} ) ;
} ) ;
2018-06-05 12:43:05 +08:00
2023-07-14 00:18:54 +08:00
describe ( 'with monochrome' , ( ) => {
beforeEach ( function ( ) {
// Mocked Data
platformResourcesDir = PATH _RESOURCE ;
preparedIcons = {
android _icons : {
mdpi : mockGetIconItem ( {
density : 'mdpi' ,
background : 'res/icon/android/mdpi-background.png' ,
foreground : 'res/icon/android/mdpi-foreground.png' ,
monochrome : 'res/icon/android/mdpi-monochrome.png'
} )
} ,
default _icon : undefined
} ;
resourceMap = createResourceMap ( ) ;
fsWriteFileSyncSpy = jasmine . createSpy ( 'writeFileSync' ) ;
prepare . _ _set _ _ ( 'fs' , {
writeFileSync : fsWriteFileSyncSpy
} ) ;
} ) ;
2018-06-05 12:43:05 +08:00
2023-07-14 00:18:54 +08:00
it ( 'Test#002 : Should update resource map with prepared icons.' , function ( ) {
// Get method for testing
const updateIconResourceForAdaptive = prepare . _ _get _ _ ( 'updateIconResourceForAdaptive' ) ;
2018-06-05 12:43:05 +08:00
2023-07-14 00:18:54 +08:00
// Run Test
const expectedModification = { } ;
expectedModification [ path . join ( PATH _RESOURCE , 'mipmap-mdpi-v26' , 'ic_launcher_background.png' ) ] = 'res/icon/android/mdpi-background.png' ;
expectedModification [ path . join ( PATH _RESOURCE , 'mipmap-mdpi-v26' , 'ic_launcher_foreground.png' ) ] = 'res/icon/android/mdpi-foreground.png' ;
expectedModification [ path . join ( PATH _RESOURCE , 'mipmap-mdpi-v26' , 'ic_launcher_monochrome.png' ) ] = 'res/icon/android/mdpi-monochrome.png' ;
2018-06-05 12:43:05 +08:00
2023-07-14 00:18:54 +08:00
const expected = Object . assign ( { } , resourceMap , expectedModification ) ;
const actual = updateIconResourceForAdaptive ( preparedIcons , resourceMap , platformResourcesDir ) ;
expect ( actual ) . toEqual ( expected ) ;
} ) ;
it ( 'Test#003 : should emit if monochrome is supplied without adaptive background' , ( ) => {
const updateIconResourceForAdaptive = prepare . _ _get _ _ ( 'updateIconResourceForAdaptive' ) ;
preparedIcons . android _icons . mdpi . background = undefined ;
updateIconResourceForAdaptive ( preparedIcons , resourceMap , platformResourcesDir ) ;
const actualEmitArgs = emitSpy . calls . mostRecent ( ) . args ;
expect ( actualEmitArgs [ 0 ] ) . toBe ( 'warn' ) ;
expect ( actualEmitArgs [ 1 ] ) . toMatch ( /Monochrome icon found but without adaptive properties./g ) ;
} ) ;
it ( 'Test#004 : should emit if monochrome is supplied without adaptive foreground' , ( ) => {
const updateIconResourceForAdaptive = prepare . _ _get _ _ ( 'updateIconResourceForAdaptive' ) ;
preparedIcons . android _icons . mdpi . foreground = undefined ;
updateIconResourceForAdaptive ( preparedIcons , resourceMap , platformResourcesDir ) ;
const actualEmitArgs = emitSpy . calls . mostRecent ( ) . args ;
expect ( actualEmitArgs [ 0 ] ) . toBe ( 'warn' ) ;
expect ( actualEmitArgs [ 1 ] ) . toMatch ( /Monochrome icon found but without adaptive properties./g ) ;
} ) ;
2018-06-05 12:43:05 +08:00
} ) ;
} ) ;
2020-10-18 05:20:37 +08:00
describe ( 'cleanIcons method' , function ( ) {
it ( 'Test#001 : should detect that the app does not have defined icons.' , function ( ) {
// Mock
const icons = [ ] ;
const projectRoot = '/mock' ;
const projectConfig = {
getIcons : function ( ) { return icons ; } ,
path : '/mock/config.xml' ,
cdvNamespacePrefix : 'cdv'
} ;
const platformResourcesDir = PATH _RESOURCE ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
const cleanIcons = prepare . _ _get _ _ ( 'cleanIcons' ) ;
cleanIcons ( projectRoot , projectConfig , platformResourcesDir ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
const actualEmitMessage = emitSpy . calls . argsFor ( 0 ) [ 1 ] ;
expect ( actualEmitMessage ) . toContain ( 'This app does not have launcher icons defined' ) ;
2018-06-05 12:43:05 +08:00
} ) ;
2020-10-18 05:20:37 +08:00
it ( 'Test#002 : Should clean paths for adaptive icons.' , function ( ) {
2018-06-05 12:43:05 +08:00
// Mock
2020-10-18 05:20:37 +08:00
const icons = [ mockGetIconItem ( {
density : 'mdpi' ,
background : 'res/icon/android/mdpi-background.png' ,
2023-04-10 07:41:38 +08:00
foreground : 'res/icon/android/mdpi-foreground.png' ,
monochrome : 'res/icon/android/mdpi-monochrome.png'
2020-10-18 05:20:37 +08:00
} ) ] ;
const projectRoot = '/mock' ;
const projectConfig = {
getIcons : function ( ) { return icons ; } ,
path : '/mock/config.xml' ,
cdvNamespacePrefix : 'cdv'
} ;
const platformResourcesDir = PATH _RESOURCE ;
2018-06-05 12:43:05 +08:00
2022-04-18 09:39:54 +08:00
const expectedResourceMapBackground = createResourceMap ( 'ic_launcher_background.png' ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
// mocking initial responses for mapImageResources
prepare . _ _set _ _ ( 'mapImageResources' , function ( rootDir , subDir , type , resourceName ) {
if ( resourceName . includes ( 'ic_launcher_background.png' ) ) {
return expectedResourceMapBackground ;
}
} ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
const cleanIcons = prepare . _ _get _ _ ( 'cleanIcons' ) ;
cleanIcons ( projectRoot , projectConfig , platformResourcesDir ) ;
2018-06-05 12:43:05 +08:00
2020-10-18 05:20:37 +08:00
const actualResourceMapBackground = updatePathsSpy . calls . argsFor ( 0 ) [ 0 ] ;
expect ( actualResourceMapBackground ) . toEqual ( expectedResourceMapBackground ) ;
2018-06-05 12:43:05 +08:00
} ) ;
2020-10-18 05:20:37 +08:00
it ( 'Test#003 : Should clean paths for legacy icons.' , function ( ) {
// Mock
const icons = [ mockGetIconItem ( {
src : 'res/icon/android/mdpi.png' ,
density : 'mdpi'
} ) ] ;
2020-04-16 20:39:22 +08:00
2020-10-18 05:20:37 +08:00
const projectRoot = '/mock' ;
const projectConfig = {
getIcons : function ( ) { return icons ; } ,
path : '/mock/config.xml' ,
cdvNamespacePrefix : 'cdv'
} ;
const platformResourcesDir = PATH _RESOURCE ;
2020-04-16 20:39:22 +08:00
2022-04-18 09:39:54 +08:00
const expectedResourceMap = createResourceMap ( ) ;
2020-04-16 20:39:22 +08:00
2020-10-18 05:20:37 +08:00
// mocking initial responses for mapImageResources
prepare . _ _set _ _ ( 'mapImageResources' , function ( rootDir , subDir , type , resourceName ) {
return expectedResourceMap ;
} ) ;
2020-04-16 20:39:22 +08:00
2020-10-18 05:20:37 +08:00
const cleanIcons = prepare . _ _get _ _ ( 'cleanIcons' ) ;
cleanIcons ( projectRoot , projectConfig , platformResourcesDir ) ;
2020-04-16 20:39:22 +08:00
2020-10-18 05:20:37 +08:00
const actualResourceMap = updatePathsSpy . calls . argsFor ( 0 ) [ 0 ] ;
expect ( actualResourceMap ) . toEqual ( expectedResourceMap ) ;
2020-04-16 20:39:22 +08:00
} ) ;
} ) ;
2020-10-18 05:20:37 +08:00
describe ( 'prepare arguments' , ( ) => {
2021-07-06 19:01:37 +08:00
// Rewire
2020-10-18 05:20:37 +08:00
let Api ;
let api ;
// Spies
let gradlePropertiesParserSpy ;
// Mock Data
let cordovaProject ;
let options ;
beforeEach ( function ( ) {
2021-07-13 17:01:50 +08:00
Api = rewire ( '../../lib/Api' ) ;
2020-10-18 05:20:37 +08:00
cordovaProject = {
root : '/mock' ,
projectConfig : {
path : '/mock/config.xml' ,
cdvNamespacePrefix : 'cdv'
} ,
locations : {
plugins : '/mock/plugins' ,
www : '/mock/www'
}
} ;
options = {
options : { }
} ;
Api . _ _set _ _ ( 'ConfigParser' ,
jasmine . createSpy ( 'ConfigParser' )
. and . returnValue ( cordovaProject . projectConfig )
) ;
Api . _ _set _ _ ( 'prepare' , prepare . prepare ) ;
2021-07-06 14:38:28 +08:00
prepare . _ _set _ _ ( 'updateUserProjectGradleConfig' , jasmine . createSpy ( ) ) ;
2020-10-18 05:20:37 +08:00
prepare . _ _set _ _ ( 'updateWww' , jasmine . createSpy ( ) ) ;
prepare . _ _set _ _ ( 'updateProjectAccordingTo' , jasmine . createSpy ( 'updateProjectAccordingTo' )
. and . returnValue ( Promise . resolve ( ) ) ) ;
2022-06-30 19:00:25 +08:00
prepare . _ _set _ _ ( 'warnForDeprecatedSplashScreen' , jasmine . createSpy ( 'warnForDeprecatedSplashScreen' )
. and . returnValue ( Promise . resolve ( ) ) ) ;
2020-10-18 05:20:37 +08:00
prepare . _ _set _ _ ( 'updateIcons' , jasmine . createSpy ( 'updateIcons' ) . and . returnValue ( Promise . resolve ( ) ) ) ;
prepare . _ _set _ _ ( 'updateFileResources' , jasmine . createSpy ( 'updateFileResources' ) . and . returnValue ( Promise . resolve ( ) ) ) ;
prepare . _ _set _ _ ( 'updateConfigFilesFrom' ,
jasmine . createSpy ( 'updateConfigFilesFrom' )
. and . returnValue ( {
getPreference : jasmine . createSpy ( 'getPreference' )
} ) ) ;
gradlePropertiesParserSpy = spyOn ( GradlePropertiesParser . prototype , 'configure' ) ;
2021-07-13 14:51:20 +08:00
api = new Api ( 'android' , cordovaProject . root ) ;
2020-10-18 05:20:37 +08:00
} ) ;
2020-04-16 20:39:22 +08:00
2021-03-30 17:46:43 +08:00
it ( 'runs without arguments' , async ( ) => {
await expectAsync (
2020-10-18 05:20:37 +08:00
api . prepare ( cordovaProject , options ) . then ( ( ) => {
expect ( gradlePropertiesParserSpy ) . toHaveBeenCalledWith ( { } ) ;
} )
) . toBeResolved ( ) ;
} ) ;
2021-03-30 17:46:43 +08:00
it ( 'runs with jvmargs' , async ( ) => {
2020-10-18 05:20:37 +08:00
options . options . argv = [ '--jvmargs=-Xmx=4096m' ] ;
2021-03-30 17:46:43 +08:00
await expectAsync (
2020-10-18 05:20:37 +08:00
api . prepare ( cordovaProject , options ) . then ( ( ) => {
expect ( gradlePropertiesParserSpy ) . toHaveBeenCalledWith ( {
'org.gradle.jvmargs' : '-Xmx=4096m'
} ) ;
} )
) . toBeResolved ( ) ;
} ) ;
2022-05-18 22:11:31 +08:00
} ) ;
describe ( 'relocate CordovaActivity class java file' , ( ) => {
// Rewire
let Api ;
let api ;
let prepare ;
// Spies
let replaceFileContents ;
let ensureDirSyncSpy ;
let copySyncSpy ;
let removeSyncSpy ;
// Mock Data
let cordovaProject ;
let options ;
let packageName ;
let initialJavaActivityPath ;
beforeEach ( ( ) => {
Api = rewire ( '../../lib/Api' ) ;
prepare = rewire ( '../../lib/prepare' ) ;
cordovaProject = {
root : '/mock' ,
projectConfig : {
path : '/mock/config.xml' ,
cdvNamespacePrefix : 'cdv' ,
shortName : ( ) => 'rn' ,
name : ( ) => 'rename' ,
android _versionCode : jasmine . createSpy ( 'android_versionCode' ) ,
android _packageName : ( ) => packageName ,
packageName : ( ) => packageName ,
getPreference : jasmine . createSpy ( 'getPreference' ) ,
version : ( ) => '1.0.0'
} ,
locations : {
plugins : '/mock/plugins' ,
www : '/mock/www' ,
strings : '/mock/res/values/strings.xml'
}
} ;
api = new Api ( 'android' , cordovaProject . root ) ;
initialJavaActivityPath = path . join ( api . locations . javaSrc , 'com/company/product/MainActivity.java' ) ;
options = {
options : { }
} ;
Api . _ _set _ _ ( 'ConfigParser' ,
jasmine . createSpy ( 'ConfigParser' )
. and . returnValue ( cordovaProject . projectConfig )
) ;
Api . _ _set _ _ ( 'prepare' , prepare . prepare ) ;
prepare . _ _set _ _ ( 'updateWww' , jasmine . createSpy ( 'updateWww' ) ) ;
prepare . _ _set _ _ ( 'updateIcons' , jasmine . createSpy ( 'updateIcons' ) . and . returnValue ( Promise . resolve ( ) ) ) ;
2022-06-30 09:49:10 +08:00
prepare . _ _set _ _ ( 'updateProjectSplashScreen' , jasmine . createSpy ( 'updateProjectSplashScreen' ) ) ;
2022-06-30 19:00:25 +08:00
prepare . _ _set _ _ ( 'warnForDeprecatedSplashScreen' , jasmine . createSpy ( 'warnForDeprecatedSplashScreen' )
. and . returnValue ( Promise . resolve ( ) ) ) ;
2022-05-18 22:11:31 +08:00
prepare . _ _set _ _ ( 'updateFileResources' , jasmine . createSpy ( 'updateFileResources' ) . and . returnValue ( Promise . resolve ( ) ) ) ;
prepare . _ _set _ _ ( 'updateConfigFilesFrom' ,
jasmine . createSpy ( 'updateConfigFilesFrom' )
. and . returnValue ( cordovaProject . projectConfig
) ) ;
prepare . _ _set _ _ ( 'glob' , {
sync : jasmine . createSpy ( 'sync' ) . and . returnValue ( {
filter : jasmine . createSpy ( 'filter' ) . and . returnValue ( [
initialJavaActivityPath
] )
} )
} ) ;
// prepare.__set__('events', {
// emit: function () {
// console.log(arguments);
// }
// });
spyOn ( GradlePropertiesParser . prototype , 'configure' ) ;
replaceFileContents = spyOn ( utils , 'replaceFileContents' ) ;
prepare . _ _set _ _ ( 'AndroidManifest' , jasmine . createSpy ( 'AndroidManifest' ) . and . returnValue ( {
getPackageId : ( ) => packageName ,
getActivity : jasmine . createSpy ( 'getActivity' ) . and . returnValue ( {
setOrientation : jasmine . createSpy ( 'setOrientation' ) . and . returnValue ( {
setLaunchMode : jasmine . createSpy ( 'setLaunchValue' )
} )
} ) ,
setVersionName : jasmine . createSpy ( 'setVersionName' ) . and . returnValue ( {
setVersionCode : jasmine . createSpy ( 'setVersionCode' ) . and . returnValue ( {
2023-04-12 13:39:47 +08:00
write : jasmine . createSpy ( 'write' )
2022-05-18 22:11:31 +08:00
} )
} )
} ) ) ;
prepare . _ _set _ _ ( 'xmlHelpers' , {
parseElementtreeSync : jasmine . createSpy ( 'parseElementtreeSync' ) . and . returnValue ( et . parse ( ` <?xml version="1.0" encoding="utf-8"?>
< resources >
<!-- App label shown within list of installed apps , battery & network usage screens . -- >
< string name = "app_name" > _ _NAME _ _ < / s t r i n g >
<!-- App label shown on the launcher . -- >
< string name = "launcher_name" > @ string / app _name < / s t r i n g >
<!-- App label shown on the task switcher . -- >
< string name = "activity_name" > @ string / launcher _name < / s t r i n g >
< / r e s o u r c e s >
` ))
} ) ;
ensureDirSyncSpy = jasmine . createSpy ( 'ensureDirSync' ) ;
copySyncSpy = jasmine . createSpy ( 'copySync' ) ;
removeSyncSpy = jasmine . createSpy ( 'removeSync' ) ;
prepare . _ _set _ _ ( 'fs' , {
writeFileSync : jasmine . createSpy ( 'writeFileSync' ) ,
writeJSONSync : jasmine . createSpy ( 'writeJSONSync' ) ,
ensureDirSync : ensureDirSyncSpy ,
copySync : copySyncSpy ,
removeSync : removeSyncSpy ,
existsSync : jasmine . createSpy ( 'existsSync' )
} ) ;
} ) ;
it ( 'moves main activity class java file to path that tracks the package name when package name changed' , async ( ) => {
packageName = 'com.company.renamed' ;
const renamedPath = path . join ( api . locations . javaSrc , packageName . replace ( /\./g , '/' ) ) ;
const renamedJavaActivityPath = path . join ( renamedPath , 'MainActivity.java' ) ;
await api . prepare ( cordovaProject , options ) . then ( ( ) => {
expect ( replaceFileContents ) . toHaveBeenCalledWith ( renamedJavaActivityPath , /package [\w.]*;/ , 'package ' + packageName + ';' ) ;
expect ( ensureDirSyncSpy ) . toHaveBeenCalledWith ( renamedPath ) ;
expect ( copySyncSpy ) . toHaveBeenCalledWith ( initialJavaActivityPath , renamedJavaActivityPath ) ;
expect ( removeSyncSpy ) . toHaveBeenCalledWith ( initialJavaActivityPath ) ;
} ) ;
} ) ;
it ( 'doesn\'t move main activity class java file when package name not changed' , async ( ) => {
packageName = 'com.company.product' ;
await api . prepare ( cordovaProject , options ) . then ( ( ) => {
expect ( replaceFileContents ) . toHaveBeenCalledTimes ( 0 ) ;
expect ( ensureDirSyncSpy ) . toHaveBeenCalledTimes ( 0 ) ;
expect ( copySyncSpy ) . toHaveBeenCalledTimes ( 0 ) ;
expect ( removeSyncSpy ) . toHaveBeenCalledTimes ( 0 ) ;
} ) ;
} ) ;
2020-04-16 20:39:22 +08:00
} ) ;
} ) ;