aboutsummaryrefslogtreecommitdiffstats
path: root/sdk_common/tests/src/com/android/ide/common/xml/AndroidManifestParserTest.java
blob: 1bd6402cc299f0ec54403f810f85f4c31b70648c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * 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.
 */

package com.android.ide.common.xml;

import com.android.ide.common.xml.ManifestData.UsesFeature;
import com.android.ide.common.xml.ManifestData.UsesLibrary;
import com.android.resources.Keyboard;
import com.android.resources.Navigation;
import com.android.resources.TouchScreen;

import java.io.InputStream;

import junit.framework.TestCase;

/**
 * Tests for {@link AndroidManifestParser}
 */
public class AndroidManifestParserTest extends TestCase {
    private ManifestData mManifestTestApp;
    private ManifestData mManifestInstrumentation;

    private static final String TESTDATA_PATH =
        "/com/android/sdklib/testdata/";  //$NON-NLS-1$
    private static final String INSTRUMENTATION_XML = TESTDATA_PATH +
        "AndroidManifest-instrumentation.xml";  //$NON-NLS-1$
    private static final String TESTAPP_XML = TESTDATA_PATH +
        "AndroidManifest-testapp.xml";  //$NON-NLS-1$
    private static final String PACKAGE_NAME =  "com.android.testapp"; //$NON-NLS-1$
    private static final Integer VERSION_CODE = 42;
    private static final String ACTIVITY_NAME = "com.android.testapp.MainActivity"; //$NON-NLS-1$
    private static final String LIBRARY_NAME = "android.test.runner"; //$NON-NLS-1$
    private static final String LIBRARY_NAME2 = "android.test.runner2"; //$NON-NLS-1$
    private static final String FEATURE_NAME = "com.foo.feature"; //$NON-NLS-1$
    private static final String INSTRUMENTATION_NAME = "android.test.InstrumentationTestRunner"; //$NON-NLS-1$
    private static final String INSTRUMENTATION_TARGET = "com.android.AndroidProject"; //$NON-NLS-1$

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        InputStream manifestStream = this.getClass().getResourceAsStream(TESTAPP_XML);

        mManifestTestApp = AndroidManifestParser.parse(manifestStream);
        assertNotNull(mManifestTestApp);

        manifestStream = this.getClass().getResourceAsStream(INSTRUMENTATION_XML);
        mManifestInstrumentation = AndroidManifestParser.parse(manifestStream);
        assertNotNull(mManifestInstrumentation);
    }

    public void testGetInstrumentationInformation() {
        assertEquals(1, mManifestInstrumentation.getInstrumentations().length);
        assertEquals(INSTRUMENTATION_NAME,
                mManifestInstrumentation.getInstrumentations()[0].getName());
        assertEquals(INSTRUMENTATION_TARGET,
                mManifestInstrumentation.getInstrumentations()[0].getTargetPackage());
    }

    public void testGetPackage() {
        assertEquals(PACKAGE_NAME, mManifestTestApp.getPackage());
    }

    public void testGetVersionCode() {
        assertEquals(VERSION_CODE, mManifestTestApp.getVersionCode());
        assertEquals(null, mManifestInstrumentation.getVersionCode());
    }

    public void testMinSdkVersion() {
        assertEquals(7, mManifestTestApp.getMinSdkVersion());
        assertEquals(8, mManifestTestApp.getTargetSdkVersion());

        assertEquals("foo", mManifestInstrumentation.getMinSdkVersionString());
        assertEquals(ManifestData.MIN_SDK_CODENAME, mManifestInstrumentation.getMinSdkVersion());
    }

    public void testGetActivities() {
        assertEquals(1, mManifestTestApp.getActivities().length);
        ManifestData.Activity activity = mManifestTestApp.getActivities()[0];
        assertEquals(ACTIVITY_NAME, activity.getName());
        assertTrue(activity.hasAction());
        assertTrue(activity.isHomeActivity());
        assertTrue(activity.hasAction());
        assertEquals(activity, mManifestTestApp.getActivities()[0]);
    }

    public void testGetLauncherActivity() {
        ManifestData.Activity activity = mManifestTestApp.getLauncherActivity();
        assertEquals(ACTIVITY_NAME, activity.getName());
        assertTrue(activity.hasAction());
        assertTrue(activity.isHomeActivity());
    }

    public void testSupportsScreen() {
        ManifestData.SupportsScreens supportsScreens =
            mManifestTestApp.getSupportsScreensFromManifest();

        assertNotNull(supportsScreens);
        assertEquals(Boolean.TRUE, supportsScreens.getAnyDensity());
        assertEquals(Boolean.TRUE, supportsScreens.getResizeable());
        assertEquals(Boolean.TRUE, supportsScreens.getSmallScreens());
        assertEquals(Boolean.TRUE, supportsScreens.getNormalScreens());
        assertEquals(Boolean.TRUE, supportsScreens.getLargeScreens());
    }

    public void testUsesConfiguration() {
        ManifestData.UsesConfiguration usesConfig = mManifestTestApp.getUsesConfiguration();

        assertNotNull(usesConfig);
        assertEquals(Boolean.TRUE, usesConfig.getReqFiveWayNav());
        assertEquals(Navigation.NONAV, usesConfig.getReqNavigation());
        assertEquals(Boolean.TRUE, usesConfig.getReqHardKeyboard());
        assertEquals(Keyboard.TWELVEKEY, usesConfig.getReqKeyboardType());
        assertEquals(TouchScreen.FINGER, usesConfig.getReqTouchScreen());
    }

    private void assertEquals(ManifestData.Activity lhs, ManifestData.Activity rhs) {
        assertTrue(lhs == rhs || (lhs != null && rhs != null));
        if (lhs != null && rhs != null) {
            assertEquals(lhs.getName(),        rhs.getName());
            assertEquals(lhs.isExported(),     rhs.isExported());
            assertEquals(lhs.hasAction(),      rhs.hasAction());
            assertEquals(lhs.isHomeActivity(), rhs.isHomeActivity());
        }
    }

    public void testGetUsesLibraries() {
        UsesLibrary[] libraries = mManifestTestApp.getUsesLibraries();

        assertEquals(2,             libraries.length);
        assertEquals(LIBRARY_NAME,  libraries[0].getName());
        assertEquals(Boolean.FALSE, libraries[0].getRequired());
        assertEquals(LIBRARY_NAME2, libraries[1].getName());
        assertEquals(Boolean.TRUE,  libraries[1].getRequired());
    }

    public void testGetUsesFeatures() {
        UsesFeature[] features = mManifestTestApp.getUsesFeatures();

        assertEquals(2,            features.length);
        assertEquals(0x00020001,   features[0].mGlEsVersion);
        assertEquals(Boolean.TRUE, features[0].getRequired());
        assertEquals(FEATURE_NAME, features[1].getName());
        assertEquals(Boolean.TRUE, features[1].getRequired());
    }

    public void testGetPackageName() {
        assertEquals(PACKAGE_NAME, mManifestTestApp.getPackage());
    }
}