@Test public void obtainStyledAttributes_shouldExtractAttributesFromAttributeSet() throws Exception { ResourceLoader resourceLoader = new PackageResourceLoader(TEST_RESOURCE_PATH); Resources resources = TestUtil.createResourcesFor(resourceLoader); RoboAttributeSet roboAttributeSet = new RoboAttributeSet(asList( new Attribute(TEST_PACKAGE + ":attr/itemType", "ungulate", TEST_PACKAGE), new Attribute(TEST_PACKAGE + ":attr/scrollBars", "horizontal|vertical", TEST_PACKAGE), new Attribute(TEST_PACKAGE + ":attr/quitKeyCombo", "^q", TEST_PACKAGE), new Attribute(TEST_PACKAGE + ":attr/aspectRatio", "1.5", TEST_PACKAGE), new Attribute(TEST_PACKAGE + ":attr/aspectRatioEnabled", "true", TEST_PACKAGE) ), resources, CustomView.class); TypedArray a = context.obtainStyledAttributes(roboAttributeSet, R.styleable.CustomView); assertThat(a.getInt(R.styleable.CustomView_itemType, -1234)).isEqualTo(1 /* ungulate */); assertThat(a.getInt(R.styleable.CustomView_scrollBars, -1234)).isEqualTo(0x300); assertThat(a.getString(R.styleable.CustomView_quitKeyCombo)).isEqualTo("^q"); assertThat(a.getText(R.styleable.CustomView_quitKeyCombo).toString()).isEqualTo("^q"); assertThat(a.getFloat(R.styleable.CustomView_aspectRatio, 1f)).isEqualTo(1.5f); assertThat(a.getBoolean(R.styleable.CustomView_aspectRatioEnabled, false)).isTrue(); TypedArray typedArray = context.obtainStyledAttributes(roboAttributeSet, new int[]{R.attr.quitKeyCombo, R.attr.itemType}); assertThat(typedArray.getString(0)).isEqualTo("^q"); assertThat(typedArray.getInt(1, -1234)).isEqualTo(1 /* ungulate */); }
@Test public void rememberThatSomeTestRunnerMethodsShouldBeOverridable() throws Exception { // super weak test for now, just remember not to make these methods static! //noinspection UnusedDeclaration class CustomTestRunner extends RobolectricTestRunner { public CustomTestRunner(Class<?> testClass) throws InitializationError { super(testClass); } @Override public PackageResourceLoader createResourceLoader(ResourcePath resourcePath) { return super.createResourceLoader(resourcePath); } @Override protected ResourceLoader createAppResourceLoader(ResourceLoader systemResourceLoader, AndroidManifest appManifest) { return super.createAppResourceLoader(systemResourceLoader, appManifest); } } }
public final ResourceLoader getAppResourceLoader(SdkConfig sdkConfig, ResourceLoader systemResourceLoader, final AndroidManifest appManifest) { Pair<AndroidManifest, SdkConfig> androidManifestSdkConfigPair = new Pair<AndroidManifest, SdkConfig>(appManifest, sdkConfig); ResourceLoader resourceLoader = resourceLoadersByManifestAndConfig.get(androidManifestSdkConfigPair); if (resourceLoader == null) { resourceLoader = createAppResourceLoader(systemResourceLoader, appManifest); resourceLoadersByManifestAndConfig.put(androidManifestSdkConfigPair, resourceLoader); } return resourceLoader; }
protected ResourceLoader createAppResourceLoader(ResourceLoader systemResourceLoader, AndroidManifest appManifest) { List<PackageResourceLoader> appAndLibraryResourceLoaders = new ArrayList<PackageResourceLoader>(); for (ResourcePath resourcePath : appManifest.getIncludedResourcePaths()) { appAndLibraryResourceLoaders.add(createResourceLoader(resourcePath)); } OverlayResourceLoader overlayResourceLoader = new OverlayResourceLoader(appManifest.getPackageName(), appAndLibraryResourceLoaders); Map<String, ResourceLoader> resourceLoaders = new HashMap<String, ResourceLoader>(); resourceLoaders.put("android", systemResourceLoader); resourceLoaders.put(appManifest.getPackageName(), overlayResourceLoader); return new RoutingResourceLoader(resourceLoaders); }
public static void convertAndFill(Attribute attribute, TypedValue outValue, ResourceLoader resourceLoader, String qualifiers) { if (attribute == null || attribute.isNull()) { outValue.type = TypedValue.TYPE_NULL; return; } TypedResource attrTypeData = resourceLoader.getValue(attribute.resName, qualifiers); if (attrTypeData == null) { throw new Resources.NotFoundException("Couldn't find " + attribute.resName + " attr data"); } AttrData attrData = (AttrData) attrTypeData.getData(); convertAndFill(attribute, outValue, resourceLoader, qualifiers, attrData); }
static AssetManager bind(AssetManager assetManager, AndroidManifest androidManifest, ResourceLoader resourceLoader) { ShadowAssetManager shadowAssetManager = shadowOf(assetManager); if (shadowAssetManager.appManifest != null) throw new RuntimeException("ResourceLoader already set!"); shadowAssetManager.appManifest = androidManifest; shadowAssetManager.resourceLoader = resourceLoader; return assetManager; }
public ImageView getHomeIcon() { ResourceLoader resourceLoader = Robolectric.getShadowApplication().getResourceLoader(); ResName internalResource = new ResName("android", "id", "home"); Integer resId = resourceLoader.getResourceIndex().getResourceId(internalResource); try { Class<?> actionBarViewClass = Class.forName("com.android.internal.widget.ActionBarView"); ViewGroup actionBarView = (ViewGroup) field("mActionBar").ofType(actionBarViewClass).in(realWindow).get(); return (ImageView) actionBarView.findViewById(resId); } catch (ClassNotFoundException e) { throw new RuntimeException("could not resolve ActionBarView"); } }
/** * Associates a {@code ResourceLoader} with an {@code Application} instance * * @param appManifest * @param resourceLoader resource loader */ public void bind(AndroidManifest appManifest, ResourceLoader resourceLoader) { if (this.resourceLoader != null) throw new RuntimeException("ResourceLoader already set!"); this.appManifest = appManifest; this.resourceLoader = resourceLoader; if (appManifest != null) { setPackageName(appManifest.getPackageName()); setApplicationName(appManifest.getApplicationName()); this.registerBroadcastReceivers(appManifest); } }
public void addManifest(AndroidManifest androidManifest, ResourceLoader loader) { androidManifests.put(androidManifest.getPackageName(), androidManifest); ResourceIndex resourceIndex = loader.getResourceIndex(); // first opportunity to access a resource index for this manifest, use it to init the references androidManifest.initMetaData(loader); PackageInfo packageInfo = new PackageInfo(); packageInfo.packageName = androidManifest.getPackageName(); packageInfo.versionName = androidManifest.getVersionName(); packageInfo.versionCode = androidManifest.getVersionCode(); ApplicationInfo applicationInfo = new ApplicationInfo(); applicationInfo.flags = androidManifest.getApplicationFlags(); applicationInfo.targetSdkVersion = androidManifest.getTargetSdkVersion(); applicationInfo.packageName = androidManifest.getPackageName(); applicationInfo.processName = androidManifest.getProcessName(); applicationInfo.name = androidManifest.getApplicationName(); applicationInfo.metaData = metaDataToBundle(androidManifest.getApplicationMetaData()); if (androidManifest.getLabelRef() != null && resourceIndex != null) { Integer id = ResName.getResourceId(resourceIndex, androidManifest.getLabelRef(), androidManifest.getPackageName()); applicationInfo.labelRes = id != null ? id : 0; } packageInfo.applicationInfo = applicationInfo; initApplicationInfo(applicationInfo); addPackage(packageInfo); }
public View inflate(Context context, String packageName, String key, ViewGroup parent, String qualifiers) { ResName resName = new ResName(packageName + ":layout/" + key); shadowOf(context.getAssets()).setQualifiers(qualifiers); ResourceLoader resourceLoader = shadowOf(context.getResources()).getResourceLoader(); Integer layoutResId = resourceLoader.getResourceIndex().getResourceId(resName); if (layoutResId == null) throw new AssertionError("no such resource " + resName); return LayoutInflater.from(context).inflate(layoutResId, parent); }
@Test public void shouldInheritThemeValuesFromImplicitParents() throws Exception { TestActivity activity = buildActivity(TestActivityWithAnotherTheme.class).create().get(); ResourceLoader resourceLoader = Robolectric.shadowOf(activity.getResources()).getResourceLoader(); Style style = ShadowAssetManager.resolveStyle(resourceLoader, new ResName(TestUtil.TEST_PACKAGE, "style", "Widget.AnotherTheme.Button.Blarf"), ""); assertThat(style.getAttrValue(new ResName("android", "attr", "background")).value) .isEqualTo("#ffff0000"); }
@Test public void whenAThemeHasExplicitlyEmptyParentAttr_shouldHaveNoParent() throws Exception { TestActivity activity = buildActivity(TestActivityWithAnotherTheme.class).create().get(); ResourceLoader resourceLoader = Robolectric.shadowOf(activity.getResources()).getResourceLoader(); Style style = ShadowAssetManager.resolveStyle(resourceLoader, new ResName(TestUtil.TEST_PACKAGE, "style", "Theme.MyTheme"), ""); assertThat(style.getAttrValue(new ResName("android", "attr", "background"))).isNull(); }
@Override protected ResourceLoader createAppResourceLoader(ResourceLoader systemResourceLoader, AndroidManifest appManifest) { List<PackageResourceLoader> appAndLibraryResourceLoaders = new ArrayList<PackageResourceLoader>(); for (ResourcePath resourcePath : appManifest.getIncludedResourcePaths()) { appAndLibraryResourceLoaders.add(createResourceLoader(resourcePath)); } OverlayResourceLoader overlayResourceLoader = new OverlayResourceLoaderWithDummyResources( appManifest.getPackageName(), appAndLibraryResourceLoaders, systemResourceLoader); Map<String, ResourceLoader> resourceLoaders = new HashMap<String, ResourceLoader>(); resourceLoaders.put("android", systemResourceLoader); resourceLoaders.put(appManifest.getPackageName(), overlayResourceLoader); return new RoutingResourceLoader(resourceLoaders); }
/*** * Allows {@link org.robolectric.res.builder.RobolectricPackageManager} to provide * a resource index for initialising the resource attributes in all the metadata elements * @param resLoader used for getting resource IDs from string identifiers */ public void initMetaData(ResourceLoader resLoader) { applicationMetaData.init(resLoader, packageName); for (ReceiverAndIntentFilter receiver : receivers) { receiver.metaData.init(resLoader, packageName); } }
public void init(ResourceLoader resLoader, String packageName) { ResourceIndex resIndex = resLoader.getResourceIndex(); if (!initialised) { for (Map.Entry<String,MetaData.VALUE_TYPE> entry : typeMap.entrySet()) { String value = valueMap.get(entry.getKey()).toString(); if (value.startsWith("@")) { ResName resName = ResName.qualifyResName(value.substring(1), packageName, null); switch (entry.getValue()) { case RESOURCE: // Was provided by resource attribute, store resource ID valueMap.put(entry.getKey(), resIndex.getResourceId(resName)); break; case VALUE: // Was provided by value attribute, need to parse it TypedResource<?> typedRes = resLoader.getValue(resName, ""); // The typed resource's data is always a String, so need to parse the value. switch (typedRes.getResType()) { case BOOLEAN: case COLOR: case INTEGER: case FLOAT: valueMap.put(entry.getKey(),parseValue(typedRes.getData().toString())); break; default: valueMap.put(entry.getKey(),typedRes.getData()); } break; } } else if (entry.getValue() == MetaData.VALUE_TYPE.VALUE) { // Raw value, so parse it in to the appropriate type and store it valueMap.put(entry.getKey(), parseValue(value)); } } // Finished parsing, mark as initialised initialised = true; } }
protected void setUpApplicationState(Method method, ParallelUniverseInterface parallelUniverseInterface, boolean strictI18n, ResourceLoader systemResourceLoader, AndroidManifest appManifest, Config config) { parallelUniverseInterface.setUpApplicationState(method, testLifecycle, strictI18n, systemResourceLoader, appManifest, config); }
public synchronized ResourceLoader getSystemResourceLoader(MavenCentral mavenCentral, RobolectricTestRunner robolectricTestRunner) { if (systemResourceLoader == null) { systemResourceLoader = createSystemResourceLoader(mavenCentral, robolectricTestRunner); } return systemResourceLoader; }
@Override public ResourceLoader getResourceLoader() { return super.getResourceLoader(); }
static Style resolveStyle(ResourceLoader resourceLoader, @NotNull ResName themeStyleName, String qualifiers) { TypedResource themeStyleResource = resourceLoader.getValue(themeStyleName, qualifiers); if (themeStyleResource == null) return null; StyleData themeStyleData = (StyleData) themeStyleResource.getData(); return new StyleResolver(resourceLoader, themeStyleData, themeStyleName, qualifiers); }
public StyleResolver(ResourceLoader resourceLoader, StyleData styleData, ResName myResName, String qualifiers) { this.resourceLoader = resourceLoader; this.leafStyle = styleData; this.myResName = myResName; this.qualifiers = qualifiers; }
@Implementation public CharSequence getDialogMessage() { return attrs.getAttributeValue(ResourceLoader.ANDROID_NS, "dialogMessage"); }
public static ResourceLoader getResourceLoader(Context context) { return shadowOf(context.getApplicationContext()).getResourceLoader(); }
@Override protected void setUpApplicationState(Method method, ParallelUniverseInterface parallelUniverseInterface, boolean strictI18n, ResourceLoader systemResourceLoader, AndroidManifest appManifest, Config config) { // Don't do any resource loading or app init, because that's what we're trying to test here. }
@Override protected void setUpApplicationState(Method method, ParallelUniverseInterface parallelUniverseInterface, boolean strictI18n, ResourceLoader systemResourceLoader, AndroidManifest appManifest, Config config) { super.setUpApplicationState(method, parallelUniverseInterface, strictI18n, systemResourceLoader, appManifest, config); this.application = parallelUniverseInterface.getCurrentApplication(); }
public static Resources createResourcesFor(ResourceLoader resourceLoader) { return ShadowResources.createFor(resourceLoader); }
public OverlayResourceLoaderWithDummyResources(String packageName, List<PackageResourceLoader> subResourceLoaders, ResourceLoader systemResourceLoader) { super(packageName, subResourceLoaders); this.systemResourceLoader = systemResourceLoader; }
/** * Non-Android accessor (and a handy way to get a working {@code ResourceLoader} * * @return the {@code ResourceLoader} associated with this Application */ public ResourceLoader getResourceLoader() { return resourceLoader; }
/** * Non-Android accessor. * * @return the {@code ResourceLoader} associated with this {@code Context} */ public ResourceLoader getResourceLoader() { return shadowOf((Application) realContext.getApplicationContext()).getResourceLoader(); }
void setUpApplicationState(Method method, TestLifecycle testLifecycle, boolean strictI18n, ResourceLoader systemResourceLoader, AndroidManifest appManifest, Config config);