Home | History | Annotate | Download | only in view
      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 
     17 package android.view;
     18 
     19 import android.view.ZeroSized;
     20 import com.android.frameworks.coretests.R;
     21 
     22 import android.test.ActivityInstrumentationTestCase;
     23 import android.test.suitebuilder.annotation.MediumTest;
     24 import android.view.View;
     25 import android.graphics.Bitmap;
     26 
     27 /**
     28  * Builds the drawing cache of Views of various dimension. The assumption is that
     29  * a View with a 0-sized dimension (width or height) will always have a null
     30  * drawing cache.
     31  */
     32 public class ZeroSizedTest extends ActivityInstrumentationTestCase<ZeroSized> {
     33     private View mWithDimension;
     34     private View mWithNoWdith;
     35     private View mWithNoHeight;
     36     private View mWithNoDimension;
     37 
     38     public ZeroSizedTest() {
     39         super("com.android.frameworks.coretests", ZeroSized.class);
     40     }
     41 
     42     @Override
     43     public void setUp() throws Exception {
     44         super.setUp();
     45 
     46         final ZeroSized activity = getActivity();
     47         mWithDimension = activity.findViewById(R.id.dimension);
     48         mWithNoWdith = activity.findViewById(R.id.noWidth);
     49         mWithNoHeight = activity.findViewById(R.id.noHeight);
     50         mWithNoDimension = activity.findViewById(R.id.noDimension);
     51     }
     52 
     53     @MediumTest
     54     public void testSetUpConditions() throws Exception {
     55         assertNotNull(mWithDimension);
     56         assertNotNull(mWithNoWdith);
     57         assertNotNull(mWithNoHeight);
     58         assertNotNull(mWithNoDimension);
     59     }
     60 
     61     @MediumTest
     62     public void testDrawingCacheWithDimension() throws Exception {
     63         assertTrue(mWithDimension.getWidth() > 0);
     64         assertTrue(mWithDimension.getHeight() > 0);
     65         assertNotNull(createCacheForView(mWithDimension));
     66     }
     67 
     68     @MediumTest
     69     public void testDrawingCacheWithNoWidth() throws Exception {
     70         assertTrue(mWithNoWdith.getWidth() == 0);
     71         assertTrue(mWithNoWdith.getHeight() > 0);
     72         assertNull(createCacheForView(mWithNoWdith));
     73     }
     74 
     75     @MediumTest
     76     public void testDrawingCacheWithNoHeight() throws Exception {
     77         assertTrue(mWithNoHeight.getWidth() > 0);
     78         assertTrue(mWithNoHeight.getHeight() == 0);
     79         assertNull(createCacheForView(mWithNoHeight));
     80     }
     81 
     82     @MediumTest
     83     public void testDrawingCacheWithNoDimension() throws Exception {
     84         assertTrue(mWithNoDimension.getWidth() == 0);
     85         assertTrue(mWithNoDimension.getHeight() == 0);
     86         assertNull(createCacheForView(mWithNoDimension));
     87     }
     88 
     89     private Bitmap createCacheForView(final View view) {
     90         final Bitmap[] cache = new Bitmap[1];
     91         getActivity().runOnUiThread(new Runnable() {
     92             public void run() {
     93                 view.setDrawingCacheEnabled(true);
     94                 view.invalidate();
     95                 view.buildDrawingCache();
     96                 cache[0] = view.getDrawingCache();
     97             }
     98         });
     99         getInstrumentation().waitForIdleSync();
    100         return cache[0];
    101     }
    102 }
    103