You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by GitBox <gi...@apache.org> on 2018/06/22 00:17:08 UTC

[GitHub] Menardi commented on a change in pull request #456: More android tests

Menardi commented on a change in pull request #456: More android tests
URL: https://github.com/apache/cordova-android/pull/456#discussion_r197310236
 
 

 ##########
 File path: spec/unit/AndroidManifest.spec.js
 ##########
 @@ -0,0 +1,311 @@
+/**
+    Licensed to the Apache Software Foundation (ASF) under one
+    or more contributor license agreements.  See the NOTICE file
+    distributed with this work for additional information
+    regarding copyright ownership.  The ASF licenses this file
+    to you 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.
+*/
+
+const fs = require('fs');
+const os = require('os');
+const path = require('path');
+const rewire = require('rewire');
+
+describe('AndroidManifest', () => {
+    const VERSION_CODE = '50407';
+    const VERSION_NAME = '5.4.7';
+    const PACKAGE_ID = 'io.cordova.test';
+    const ACTIVITY_LAUNCH_MODE = 'singleTop';
+    const ACTIVITY_NAME = 'MainActivity';
+    const ACTIVITY_ORIENTATION = 'portrait';
+    const MIN_SDK_VERSION = '12';
+    const MAX_SDK_VERSION = '88';
+    const TARGET_SDK_VERSION = '27';
+
+    const DEFAULT_MANIFEST = `<?xml version='1.0' encoding='utf-8'?>
+<manifest android:hardwareAccelerated="true" android:versionCode="${VERSION_CODE}" android:versionName="${VERSION_NAME}" package="${PACKAGE_ID}" xmlns:android="http://schemas.android.com/apk/res/android">
+    <supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
+    <uses-permission android:name="android.permission.INTERNET" />
+    <application android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:supportsRtl="true" android:debuggable="true">
+        <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="${ACTIVITY_LAUNCH_MODE}" android:name="${ACTIVITY_NAME}" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize" android:screenOrientation="${ACTIVITY_ORIENTATION}">
+            <intent-filter android:label="@string/launcher_name">
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+    </application>
+    <uses-sdk android:minSdkVersion="${MIN_SDK_VERSION}" android:maxSdkVersion="${MAX_SDK_VERSION}" android:targetSdkVersion="${TARGET_SDK_VERSION}" />
+</manifest>`;
+
+    const manifestPath = path.join(os.tmpdir(), `AndroidManifest${Date.now()}.xml`);
+
+    function createTempManifestFile (xml) {
+        fs.writeFileSync(manifestPath, xml);
+    }
+
+    function removeTempManifestFile () {
+        fs.unlinkSync(manifestPath);
+    }
+
+    let AndroidManifest;
+    let manifest;
+
+    beforeEach(() => {
+        createTempManifestFile(DEFAULT_MANIFEST);
+
+        AndroidManifest = rewire('../../bin/templates/cordova/lib/AndroidManifest');
+        manifest = new AndroidManifest(manifestPath);
+    });
+
+    afterEach(() => {
+        removeTempManifestFile();
+    });
+
+    describe('constructor', () => {
+        it('should parse the manifest', () => {
+            expect(manifest.doc.getroot().tag).toBe('manifest');
+        });
+
+        it('should throw an error if not a valid manifest', () => {
+            createTempManifestFile(`<?xml version='1.0' encoding='utf-8'?><notamanifest></notamanifest>`);
+
+            try {
+                manifest = new AndroidManifest(manifestPath);
+                fail('Expected error to be thrown');
+            } catch (e) {
+                // Pass
+            }
+        });
+    });
+
+    describe('versionName', () => {
+        it('should get the version name', () => {
+            expect(manifest.getVersionName()).toBe(VERSION_NAME);
+        });
+
+        it('should set the version name', () => {
+            manifest.setVersionName('6.1.0');
+            expect(manifest.getVersionName()).toBe('6.1.0');
+        });
+    });
+
+    describe('versionCode', () => {
+        it('should get the version code', () => {
+            expect(manifest.getVersionCode()).toBe(VERSION_CODE);
+        });
+
+        it('should set the version code', () => {
+            manifest.setVersionCode('95426');
+            expect(manifest.getVersionCode()).toBe('95426');
+        });
+    });
+
+    describe('packageId', () => {
+        it('should get the package ID', () => {
+            expect(manifest.getPackageId()).toBe(PACKAGE_ID);
+        });
+
+        it('should set the package ID', () => {
+            manifest.setPackageId('io.cordova.newtest');
+            expect(manifest.getPackageId()).toBe('io.cordova.newtest');
+        });
+    });
+
+    describe('activity', () => {
+        let activity;
+
+        beforeEach(() => {
+            activity = manifest.getActivity();
+        });
+
+        describe('name', () => {
+            it('should get the activity name', () => {
+                expect(activity.getName()).toBe(ACTIVITY_NAME);
+            });
+
+            it('should set the activity name', () => {
+                activity.setName('NewActivity');
+                expect(activity.getName()).toBe('NewActivity');
+            });
+
+            it('should remove the activity name if set to empty', () => {
+                activity.setName();
+                expect(activity.getName()).toBe(undefined);
+            });
+        });
+
+        describe('orientation', () => {
+            it('should get the activity orientation', () => {
+                expect(activity.getOrientation()).toBe(ACTIVITY_ORIENTATION);
+            });
+
+            it('should set the activity orienation', () => {
+                activity.setOrientation('landscape');
+                expect(activity.getOrientation()).toBe('landscape');
+            });
+
+            it('should remove the orientation if set to default', () => {
+                activity.setOrientation(AndroidManifest.__get__('DEFAULT_ORIENTATION'));
+                expect(activity.getOrientation()).toBe(undefined);
+            });
+
+            it('should remove the orientation if set to empty', () => {
+                activity.setOrientation();
+                expect(activity.getOrientation()).toBe(undefined);
+            });
+        });
+
+        describe('launch mode', () => {
+            it('should get the activity launch mode', () => {
+                expect(activity.getLaunchMode()).toBe(ACTIVITY_LAUNCH_MODE);
+            });
+
+            it('should set the activity launch mode', () => {
+                activity.setLaunchMode('standard');
+                expect(activity.getLaunchMode()).toBe('standard');
+            });
+
+            it('should remove the launch mode if set to empty', () => {
+                activity.setLaunchMode();
+                expect(activity.getLaunchMode()).toBe(undefined);
+            });
+        });
+    });
+
+    describe('minSdkVersion', () => {
+        it('should get minSdkVersion', () => {
+            expect(manifest.getMinSdkVersion()).toBe(MIN_SDK_VERSION);
+        });
+
+        it('should set minSdkVersion', () => {
+            manifest.setMinSdkVersion(1);
+            expect(manifest.getMinSdkVersion()).toBe(1);
+        });
+
+        it('should create the uses-sdk node if it does not exist when setting minSdkVersion', () => {
+            const NO_SDK_MANIFEST = DEFAULT_MANIFEST.replace(/<uses-sdk.*\/>/, '');
+            createTempManifestFile(NO_SDK_MANIFEST);
+            manifest = new AndroidManifest(manifestPath);
+
+            expect(manifest.doc.getroot().find('./uses-sdk')).toBe(null);
+
+            manifest.setMinSdkVersion(1);
+
+            expect(manifest.doc.getroot().find('./uses-sdk')).not.toBe(null);
+            expect(manifest.getMinSdkVersion()).toBe(1);
+        });
+    });
+
+    describe('maxSdkVersion', () => {
+        it('should get maxSdkVersion', () => {
+            expect(manifest.getMaxSdkVersion()).toBe(MAX_SDK_VERSION);
+        });
+
+        it('should set maxSdkVersion', () => {
+            manifest.setMaxSdkVersion(1);
+            expect(manifest.getMaxSdkVersion()).toBe(1);
+        });
+
+        it('should create the uses-sdk node if it does not exist when setting maxSdkVersion', () => {
+            const NO_SDK_MANIFEST = DEFAULT_MANIFEST.replace(/<uses-sdk.*\/>/, '');
+            createTempManifestFile(NO_SDK_MANIFEST);
+            manifest = new AndroidManifest(manifestPath);
+
+            expect(manifest.doc.getroot().find('./uses-sdk')).toBe(null);
+
+            manifest.setMaxSdkVersion(1);
+
+            expect(manifest.doc.getroot().find('./uses-sdk')).not.toBe(null);
+            expect(manifest.getMaxSdkVersion()).toBe(1);
+        });
+    });
+
+    describe('targetSdkVersion', () => {
+        it('should get targetSdkVersion', () => {
+            expect(manifest.getTargetSdkVersion()).toBe(TARGET_SDK_VERSION);
+        });
+
+        it('should set targetSdkVersion', () => {
+            manifest.setTargetSdkVersion(1);
+            expect(manifest.getTargetSdkVersion()).toBe(1);
+        });
+
+        it('should create the uses-sdk node if it does not exist when setting targetSdkVersion', () => {
+            const NO_SDK_MANIFEST = DEFAULT_MANIFEST.replace(/<uses-sdk.*\/>/, '');
 
 Review comment:
   Ah, that's much better! I couldn't quite figure out the XML lib, and the docs didn't describe how to remove a node. I'll do this, thanks!

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org