aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/manifest/ManifestInfoTest.java
blob: 43f9d681f56cdba11d4727b1b117d5dbbe77056e (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
 *
 * 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.eclipse.adt.internal.editors.manifest;

import static com.android.resources.ScreenSize.LARGE;
import static com.android.resources.ScreenSize.NORMAL;
import static com.android.resources.ScreenSize.XLARGE;

import com.android.annotations.NonNull;
import com.android.ide.eclipse.adt.internal.editors.layout.refactoring.AdtProjectTest;
import com.android.ide.eclipse.adt.internal.editors.manifest.ManifestInfo.ActivityAttributes;
import com.android.ide.eclipse.adt.internal.resources.ResourceHelper;
import com.android.sdklib.AndroidVersion;
import com.android.sdklib.BuildToolInfo;
import com.android.sdklib.IAndroidTarget;
import com.android.sdklib.ISystemImage;
import com.android.sdklib.repository.descriptors.IdDisplay;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.NullProgressMonitor;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@SuppressWarnings("javadoc")
public class ManifestInfoTest extends AdtProjectTest {
    @Override
    protected boolean testCaseNeedsUniqueProject() {
        return true;
    }

    public void testGetActivityThemes1() throws Exception {
        ManifestInfo info = getManifestInfo(
                "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
                "    package='com.android.unittest'>\n" +
                "    <uses-sdk android:minSdkVersion='3' android:targetSdkVersion='4'/>\n" +
                "</manifest>\n");
        Map<String, ActivityAttributes> map = info.getActivityAttributesMap();
        assertEquals(map.toString(), 0, map.size());
        assertEquals("com.android.unittest", info.getPackage());
        assertEquals("Theme", ResourceHelper.styleToTheme(info.getDefaultTheme(null, NORMAL)));
        assertEquals("@android:style/Theme", info.getDefaultTheme(null, null));
        assertEquals("Theme", ResourceHelper.styleToTheme(info.getDefaultTheme(null, XLARGE)));
    }

    public void testGetActivityThemes2() throws Exception {
        ManifestInfo info = getManifestInfo(
                "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
                "    package='com.android.unittest'>\n" +
                "    <uses-sdk android:minSdkVersion='3' android:targetSdkVersion='11'/>\n" +
                "</manifest>\n");
        Map<String, ActivityAttributes> map = info.getActivityAttributesMap();
        assertEquals(map.toString(), 0, map.size());
        assertEquals("com.android.unittest", info.getPackage());
        assertEquals("Theme.Holo", ResourceHelper.styleToTheme(info.getDefaultTheme(null,
                XLARGE)));
        assertEquals("Theme", ResourceHelper.styleToTheme(info.getDefaultTheme(null, LARGE)));
    }

    public void testGetActivityThemes3() throws Exception {
        ManifestInfo info = getManifestInfo(
                "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
                "    package='com.android.unittest'>\n" +
                "    <uses-sdk android:minSdkVersion='11'/>\n" +
                "</manifest>\n");
        Map<String, ActivityAttributes> map = info.getActivityAttributesMap();
        assertEquals(map.toString(), 0, map.size());
        assertEquals("com.android.unittest", info.getPackage());
        assertEquals("Theme.Holo", ResourceHelper.styleToTheme(info.getDefaultTheme(null,
                XLARGE)));
        assertEquals("Theme", ResourceHelper.styleToTheme(info.getDefaultTheme(null, NORMAL)));
    }

    public void testGetActivityThemes4() throws Exception {
        ManifestInfo info = getManifestInfo(
                "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
                "    package='com.android.unittest'>\n" +
                "    <application\n" +
                "        android:label='@string/app_name'\n" +
                "        android:name='.app.TestApp' android:icon='@drawable/app_icon'>\n" +
                "\n" +
                "        <activity\n" +
                "            android:name='.prefs.PrefsActivity'\n" +
                "            android:label='@string/prefs_title' />\n" +
                "\n" +
                "        <activity\n" +
                "            android:name='.app.IntroActivity'\n" +
                "            android:label='@string/intro_title'\n" +
                "            android:theme='@android:style/Theme.Dialog' />\n" +
                "    </application>\n" +
                "    <uses-sdk android:minSdkVersion='3' android:targetSdkVersion='4'/>\n" +
                "</manifest>\n" +
                ""
                );
        assertEquals("com.android.unittest", info.getPackage());
        assertEquals("Theme", ResourceHelper.styleToTheme(info.getDefaultTheme(null, XLARGE)));

        Map<String, ActivityAttributes> map = info.getActivityAttributesMap();
        assertEquals(map.toString(), 2, map.size());
        assertNull(map.get("com.android.unittest.prefs.PrefsActivity").getTheme());
        assertEquals("@android:style/Theme.Dialog",
                map.get("com.android.unittest.app.IntroActivity").getTheme());
    }

    public void testGetActivityThemes5() throws Exception {
        ManifestInfo info = getManifestInfo(
                "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
                "    package='com.android.unittest'>\n" +
                "    <application\n" +
                "        android:label='@string/app_name'\n" +
                "        android:theme='@style/NoBackground'\n" +
                "        android:name='.app.TestApp' android:icon='@drawable/app_icon'>\n" +
                "\n" +
                "        <activity\n" +
                "            android:name='.prefs.PrefsActivity'\n" +
                "            android:label='@string/prefs_title' />\n" +
                "\n" +
                "        <activity\n" +
                "            android:name='.app.IntroActivity'\n" +
                "            android:label='@string/intro_title'\n" +
                "            android:theme='@android:style/Theme.Dialog' />\n" +
                "    </application>\n" +
                "    <uses-sdk android:minSdkVersion='3' android:targetSdkVersion='4'/>\n" +
                "</manifest>\n" +
                ""
                );

        assertEquals("@style/NoBackground", info.getDefaultTheme(null, XLARGE));
        assertEquals("@style/NoBackground", info.getDefaultTheme(null, NORMAL));
        assertEquals("NoBackground", ResourceHelper.styleToTheme(info.getDefaultTheme(null,
                NORMAL)));

        Map<String, ActivityAttributes> map = info.getActivityAttributesMap();
        assertEquals(map.toString(), 2, map.size());
        assertNull(map.get("com.android.unittest.prefs.PrefsActivity").getTheme());
        assertEquals("@android:style/Theme.Dialog",
                map.get("com.android.unittest.app.IntroActivity").getTheme());
    }

    public void testGetActivityThemes6() throws Exception {
        // Ensures that when the *rendering* target is less than version 11, we don't
        // use Holo even though the manifest SDK version calls for it.
        ManifestInfo info = getManifestInfo(
                "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
                "    package='com.android.unittest'>\n" +
                "    <uses-sdk android:minSdkVersion='3' android:targetSdkVersion='11'/>\n" +
                "</manifest>\n");
        Map<String, ActivityAttributes> map = info.getActivityAttributesMap();
        assertEquals(map.toString(), 0, map.size());
        assertEquals("com.android.unittest", info.getPackage());
        assertEquals("Theme.Holo", ResourceHelper.styleToTheme(info.getDefaultTheme(null,
                XLARGE)));

        // Here's the check
        IAndroidTarget olderVersion = new TestAndroidTarget(4);
        assertEquals("Theme", ResourceHelper.styleToTheme(info.getDefaultTheme(olderVersion,
                XLARGE)));

    }

    public void testGetApplicationLabelAndIcon() throws Exception {
        ManifestInfo info = getManifestInfo(
                "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
                "    package='com.android.unittest'>\n" +
                "    <application android:icon=\"@drawable/icon\"\n" +
                "                 android:label=\"@string/app_name\">\n" +
                "    </application>\n" +
                "" +
                "</manifest>\n");
        Map<String, ActivityAttributes> map = info.getActivityAttributesMap();
        assertEquals(map.toString(), 0, map.size());
        assertEquals("com.android.unittest", info.getPackage());

        assertEquals("Theme", ResourceHelper.styleToTheme(info.getDefaultTheme(null, NORMAL)));
        assertEquals("@drawable/icon", info.getApplicationIcon());
        assertEquals("@string/app_name", info.getApplicationLabel());
    }

    public void testGetApplicationNoLabelOrIcon() throws Exception {
        ManifestInfo info = getManifestInfo(
                "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
                "    package='com.android.unittest'>\n" +
                "    <application>\n" +
                "    </application>\n" +
                "" +
                "</manifest>\n");
        Map<String, ActivityAttributes> map = info.getActivityAttributesMap();
        assertEquals(map.toString(), 0, map.size());
        assertEquals("com.android.unittest", info.getPackage());

        assertEquals("Theme", ResourceHelper.styleToTheme(info.getDefaultTheme(null, NORMAL)));
        assertNull(info.getApplicationIcon());
        assertNull(info.getApplicationLabel());
    }

    private ManifestInfo getManifestInfo(String manifestContents) throws Exception {
        InputStream bstream = new ByteArrayInputStream(
                manifestContents.getBytes("UTF-8")); //$NON-NLS-1$

        IFile file = getProject().getFile("AndroidManifest.xml");
        if (file.exists()) {
            file.setContents(bstream, IFile.FORCE, new NullProgressMonitor());
        } else {
            file.create(bstream, false /* force */, new NullProgressMonitor());
        }
        ManifestInfo info = ManifestInfo.get(getProject());
        info.clear();
        return info;
    }

    public void testGetMinSdkVersionName() throws Exception {
        ManifestInfo info;

        info = getManifestInfo(
                "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
                "    package='com.android.unittest'>\n" +
                "    <uses-sdk android:minSdkVersion='3' android:targetSdkVersion='4'/>\n" +
                "</manifest>\n");
        assertEquals(3, info.getMinSdkVersion());
        assertEquals("3", info.getMinSdkName());
        assertEquals(4, info.getTargetSdkVersion());
        assertNull(info.getMinSdkCodeName());

        info = getManifestInfo(
                "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
                "    package='com.android.unittest'>\n" +
                "    <uses-sdk android:targetSdkVersion='4'/>\n" +
                "</manifest>\n");
        assertEquals("1", info.getMinSdkName());
        assertEquals(1, info.getMinSdkVersion());
        assertEquals(4, info.getTargetSdkVersion());
        assertNull(info.getMinSdkCodeName());

        info = getManifestInfo(
                "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
                "    package='com.android.unittest'>\n" +
                "    <uses-sdk android:minSdkVersion='JellyBean' />\n" +
                "</manifest>\n");
        assertEquals("JellyBean", info.getMinSdkName());
        assertEquals("JellyBean", info.getMinSdkCodeName());
    }

    private static class TestAndroidTarget implements IAndroidTarget {
        private final int mApiLevel;

        public TestAndroidTarget(int apiLevel) {
            mApiLevel = apiLevel;
        }

        @Override
        public boolean canRunOn(IAndroidTarget target) {
            return false;
        }

        @Override
        public String getClasspathName() {
            return null;
        }

        @Override
        public File getDefaultSkin() {
            return null;
        }

        @Override
        public String getDescription() {
            return null;
        }

        @Override
        public String getFullName() {
            return null;
        }

        @Override
        public ISystemImage getSystemImage(IdDisplay tag, String abiType) {
            return null;
        }

        @Override
        public ISystemImage[] getSystemImages() {
            return new ISystemImage[0];
        }

        @Override
        public String getLocation() {
            return null;
        }

        @Override
        public String getName() {
            return null;
        }

        @Override
        public IOptionalLibrary[] getOptionalLibraries() {
            return null;
        }

        @Override
        public IAndroidTarget getParent() {
            return null;
        }

        @Override
        public String getPath(int pathId) {
            return null;
        }

        @Override
        public File getFile(int pathId) {
          return null;
        }


        @Override
        public String[] getPlatformLibraries() {
            return null;
        }

        @Override
        public Map<String, String> getProperties() {
            return null;
        }

        @Override
        public String getProperty(String name) {
            return null;
        }

        @Override
        public Integer getProperty(String name, Integer defaultValue) {
            return null;
        }

        @Override
        public Boolean getProperty(String name, Boolean defaultValue) {
            return null;
        }

        @Override
        public int getRevision() {
            return 0;
        }

        @Override
        public File[] getSkins() {
            return null;
        }

        @Override
        public int getUsbVendorId() {
            return 0;
        }

        @Override
        public String getVendor() {
            return null;
        }

        @Override
        public AndroidVersion getVersion() {
            return new AndroidVersion(mApiLevel, null);
        }

        @Override
        public String getVersionName() {
            return null;
        }

        @Override
        public String hashString() {
            return null;
        }

        @Override
        public boolean isPlatform() {
            return false;
        }

        @Override
        public int compareTo(IAndroidTarget o) {
            return 0;
        }

        @Override
        public boolean hasRenderingLibrary() {
            return false;
        }

        @Override
        public String getShortClasspathName() {
            return null;
        }

        @Override
        @NonNull
        public List<String> getBootClasspath() {
            return new ArrayList<String>();
        }

        @Override
        public BuildToolInfo getBuildToolInfo() {
            return null;
        }
    }
}