Home | History | Annotate | Download | only in drawable
      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.graphics.drawable;
     18 
     19 import android.R;
     20 import android.graphics.Canvas;
     21 import android.graphics.ColorFilter;
     22 import android.util.StateSet;
     23 import android.view.MockView;
     24 
     25 import junit.framework.TestCase;
     26 
     27 /**
     28  * Tests for StateListDrawable
     29  *
     30  */
     31 
     32 public class StateListDrawableTest extends TestCase {
     33 
     34     private StateListDrawable slDrawable;
     35     private MockDrawable mockFocusedDrawable;
     36     private MockDrawable mockCheckedDrawable;
     37     private MockView mockView;
     38     private MockDrawable mockDefaultDrawable;
     39 
     40 
     41     // Re-enable tests when we are running in the framework-test directory which allows
     42     // access to package private access for MockView
     43 
     44     public void broken_testFocusScenarioSetStringWildcardFirst() throws Exception {
     45         int focusedStateSet[] = {R.attr.state_focused};
     46         int checkedStateSet[] = {R.attr.state_checked};
     47         slDrawable.addState(StateSet.WILD_CARD,
     48                                mockDefaultDrawable);
     49         slDrawable.addState(checkedStateSet, mockCheckedDrawable);
     50         slDrawable.addState(focusedStateSet, mockFocusedDrawable);
     51         mockView.requestFocus();
     52         mockView.getBackground().draw(null);
     53         assertTrue(mockDefaultDrawable.wasDrawn);
     54     }
     55 
     56     public void broken_testFocusScenarioStateSetWildcardLast() throws Exception {
     57         int focusedStateSet[] = {R.attr.state_focused};
     58         int checkedStateSet[] = {R.attr.state_checked};
     59         slDrawable.addState(checkedStateSet, mockCheckedDrawable);
     60         slDrawable.addState(focusedStateSet, mockFocusedDrawable);
     61         slDrawable.addState(StateSet.WILD_CARD,
     62                                mockDefaultDrawable);
     63         mockView.requestFocus();
     64         mockView.getBackground().draw(null);
     65         assertTrue(mockFocusedDrawable.wasDrawn);
     66     }
     67 
     68 
     69     protected void setUp() throws Exception {
     70         super.setUp();
     71         slDrawable = new StateListDrawable();
     72         mockFocusedDrawable = new MockDrawable();
     73         mockCheckedDrawable = new MockDrawable();
     74         mockDefaultDrawable = new MockDrawable();
     75         mockView = new MockView();
     76         mockView.setBackgroundDrawable(slDrawable);
     77     }
     78 
     79     static class MockDrawable extends Drawable {
     80 
     81         public boolean wasDrawn = false;
     82 
     83         public void draw(Canvas canvas) {
     84             wasDrawn = true;
     85         }
     86 
     87         public void setAlpha(int alpha) {
     88         }
     89 
     90         public void setColorFilter(ColorFilter cf) {
     91         }
     92 
     93         public int getOpacity() {
     94             return android.graphics.PixelFormat.UNKNOWN;
     95         }
     96     }
     97 
     98 }
     99