FileDocCategorySizeDatePackage
ConfigurationBoundResourceCacheTest.javaAPI DocAndroid 5.1 API10366Thu Mar 12 22:22:12 GMT 2015android.content.res

ConfigurationBoundResourceCacheTest

public class ConfigurationBoundResourceCacheTest extends android.test.ActivityInstrumentationTestCase2

Fields Summary
ConfigurationBoundResourceCache
mCache
Method
mCalcConfigChanges
Constructors Summary
public ConfigurationBoundResourceCacheTest()

        super(ResourceCacheActivity.class);
    
Methods Summary
private intcalcConfigChanges(Resources resources, Configuration configuration)

        if (mCalcConfigChanges == null) {
            mCalcConfigChanges = Resources.class.getDeclaredMethod("calcConfigChanges",
                    Configuration.class);
            mCalcConfigChanges.setAccessible(true);
        }
        return (Integer) mCalcConfigChanges.invoke(resources, configuration);

    
protected voidsetUp()

        super.setUp();
        mCache = new ConfigurationBoundResourceCache<Float>(getActivity().getResources());
    
public voidtestConfigChangeMultipleResources()

        TypedValue staticValue = new TypedValue();
        TypedValue changingValue = new TypedValue();
        final Resources res = getActivity().getResources();
        res.getValue(R.dimen.resource_cache_test_generic, staticValue, true);
        res.getValue(R.dimen.resource_cache_test_orientation_dependent, changingValue, true);
        float staticDim = TypedValue.complexToDimension(staticValue.data, res.getDisplayMetrics());
        float changingDim = TypedValue.complexToDimension(changingValue.data,
                res.getDisplayMetrics());
        mCache.put(R.dimen.resource_cache_test_generic, getActivity().getTheme(),
                new DummyFloatConstantState(staticDim, staticValue.changingConfigurations));
        mCache.put(R.dimen.resource_cache_test_orientation_dependent, getActivity().getTheme(),
                new DummyFloatConstantState(changingDim, changingValue.changingConfigurations));
        final Configuration cfg = res.getConfiguration();
        Configuration newCnf = new Configuration(cfg);
        newCnf.orientation = cfg.orientation == Configuration.ORIENTATION_LANDSCAPE ?
                Configuration.ORIENTATION_PORTRAIT
                : Configuration.ORIENTATION_LANDSCAPE;
        int changes = calcConfigChanges(res, newCnf);
        assertEquals(staticDim, mCache.get(R.dimen.resource_cache_test_generic,
                getActivity().getTheme()));
        assertEquals(changingDim, mCache.get(R.dimen.resource_cache_test_orientation_dependent,
                getActivity().getTheme()));
        mCache.onConfigurationChange(changes);
        assertEquals(staticDim, mCache.get(R.dimen.resource_cache_test_generic,
                getActivity().getTheme()));
        assertNull(mCache.get(R.dimen.resource_cache_test_orientation_dependent,
                getActivity().getTheme()));
    
public voidtestConfigChangeMultipleThemes()

        TypedValue[] staticValues = new TypedValue[]{new TypedValue(), new TypedValue()};
        TypedValue[] changingValues = new TypedValue[]{new TypedValue(), new TypedValue()};
        float staticDim = 0;
        float changingDim = 0;
        final Resources res = getActivity().getResources();
        for (int i = 0; i < 2; i++) {
            res.getValue(R.dimen.resource_cache_test_generic, staticValues[i], true);
            staticDim = TypedValue
                    .complexToDimension(staticValues[i].data, res.getDisplayMetrics());

            res.getValue(R.dimen.resource_cache_test_orientation_dependent, changingValues[i],
                    true);
            changingDim = TypedValue.complexToDimension(changingValues[i].data,
                    res.getDisplayMetrics());
            final Resources.Theme theme = i == 0 ? getActivity().getTheme() : null;
            mCache.put(R.dimen.resource_cache_test_generic, theme,
                    new DummyFloatConstantState(staticDim, staticValues[i].changingConfigurations));
            mCache.put(R.dimen.resource_cache_test_orientation_dependent, theme,
                    new DummyFloatConstantState(changingDim,
                            changingValues[i].changingConfigurations));
        }
        final Configuration cfg = res.getConfiguration();
        Configuration newCnf = new Configuration(cfg);
        newCnf.orientation = cfg.orientation == Configuration.ORIENTATION_LANDSCAPE ?
                Configuration.ORIENTATION_PORTRAIT
                : Configuration.ORIENTATION_LANDSCAPE;
        int changes = calcConfigChanges(res, newCnf);
        for (int i = 0; i < 2; i++) {
            final Resources.Theme theme = i == 0 ? getActivity().getTheme() : null;
            assertEquals(staticDim, mCache.get(R.dimen.resource_cache_test_generic, theme));
            assertEquals(changingDim,
                    mCache.get(R.dimen.resource_cache_test_orientation_dependent, theme));
        }
        mCache.onConfigurationChange(changes);
        for (int i = 0; i < 2; i++) {
            final Resources.Theme theme = i == 0 ? getActivity().getTheme() : null;
            assertEquals(staticDim, mCache.get(R.dimen.resource_cache_test_generic, theme));
            assertNull(mCache.get(R.dimen.resource_cache_test_orientation_dependent, theme));
        }
    
public voidtestEffectiveConfigChange()

        TypedValue changingValue = new TypedValue();
        long key = 4L;
        final Resources res = getActivity().getResources();
        res.getValue(R.dimen.resource_cache_test_orientation_dependent, changingValue, true);
        float changingDim = TypedValue.complexToDimension(changingValue.data,
                res.getDisplayMetrics());
        mCache.put(key, getActivity().getTheme(),
                new DummyFloatConstantState(changingDim, changingValue.changingConfigurations));

        final Configuration cfg = res.getConfiguration();
        Configuration newCnf = new Configuration(cfg);
        newCnf.orientation = cfg.orientation == Configuration.ORIENTATION_LANDSCAPE ?
                Configuration.ORIENTATION_PORTRAIT
                : Configuration.ORIENTATION_LANDSCAPE;
        int changes = calcConfigChanges(res, newCnf);
        assertEquals(changingDim, mCache.get(key, getActivity().getTheme()));
        mCache.onConfigurationChange(changes);
        assertNull(mCache.get(key, getActivity().getTheme()));
    
public voidtestGetEmpty()

        assertNull(mCache.get(-1, null));
    
public voidtestMultiThreadPutGet()

        mCache.put(1, getActivity().getTheme(), new DummyFloatConstantState(5f));
        mCache.put(1, null, new DummyFloatConstantState(10f));
        assertEquals(10f, mCache.get(1, null));
        assertNotSame(10f, mCache.get(1, null));
        assertEquals(5f, mCache.get(1, getActivity().getTheme()));
        assertNotSame(5f, mCache.get(1, getActivity().getTheme()));
    
public voidtestSetGet()

        mCache.put(1, null, new DummyFloatConstantState(5f));
        assertEquals(5f, mCache.get(1, null));
        assertNotSame(5f, mCache.get(1, null));
        assertEquals(null, mCache.get(1, getActivity().getTheme()));
    
public voidtestSetGetThemed()

        mCache.put(1, getActivity().getTheme(), new DummyFloatConstantState(5f));
        assertEquals(null, mCache.get(1, null));
        assertEquals(5f, mCache.get(1, getActivity().getTheme()));
        assertNotSame(5f, mCache.get(1, getActivity().getTheme()));
    
public voidtestVoidConfigChange()

        TypedValue staticValue = new TypedValue();
        long key = 3L;
        final Resources res = getActivity().getResources();
        res.getValue(R.dimen.resource_cache_test_generic, staticValue, true);
        float staticDim = TypedValue.complexToDimension(staticValue.data, res.getDisplayMetrics());
        mCache.put(key, getActivity().getTheme(),
                new DummyFloatConstantState(staticDim, staticValue.changingConfigurations));
        final Configuration cfg = res.getConfiguration();
        Configuration newCnf = new Configuration(cfg);
        newCnf.orientation = cfg.orientation == Configuration.ORIENTATION_LANDSCAPE ?
                Configuration.ORIENTATION_PORTRAIT
                : Configuration.ORIENTATION_LANDSCAPE;
        int changes = calcConfigChanges(res, newCnf);
        assertEquals(staticDim, mCache.get(key, getActivity().getTheme()));
        mCache.onConfigurationChange(changes);
        assertEquals(staticDim, mCache.get(key, getActivity().getTheme()));