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

prepare.spec.js 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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. var rewire = require('rewire');
  18. var path = require('path');
  19. var CordovaError = require('cordova-common').CordovaError;
  20. const PATH_RESOURCE = path.join('platforms', 'android', 'app', 'src', 'main', 'res');
  21. /**
  22. * Creates blank resource map object, used for testing.
  23. *
  24. * @param {String} target specific resource item
  25. */
  26. function createResourceMap (target) {
  27. let resources = {};
  28. [
  29. 'mipmap-ldpi',
  30. 'mipmap-mdpi',
  31. 'mipmap-hdpi',
  32. 'mipmap-xhdpi',
  33. 'mipmap-xxhdpi',
  34. 'mipmap-xxxhdpi',
  35. 'mipmap-ldpi-v26',
  36. 'mipmap-mdpi-v26',
  37. 'mipmap-hdpi-v26',
  38. 'mipmap-xhdpi-v26',
  39. 'mipmap-xxhdpi-v26',
  40. 'mipmap-xxxhdpi-v26'
  41. ].forEach((mipmap) => {
  42. if (!target || target === 'ic_launcher.png') resources[path.join(PATH_RESOURCE, mipmap, 'ic_launcher.png')] = null;
  43. if (!target || target === 'ic_launcher_foreground.png') resources[path.join(PATH_RESOURCE, mipmap, 'ic_launcher_foreground.png')] = null;
  44. if (!target || target === 'ic_launcher_background.png') resources[path.join(PATH_RESOURCE, mipmap, 'ic_launcher_background.png')] = null;
  45. if (!target || target === 'ic_launcher_foreground.xml') resources[path.join(PATH_RESOURCE, mipmap, 'ic_launcher_foreground.xml')] = null;
  46. if (!target || target === 'ic_launcher_background.xml') resources[path.join(PATH_RESOURCE, mipmap, 'ic_launcher_background.xml')] = null;
  47. if (
  48. !mipmap.includes('-v26') &&
  49. (!target || target === 'ic_launcher.xml')
  50. ) {
  51. resources[path.join(PATH_RESOURCE, mipmap, 'ic_launcher.xml')] = null;
  52. }
  53. });
  54. return resources;
  55. }
  56. /**
  57. * Create a mock item from the getIcon collection with the supplied updated data.
  58. *
  59. * @param {Object} data Changes to apply to the mock getIcon item
  60. */
  61. function mockGetIconItem (data) {
  62. return Object.assign({}, {
  63. src: undefined,
  64. target: undefined,
  65. density: undefined,
  66. platform: 'android',
  67. width: undefined,
  68. height: undefined,
  69. background: undefined,
  70. foreground: undefined
  71. }, data);
  72. }
  73. describe('updateIcons method', function () {
  74. // Rewire
  75. let prepare;
  76. // Spies
  77. let updateIconResourceForAdaptiveSpy;
  78. let updateIconResourceForLegacySpy;
  79. let emitSpy;
  80. let updatePathsSpy;
  81. // Mock Data
  82. let cordovaProject;
  83. let platformResourcesDir;
  84. beforeEach(function () {
  85. prepare = rewire('../../bin/templates/cordova/lib/prepare');
  86. cordovaProject = {
  87. root: '/mock',
  88. projectConfig: {
  89. path: '/mock/config.xml',
  90. cdvNamespacePrefix: 'cdv'
  91. },
  92. locations: {
  93. plugins: '/mock/plugins',
  94. www: '/mock/www'
  95. }
  96. };
  97. platformResourcesDir = PATH_RESOURCE;
  98. emitSpy = jasmine.createSpy('emit');
  99. prepare.__set__('events', {
  100. emit: emitSpy
  101. });
  102. updatePathsSpy = jasmine.createSpy('updatePaths');
  103. prepare.__set__('FileUpdater', {
  104. updatePaths: updatePathsSpy
  105. });
  106. // mocking initial responses for mapImageResources
  107. prepare.__set__('mapImageResources', function (rootDir, subDir, type, resourceName) {
  108. if (resourceName.includes('ic_launcher.png')) {
  109. return createResourceMap('ic_launcher.png');
  110. } else if (resourceName.includes('ic_launcher_foreground.png')) {
  111. return createResourceMap('ic_launcher_foreground.png');
  112. } else if (resourceName.includes('ic_launcher_background.png')) {
  113. return createResourceMap('ic_launcher_background.png');
  114. } else if (resourceName.includes('ic_launcher_foreground.xml')) {
  115. return createResourceMap('ic_launcher_foreground.xml');
  116. } else if (resourceName.includes('ic_launcher_background.xml')) {
  117. return createResourceMap('ic_launcher_background.xml');
  118. } else if (resourceName.includes('ic_launcher.xml')) {
  119. return createResourceMap('ic_launcher.xml');
  120. }
  121. });
  122. });
  123. it('Test#001 : Should detect no defined icons.', function () {
  124. const updateIcons = prepare.__get__('updateIcons');
  125. // mock data.
  126. cordovaProject.projectConfig.getIcons = function () {
  127. return [];
  128. };
  129. updateIcons(cordovaProject, platformResourcesDir);
  130. // The emit was called
  131. expect(emitSpy).toHaveBeenCalled();
  132. // The emit message was.
  133. let actual = emitSpy.calls.argsFor(0)[1];
  134. let expected = 'This app does not have launcher icons defined';
  135. expect(actual).toEqual(expected);
  136. });
  137. it('Test#002 : Should detech incorrect configrations for adaptive icon and throws error.', function () {
  138. const updateIcons = prepare.__get__('updateIcons');
  139. // mock data.
  140. cordovaProject.projectConfig.getIcons = function () {
  141. return [mockGetIconItem({
  142. density: 'mdpi',
  143. background: 'res/icon/android/mdpi-background.png'
  144. })];
  145. };
  146. expect(function () {
  147. updateIcons(cordovaProject, platformResourcesDir);
  148. }).toThrow(
  149. new CordovaError('One of the following attributes are set but missing the other for the density type: mdpi. Please ensure that all require attributes are defined.')
  150. );
  151. });
  152. it('Test#003 : Should detech incorrect configrations (missing foreground) for adaptive icon and throw an error.', function () {
  153. const updateIcons = prepare.__get__('updateIcons');
  154. // mock data.
  155. cordovaProject.projectConfig.getIcons = function () {
  156. return [mockGetIconItem({
  157. density: 'mdpi',
  158. background: 'res/icon/android/mdpi-background.png'
  159. })];
  160. };
  161. expect(function () {
  162. updateIcons(cordovaProject, platformResourcesDir);
  163. }).toThrow(
  164. new CordovaError('One of the following attributes are set but missing the other for the density type: mdpi. Please ensure that all require attributes are defined.')
  165. );
  166. });
  167. it('Test#004 : Should detech incorrect configrations (missing background) for adaptive icon and throw an error.', function () {
  168. const updateIcons = prepare.__get__('updateIcons');
  169. // mock data.
  170. cordovaProject.projectConfig.getIcons = function () {
  171. return [mockGetIconItem({
  172. density: 'mdpi',
  173. foreground: 'res/icon/android/mdpi-foreground.png'
  174. })];
  175. };
  176. expect(function () {
  177. updateIcons(cordovaProject, platformResourcesDir);
  178. }).toThrow(
  179. new CordovaError('One of the following attributes are set but missing the other for the density type: mdpi. Please ensure that all require attributes are defined.')
  180. );
  181. });
  182. it('Test#005 : Should detech incorrect configrations and throw an error.', function () {
  183. const updateIcons = prepare.__get__('updateIcons');
  184. // mock data.
  185. cordovaProject.projectConfig.getIcons = function () {
  186. return [mockGetIconItem({ density: 'mdpi' })];
  187. };
  188. expect(function () {
  189. updateIcons(cordovaProject, platformResourcesDir);
  190. }).toThrow(
  191. new CordovaError('One of the following attributes are set but missing the other for the density type: mdpi. Please ensure that all require attributes are defined.')
  192. );
  193. });
  194. it('Test#006 : Should display incorrect configuration with density in message.', function () {
  195. const updateIcons = prepare.__get__('updateIcons');
  196. // mock data.
  197. cordovaProject.projectConfig.getIcons = function () {
  198. return [mockGetIconItem({ density: 'mdpi' })];
  199. };
  200. expect(function () {
  201. updateIcons(cordovaProject, platformResourcesDir);
  202. }).toThrow(
  203. new CordovaError('One of the following attributes are set but missing the other for the density type: mdpi. Please ensure that all require attributes are defined.')
  204. );
  205. });
  206. it('Test#007 : Should display incorrect configuration with size in message from height.', function () {
  207. const updateIcons = prepare.__get__('updateIcons');
  208. // mock data.
  209. cordovaProject.projectConfig.getIcons = function () {
  210. return [mockGetIconItem({ height: '192' })];
  211. };
  212. expect(function () {
  213. updateIcons(cordovaProject, platformResourcesDir);
  214. }).toThrow(
  215. new CordovaError('One of the following attributes are set but missing the other for the density type: size=192. Please ensure that all require attributes are defined.')
  216. );
  217. });
  218. it('Test#008 : Should display incorrect configuration with size in message from width.', function () {
  219. const updateIcons = prepare.__get__('updateIcons');
  220. // mock data.
  221. cordovaProject.projectConfig.getIcons = function () {
  222. return [mockGetIconItem({ width: '192' })];
  223. };
  224. expect(function () {
  225. updateIcons(cordovaProject, platformResourcesDir);
  226. }).toThrow(
  227. new CordovaError('One of the following attributes are set but missing the other for the density type: size=192. Please ensure that all require attributes are defined.')
  228. );
  229. });
  230. it('Test#009 : Should detech incorrect configrations (missing background) for adaptive icon and throw an error.', function () {
  231. const updateIcons = prepare.__get__('updateIcons');
  232. // mock data.
  233. cordovaProject.projectConfig.getIcons = function () {
  234. return [mockGetIconItem({
  235. density: 'mdpi',
  236. foreground: 'res/icon/android/mdpi-foreground.png'
  237. })];
  238. };
  239. expect(function () {
  240. updateIcons(cordovaProject, platformResourcesDir);
  241. }).toThrow(
  242. new CordovaError('One of the following attributes are set but missing the other for the density type: mdpi. Please ensure that all require attributes are defined.')
  243. );
  244. });
  245. it('Test#010 : Should detech adaptive icon with vector foreground and throws error for missing backwards compatability settings.', function () {
  246. const updateIcons = prepare.__get__('updateIcons');
  247. // mock data.
  248. cordovaProject.projectConfig.getIcons = function () {
  249. return [mockGetIconItem({
  250. density: 'mdpi',
  251. background: 'res/icon/android/mdpi-background.png',
  252. foreground: 'res/icon/android/mdpi-foreground.xml'
  253. })];
  254. };
  255. expect(function () {
  256. updateIcons(cordovaProject, platformResourcesDir);
  257. }).toThrow(
  258. new CordovaError('For the following icons with the density of: mdpi, adaptive foreground with a defined color or vector can not be used as a standard fallback icon for older Android devices. To support older Android environments, please provide a value for the src attribute.')
  259. );
  260. });
  261. it('Test#011 : Should detech adaptive icon with color foreground and throws error for missing backwards compatability settings.', function () {
  262. const updateIcons = prepare.__get__('updateIcons');
  263. // mock data.
  264. cordovaProject.projectConfig.getIcons = function () {
  265. return [mockGetIconItem({
  266. density: 'mdpi',
  267. background: 'res/icon/android/mdpi-background.png',
  268. foreground: '@color/background'
  269. })];
  270. };
  271. expect(function () {
  272. updateIcons(cordovaProject, platformResourcesDir);
  273. }).toThrow(
  274. new CordovaError('For the following icons with the density of: mdpi, adaptive foreground with a defined color or vector can not be used as a standard fallback icon for older Android devices. To support older Android environments, please provide a value for the src attribute.')
  275. );
  276. });
  277. it('Test#012 : Should update paths with adaptive and standard icons. Standard icon comes from adaptive foreground', function () {
  278. const updateIcons = prepare.__get__('updateIcons');
  279. // mock data.
  280. cordovaProject.projectConfig.getIcons = function () {
  281. return [mockGetIconItem({
  282. density: 'mdpi',
  283. background: 'res/icon/android/mdpi-background.png',
  284. foreground: 'res/icon/android/mdpi-foreground.png'
  285. })];
  286. };
  287. // Creating Spies
  288. let resourceMap = createResourceMap();
  289. let phaseOneModification = {};
  290. phaseOneModification[path.join(PATH_RESOURCE, 'mipmap-mdpi-v26', 'ic_launcher_foreground.png')] = 'res/icon/android/mdpi-foreground.png';
  291. phaseOneModification[path.join(PATH_RESOURCE, 'mipmap-mdpi-v26', 'ic_launcher_background.png')] = 'res/icon/android/mdpi-background.png';
  292. let phaseOneUpdatedIconsForAdaptive = Object.assign({}, resourceMap, phaseOneModification);
  293. updateIconResourceForAdaptiveSpy = jasmine.createSpy('updateIconResourceForAdaptiveSpy');
  294. prepare.__set__('updateIconResourceForAdaptive', function (preparedIcons, resourceMap, platformResourcesDir) {
  295. updateIconResourceForAdaptiveSpy();
  296. return phaseOneUpdatedIconsForAdaptive;
  297. });
  298. let phaseTwoModification = {};
  299. phaseTwoModification[path.join(PATH_RESOURCE, 'mipmap-mdpi', 'ic_launcher.png')] = 'res/icon/android/mdpi-foreground.png';
  300. phaseTwoModification[path.join(PATH_RESOURCE, 'mipmap-mdpi-v26', 'ic_launcher_background.png')] = 'res/icon/android/mdpi-background.png';
  301. let phaseTwoUpdatedIconsForLegacy = Object.assign({}, phaseOneUpdatedIconsForAdaptive, phaseTwoModification);
  302. updateIconResourceForLegacySpy = jasmine.createSpy('updateIconResourceForLegacySpy');
  303. prepare.__set__('updateIconResourceForLegacy', function (preparedIcons, resourceMap, platformResourcesDir) {
  304. updateIconResourceForLegacySpy();
  305. return phaseTwoUpdatedIconsForLegacy;
  306. });
  307. updateIcons(cordovaProject, platformResourcesDir);
  308. // The emit was called
  309. expect(emitSpy).toHaveBeenCalled();
  310. // The emit message was.
  311. let actual = emitSpy.calls.argsFor(0)[1];
  312. let expected = 'Updating icons at ' + PATH_RESOURCE;
  313. expect(actual).toEqual(expected);
  314. // Expected to be called.
  315. expect(updatePathsSpy).toHaveBeenCalled();
  316. expect(updateIconResourceForAdaptiveSpy).toHaveBeenCalled();
  317. expect(updateIconResourceForLegacySpy).toHaveBeenCalled();
  318. let actualResourceMap = updatePathsSpy.calls.argsFor(0)[0];
  319. let expectedResourceMap = phaseTwoUpdatedIconsForLegacy;
  320. expect(actualResourceMap).toEqual(expectedResourceMap);
  321. });
  322. it('Test#013 : Should update paths with adaptive and standard icons.', function () {
  323. const updateIcons = prepare.__get__('updateIcons');
  324. // mock data.
  325. cordovaProject.projectConfig.getIcons = function () {
  326. return [mockGetIconItem({
  327. density: 'mdpi',
  328. src: 'res/icon/android/mdpi-icon.png',
  329. background: 'res/icon/android/mdpi-background.png',
  330. foreground: 'res/icon/android/mdpi-foreground.png'
  331. })];
  332. };
  333. // Creating Spies
  334. let resourceMap = createResourceMap();
  335. let phaseOneModification = {};
  336. phaseOneModification[path.join(PATH_RESOURCE, 'mipmap-mdpi-v26', 'ic_launcher_foreground.png')] = 'res/icon/android/mdpi-foreground.png';
  337. phaseOneModification[path.join(PATH_RESOURCE, 'mipmap-mdpi-v26', 'ic_launcher_background.png')] = 'res/icon/android/mdpi-background.png';
  338. let phaseOneUpdatedIconsForAdaptive = Object.assign({}, resourceMap, phaseOneModification);
  339. updateIconResourceForAdaptiveSpy = jasmine.createSpy('updateIconResourceForAdaptiveSpy');
  340. prepare.__set__('updateIconResourceForAdaptive', function (preparedIcons, resourceMap, platformResourcesDir) {
  341. updateIconResourceForAdaptiveSpy();
  342. return phaseOneUpdatedIconsForAdaptive;
  343. });
  344. let phaseTwoModification = {};
  345. phaseTwoModification[path.join(PATH_RESOURCE, 'mipmap-mdpi', 'ic_launcher.png')] = 'res/icon/android/mdpi-foreground.png';
  346. phaseTwoModification[path.join(PATH_RESOURCE, 'mipmap-mdpi-v26', 'ic_launcher_background.png')] = 'res/icon/android/mdpi-background.png';
  347. let phaseTwoUpdatedIconsForLegacy = Object.assign({}, phaseOneUpdatedIconsForAdaptive, phaseTwoModification);
  348. updateIconResourceForLegacySpy = jasmine.createSpy('updateIconResourceForLegacySpy');
  349. prepare.__set__('updateIconResourceForLegacy', function (preparedIcons, resourceMap, platformResourcesDir) {
  350. updateIconResourceForLegacySpy();
  351. return phaseTwoUpdatedIconsForLegacy;
  352. });
  353. updateIcons(cordovaProject, platformResourcesDir);
  354. // The emit was called
  355. expect(emitSpy).toHaveBeenCalled();
  356. // The emit message was.
  357. let actual = emitSpy.calls.argsFor(0)[1];
  358. let expected = 'Updating icons at ' + PATH_RESOURCE;
  359. expect(actual).toEqual(expected);
  360. // Expected to be called.
  361. expect(updatePathsSpy).toHaveBeenCalled();
  362. expect(updateIconResourceForAdaptiveSpy).toHaveBeenCalled();
  363. expect(updateIconResourceForLegacySpy).toHaveBeenCalled();
  364. let actualResourceMap = updatePathsSpy.calls.argsFor(0)[0];
  365. let expectedResourceMap = phaseTwoUpdatedIconsForLegacy;
  366. expect(actualResourceMap).toEqual(expectedResourceMap);
  367. });
  368. it('Test#014 : Should update paths with standard icons.', function () {
  369. const updateIcons = prepare.__get__('updateIcons');
  370. // mock data.
  371. cordovaProject.projectConfig.getIcons = function () {
  372. return [mockGetIconItem({
  373. density: 'mdpi',
  374. src: 'res/icon/android/mdpi-icon.png'
  375. })];
  376. };
  377. // Creating Spies
  378. let phaseOneUpdatedIconsForAdaptive = createResourceMap();
  379. updateIconResourceForAdaptiveSpy = jasmine.createSpy('updateIconResourceForAdaptiveSpy');
  380. prepare.__set__('updateIconResourceForAdaptive', function (preparedIcons, resourceMap, platformResourcesDir) {
  381. updateIconResourceForAdaptiveSpy();
  382. return phaseOneUpdatedIconsForAdaptive;
  383. });
  384. let phaseTwoModification = {};
  385. phaseTwoModification[path.join(PATH_RESOURCE, 'mipmap-mdpi', 'ic_launcher.png')] = 'res/icon/android/mdpi-icon.png';
  386. let phaseTwoUpdatedIconsForLegacy = Object.assign({}, phaseOneUpdatedIconsForAdaptive, phaseTwoModification);
  387. updateIconResourceForLegacySpy = jasmine.createSpy('updateIconResourceForLegacySpy');
  388. prepare.__set__('updateIconResourceForLegacy', function (preparedIcons, resourceMap, platformResourcesDir) {
  389. updateIconResourceForLegacySpy();
  390. return phaseTwoUpdatedIconsForLegacy;
  391. });
  392. updateIcons(cordovaProject, platformResourcesDir);
  393. // The emit was called
  394. expect(emitSpy).toHaveBeenCalled();
  395. // The emit message was.
  396. let actual = emitSpy.calls.argsFor(0)[1];
  397. let expected = 'Updating icons at ' + PATH_RESOURCE;
  398. expect(actual).toEqual(expected);
  399. // Expected to be called.
  400. expect(updatePathsSpy).toHaveBeenCalled();
  401. expect(updateIconResourceForAdaptiveSpy).not.toHaveBeenCalled();
  402. expect(updateIconResourceForLegacySpy).toHaveBeenCalled();
  403. let actualResourceMap = updatePathsSpy.calls.argsFor(0)[0];
  404. let expectedResourceMap = phaseTwoUpdatedIconsForLegacy;
  405. expect(actualResourceMap).toEqual(expectedResourceMap);
  406. });
  407. });
  408. describe('prepareIcons method', function () {
  409. let prepare;
  410. let emitSpy;
  411. let prepareIcons;
  412. beforeEach(function () {
  413. prepare = rewire('../../bin/templates/cordova/lib/prepare');
  414. prepareIcons = prepare.__get__('prepareIcons');
  415. // Creating Spies
  416. emitSpy = jasmine.createSpy('emit');
  417. prepare.__set__('events', {
  418. emit: emitSpy
  419. });
  420. });
  421. it('Test#001 : should emit extra default icon found for adaptive use case.', function () {
  422. // mock data.
  423. let ldpi = mockGetIconItem({
  424. density: 'ldpi',
  425. background: 'res/icon/android/ldpi-background.png',
  426. foreground: 'res/icon/android/ldpi-foreground.png'
  427. });
  428. let mdpi = mockGetIconItem({
  429. density: 'mdpi',
  430. background: 'res/icon/android/mdpi-background.png',
  431. foreground: 'res/icon/android/mdpi-foreground.png'
  432. });
  433. let icons = [ldpi, mdpi];
  434. let actual = prepareIcons(icons);
  435. let expected = {
  436. android_icons: { ldpi, mdpi },
  437. default_icon: undefined
  438. };
  439. expect(expected).toEqual(actual);
  440. });
  441. it('Test#002 : should emit extra default icon found for legacy use case.', function () {
  442. // mock data.
  443. let ldpi = mockGetIconItem({
  444. src: 'res/icon/android/ldpi-icon.png',
  445. density: 'ldpi'
  446. });
  447. let mdpi = mockGetIconItem({
  448. src: 'res/icon/android/mdpi-icon.png',
  449. density: 'mdpi'
  450. });
  451. let icons = [ldpi, mdpi];
  452. let actual = prepareIcons(icons);
  453. let expected = {
  454. android_icons: { ldpi, mdpi },
  455. default_icon: undefined
  456. };
  457. expect(expected).toEqual(actual);
  458. });
  459. });
  460. describe('updateIconResourceForLegacy method', function () {
  461. let prepare;
  462. // Spies
  463. let fsWriteFileSyncSpy;
  464. // Mock Data
  465. let platformResourcesDir;
  466. let preparedIcons;
  467. let resourceMap;
  468. beforeEach(function () {
  469. prepare = rewire('../../bin/templates/cordova/lib/prepare');
  470. // Mocked Data
  471. platformResourcesDir = PATH_RESOURCE;
  472. preparedIcons = {
  473. android_icons: {
  474. mdpi: mockGetIconItem({
  475. src: 'res/icon/android/mdpi-icon.png',
  476. density: 'mdpi'
  477. })
  478. },
  479. default_icon: undefined
  480. };
  481. resourceMap = createResourceMap();
  482. fsWriteFileSyncSpy = jasmine.createSpy('writeFileSync');
  483. prepare.__set__('fs', {
  484. writeFileSync: fsWriteFileSyncSpy
  485. });
  486. });
  487. it('Test#001 : Should update resource map with prepared icons.', function () {
  488. // Get method for testing
  489. const updateIconResourceForLegacy = prepare.__get__('updateIconResourceForLegacy');
  490. // Run Test
  491. let expectedModification = {};
  492. expectedModification[path.join(PATH_RESOURCE, 'mipmap-mdpi', 'ic_launcher.png')] = 'res/icon/android/mdpi-icon.png';
  493. let expected = Object.assign({}, resourceMap, expectedModification);
  494. let actual = updateIconResourceForLegacy(preparedIcons, resourceMap, platformResourcesDir);
  495. expect(actual).toEqual(expected);
  496. });
  497. });
  498. describe('updateIconResourceForAdaptive method', function () {
  499. let prepare;
  500. // Spies
  501. let fsWriteFileSyncSpy;
  502. // Mock Data
  503. let platformResourcesDir;
  504. let preparedIcons;
  505. let resourceMap;
  506. beforeEach(function () {
  507. prepare = rewire('../../bin/templates/cordova/lib/prepare');
  508. // Mocked Data
  509. platformResourcesDir = PATH_RESOURCE;
  510. preparedIcons = {
  511. android_icons: {
  512. mdpi: mockGetIconItem({
  513. density: 'mdpi',
  514. background: 'res/icon/android/mdpi-background.png',
  515. foreground: 'res/icon/android/mdpi-foreground.png'
  516. })
  517. },
  518. default_icon: undefined
  519. };
  520. resourceMap = createResourceMap();
  521. fsWriteFileSyncSpy = jasmine.createSpy('writeFileSync');
  522. prepare.__set__('fs', {
  523. writeFileSync: fsWriteFileSyncSpy
  524. });
  525. });
  526. it('Test#001 : Should update resource map with prepared icons.', function () {
  527. // Get method for testing
  528. const updateIconResourceForAdaptive = prepare.__get__('updateIconResourceForAdaptive');
  529. // Run Test
  530. let expectedModification = {};
  531. expectedModification[path.join(PATH_RESOURCE, 'mipmap-mdpi-v26', 'ic_launcher_background.png')] = 'res/icon/android/mdpi-background.png';
  532. expectedModification[path.join(PATH_RESOURCE, 'mipmap-mdpi-v26', 'ic_launcher_foreground.png')] = 'res/icon/android/mdpi-foreground.png';
  533. let expected = Object.assign({}, resourceMap, expectedModification);
  534. let actual = updateIconResourceForAdaptive(preparedIcons, resourceMap, platformResourcesDir);
  535. expect(actual).toEqual(expected);
  536. });
  537. });
  538. describe('cleanIcons method', function () {
  539. let prepare;
  540. let emitSpy;
  541. let updatePathsSpy;
  542. beforeEach(function () {
  543. prepare = rewire('../../bin/templates/cordova/lib/prepare');
  544. emitSpy = jasmine.createSpy('emit');
  545. prepare.__set__('events', {
  546. emit: emitSpy
  547. });
  548. updatePathsSpy = jasmine.createSpy('updatePaths');
  549. prepare.__set__('FileUpdater', {
  550. updatePaths: updatePathsSpy
  551. });
  552. });
  553. it('Test#001 : should detect that the app does not have defined icons.', function () {
  554. // Mock
  555. let icons = [];
  556. let projectRoot = '/mock';
  557. let projectConfig = {
  558. getIcons: function () { return icons; },
  559. path: '/mock/config.xml',
  560. cdvNamespacePrefix: 'cdv'
  561. };
  562. let platformResourcesDir = PATH_RESOURCE;
  563. const cleanIcons = prepare.__get__('cleanIcons');
  564. cleanIcons(projectRoot, projectConfig, platformResourcesDir);
  565. let actualEmitMessage = emitSpy.calls.argsFor(0)[1];
  566. expect(actualEmitMessage).toContain('This app does not have launcher icons defined');
  567. });
  568. it('Test#002 : Should clean paths for adaptive icons.', function () {
  569. // Mock
  570. let icons = [mockGetIconItem({
  571. density: 'mdpi',
  572. background: 'res/icon/android/mdpi-background.png',
  573. foreground: 'res/icon/android/mdpi-foreground.png'
  574. })];
  575. let projectRoot = '/mock';
  576. let projectConfig = {
  577. getIcons: function () { return icons; },
  578. path: '/mock/config.xml',
  579. cdvNamespacePrefix: 'cdv'
  580. };
  581. let platformResourcesDir = PATH_RESOURCE;
  582. var expectedResourceMapBackground = createResourceMap('ic_launcher_background.png');
  583. // mocking initial responses for mapImageResources
  584. prepare.__set__('mapImageResources', function (rootDir, subDir, type, resourceName) {
  585. if (resourceName.includes('ic_launcher_background.png')) {
  586. return expectedResourceMapBackground;
  587. }
  588. });
  589. const cleanIcons = prepare.__get__('cleanIcons');
  590. cleanIcons(projectRoot, projectConfig, platformResourcesDir);
  591. let actualResourceMapBackground = updatePathsSpy.calls.argsFor(0)[0];
  592. expect(actualResourceMapBackground).toEqual(expectedResourceMapBackground);
  593. });
  594. it('Test#003 : Should clean paths for legacy icons.', function () {
  595. // Mock
  596. let icons = [mockGetIconItem({
  597. src: 'res/icon/android/mdpi.png',
  598. density: 'mdpi'
  599. })];
  600. let projectRoot = '/mock';
  601. let projectConfig = {
  602. getIcons: function () { return icons; },
  603. path: '/mock/config.xml',
  604. cdvNamespacePrefix: 'cdv'
  605. };
  606. let platformResourcesDir = PATH_RESOURCE;
  607. var expectedResourceMap = createResourceMap();
  608. // mocking initial responses for mapImageResources
  609. prepare.__set__('mapImageResources', function (rootDir, subDir, type, resourceName) {
  610. return expectedResourceMap;
  611. });
  612. const cleanIcons = prepare.__get__('cleanIcons');
  613. cleanIcons(projectRoot, projectConfig, platformResourcesDir);
  614. let actualResourceMap = updatePathsSpy.calls.argsFor(0)[0];
  615. expect(actualResourceMap).toEqual(expectedResourceMap);
  616. });
  617. });