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

AndroidManifest.spec.js 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 fs = require('fs');
  18. const os = require('os');
  19. const path = require('path');
  20. const rewire = require('rewire');
  21. describe('AndroidManifest', () => {
  22. const VERSION_CODE = '50407';
  23. const VERSION_NAME = '5.4.7';
  24. const PACKAGE_ID = 'io.cordova.test';
  25. const ACTIVITY_LAUNCH_MODE = 'singleTop';
  26. const ACTIVITY_NAME = 'MainActivity';
  27. const ACTIVITY_ORIENTATION = 'portrait';
  28. const DEFAULT_MANIFEST = `<?xml version='1.0' encoding='utf-8'?>
  29. <manifest android:hardwareAccelerated="true" android:versionCode="${VERSION_CODE}" android:versionName="${VERSION_NAME}"
  30. package="${PACKAGE_ID}" xmlns:android="http://schemas.android.com/apk/res/android">
  31. <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true"
  32. android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
  33. <uses-permission android:name="android.permission.INTERNET" />
  34. <application android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name"
  35. android:supportsRtl="true" android:debuggable="true">
  36. <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
  37. android:label="@string/activity_name" android:launchMode="${ACTIVITY_LAUNCH_MODE}"
  38. android:name="${ACTIVITY_NAME}" android:theme="@android:style/Theme.DeviceDefault.NoActionBar"
  39. android:windowSoftInputMode="adjustResize" android:screenOrientation="${ACTIVITY_ORIENTATION}">
  40. <intent-filter android:label="@string/launcher_name">
  41. <action android:name="android.intent.action.MAIN" />
  42. <category android:name="android.intent.category.LAUNCHER" />
  43. </intent-filter>
  44. </activity>
  45. </application>
  46. </manifest>`;
  47. const manifestPath = path.join(os.tmpdir(), `AndroidManifest${Date.now()}.xml`);
  48. function createTempManifestFile (xml) {
  49. fs.writeFileSync(manifestPath, xml);
  50. }
  51. function removeTempManifestFile () {
  52. fs.unlinkSync(manifestPath);
  53. }
  54. let AndroidManifest;
  55. let manifest;
  56. beforeEach(() => {
  57. createTempManifestFile(DEFAULT_MANIFEST);
  58. AndroidManifest = rewire('../../bin/templates/cordova/lib/AndroidManifest');
  59. manifest = new AndroidManifest(manifestPath);
  60. });
  61. afterEach(() => {
  62. removeTempManifestFile();
  63. });
  64. describe('constructor', () => {
  65. it('should parse the manifest', () => {
  66. expect(manifest.doc.getroot().tag).toBe('manifest');
  67. });
  68. it('should throw an error if not a valid manifest', () => {
  69. createTempManifestFile(`<?xml version='1.0' encoding='utf-8'?><notamanifest></notamanifest>`);
  70. expect(() => new AndroidManifest(manifestPath)).toThrowError();
  71. });
  72. });
  73. describe('versionName', () => {
  74. it('should get the version name', () => {
  75. expect(manifest.getVersionName()).toBe(VERSION_NAME);
  76. });
  77. it('should set the version name', () => {
  78. const newVersionName = `${VERSION_NAME}55555`;
  79. manifest.setVersionName(newVersionName);
  80. expect(manifest.getVersionName()).toBe(newVersionName);
  81. });
  82. });
  83. describe('versionCode', () => {
  84. it('should get the version code', () => {
  85. expect(manifest.getVersionCode()).toBe(VERSION_CODE);
  86. });
  87. it('should set the version code', () => {
  88. const newVersionName = `${VERSION_CODE}12345`;
  89. manifest.setVersionCode(newVersionName);
  90. expect(manifest.getVersionCode()).toBe(newVersionName);
  91. });
  92. });
  93. describe('packageId', () => {
  94. it('should get the package ID', () => {
  95. expect(manifest.getPackageId()).toBe(PACKAGE_ID);
  96. });
  97. it('should set the package ID', () => {
  98. const newPackageId = `${PACKAGE_ID}new`;
  99. manifest.setPackageId(newPackageId);
  100. expect(manifest.getPackageId()).toBe(newPackageId);
  101. });
  102. });
  103. describe('activity', () => {
  104. let activity;
  105. beforeEach(() => {
  106. activity = manifest.getActivity();
  107. });
  108. describe('name', () => {
  109. it('should get the activity name', () => {
  110. expect(activity.getName()).toBe(ACTIVITY_NAME);
  111. });
  112. it('should set the activity name', () => {
  113. const newActivityName = `${ACTIVITY_NAME}New`;
  114. activity.setName(newActivityName);
  115. expect(activity.getName()).toBe(newActivityName);
  116. });
  117. it('should remove the activity name if set to empty', () => {
  118. activity.setName();
  119. expect(activity.getName()).toBe(undefined);
  120. });
  121. });
  122. describe('orientation', () => {
  123. it('should get the activity orientation', () => {
  124. expect(activity.getOrientation()).toBe(ACTIVITY_ORIENTATION);
  125. });
  126. it('should set the activity orienation', () => {
  127. const newOrientation = 'landscape';
  128. activity.setOrientation(newOrientation);
  129. expect(activity.getOrientation()).toBe(newOrientation);
  130. });
  131. it('should remove the orientation if set to default', () => {
  132. activity.setOrientation(AndroidManifest.__get__('DEFAULT_ORIENTATION'));
  133. expect(activity.getOrientation()).toBe(undefined);
  134. });
  135. it('should remove the orientation if set to empty', () => {
  136. activity.setOrientation();
  137. expect(activity.getOrientation()).toBe(undefined);
  138. });
  139. });
  140. describe('launch mode', () => {
  141. it('should get the activity launch mode', () => {
  142. expect(activity.getLaunchMode()).toBe(ACTIVITY_LAUNCH_MODE);
  143. });
  144. it('should set the activity launch mode', () => {
  145. const newLaunchMode = 'standard';
  146. activity.setLaunchMode(newLaunchMode);
  147. expect(activity.getLaunchMode()).toBe(newLaunchMode);
  148. });
  149. it('should remove the launch mode if set to empty', () => {
  150. activity.setLaunchMode();
  151. expect(activity.getLaunchMode()).toBe(undefined);
  152. });
  153. });
  154. });
  155. describe('debuggable', () => {
  156. it('should get debuggable', () => {
  157. expect(manifest.getDebuggable()).toBe(true);
  158. });
  159. it('should remove debuggable if set to a falsy value', () => {
  160. manifest.setDebuggable(false);
  161. expect(manifest.doc.getroot().find('./application').attrib['android:debuggable']).toBe(undefined);
  162. });
  163. it('should set debuggable to true', () => {
  164. const NO_DEBUGGABLE_MANIFEST = DEFAULT_MANIFEST.replace('android:debuggable="true"', '');
  165. createTempManifestFile(NO_DEBUGGABLE_MANIFEST);
  166. manifest = new AndroidManifest(manifestPath);
  167. expect(manifest.getDebuggable()).toBe(false);
  168. manifest.setDebuggable(true);
  169. expect(manifest.getDebuggable()).toBe(true);
  170. });
  171. });
  172. describe('write', () => {
  173. let fsSpy;
  174. beforeEach(() => {
  175. fsSpy = jasmine.createSpyObj('fs', ['writeFileSync']);
  176. AndroidManifest.__set__('fs', fsSpy);
  177. });
  178. it('should overwrite existing manifest if path not specified', () => {
  179. manifest.write();
  180. expect(fsSpy.writeFileSync).toHaveBeenCalledWith(manifestPath, jasmine.any(String), jasmine.any(String));
  181. });
  182. it('should save to the specified path', () => {
  183. const testPath = 'NewAndroidManifest.xml';
  184. manifest.write(testPath);
  185. expect(fsSpy.writeFileSync).toHaveBeenCalledWith(testPath, jasmine.any(String), jasmine.any(String));
  186. });
  187. it('should write the manifest from the parsed XML as utf-8', () => {
  188. const newXml = '<test></test>';
  189. spyOn(manifest.doc, 'write').and.returnValue(newXml);
  190. manifest.write();
  191. expect(fsSpy.writeFileSync).toHaveBeenCalledWith(jasmine.any(String), newXml, 'utf-8');
  192. });
  193. });
  194. });