Skip to content

Commit

Permalink
CB-11023 Add edit-config functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ktop committed Jun 16, 2016
1 parent 569983e commit 1ed0e93
Show file tree
Hide file tree
Showing 11 changed files with 525 additions and 47 deletions.
85 changes: 85 additions & 0 deletions cordova-common/spec/ConfigChanges/ConfigChanges.spec.js
Expand Up @@ -32,6 +32,8 @@ var configChanges = require('../../src/ConfigChanges/ConfigChanges'),
childrenplugin = path.join(__dirname, '../fixtures/plugins/org.test.multiple-children'),
shareddepsplugin = path.join(__dirname, '../fixtures/plugins/org.test.shareddeps'),
configplugin = path.join(__dirname, '../fixtures/plugins/org.test.configtest'),
editconfigplugin = path.join(__dirname, '../fixtures/plugins/org.test.editconfigtest'),
editconfigplugin_two = path.join(__dirname, '../fixtures/plugins/org.test.editconfigtest_two'),
varplugin = path.join(__dirname, '../fixtures/plugins/com.adobe.vars'),
plistplugin = path.join(__dirname, '../fixtures/plugins/org.apache.plist'),
android_two_project = path.join(__dirname, '../fixtures/projects/android_two/*'),
Expand Down Expand Up @@ -273,6 +275,70 @@ describe('config-changes module', function() {
munger.process(plugins_dir);
expect(spy).not.toHaveBeenCalledWith(path.join(temp, 'res', 'xml', 'plugins.xml'), 'utf-8');
});
it('should call graftXMLMerge for every new config munge with mode \'merge\' it introduces', function() {
shell.cp('-rf', editconfigplugin, plugins_dir);
var platformJson = PlatformJson.load(plugins_dir, 'android');
platformJson.addInstalledPluginToPrepareQueue('org.test.editconfigtest', {});

var spy = spyOn(xml_helpers, 'graftXMLMerge').andReturn(true);

var munger = new configChanges.PlatformMunger('android', temp, platformJson, pluginInfoProvider);
munger.process(plugins_dir);
expect(spy.calls.length).toEqual(1);
expect(spy.argsForCall[0][2]).toEqual('/manifest/application/activity[@android:name=\'org.test.DroidGap\']');
});
it('should call graftXMLMerge with --force for every new config munge with mode \'merge\' it introduces', function() {
shell.cp('-rf', editconfigplugin, plugins_dir);
shell.cp('-rf', editconfigplugin_two, plugins_dir);
var platformJson = PlatformJson.load(plugins_dir, 'android');
platformJson.addInstalledPluginToPrepareQueue('org.test.editconfigtest', {});
platformJson.addInstalledPluginToPrepareQueue('org.test.editconfigtest_two', {}, true, true);

var spy = spyOn(xml_helpers, 'graftXMLMerge').andReturn(true);

var munger = new configChanges.PlatformMunger('android', temp, platformJson, pluginInfoProvider);
munger.process(plugins_dir);
expect(spy.calls.length).toEqual(2);
expect(spy.argsForCall[0][2]).toEqual('/manifest/application/activity[@android:name=\'org.test.DroidGap\']');
expect(spy.argsForCall[1][2]).toEqual('/manifest/application/activity[@android:name=\'org.test.DroidGap\']');
});
it('should call graftXMLOverwrite for every new config munge with mode \'overwrite\' it introduces', function() {
shell.cp('-rf', editconfigplugin, plugins_dir);
var platformJson = PlatformJson.load(plugins_dir, 'android');
platformJson.addInstalledPluginToPrepareQueue('org.test.editconfigtest', {});

var spy = spyOn(xml_helpers, 'graftXMLOverwrite').andReturn(true);

var munger = new configChanges.PlatformMunger('android', temp, platformJson, pluginInfoProvider);
munger.process(plugins_dir);
expect(spy.calls.length).toEqual(1);
expect(spy.argsForCall[0][2]).toEqual('/manifest/application/activity');
});
it('should call graftXMLOverwrite with --force for every new config munge with mode \'overwrite\' it introduces', function() {
shell.cp('-rf', editconfigplugin, plugins_dir);
shell.cp('-rf', editconfigplugin_two, plugins_dir);
var platformJson = PlatformJson.load(plugins_dir, 'android');
platformJson.addInstalledPluginToPrepareQueue('org.test.editconfigtest', {});
platformJson.addInstalledPluginToPrepareQueue('org.test.editconfigtest_two', {}, true, true);

var spy = spyOn(xml_helpers, 'graftXMLOverwrite').andReturn(true);

var munger = new configChanges.PlatformMunger('android', temp, platformJson, pluginInfoProvider);
munger.process(plugins_dir);
expect(spy.calls.length).toEqual(2);
expect(spy.argsForCall[0][2]).toEqual('/manifest/application/activity');
expect(spy.argsForCall[1][2]).toEqual('/manifest/application/activity[@android:name=\'ChildApp\']');
});
it('should not install plugin when there are edit-config conflicts', function() {
shell.cp('-rf', editconfigplugin, plugins_dir);
shell.cp('-rf', editconfigplugin_two, plugins_dir);
var platformJson = PlatformJson.load(plugins_dir, 'android');
platformJson.addInstalledPluginToPrepareQueue('org.test.editconfigtest', {});
platformJson.addInstalledPluginToPrepareQueue('org.test.editconfigtest_two', {});

var munger = new configChanges.PlatformMunger('android', temp, platformJson, pluginInfoProvider);
expect(function() {munger.process(plugins_dir);}).toThrow(new Error('There was a conflict trying to modify attributes with <edit-config> in plugin org.test.editconfigtest_two. The conflicting plugin, org.test.editconfigtest, already modified the same attributes. The conflict must be resolved before org.test.editconfigtest_two can be added. You may use --force to add the plugin and overwrite the conflicting attributes.'));
});
});
describe('of plist config files', function() {
it('should write empty string nodes with no whitespace', function() {
Expand Down Expand Up @@ -411,6 +477,25 @@ describe('config-changes module', function() {
expect(platformJson.root.prepare_queue.uninstalled.length).toEqual(0);
expect(platformJson.root.installed_plugins['com.adobe.vars']).not.toBeDefined();
});
it('should call pruneXMLRestore for every config munge with mode \'merge\' or \'overwrite\' it removes from the app', function() {
shell.cp('-rf', android_two_project, temp);
shell.cp('-rf', editconfigplugin, plugins_dir);

// Run through an "install"
var platformJson = PlatformJson.load(plugins_dir, 'android');
platformJson.addInstalledPluginToPrepareQueue('org.test.editconfigtest', {});
var munger = new configChanges.PlatformMunger('android', temp, platformJson, pluginInfoProvider);
munger.process(plugins_dir);

// Now set up an uninstall and make sure pruneXMLMerge is called properly
platformJson.addUninstalledPluginToPrepareQueue('org.test.editconfigtest');
var spy = spyOn(xml_helpers, 'pruneXMLRestore').andReturn(true);
munger.process(plugins_dir);

expect(spy.calls.length).toEqual(2);
expect(spy.argsForCall[0][1]).toEqual('/manifest/application/activity[@android:name=\'org.test.DroidGap\']');
expect(spy.argsForCall[1][1]).toEqual('/manifest/application/activity');
});
});
});
});
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2013 Anis Kadri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

<plugin xmlns="http://cordova.apache.org/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="org.test.editconfigtest"
version="3.0.0">

<name>Test edit-config</name>

<!-- android -->
<platform name="android">
<edit-config file="AndroidManifest.xml" target="/manifest/application/activity[@android:name='org.test.DroidGap']" mode="merge">
<activity android:enabled="true" android:configChanges="keyboardHidden" />
</edit-config>
<edit-config file="AndroidManifest.xml" target="/manifest/application/activity" mode="overwrite">
<activity android:name="ChildApp" android:label="@string/app_name" android:enabled="true" />
</edit-config>
</platform>
</plugin>
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2013 Anis Kadri
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

<plugin xmlns="http://cordova.apache.org/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="org.test.editconfigtest_two"
version="3.0.0">

<name>Test edit-config with conflicts</name>

<!-- android -->
<platform name="android">
<edit-config file="AndroidManifest.xml" target="/manifest/application/activity[@android:name='org.test.DroidGap']" mode="merge">
<activity android:enabled="true" android:configChanges="orientation|keyboardHidden" />
</edit-config>
<edit-config file="AndroidManifest.xml" target="/manifest/application/activity[@android:name='ChildApp']" mode="overwrite">
<activity android:name="ChildApp" android:label="@string/app_name" android:enabled="false" />
</edit-config>
</platform>
</plugin>

0 comments on commit 1ed0e93

Please sign in to comment.