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

run.spec.js 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. describe('run', () => {
  19. let run;
  20. beforeEach(() => {
  21. run = rewire('../../bin/templates/cordova/lib/run');
  22. });
  23. describe('getInstallTarget', () => {
  24. const targetOpts = { target: 'emu' };
  25. const deviceOpts = { device: true };
  26. const emulatorOpts = { emulator: true };
  27. const emptyOpts = {};
  28. it('Test#001 : should select correct target based on the run opts', () => {
  29. const getInstallTarget = run.__get__('getInstallTarget');
  30. expect(getInstallTarget(targetOpts)).toBe('emu');
  31. expect(getInstallTarget(deviceOpts)).toBe('--device');
  32. expect(getInstallTarget(emulatorOpts)).toBe('--emulator');
  33. expect(getInstallTarget(emptyOpts)).toBeUndefined();
  34. });
  35. });
  36. describe('run method', () => {
  37. let deviceSpyObj;
  38. let emulatorSpyObj;
  39. let eventsSpyObj;
  40. let getInstallTargetSpy;
  41. beforeEach(() => {
  42. deviceSpyObj = jasmine.createSpyObj('deviceSpy', ['install', 'list', 'resolveTarget']);
  43. emulatorSpyObj = jasmine.createSpyObj('emulatorSpy', ['install', 'list_images', 'list_started', 'resolveTarget', 'start', 'wait_for_boot']);
  44. eventsSpyObj = jasmine.createSpyObj('eventsSpy', ['emit']);
  45. getInstallTargetSpy = jasmine.createSpy('getInstallTargetSpy');
  46. run.__set__({
  47. device: deviceSpyObj,
  48. emulator: emulatorSpyObj,
  49. events: eventsSpyObj,
  50. getInstallTarget: getInstallTargetSpy
  51. });
  52. });
  53. it('should run on default device when no target arguments are specified', () => {
  54. const deviceList = ['testDevice1', 'testDevice2'];
  55. getInstallTargetSpy.and.returnValue(null);
  56. deviceSpyObj.list.and.returnValue(Promise.resolve(deviceList));
  57. return run.run().then(() => {
  58. expect(deviceSpyObj.resolveTarget).toHaveBeenCalledWith(deviceList[0]);
  59. });
  60. });
  61. it('should run on emulator when no target arguments are specified, and no devices are found', () => {
  62. const deviceList = [];
  63. getInstallTargetSpy.and.returnValue(null);
  64. deviceSpyObj.list.and.returnValue(Promise.resolve(deviceList));
  65. emulatorSpyObj.list_started.and.returnValue(Promise.resolve([]));
  66. return run.run().then(() => {
  67. expect(emulatorSpyObj.list_started).toHaveBeenCalled();
  68. });
  69. });
  70. it('should run on default device when device is requested, but none specified', () => {
  71. getInstallTargetSpy.and.returnValue('--device');
  72. return run.run().then(() => {
  73. // Default device is selected by calling device.resolveTarget(null)
  74. expect(deviceSpyObj.resolveTarget).toHaveBeenCalledWith(null);
  75. });
  76. });
  77. it('should run on a running emulator if one exists', () => {
  78. const emulatorList = ['emulator1', 'emulator2'];
  79. getInstallTargetSpy.and.returnValue('--emulator');
  80. emulatorSpyObj.list_started.and.returnValue(Promise.resolve(emulatorList));
  81. return run.run().then(() => {
  82. expect(emulatorSpyObj.resolveTarget).toHaveBeenCalledWith(emulatorList[0]);
  83. });
  84. });
  85. it('should start an emulator and run on that if none is running', () => {
  86. const emulatorList = [];
  87. const defaultEmulator = 'default-emu';
  88. getInstallTargetSpy.and.returnValue('--emulator');
  89. emulatorSpyObj.list_started.and.returnValue(Promise.resolve(emulatorList));
  90. emulatorSpyObj.start.and.returnValue(Promise.resolve(defaultEmulator));
  91. return run.run().then(() => {
  92. expect(emulatorSpyObj.resolveTarget).toHaveBeenCalledWith(defaultEmulator);
  93. });
  94. });
  95. it('should run on a named device if it is specified', () => {
  96. const deviceList = ['device1', 'device2', 'device3'];
  97. getInstallTargetSpy.and.returnValue(deviceList[1]);
  98. deviceSpyObj.list.and.returnValue(Promise.resolve(deviceList));
  99. return run.run().then(() => {
  100. expect(deviceSpyObj.resolveTarget).toHaveBeenCalledWith(deviceList[1]);
  101. });
  102. });
  103. it('should run on a named emulator if it is specified', () => {
  104. const startedEmulatorList = ['emu1', 'emu2', 'emu3'];
  105. getInstallTargetSpy.and.returnValue(startedEmulatorList[2]);
  106. deviceSpyObj.list.and.returnValue(Promise.resolve([]));
  107. emulatorSpyObj.list_started.and.returnValue(Promise.resolve(startedEmulatorList));
  108. return run.run().then(() => {
  109. expect(emulatorSpyObj.resolveTarget).toHaveBeenCalledWith(startedEmulatorList[2]);
  110. });
  111. });
  112. it('should start named emulator and then run on it if it is specified', () => {
  113. const emulatorList = [
  114. { name: 'emu1', id: 1 },
  115. { name: 'emu2', id: 2 },
  116. { name: 'emu3', id: 3 }
  117. ];
  118. getInstallTargetSpy.and.returnValue(emulatorList[2].name);
  119. deviceSpyObj.list.and.returnValue(Promise.resolve([]));
  120. emulatorSpyObj.list_started.and.returnValue(Promise.resolve([]));
  121. emulatorSpyObj.list_images.and.returnValue(Promise.resolve(emulatorList));
  122. emulatorSpyObj.start.and.returnValue(Promise.resolve(emulatorList[2].id));
  123. return run.run().then(() => {
  124. expect(emulatorSpyObj.start).toHaveBeenCalledWith(emulatorList[2].name);
  125. expect(emulatorSpyObj.resolveTarget).toHaveBeenCalledWith(emulatorList[2].id);
  126. });
  127. });
  128. it('should throw an error if target is specified but does not exist', () => {
  129. const emulatorList = [{ name: 'emu1', id: 1 }];
  130. const deviceList = ['device1'];
  131. const target = 'nonexistentdevice';
  132. getInstallTargetSpy.and.returnValue(target);
  133. deviceSpyObj.list.and.returnValue(Promise.resolve(deviceList));
  134. emulatorSpyObj.list_started.and.returnValue(Promise.resolve([]));
  135. emulatorSpyObj.list_images.and.returnValue(Promise.resolve(emulatorList));
  136. return run.run().then(
  137. () => fail('Expected error to be thrown'),
  138. err => expect(err).toContain(target)
  139. );
  140. });
  141. it('should install on device after build', () => {
  142. const deviceTarget = { target: 'device1', isEmulator: false };
  143. getInstallTargetSpy.and.returnValue('--device');
  144. deviceSpyObj.resolveTarget.and.returnValue(deviceTarget);
  145. return run.run().then(() => {
  146. expect(deviceSpyObj.install).toHaveBeenCalledWith(deviceTarget, { apkPaths: [], buildType: 'debug' });
  147. });
  148. });
  149. it('should install on emulator after build', () => {
  150. const emulatorTarget = { target: 'emu1', isEmulator: true };
  151. getInstallTargetSpy.and.returnValue('--emulator');
  152. emulatorSpyObj.list_started.and.returnValue(Promise.resolve([emulatorTarget.target]));
  153. emulatorSpyObj.resolveTarget.and.returnValue(emulatorTarget);
  154. emulatorSpyObj.wait_for_boot.and.returnValue(Promise.resolve());
  155. return run.run().then(() => {
  156. expect(emulatorSpyObj.install).toHaveBeenCalledWith(emulatorTarget, { apkPaths: [], buildType: 'debug' });
  157. });
  158. });
  159. it('should fail with the error message if --packageType=bundle setting is used', () => {
  160. const deviceList = ['testDevice1', 'testDevice2'];
  161. getInstallTargetSpy.and.returnValue(null);
  162. deviceSpyObj.list.and.returnValue(Promise.resolve(deviceList));
  163. return run.run({ argv: ['--packageType=bundle'] }).then(
  164. () => fail('Expected error to be thrown'),
  165. err => expect(err).toContain('Package type "bundle" is not supported during cordova run.')
  166. );
  167. });
  168. });
  169. describe('help', () => {
  170. it('should print out usage and help', () => {
  171. spyOn(console, 'log');
  172. spyOn(process, 'exit');
  173. run.help();
  174. expect(console.log).toHaveBeenCalledWith(jasmine.stringMatching(/^Usage:/));
  175. });
  176. });
  177. });