2017-03-15 22:47:51 +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 .
* /
2017-06-14 02:42:20 +08:00
var android _sdk = require ( '../../bin/templates/cordova/lib/android_sdk' ) ;
var superspawn = require ( 'cordova-common' ) . superspawn ;
var fs = require ( 'fs' ) ;
var path = require ( 'path' ) ;
var Q = require ( 'q' ) ;
2017-03-15 22:47:51 +08:00
2017-06-14 02:42:20 +08:00
describe ( 'android_sdk' , function ( ) {
describe ( 'list_targets_with_android' , function ( ) {
it ( 'should invoke `android` with the `list target` command and _not_ the `list targets` command, as the plural form is not supported in some Android SDK Tools versions' , function ( ) {
2017-04-07 04:48:58 +08:00
var deferred = Q . defer ( ) ;
2017-06-14 02:42:20 +08:00
spyOn ( superspawn , 'spawn' ) . and . returnValue ( deferred . promise ) ;
2017-04-07 04:48:58 +08:00
android _sdk . list _targets _with _android ( ) ;
2017-06-14 02:42:20 +08:00
expect ( superspawn . spawn ) . toHaveBeenCalledWith ( 'android' , [ 'list' , 'target' ] ) ;
2017-04-07 04:48:58 +08:00
} ) ;
2017-06-14 02:42:20 +08:00
it ( 'should parse and return results from `android list targets` command' , function ( done ) {
2017-03-15 22:47:51 +08:00
var deferred = Q . defer ( ) ;
2017-06-14 02:42:20 +08:00
spyOn ( superspawn , 'spawn' ) . and . returnValue ( deferred . promise ) ;
deferred . resolve ( fs . readFileSync ( path . join ( 'spec' , 'fixtures' , 'sdk25.2-android_list_targets.txt' ) , 'utf-8' ) ) ;
return android _sdk . list _targets _with _android ( ) . then ( function ( list ) {
[ 'Google Inc.:Google APIs:23' ,
'Google Inc.:Google APIs:22' ,
'Google Inc.:Google APIs:21' ,
'android-25' ,
'android-24' ,
'android-N' ,
'android-23' ,
'android-MNC' ,
'android-22' ,
'android-21' ,
'android-20' ] . forEach ( function ( target ) { expect ( list ) . toContain ( target ) ; } ) ;
} ) . fail ( function ( err ) {
2017-03-15 22:47:51 +08:00
console . log ( err ) ;
expect ( err ) . toBeUndefined ( ) ;
2017-06-14 02:42:20 +08:00
} ) . fin ( function ( ) {
2017-03-15 22:47:51 +08:00
done ( ) ;
} ) ;
} ) ;
} ) ;
2017-06-14 02:42:20 +08:00
describe ( 'list_targets_with_avdmanager' , function ( ) {
it ( 'should parse and return results from `avdmanager list target` command' , function ( done ) {
2017-03-15 22:47:51 +08:00
var deferred = Q . defer ( ) ;
2017-06-14 02:42:20 +08:00
spyOn ( superspawn , 'spawn' ) . and . returnValue ( deferred . promise ) ;
deferred . resolve ( fs . readFileSync ( path . join ( 'spec' , 'fixtures' , 'sdk25.3-avdmanager_list_target.txt' ) , 'utf-8' ) ) ;
return android _sdk . list _targets _with _avdmanager ( ) . then ( function ( list ) {
expect ( list ) . toContain ( 'android-25' ) ;
} ) . fail ( function ( err ) {
2017-03-15 22:47:51 +08:00
console . log ( err ) ;
expect ( err ) . toBeUndefined ( ) ;
2017-06-14 02:42:20 +08:00
} ) . fin ( function ( ) {
2017-03-15 22:47:51 +08:00
done ( ) ;
} ) ;
} ) ;
} ) ;
2017-06-14 02:42:20 +08:00
describe ( 'list_targets' , function ( ) {
it ( 'should parse Android SDK installed target information with `avdmanager` command first' , function ( ) {
2017-03-15 22:47:51 +08:00
var deferred = Q . defer ( ) ;
2017-06-14 02:42:20 +08:00
var avdmanager _spy = spyOn ( android _sdk , 'list_targets_with_avdmanager' ) . and . returnValue ( deferred . promise ) ;
2017-03-15 22:47:51 +08:00
android _sdk . list _targets ( ) ;
2017-04-07 04:13:38 +08:00
expect ( avdmanager _spy ) . toHaveBeenCalled ( ) ;
2017-03-15 22:47:51 +08:00
} ) ;
2017-06-14 02:42:20 +08:00
it ( 'should parse Android SDK installed target information with `android` command if list_targets_with_avdmanager fails with ENOENT' , function ( done ) {
2017-03-15 22:47:51 +08:00
var deferred = Q . defer ( ) ;
2017-06-14 02:42:20 +08:00
spyOn ( android _sdk , 'list_targets_with_avdmanager' ) . and . returnValue ( deferred . promise ) ;
2017-03-15 22:47:51 +08:00
deferred . reject ( {
2017-06-14 02:42:20 +08:00
code : 'ENOENT'
2017-03-15 22:47:51 +08:00
} ) ;
var twoferred = Q . defer ( ) ;
2017-06-14 02:42:20 +08:00
twoferred . resolve ( [ 'target1' ] ) ;
var avdmanager _spy = spyOn ( android _sdk , 'list_targets_with_android' ) . and . returnValue ( twoferred . promise ) ;
return android _sdk . list _targets ( ) . then ( function ( targets ) {
2017-04-07 04:13:38 +08:00
expect ( avdmanager _spy ) . toHaveBeenCalled ( ) ;
2017-06-14 02:42:20 +08:00
expect ( targets [ 0 ] ) . toEqual ( 'target1' ) ;
2017-03-15 22:47:51 +08:00
done ( ) ;
} ) ;
} ) ;
2017-06-14 02:42:20 +08:00
it ( 'should parse Android SDK installed target information with `android` command if list_targets_with_avdmanager fails with not-recognized error (Windows)' , function ( done ) {
2017-04-07 06:08:22 +08:00
var deferred = Q . defer ( ) ;
2017-06-14 02:42:20 +08:00
spyOn ( android _sdk , 'list_targets_with_avdmanager' ) . and . returnValue ( deferred . promise ) ;
2017-04-07 06:08:22 +08:00
deferred . reject ( {
code : 1 ,
stderr : "'avdmanager' is not recognized as an internal or external commmand,\r\noperable program or batch file.\r\n"
} ) ;
var twoferred = Q . defer ( ) ;
2017-06-14 02:42:20 +08:00
twoferred . resolve ( [ 'target1' ] ) ;
var avdmanager _spy = spyOn ( android _sdk , 'list_targets_with_android' ) . and . returnValue ( twoferred . promise ) ;
return android _sdk . list _targets ( ) . then ( function ( targets ) {
2017-04-07 06:08:22 +08:00
expect ( avdmanager _spy ) . toHaveBeenCalled ( ) ;
2017-06-14 02:42:20 +08:00
expect ( targets [ 0 ] ) . toEqual ( 'target1' ) ;
2017-04-07 06:08:22 +08:00
done ( ) ;
} ) ;
} ) ;
2017-06-14 02:42:20 +08:00
it ( 'should throw an error if no Android targets were found.' , function ( done ) {
2017-03-15 22:47:51 +08:00
var deferred = Q . defer ( ) ;
2017-06-14 02:42:20 +08:00
spyOn ( android _sdk , 'list_targets_with_avdmanager' ) . and . returnValue ( deferred . promise ) ;
2017-03-15 22:47:51 +08:00
deferred . resolve ( [ ] ) ;
2017-06-14 02:42:20 +08:00
return android _sdk . list _targets ( ) . then ( function ( targets ) {
2017-03-15 22:47:51 +08:00
done . fail ( ) ;
2017-06-14 02:42:20 +08:00
} ) . catch ( function ( err ) {
2017-03-15 22:47:51 +08:00
expect ( err ) . toBeDefined ( ) ;
2017-06-14 02:42:20 +08:00
expect ( err . message ) . toContain ( 'No android targets (SDKs) installed!' ) ;
2017-03-15 22:47:51 +08:00
done ( ) ;
} ) ;
} ) ;
} ) ;
} ) ;