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