Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

device.spec.js 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. const rewire = require('rewire');
  18. const CordovaError = require('cordova-common').CordovaError;
  19. describe('device', () => {
  20. const DEVICE_LIST = ['device1', 'device2', 'device3'];
  21. let AdbSpy;
  22. let device;
  23. beforeEach(() => {
  24. device = rewire('../../bin/templates/cordova/lib/device');
  25. AdbSpy = jasmine.createSpyObj('Adb', ['devices', 'install', 'shell', 'start', 'uninstall']);
  26. device.__set__('Adb', AdbSpy);
  27. });
  28. describe('list', () => {
  29. it('should return the list from adb devices', () => {
  30. AdbSpy.devices.and.returnValue(Promise.resolve(DEVICE_LIST));
  31. return device.list().then(list => {
  32. expect(list).toEqual(DEVICE_LIST);
  33. });
  34. });
  35. it('should kill adb and try to get devices again if none are found the first time, and `lookHarder` is set', () => {
  36. const spawnSpy = jasmine.createSpy('spawn').and.returnValue(Promise.resolve());
  37. device.__set__('spawn', spawnSpy);
  38. AdbSpy.devices.and.returnValues(Promise.resolve([]), Promise.resolve(DEVICE_LIST));
  39. return device.list(true).then(list => {
  40. expect(spawnSpy).toHaveBeenCalledWith('killall', ['adb']);
  41. expect(list).toBe(DEVICE_LIST);
  42. expect(AdbSpy.devices).toHaveBeenCalledTimes(2);
  43. });
  44. });
  45. it('should return the empty list if killing adb fails', () => {
  46. const emptyDevices = [];
  47. const spawnSpy = jasmine.createSpy('spawn').and.returnValue(Promise.reject());
  48. device.__set__('spawn', spawnSpy);
  49. AdbSpy.devices.and.returnValues(Promise.resolve(emptyDevices));
  50. return device.list(true).then(list => {
  51. expect(spawnSpy).toHaveBeenCalledWith('killall', ['adb']);
  52. expect(list).toBe(emptyDevices);
  53. expect(AdbSpy.devices).toHaveBeenCalledTimes(1);
  54. });
  55. });
  56. });
  57. describe('resolveTarget', () => {
  58. let buildSpy;
  59. beforeEach(() => {
  60. buildSpy = jasmine.createSpyObj('build', ['detectArchitecture']);
  61. buildSpy.detectArchitecture.and.returnValue(Promise.resolve());
  62. device.__set__('build', buildSpy);
  63. spyOn(device, 'list').and.returnValue(Promise.resolve(DEVICE_LIST));
  64. });
  65. it('should select the first device to be the target if none is specified', () => {
  66. return device.resolveTarget().then(deviceInfo => {
  67. expect(deviceInfo.target).toBe(DEVICE_LIST[0]);
  68. });
  69. });
  70. it('should use the given target instead of the default', () => {
  71. return device.resolveTarget(DEVICE_LIST[2]).then(deviceInfo => {
  72. expect(deviceInfo.target).toBe(DEVICE_LIST[2]);
  73. });
  74. });
  75. it('should set emulator to false', () => {
  76. return device.resolveTarget().then(deviceInfo => {
  77. expect(deviceInfo.isEmulator).toBe(false);
  78. });
  79. });
  80. it('should throw an error if there are no devices', () => {
  81. device.list.and.returnValue(Promise.resolve([]));
  82. return device.resolveTarget().then(
  83. () => fail('Unexpectedly resolved'),
  84. err => {
  85. expect(err).toEqual(jasmine.any(CordovaError));
  86. }
  87. );
  88. });
  89. it('should throw an error if the specified target does not exist', () => {
  90. return device.resolveTarget('nonexistent-target').then(
  91. () => fail('Unexpectedly resolved'),
  92. err => {
  93. expect(err).toEqual(jasmine.any(CordovaError));
  94. }
  95. );
  96. });
  97. it('should detect the architecture and return it with the device info', () => {
  98. const target = DEVICE_LIST[1];
  99. const arch = 'unittestarch';
  100. buildSpy.detectArchitecture.and.returnValue(Promise.resolve(arch));
  101. return device.resolveTarget(target).then(deviceInfo => {
  102. expect(buildSpy.detectArchitecture).toHaveBeenCalledWith(target);
  103. expect(deviceInfo.arch).toBe(arch);
  104. });
  105. });
  106. });
  107. describe('install', () => {
  108. let AndroidManifestSpy;
  109. let AndroidManifestFns;
  110. let AndroidManifestGetActivitySpy;
  111. let buildSpy;
  112. let target;
  113. beforeEach(() => {
  114. target = { target: DEVICE_LIST[0], arch: 'arm7', isEmulator: false };
  115. buildSpy = jasmine.createSpyObj('build', ['findBestApkForArchitecture']);
  116. device.__set__('build', buildSpy);
  117. AndroidManifestFns = jasmine.createSpyObj('AndroidManifestFns', ['getPackageId', 'getActivity']);
  118. AndroidManifestGetActivitySpy = jasmine.createSpyObj('getActivity', ['getName']);
  119. AndroidManifestFns.getActivity.and.returnValue(AndroidManifestGetActivitySpy);
  120. AndroidManifestSpy = jasmine.createSpy('AndroidManifest').and.returnValue(AndroidManifestFns);
  121. device.__set__('AndroidManifest', AndroidManifestSpy);
  122. AdbSpy.install.and.returnValue(Promise.resolve());
  123. AdbSpy.shell.and.returnValue(Promise.resolve());
  124. AdbSpy.start.and.returnValue(Promise.resolve());
  125. });
  126. it('should get the full target object if only id is specified', () => {
  127. const targetId = DEVICE_LIST[0];
  128. spyOn(device, 'resolveTarget').and.returnValue(Promise.resolve(target));
  129. return device.install(targetId).then(() => {
  130. expect(device.resolveTarget).toHaveBeenCalledWith(targetId);
  131. });
  132. });
  133. it('should install to the passed target', () => {
  134. return device.install(target).then(() => {
  135. expect(AdbSpy.install).toHaveBeenCalledWith(target.target, undefined, jasmine.anything());
  136. });
  137. });
  138. it('should install the correct apk based on the architecture and build results', () => {
  139. const buildResults = {
  140. apkPaths: 'path/to/apks',
  141. buildType: 'debug',
  142. buildMethod: 'foo'
  143. };
  144. const apkPath = 'my/apk/path/app.apk';
  145. buildSpy.findBestApkForArchitecture.and.returnValue(apkPath);
  146. return device.install(target, buildResults).then(() => {
  147. expect(buildSpy.findBestApkForArchitecture).toHaveBeenCalledWith(buildResults, target.arch);
  148. expect(AdbSpy.install).toHaveBeenCalledWith(jasmine.anything(), apkPath, jasmine.anything());
  149. });
  150. });
  151. it('should uninstall and reinstall app if failure is due to different certificates', () => {
  152. AdbSpy.install.and.returnValues(
  153. Promise.reject('Failed to install: INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES'),
  154. Promise.resolve()
  155. );
  156. AdbSpy.uninstall.and.callFake(() => {
  157. expect(AdbSpy.install).toHaveBeenCalledTimes(1);
  158. return Promise.resolve();
  159. });
  160. return device.install(target).then(() => {
  161. expect(AdbSpy.install).toHaveBeenCalledTimes(2);
  162. expect(AdbSpy.uninstall).toHaveBeenCalled();
  163. });
  164. });
  165. it('should throw any error not caused by different certificates', () => {
  166. const errorMsg = new CordovaError('Failed to install');
  167. AdbSpy.install.and.returnValues(Promise.reject(errorMsg));
  168. return device.install(target).then(
  169. () => fail('Unexpectedly resolved'),
  170. err => {
  171. expect(err).toBe(errorMsg);
  172. }
  173. );
  174. });
  175. it('should unlock the screen on device', () => {
  176. return device.install(target).then(() => {
  177. expect(AdbSpy.shell).toHaveBeenCalledWith(target.target, 'input keyevent 82');
  178. });
  179. });
  180. it('should start the newly installed app on the device', () => {
  181. const packageId = 'unittestapp';
  182. const activityName = 'TestActivity';
  183. AndroidManifestFns.getPackageId.and.returnValue(packageId);
  184. AndroidManifestGetActivitySpy.getName.and.returnValue(activityName);
  185. return device.install(target).then(() => {
  186. expect(AdbSpy.start).toHaveBeenCalledWith(target.target, `${packageId}/.${activityName}`);
  187. });
  188. });
  189. });
  190. });