UNPKG

13 kBJavaScriptView Raw
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
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18*/
19
20var BaseMunger = require('cordova-common').ConfigChanges.PlatformMunger;
21var PlatformMunger = require('../../template/cordova/lib/ConfigChanges').PlatformMunger;
22var PluginInfoProvider = require('cordova-common').PluginInfoProvider;
23var PluginInfo = require('../../template/cordova/lib/PluginInfo.js').PluginInfo;
24var Api = require('../../template/cordova/Api');
25var AppxManifest = require('../../template/cordova/lib/AppxManifest');
26
27var os = require('os');
28var path = require('path');
29var shell = require('shelljs');
30
31var configChanges = require('../../template/cordova/lib/ConfigChanges');
32var tempDir = path.join(os.tmpdir(), 'windows');
33var WINDOWS_MANIFEST = 'package.windows.appxmanifest';
34var WINDOWS10_MANIFEST = 'package.windows10.appxmanifest';
35var FIXTURES = path.join(__dirname, 'fixtures');
36var DUMMY_PLUGIN = 'org.test.plugins.capabilityplugin';
37var CONFIG_PLUGIN = 'org.test.configtest';
38
39var dummyPlugin = path.join(FIXTURES, DUMMY_PLUGIN);
40var configplugin = path.join(FIXTURES, CONFIG_PLUGIN);
41var dummyProjName = 'testProj';
42var windowsProject = path.join(FIXTURES, dummyProjName);
43var windows_testapp_jsproj = path.join(FIXTURES, 'testProj/platforms/windows/CordovaApp.Windows.jsproj');
44
45describe('PlatformMunger', function () {
46 var munge, munger;
47
48 beforeEach(function () {
49 shell.mkdir('-p', tempDir);
50 munge = { parents: { 'foo/bar': [
51 { before: undefined, count: 1, xml: '<DummyElement name="Dummy" />' }
52 ] } };
53 munger = new PlatformMunger('windows', tempDir);
54 spyOn(BaseMunger.prototype, 'apply_file_munge').and.callThrough();
55 });
56
57 afterEach(function () {
58 shell.rm('-rf', tempDir);
59 });
60
61 describe('apply_file_munge method', function () {
62
63 it('should call parent\'s method with the same parameters', function () {
64 munger.apply_file_munge(WINDOWS_MANIFEST, munge, false);
65 expect(BaseMunger.prototype.apply_file_munge).toHaveBeenCalledWith(WINDOWS_MANIFEST, munge, false);
66 });
67
68 it('should additionally call parent\'s method with another munge if removing changes from windows 10 appxmanifest', function () {
69 munger.apply_file_munge(WINDOWS10_MANIFEST, munge, /* remove= */true);
70 expect(BaseMunger.prototype.apply_file_munge).toHaveBeenCalledWith(WINDOWS10_MANIFEST, munge, true);
71 });
72
73 it('should remove uap: capabilities added by windows prepare step', function () {
74 // Generate a munge that contain non-prefixed capabilities changes
75 var baseMunge = { parents: { '/Package/Capabilities': [
76 // Emulate capability that was initially added with uap prefix
77 { before: undefined, count: 1, xml: '<uap:Capability Name=\"privateNetworkClientServer\">' }, /* eslint no-useless-escape : 0 */
78 { before: undefined, count: 1, xml: '<Capability Name=\"enterpriseAuthentication\">' } /* eslint no-useless-escape : 0 */
79 ] } };
80
81 var capabilitiesMunge = { parents: { '/Package/Capabilities': [
82 { before: undefined, count: 1, xml: '<uap:Capability Name=\"privateNetworkClientServer\">' },
83 { before: undefined, count: 1, xml: '<uap:Capability Name=\"enterpriseAuthentication\">' }
84 ] } };
85 munger.apply_file_munge(WINDOWS10_MANIFEST, baseMunge, /* remove= */true);
86 expect(BaseMunger.prototype.apply_file_munge).toHaveBeenCalledWith(WINDOWS10_MANIFEST, capabilitiesMunge, true);
87 });
88 });
89});
90
91describe('Capabilities within package.windows.appxmanifest', function () {
92
93 var testDir, windowsPlatform, windowsManifest, windowsManifest10, dummyPluginInfo, api;
94
95 beforeEach(function () {
96 testDir = path.join(__dirname, 'testDir');
97 shell.mkdir('-p', testDir);
98 shell.cp('-rf', windowsProject + '/*', testDir);
99 windowsPlatform = path.join(testDir, 'platforms/windows');
100 windowsManifest = path.join(windowsPlatform, WINDOWS_MANIFEST);
101 windowsManifest10 = path.join(windowsPlatform, WINDOWS10_MANIFEST);
102 dummyPluginInfo = new PluginInfo(dummyPlugin);
103 api = new Api();
104 api.root = windowsPlatform;
105 api.locations.root = windowsPlatform;
106 api.locations.www = path.join(windowsPlatform, 'www');
107 });
108
109 afterEach(function () {
110 shell.rm('-rf', testDir);
111 });
112
113 function getPluginCapabilities (pluginInfo) {
114 return pluginInfo.getConfigFiles()[0].xmls;
115 }
116
117 function getManifestCapabilities (manifest) {
118 var appxmanifest = AppxManifest.get(manifest, true);
119 return appxmanifest.getCapabilities();
120 }
121
122 var fail = jasmine.createSpy('fail')
123 .and.callFake(function (err) {
124 console.error(err);
125 });
126
127 it('should be removed using overriden PlatformMunger', function (done) {
128 api.addPlugin(dummyPluginInfo)
129 .then(function () {
130 // There is the one default capability in manifest with 'internetClient' name
131 expect(getManifestCapabilities(windowsManifest).length).toBe(getPluginCapabilities(dummyPluginInfo).length + 1);
132 api.removePlugin(dummyPluginInfo);
133 })
134 .then(function () {
135 expect(getManifestCapabilities(windowsManifest).length).toBe(1);
136 })
137 .catch(fail)
138 .finally(function () {
139 expect(fail).not.toHaveBeenCalled();
140 done();
141 });
142 });
143
144 it('should be added with uap prefixes when install plugin', function (done) {
145 api.addPlugin(dummyPluginInfo)
146 .then(function () {
147 // There is the one default capability in manifest with 'internetClient' name
148 var manifestCapabilities = getManifestCapabilities(windowsManifest10);
149 expect(manifestCapabilities.length).toBe(getPluginCapabilities(dummyPluginInfo).length + 1);
150
151 // Count 'uap' prefixed capabilities
152 var uapPrefixedCapsCount = manifestCapabilities.filter(function (capability) {
153 return capability.type === 'uap:Capability';
154 }).length;
155
156 expect(uapPrefixedCapsCount).toBe(2);
157 api.removePlugin(dummyPluginInfo);
158 })
159 .then(function () {
160 expect(getManifestCapabilities(windowsManifest10).length).toBe(1);
161 })
162 .catch(fail)
163 .finally(function () {
164 expect(fail).not.toHaveBeenCalled();
165 done();
166 });
167 });
168
169 it('should be added as DeviceCapabilities when install plugin', function (done) {
170 function isDeviceCapability (capability) {
171 return capability.type === 'DeviceCapability';
172 }
173
174 function checkCapabilitiesAfterInstall (manifest) {
175 // There is the one default capability in manifest with 'internetClient' name
176 var manifestCapabilities = getManifestCapabilities(manifest);
177 var pluginCapabilities = getPluginCapabilities(dummyPluginInfo);
178
179 expect(manifestCapabilities.length).toBe(pluginCapabilities.length + 1);
180
181 var manifestDeviceCapabilties = manifestCapabilities.filter(isDeviceCapability);
182 expect(manifestDeviceCapabilties.length).toBe(1);
183 }
184
185 function checkCapabilitiesAfterRemove (manifest) {
186 var manifestCapabilities = getManifestCapabilities(manifest);
187 expect(manifestCapabilities.length).toBe(1);
188 }
189
190 api.addPlugin(dummyPluginInfo)
191 .then(function () {
192 checkCapabilitiesAfterInstall(windowsManifest);
193 checkCapabilitiesAfterInstall(windowsManifest10);
194 api.removePlugin(dummyPluginInfo);
195 })
196 .then(function () {
197 checkCapabilitiesAfterRemove(windowsManifest);
198 checkCapabilitiesAfterRemove(windowsManifest10);
199 })
200 .catch(fail)
201 .finally(function () {
202 expect(fail).not.toHaveBeenCalled();
203 done();
204 });
205 });
206});
207
208describe('generate_plugin_config_munge for windows project', function () {
209 beforeEach(function () {
210 shell.mkdir('-p', tempDir);
211 shell.cp('-rf', windows_testapp_jsproj, tempDir);
212 });
213
214 afterEach(function () {
215 shell.rm('-rf', tempDir);
216 });
217
218 it('should special case config-file elements for windows', function () {
219 var pluginInfoProvider = new PluginInfoProvider();
220 var munger = new configChanges.PlatformMunger('windows', tempDir, 'unused', null, pluginInfoProvider);
221 var munge = munger.generate_plugin_config_munge(new PluginInfo(configplugin), {});
222 var windows81AppxManifest = munge.files['package.windows.appxmanifest'];
223 var winphone81AppxManifest = munge.files['package.phone.appxmanifest'];
224 var windows10AppxManifest = munge.files['package.windows10.appxmanifest'];
225
226 // 1 comes from versions="=8.1.0" + 1 from versions="=8.1.0" device-target="windows"
227 expect(windows81AppxManifest.parents['/Parent/Capabilities'][0].xml).toBe('<Capability Note="should-exist-for-all-appxmanifest-target-files" />');
228 expect(windows81AppxManifest.parents['/Parent/Capabilities'][1].xml).toBe('<Capability Note="should-exist-for-win81-win-and-phone" />');
229 expect(windows81AppxManifest.parents['/Parent/Capabilities'][2].xml).toBe('<Capability Note="should-exist-for-win81-win-only" />');
230 expect(windows81AppxManifest.parents['/Parent/Capabilities'][3].xml).toBe('<Capability Note="should-exist-for-win10-and-win81-win-and-phone" />');
231 expect(windows81AppxManifest.parents['/Parent/Capabilities'].length).toBe(4);
232
233 // 1 comes from versions="=8.1.0" + 1 from versions="=8.1.0" device-target="phone"
234 expect(winphone81AppxManifest.parents['/Parent/Capabilities'][0].xml).toBe('<Capability Note="should-exist-for-all-appxmanifest-target-files" />');
235 expect(winphone81AppxManifest.parents['/Parent/Capabilities'][1].xml).toBe('<Capability Note="should-exist-for-win81-win-and-phone" />');
236 expect(winphone81AppxManifest.parents['/Parent/Capabilities'][2].xml).toBe('<Capability Note="should-exist-for-win81-phone-only" />');
237 expect(winphone81AppxManifest.parents['/Parent/Capabilities'][3].xml).toBe('<Capability Note="should-exist-for-win10-and-win81-win-and-phone" />');
238 expect(winphone81AppxManifest.parents['/Parent/Capabilities'].length).toBe(4);
239
240 expect(windows10AppxManifest.parents['/Parent/Capabilities'][0].xml).toBe('<Capability Note="should-exist-for-all-appxmanifest-target-files" />');
241 expect(windows10AppxManifest.parents['/Parent/Capabilities'][1].xml).toBe('<Capability Note="should-exist-for-win10-and-win81-win-and-phone" />');
242 expect(windows10AppxManifest.parents['/Parent/Capabilities'][2].xml).toBe('<Capability Note="should-exist-in-win10-only" />');
243 expect(windows10AppxManifest.parents['/Parent/Capabilities'].length).toBe(3);
244 });
245
246 it('should not process change w/o target package.appxmanifest', function () {
247 var testChanges = [
248 {
249 target: 'package.windows.appxmanifest'
250 },
251 {
252 target: 'package.appxmanifest'
253 }
254 ];
255
256 var changes = AppxManifest.processChanges(testChanges);
257 expect(changes.length).toBe(4);
258 expect(changes[0].target).toBe(testChanges[0].target);
259 });
260
261 it('should apply changes to all manifests in case of incorrect "deviceTarget" attribute', function () {
262 var testChanges = [{
263 deviceTarget: 'wrong_device_target',
264 target: 'package.appxmanifest'
265 }];
266
267 var changes = AppxManifest.processChanges(testChanges);
268 expect(changes.length).toBe(3);
269 expect(changes[0].target).toBe('package.windows.appxmanifest');
270 expect(changes[1].target).toBe('package.phone.appxmanifest');
271 expect(changes[2].target).toBe('package.windows10.appxmanifest');
272 });
273});