2016-02-19 02:27:40 +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 .
* /
2018-06-18 14:15:24 +08:00
const rewire = require ( 'rewire' ) ;
2021-07-13 17:01:50 +08:00
const builders = require ( '../../lib/builders/builders' ) ;
2023-04-12 13:39:47 +08:00
const MockCordovaGradleConfigParser = require ( './mocks/config/MockCordovaGradleConfigParser' ) ;
const CordovaGradleConfigParser = require ( '../../lib/config/CordovaGradleConfigParser' ) ;
const CordovaGradleConfigParserFactory = require ( '../../lib/config/CordovaGradleConfigParserFactory' ) ;
2018-06-18 14:15:24 +08:00
describe ( 'run' , ( ) => {
let run ;
2023-04-12 13:39:47 +08:00
const PROJECT _DIR = 'platforms/android' ;
beforeAll ( ( ) => {
spyOn ( CordovaGradleConfigParserFactory , 'create' ) . and . returnValue ( new MockCordovaGradleConfigParser ( PROJECT _DIR ) ) ;
} ) ;
2018-06-18 14:15:24 +08:00
beforeEach ( ( ) => {
2021-07-13 17:01:50 +08:00
run = rewire ( '../../lib/run' ) ;
2021-04-09 14:37:56 +08:00
run . _ _set _ _ ( {
events : jasmine . createSpyObj ( 'eventsSpy' , [ 'emit' ] )
} ) ;
2018-06-18 14:15:24 +08:00
} ) ;
2021-04-09 14:37:56 +08:00
describe ( 'buildTargetSpec' , ( ) => {
2018-06-18 14:15:24 +08:00
it ( 'Test#001 : should select correct target based on the run opts' , ( ) => {
2021-04-09 14:37:56 +08:00
const buildTargetSpec = run . _ _get _ _ ( 'buildTargetSpec' ) ;
expect ( buildTargetSpec ( { target : 'emu' } ) ) . toEqual ( { id : 'emu' } ) ;
expect ( buildTargetSpec ( { device : true } ) ) . toEqual ( { type : 'device' } ) ;
expect ( buildTargetSpec ( { emulator : true } ) ) . toEqual ( { type : 'emulator' } ) ;
expect ( buildTargetSpec ( { } ) ) . toEqual ( { } ) ;
2017-06-14 02:42:20 +08:00
} ) ;
2016-02-19 02:27:40 +08:00
} ) ;
2018-06-18 14:15:24 +08:00
describe ( 'run method' , ( ) => {
2021-04-09 14:37:56 +08:00
let targetSpyObj , emulatorSpyObj , resolvedTarget ;
2018-06-18 14:15:24 +08:00
beforeEach ( ( ) => {
2021-04-09 14:37:56 +08:00
resolvedTarget = { id : 'dev1' , type : 'device' , arch : 'atari' } ;
2018-06-18 14:15:24 +08:00
2021-04-09 14:37:56 +08:00
targetSpyObj = jasmine . createSpyObj ( 'deviceSpy' , [ 'resolve' , 'install' ] ) ;
targetSpyObj . resolve . and . resolveTo ( resolvedTarget ) ;
2020-11-21 05:12:18 +08:00
targetSpyObj . install . and . resolveTo ( ) ;
2021-04-09 14:37:56 +08:00
emulatorSpyObj = jasmine . createSpyObj ( 'emulatorSpy' , [ 'wait_for_boot' ] ) ;
emulatorSpyObj . wait _for _boot . and . resolveTo ( ) ;
2018-06-18 14:15:24 +08:00
run . _ _set _ _ ( {
2020-11-21 05:12:18 +08:00
target : targetSpyObj ,
2021-07-13 14:51:20 +08:00
emulator : emulatorSpyObj ,
AndroidManifest : class { }
2018-06-18 14:15:24 +08:00
} ) ;
2020-07-04 00:54:24 +08:00
2021-07-12 15:48:36 +08:00
const builder = builders . getBuilder ( 'FakeRootPath' ) ;
spyOn ( builder , 'fetchBuildResults' ) . and . returnValue ( {
buildType : 'debug' ,
apkPaths : [ 'fake.apk' ]
2020-07-04 00:54:24 +08:00
} ) ;
2021-07-12 15:48:36 +08:00
// run needs `this` to behave like an Api instance
2021-07-13 14:51:20 +08:00
run . run = run . run . bind ( {
_builder : builder ,
locations : { manifest : 'FakeManifestPath' }
} ) ;
2018-06-18 14:15:24 +08:00
} ) ;
2021-04-09 14:37:56 +08:00
it ( 'should install on target after build' , ( ) => {
2021-07-13 14:51:20 +08:00
const AndroidManifest = run . _ _get _ _ ( 'AndroidManifest' ) ;
2018-06-18 14:15:24 +08:00
return run . run ( ) . then ( ( ) => {
2021-04-09 14:37:56 +08:00
expect ( targetSpyObj . install ) . toHaveBeenCalledWith (
resolvedTarget ,
2021-07-13 14:51:20 +08:00
{
manifest : jasmine . any ( AndroidManifest ) ,
buildResults : {
buildType : 'debug' ,
apkPaths : [ 'fake.apk' ]
2023-04-12 13:39:47 +08:00
} ,
cordovaGradleConfigParser : jasmine . any ( CordovaGradleConfigParser )
2021-07-13 14:51:20 +08:00
}
2021-04-09 14:37:56 +08:00
) ;
2018-06-18 14:15:24 +08:00
} ) ;
} ) ;
2019-08-09 00:53:10 +08:00
it ( 'should fail with the error message if --packageType=bundle setting is used' , ( ) => {
2021-04-09 14:37:56 +08:00
targetSpyObj . resolve . and . resolveTo ( resolvedTarget ) ;
return expectAsync ( run . run ( { argv : [ '--packageType=bundle' ] } ) )
2021-07-11 21:47:07 +08:00
. toBeRejectedWithError ( /Package type "bundle" is not supported/ ) ;
2019-08-09 00:53:10 +08:00
} ) ;
2018-06-18 14:15:24 +08:00
} ) ;
2023-05-08 21:27:17 +08:00
describe ( '--list option' , ( ) => {
beforeEach ( ( ) => {
spyOn ( run , 'listDevices' ) . and . returnValue ( Promise . resolve ( ) ) ;
spyOn ( run , 'listEmulators' ) . and . returnValue ( Promise . resolve ( ) ) ;
} ) ;
it ( 'should delegate to "listDevices" when the "runListDevices" method options param contains "options.device".' , ( ) => {
return run . runListDevices ( { options : { device : true } } ) . then ( ( ) => {
expect ( run . listDevices ) . toHaveBeenCalled ( ) ;
expect ( run . listEmulators ) . not . toHaveBeenCalled ( ) ;
} ) ;
} ) ;
it ( 'should delegate to "listDevices" when the "runListDevices" method options param contains "options.emulator".' , ( ) => {
return run . runListDevices ( { options : { emulator : true } } ) . then ( ( ) => {
expect ( run . listDevices ) . not . toHaveBeenCalled ( ) ;
expect ( run . listEmulators ) . toHaveBeenCalled ( ) ;
} ) ;
} ) ;
it ( 'should delegate to both "listEmulators" and "listDevices" when the "runListDevices" method does not contain "options.device" or "options.emulator".' , ( ) => {
return run . runListDevices ( { options : { } } ) . then ( ( ) => {
expect ( run . listDevices ) . toHaveBeenCalled ( ) ;
expect ( run . listEmulators ) . toHaveBeenCalled ( ) ;
} ) ;
} ) ;
} ) ;
2016-02-19 02:27:40 +08:00
} ) ;