1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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
17package android.view;
18
19import android.view.BigCache;
20import com.android.frameworks.coretests.R;
21
22import android.test.ActivityInstrumentationTestCase;
23import android.test.suitebuilder.annotation.MediumTest;
24import android.view.View;
25import android.view.ViewConfiguration;
26import android.graphics.Bitmap;
27
28/**
29 * Builds the drawing cache of two Views, one smaller than the maximum cache size,
30 * one larger than the maximum cache size. The latter should always have a null
31 * drawing cache.
32 */
33public class BigCacheTest extends ActivityInstrumentationTestCase<BigCache> {
34    private View mTiny;
35    private View mLarge;
36
37    public BigCacheTest() {
38        super("com.android.frameworks.coretests", BigCache.class);
39    }
40
41    @Override
42    public void setUp() throws Exception {
43        super.setUp();
44
45        final BigCache activity = getActivity();
46        mTiny = activity.findViewById(R.id.a);
47        mLarge = activity.findViewById(R.id.b);
48    }
49
50    @MediumTest
51    public void testSetUpConditions() throws Exception {
52        assertNotNull(mTiny);
53        assertNotNull(mLarge);
54    }
55
56    @MediumTest
57    public void testDrawingCacheBelowMaximumSize() throws Exception {
58        final int max = ViewConfiguration.get(getActivity()).getScaledMaximumDrawingCacheSize();
59        assertTrue(mTiny.getWidth() * mTiny.getHeight() * 2 < max);
60        assertNotNull(createCacheForView(mTiny));
61    }
62
63    // TODO: needs to be adjusted to pass on non-HVGA displays
64    // @MediumTest
65    public void testDrawingCacheAboveMaximumSize() throws Exception {
66        final int max = ViewConfiguration.get(getActivity()).getScaledMaximumDrawingCacheSize();
67        assertTrue(mLarge.getWidth() * mLarge.getHeight() * 2 > max);
68        assertNull(createCacheForView(mLarge));
69    }
70
71    private Bitmap createCacheForView(final View view) {
72        final Bitmap[] cache = new Bitmap[1];
73        getActivity().runOnUiThread(new Runnable() {
74            public void run() {
75                view.setDrawingCacheEnabled(true);
76                view.invalidate();
77                view.buildDrawingCache();
78                cache[0] = view.getDrawingCache();
79            }
80        });
81        getInstrumentation().waitForIdleSync();
82        return cache[0];
83    }
84}
85