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

Adb.spec.js 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 CordovaError = require('cordova-common').CordovaError;
  18. const rewire = require('rewire');
  19. describe('Adb', () => {
  20. const adbOutput = `List of devices attached
  21. emulator-5554\tdevice
  22. 123a76565509e124\tdevice`;
  23. const [, emulatorLine, deviceLine] = adbOutput.split('\n');
  24. const emulatorId = emulatorLine.split('\t')[0];
  25. const deviceId = deviceLine.split('\t')[0];
  26. const alreadyExistsError = 'adb: failed to install app.apk: Failure[INSTALL_FAILED_ALREADY_EXISTS]';
  27. const certificateError = 'adb: failed to install app.apk: Failure[INSTALL_PARSE_FAILED_NO_CERTIFICATES]';
  28. const downgradeError = 'adb: failed to install app.apk: Failure[INSTALL_FAILED_VERSION_DOWNGRADE]';
  29. let Adb;
  30. let spawnSpy;
  31. beforeEach(() => {
  32. Adb = rewire('../../bin/templates/cordova/lib/Adb');
  33. spawnSpy = jasmine.createSpy('spawn');
  34. Adb.__set__('spawn', spawnSpy);
  35. });
  36. describe('isDevice', () => {
  37. it('should return true for a real device', () => {
  38. const isDevice = Adb.__get__('isDevice');
  39. expect(isDevice(deviceLine)).toBeTruthy();
  40. expect(isDevice(emulatorLine)).toBeFalsy();
  41. });
  42. });
  43. describe('isEmulator', () => {
  44. it('should return true for an emulator', () => {
  45. const isEmulator = Adb.__get__('isEmulator');
  46. expect(isEmulator(emulatorLine)).toBeTruthy();
  47. expect(isEmulator(deviceLine)).toBeFalsy();
  48. });
  49. });
  50. describe('devices', () => {
  51. beforeEach(() => {
  52. spawnSpy.and.returnValue(Promise.resolve(adbOutput));
  53. });
  54. it('should return only devices if no options are specified', () => {
  55. return Adb.devices().then(devices => {
  56. expect(devices.length).toBe(1);
  57. expect(devices[0]).toBe(deviceId);
  58. });
  59. });
  60. it('should return only emulators if opts.emulators is true', () => {
  61. return Adb.devices({ emulators: true }).then(devices => {
  62. expect(devices.length).toBe(1);
  63. expect(devices[0]).toBe(emulatorId);
  64. });
  65. });
  66. });
  67. describe('install', () => {
  68. beforeEach(() => {
  69. spawnSpy.and.returnValue(Promise.resolve(''));
  70. });
  71. it('should target the passed device id to adb', () => {
  72. return Adb.install(deviceId).then(() => {
  73. const args = spawnSpy.calls.argsFor(0);
  74. expect(args[0]).toBe('adb');
  75. const adbArgs = args[1].join(' ');
  76. expect(adbArgs).toMatch(`-s ${deviceId}`);
  77. });
  78. });
  79. it('should add the -r flag if opts.replace is set', () => {
  80. return Adb.install(deviceId, '', { replace: true }).then(() => {
  81. const adbArgs = spawnSpy.calls.argsFor(0)[1];
  82. expect(adbArgs).toContain('-r');
  83. });
  84. });
  85. it('should pass the correct package path to adb', () => {
  86. const packagePath = 'build/test/app.apk';
  87. return Adb.install(deviceId, packagePath).then(() => {
  88. const adbArgs = spawnSpy.calls.argsFor(0)[1];
  89. expect(adbArgs).toContain(packagePath);
  90. });
  91. });
  92. it('should reject with a CordovaError if the adb output suggests a failure', () => {
  93. spawnSpy.and.returnValue(Promise.resolve(alreadyExistsError));
  94. return Adb.install(deviceId, '').then(
  95. () => fail('Unexpectedly resolved'),
  96. err => {
  97. expect(err).toEqual(jasmine.any(CordovaError));
  98. }
  99. );
  100. });
  101. // The following two tests are somewhat brittle as they are dependent on the
  102. // exact message returned. But it is better to have them tested than not at all.
  103. it('should give a more specific error message if there is a certificate failure', () => {
  104. spawnSpy.and.returnValue(Promise.resolve(certificateError));
  105. return Adb.install(deviceId, '').then(
  106. () => fail('Unexpectedly resolved'),
  107. err => {
  108. expect(err).toEqual(jasmine.any(CordovaError));
  109. expect(err.message).toMatch('Sign the build');
  110. }
  111. );
  112. });
  113. it('should give a more specific error message if there is a downgrade error', () => {
  114. spawnSpy.and.returnValue(Promise.resolve(downgradeError));
  115. return Adb.install(deviceId, '').then(
  116. () => fail('Unexpectedly resolved'),
  117. err => {
  118. expect(err).toEqual(jasmine.any(CordovaError));
  119. expect(err.message).toMatch('lower versionCode');
  120. }
  121. );
  122. });
  123. });
  124. describe('uninstall', () => {
  125. it('should call adb uninstall with the correct arguments', () => {
  126. const packageId = 'io.cordova.test';
  127. spawnSpy.and.returnValue(Promise.resolve(''));
  128. return Adb.uninstall(deviceId, packageId).then(() => {
  129. const args = spawnSpy.calls.argsFor(0);
  130. expect(args[0]).toBe('adb');
  131. const adbArgs = args[1];
  132. expect(adbArgs).toContain('uninstall');
  133. expect(adbArgs.join(' ')).toContain(`-s ${deviceId}`);
  134. expect(adbArgs[adbArgs.length - 1]).toBe(packageId);
  135. });
  136. });
  137. });
  138. describe('shell', () => {
  139. const shellCommand = 'ls -l /sdcard';
  140. it('should run the passed command on the target device', () => {
  141. spawnSpy.and.returnValue(Promise.resolve(''));
  142. return Adb.shell(deviceId, shellCommand).then(() => {
  143. const args = spawnSpy.calls.argsFor(0);
  144. expect(args[0]).toBe('adb');
  145. const adbArgs = args[1].join(' ');
  146. expect(adbArgs).toContain('shell');
  147. expect(adbArgs).toContain(`-s ${deviceId}`);
  148. expect(adbArgs).toMatch(new RegExp(`${shellCommand}$`));
  149. });
  150. });
  151. it('should reject with a CordovaError on failure', () => {
  152. const errorMessage = 'shell error';
  153. spawnSpy.and.returnValue(Promise.reject(errorMessage));
  154. return Adb.shell(deviceId, shellCommand).then(
  155. () => fail('Unexpectedly resolved'),
  156. err => {
  157. expect(err).toEqual(jasmine.any(CordovaError));
  158. expect(err.message).toMatch(errorMessage);
  159. }
  160. );
  161. });
  162. });
  163. describe('start', () => {
  164. const activityName = 'io.cordova.test/.MainActivity';
  165. it('should start an activity using the shell activity manager', () => {
  166. const shellSpy = spyOn(Adb, 'shell').and.returnValue(Promise.resolve(''));
  167. return Adb.start(deviceId, activityName).then(() => {
  168. expect(shellSpy).toHaveBeenCalled();
  169. const [target, command] = shellSpy.calls.argsFor(0);
  170. expect(target).toBe(deviceId);
  171. expect(command).toContain('am start');
  172. expect(command).toContain(`-n${activityName}`);
  173. });
  174. });
  175. it('should reject with a CordovaError on a shell error', () => {
  176. const errorMessage = 'Test Start error';
  177. spyOn(Adb, 'shell').and.returnValue(Promise.reject(errorMessage));
  178. return Adb.start(deviceId, activityName).then(
  179. () => fail('Unexpectedly resolved'),
  180. err => {
  181. expect(err).toEqual(jasmine.any(CordovaError));
  182. expect(err.message).toMatch(errorMessage);
  183. }
  184. );
  185. });
  186. });
  187. });