• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/org/documents/epl-v10.php
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.ide.eclipse.adt.internal.editors.manifest;
17 
18 import static com.android.resources.ScreenSize.LARGE;
19 import static com.android.resources.ScreenSize.NORMAL;
20 import static com.android.resources.ScreenSize.XLARGE;
21 
22 import com.android.ide.eclipse.adt.internal.editors.layout.refactoring.AdtProjectTest;
23 import com.android.ide.eclipse.adt.internal.resources.ResourceHelper;
24 import com.android.sdklib.AndroidVersion;
25 import com.android.sdklib.BuildToolInfo;
26 import com.android.sdklib.IAndroidTarget;
27 import com.android.sdklib.ISystemImage;
28 
29 import org.eclipse.core.resources.IFile;
30 import org.eclipse.core.runtime.NullProgressMonitor;
31 
32 import java.io.ByteArrayInputStream;
33 import java.io.InputStream;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Map;
37 
38 @SuppressWarnings("javadoc")
39 public class ManifestInfoTest extends AdtProjectTest {
40     @Override
testCaseNeedsUniqueProject()41     protected boolean testCaseNeedsUniqueProject() {
42         return true;
43     }
44 
testGetActivityThemes1()45     public void testGetActivityThemes1() throws Exception {
46         ManifestInfo info = getManifestInfo(
47                 "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
48                 "    package='com.android.unittest'>\n" +
49                 "    <uses-sdk android:minSdkVersion='3' android:targetSdkVersion='4'/>\n" +
50                 "</manifest>\n");
51         Map<String, String> map = info.getActivityThemes();
52         assertEquals(map.toString(), 0, map.size());
53         assertEquals("com.android.unittest", info.getPackage());
54         assertEquals("Theme", ResourceHelper.styleToTheme(info.getDefaultTheme(null, NORMAL)));
55         assertEquals("@android:style/Theme", info.getDefaultTheme(null, null));
56         assertEquals("Theme", ResourceHelper.styleToTheme(info.getDefaultTheme(null, XLARGE)));
57     }
58 
testGetActivityThemes2()59     public void testGetActivityThemes2() throws Exception {
60         ManifestInfo info = getManifestInfo(
61                 "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
62                 "    package='com.android.unittest'>\n" +
63                 "    <uses-sdk android:minSdkVersion='3' android:targetSdkVersion='11'/>\n" +
64                 "</manifest>\n");
65         Map<String, String> map = info.getActivityThemes();
66         assertEquals(map.toString(), 0, map.size());
67         assertEquals("com.android.unittest", info.getPackage());
68         assertEquals("Theme.Holo", ResourceHelper.styleToTheme(info.getDefaultTheme(null,
69                 XLARGE)));
70         assertEquals("Theme", ResourceHelper.styleToTheme(info.getDefaultTheme(null, LARGE)));
71     }
72 
testGetActivityThemes3()73     public void testGetActivityThemes3() throws Exception {
74         ManifestInfo info = getManifestInfo(
75                 "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
76                 "    package='com.android.unittest'>\n" +
77                 "    <uses-sdk android:minSdkVersion='11'/>\n" +
78                 "</manifest>\n");
79         Map<String, String> map = info.getActivityThemes();
80         assertEquals(map.toString(), 0, map.size());
81         assertEquals("com.android.unittest", info.getPackage());
82         assertEquals("Theme.Holo", ResourceHelper.styleToTheme(info.getDefaultTheme(null,
83                 XLARGE)));
84         assertEquals("Theme", ResourceHelper.styleToTheme(info.getDefaultTheme(null, NORMAL)));
85     }
86 
testGetActivityThemes4()87     public void testGetActivityThemes4() throws Exception {
88         ManifestInfo info = getManifestInfo(
89                 "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
90                 "    package='com.android.unittest'>\n" +
91                 "    <application\n" +
92                 "        android:label='@string/app_name'\n" +
93                 "        android:name='.app.TestApp' android:icon='@drawable/app_icon'>\n" +
94                 "\n" +
95                 "        <activity\n" +
96                 "            android:name='.prefs.PrefsActivity'\n" +
97                 "            android:label='@string/prefs_title' />\n" +
98                 "\n" +
99                 "        <activity\n" +
100                 "            android:name='.app.IntroActivity'\n" +
101                 "            android:label='@string/intro_title'\n" +
102                 "            android:theme='@android:style/Theme.Dialog' />\n" +
103                 "    </application>\n" +
104                 "    <uses-sdk android:minSdkVersion='3' android:targetSdkVersion='4'/>\n" +
105                 "</manifest>\n" +
106                 ""
107                 );
108         assertEquals("com.android.unittest", info.getPackage());
109         assertEquals("Theme", ResourceHelper.styleToTheme(info.getDefaultTheme(null, XLARGE)));
110 
111         Map<String, String> map = info.getActivityThemes();
112         assertEquals(map.toString(), 1, map.size());
113         assertNull(map.get("com.android.unittest.prefs.PrefsActivity"));
114         assertEquals("@android:style/Theme.Dialog",
115                 map.get("com.android.unittest.app.IntroActivity"));
116     }
117 
testGetActivityThemes5()118     public void testGetActivityThemes5() throws Exception {
119         ManifestInfo info = getManifestInfo(
120                 "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
121                 "    package='com.android.unittest'>\n" +
122                 "    <application\n" +
123                 "        android:label='@string/app_name'\n" +
124                 "        android:theme='@style/NoBackground'\n" +
125                 "        android:name='.app.TestApp' android:icon='@drawable/app_icon'>\n" +
126                 "\n" +
127                 "        <activity\n" +
128                 "            android:name='.prefs.PrefsActivity'\n" +
129                 "            android:label='@string/prefs_title' />\n" +
130                 "\n" +
131                 "        <activity\n" +
132                 "            android:name='.app.IntroActivity'\n" +
133                 "            android:label='@string/intro_title'\n" +
134                 "            android:theme='@android:style/Theme.Dialog' />\n" +
135                 "    </application>\n" +
136                 "    <uses-sdk android:minSdkVersion='3' android:targetSdkVersion='4'/>\n" +
137                 "</manifest>\n" +
138                 ""
139                 );
140 
141         assertEquals("@style/NoBackground", info.getDefaultTheme(null, XLARGE));
142         assertEquals("@style/NoBackground", info.getDefaultTheme(null, NORMAL));
143         assertEquals("NoBackground", ResourceHelper.styleToTheme(info.getDefaultTheme(null,
144                 NORMAL)));
145 
146         Map<String, String> map = info.getActivityThemes();
147         assertEquals(map.toString(), 1, map.size());
148         assertNull(map.get("com.android.unittest.prefs.PrefsActivity"));
149         assertEquals("@android:style/Theme.Dialog",
150                 map.get("com.android.unittest.app.IntroActivity"));
151     }
152 
testGetActivityThemes6()153     public void testGetActivityThemes6() throws Exception {
154         // Ensures that when the *rendering* target is less than version 11, we don't
155         // use Holo even though the manifest SDK version calls for it.
156         ManifestInfo info = getManifestInfo(
157                 "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
158                 "    package='com.android.unittest'>\n" +
159                 "    <uses-sdk android:minSdkVersion='3' android:targetSdkVersion='11'/>\n" +
160                 "</manifest>\n");
161         Map<String, String> map = info.getActivityThemes();
162         assertEquals(map.toString(), 0, map.size());
163         assertEquals("com.android.unittest", info.getPackage());
164         assertEquals("Theme.Holo", ResourceHelper.styleToTheme(info.getDefaultTheme(null,
165                 XLARGE)));
166 
167         // Here's the check
168         IAndroidTarget olderVersion = new TestAndroidTarget(4);
169         assertEquals("Theme", ResourceHelper.styleToTheme(info.getDefaultTheme(olderVersion,
170                 XLARGE)));
171 
172     }
173 
testGetApplicationLabelAndIcon()174     public void testGetApplicationLabelAndIcon() throws Exception {
175         ManifestInfo info = getManifestInfo(
176                 "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
177                 "    package='com.android.unittest'>\n" +
178                 "    <application android:icon=\"@drawable/icon\"\n" +
179                 "                 android:label=\"@string/app_name\">\n" +
180                 "    </application>\n" +
181                 "" +
182                 "</manifest>\n");
183         Map<String, String> map = info.getActivityThemes();
184         assertEquals(map.toString(), 0, map.size());
185         assertEquals("com.android.unittest", info.getPackage());
186 
187         assertEquals("Theme", ResourceHelper.styleToTheme(info.getDefaultTheme(null, NORMAL)));
188         assertEquals("@drawable/icon", info.getApplicationIcon());
189         assertEquals("@string/app_name", info.getApplicationLabel());
190     }
191 
testGetApplicationNoLabelOrIcon()192     public void testGetApplicationNoLabelOrIcon() throws Exception {
193         ManifestInfo info = getManifestInfo(
194                 "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
195                 "    package='com.android.unittest'>\n" +
196                 "    <application>\n" +
197                 "    </application>\n" +
198                 "" +
199                 "</manifest>\n");
200         Map<String, String> map = info.getActivityThemes();
201         assertEquals(map.toString(), 0, map.size());
202         assertEquals("com.android.unittest", info.getPackage());
203 
204         assertEquals("Theme", ResourceHelper.styleToTheme(info.getDefaultTheme(null, NORMAL)));
205         assertNull(info.getApplicationIcon());
206         assertNull(info.getApplicationLabel());
207     }
208 
getManifestInfo(String manifestContents)209     private ManifestInfo getManifestInfo(String manifestContents) throws Exception {
210         InputStream bstream = new ByteArrayInputStream(
211                 manifestContents.getBytes("UTF-8")); //$NON-NLS-1$
212 
213         IFile file = getProject().getFile("AndroidManifest.xml");
214         if (file.exists()) {
215             file.setContents(bstream, IFile.FORCE, new NullProgressMonitor());
216         } else {
217             file.create(bstream, false /* force */, new NullProgressMonitor());
218         }
219         ManifestInfo info = ManifestInfo.get(getProject());
220         info.clear();
221         return info;
222     }
223 
testGetMinSdkVersionName()224     public void testGetMinSdkVersionName() throws Exception {
225         ManifestInfo info;
226 
227         info = getManifestInfo(
228                 "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
229                 "    package='com.android.unittest'>\n" +
230                 "    <uses-sdk android:minSdkVersion='3' android:targetSdkVersion='4'/>\n" +
231                 "</manifest>\n");
232         assertEquals(3, info.getMinSdkVersion());
233         assertEquals("3", info.getMinSdkName());
234         assertEquals(4, info.getTargetSdkVersion());
235         assertNull(info.getMinSdkCodeName());
236 
237         info = getManifestInfo(
238                 "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
239                 "    package='com.android.unittest'>\n" +
240                 "    <uses-sdk android:targetSdkVersion='4'/>\n" +
241                 "</manifest>\n");
242         assertEquals("1", info.getMinSdkName());
243         assertEquals(1, info.getMinSdkVersion());
244         assertEquals(4, info.getTargetSdkVersion());
245         assertNull(info.getMinSdkCodeName());
246 
247         info = getManifestInfo(
248                 "<manifest xmlns:android='http://schemas.android.com/apk/res/android'\n" +
249                 "    package='com.android.unittest'>\n" +
250                 "    <uses-sdk android:minSdkVersion='JellyBean' />\n" +
251                 "</manifest>\n");
252         assertEquals("JellyBean", info.getMinSdkName());
253         assertEquals("JellyBean", info.getMinSdkCodeName());
254     }
255 
256     private static class TestAndroidTarget implements IAndroidTarget {
257         private final int mApiLevel;
258 
TestAndroidTarget(int apiLevel)259         public TestAndroidTarget(int apiLevel) {
260             mApiLevel = apiLevel;
261         }
262 
263         @Override
canRunOn(IAndroidTarget target)264         public boolean canRunOn(IAndroidTarget target) {
265             return false;
266         }
267 
268         @Override
getClasspathName()269         public String getClasspathName() {
270             return null;
271         }
272 
273         @Override
getDefaultSkin()274         public String getDefaultSkin() {
275             return null;
276         }
277 
278         @Override
getDescription()279         public String getDescription() {
280             return null;
281         }
282 
283         @Override
getFullName()284         public String getFullName() {
285             return null;
286         }
287 
288         @Override
getSystemImage(String abiType)289         public ISystemImage getSystemImage(String abiType) {
290             return null;
291         }
292 
293         @Override
getSystemImages()294         public ISystemImage[] getSystemImages() {
295             return new ISystemImage[0];
296         }
297 
298         @Override
getLocation()299         public String getLocation() {
300             return null;
301         }
302 
303         @Override
getName()304         public String getName() {
305             return null;
306         }
307 
308         @Override
getOptionalLibraries()309         public IOptionalLibrary[] getOptionalLibraries() {
310             return null;
311         }
312 
313         @Override
getParent()314         public IAndroidTarget getParent() {
315             return null;
316         }
317 
318         @Override
getPath(int pathId)319         public String getPath(int pathId) {
320             return null;
321         }
322 
323         @Override
getPlatformLibraries()324         public String[] getPlatformLibraries() {
325             return null;
326         }
327 
328         @Override
getProperties()329         public Map<String, String> getProperties() {
330             return null;
331         }
332 
333         @Override
getProperty(String name)334         public String getProperty(String name) {
335             return null;
336         }
337 
338         @Override
getProperty(String name, Integer defaultValue)339         public Integer getProperty(String name, Integer defaultValue) {
340             return null;
341         }
342 
343         @Override
getProperty(String name, Boolean defaultValue)344         public Boolean getProperty(String name, Boolean defaultValue) {
345             return null;
346         }
347 
348         @Override
getRevision()349         public int getRevision() {
350             return 0;
351         }
352 
353         @Override
getSkins()354         public String[] getSkins() {
355             return null;
356         }
357 
358         @Override
getUsbVendorId()359         public int getUsbVendorId() {
360             return 0;
361         }
362 
363         @Override
getVendor()364         public String getVendor() {
365             return null;
366         }
367 
368         @Override
getVersion()369         public AndroidVersion getVersion() {
370             return new AndroidVersion(mApiLevel, null);
371         }
372 
373         @Override
getVersionName()374         public String getVersionName() {
375             return null;
376         }
377 
378         @Override
hashString()379         public String hashString() {
380             return null;
381         }
382 
383         @Override
isPlatform()384         public boolean isPlatform() {
385             return false;
386         }
387 
388         @Override
compareTo(IAndroidTarget o)389         public int compareTo(IAndroidTarget o) {
390             return 0;
391         }
392 
393         @Override
hasRenderingLibrary()394         public boolean hasRenderingLibrary() {
395             return false;
396         }
397 
398         @Override
getShortClasspathName()399         public String getShortClasspathName() {
400             return null;
401         }
402 
403         @Override
getBootClasspath()404         public List<String> getBootClasspath() {
405             return new ArrayList<String>();
406         }
407 
408         @Override
getBuildToolInfo()409         public BuildToolInfo getBuildToolInfo() {
410             return null;
411         }
412     }
413 }
414