Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2016 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.cts;
     18 
     19 import static org.junit.Assert.assertNotNull;
     20 
     21 import android.graphics.Color;
     22 import android.graphics.Rect;
     23 import android.graphics.drawable.ColorDrawable;
     24 import android.graphics.drawable.Drawable;
     25 import android.support.test.annotation.UiThreadTest;
     26 import android.support.test.filters.MediumTest;
     27 import android.support.test.rule.ActivityTestRule;
     28 import android.support.test.runner.AndroidJUnit4;
     29 import android.util.Pair;
     30 import android.view.View;
     31 import android.view.ViewOverlay;
     32 import android.view.cts.util.DrawingUtils;
     33 
     34 import org.junit.Before;
     35 import org.junit.Rule;
     36 import org.junit.Test;
     37 import org.junit.runner.RunWith;
     38 
     39 import java.util.ArrayList;
     40 import java.util.List;
     41 
     42 @MediumTest
     43 @RunWith(AndroidJUnit4.class)
     44 public class ViewOverlayTest {
     45     private View mViewWithOverlay;
     46     private ViewOverlay mViewOverlay;
     47 
     48     @Rule
     49     public ActivityTestRule<ViewOverlayCtsActivity> mActivityRule =
     50             new ActivityTestRule<>(ViewOverlayCtsActivity.class);
     51 
     52     @Before
     53     public void setup() {
     54         mViewWithOverlay = mActivityRule.getActivity().findViewById(R.id.view_with_overlay);
     55         mViewOverlay = mViewWithOverlay.getOverlay();
     56     }
     57 
     58     @Test
     59     public void testBasics() {
     60         DrawingUtils.assertAllPixelsOfColor("Default fill", mViewWithOverlay,
     61                 Color.WHITE, null);
     62         assertNotNull("Overlay is not null", mViewOverlay);
     63     }
     64 
     65     @UiThreadTest
     66     @Test(expected=IllegalArgumentException.class)
     67     public void testAddNullDrawable() {
     68          mViewOverlay.add(null);
     69     }
     70 
     71     @UiThreadTest
     72     @Test(expected=IllegalArgumentException.class)
     73     public void testRemoveNullDrawable() {
     74         mViewOverlay.remove(null);
     75     }
     76 
     77     @UiThreadTest
     78     @Test
     79     public void testOverlayWithOneDrawable() {
     80         // Add one colored drawable to the overlay
     81         final Drawable redDrawable = new ColorDrawable(Color.RED);
     82         redDrawable.setBounds(20, 30, 40, 50);
     83         mViewOverlay.add(redDrawable);
     84 
     85         final List<Pair<Rect, Integer>> colorRectangles = new ArrayList<>();
     86         colorRectangles.add(new Pair<>(new Rect(20, 30, 40, 50), Color.RED));
     87         DrawingUtils.assertAllPixelsOfColor("Overlay with one red drawable", mViewWithOverlay,
     88                 Color.WHITE, colorRectangles);
     89 
     90         // Now remove that drawable from the overlay and test that we're back to pure white fill
     91         mViewOverlay.remove(redDrawable);
     92         DrawingUtils.assertAllPixelsOfColor("Back to default fill", mViewWithOverlay,
     93                 Color.WHITE, null);
     94     }
     95 
     96     @UiThreadTest
     97     @Test
     98     public void testAddTheSameDrawableTwice() {
     99         final Drawable redDrawable = new ColorDrawable(Color.RED);
    100         redDrawable.setBounds(20, 30, 40, 50);
    101         // Add the same drawable twice
    102         mViewOverlay.add(redDrawable);
    103         mViewOverlay.add(redDrawable);
    104 
    105         final List<Pair<Rect, Integer>> colorRectangles = new ArrayList<>();
    106         colorRectangles.add(new Pair<>(new Rect(20, 30, 40, 50), Color.RED));
    107         DrawingUtils.assertAllPixelsOfColor("Overlay with one red drawable", mViewWithOverlay,
    108                 Color.WHITE, colorRectangles);
    109 
    110         // Now remove that drawable from the overlay and test that we're back to pure white fill
    111         mViewOverlay.remove(redDrawable);
    112         DrawingUtils.assertAllPixelsOfColor("Back to default fill", mViewWithOverlay,
    113                 Color.WHITE, null);
    114     }
    115 
    116     @UiThreadTest
    117     @Test
    118     public void testRemoveTheSameDrawableTwice() {
    119         // Add one colored drawable to the overlay
    120         final Drawable redDrawable = new ColorDrawable(Color.RED);
    121         redDrawable.setBounds(20, 30, 40, 50);
    122         mViewOverlay.add(redDrawable);
    123 
    124         final List<Pair<Rect, Integer>> colorRectangles = new ArrayList<>();
    125         colorRectangles.add(new Pair<>(new Rect(20, 30, 40, 50), Color.RED));
    126         DrawingUtils.assertAllPixelsOfColor("Overlay with one red drawable", mViewWithOverlay,
    127                 Color.WHITE, colorRectangles);
    128 
    129         // Now remove that drawable from the overlay and test that we're back to pure white fill.
    130         // Remove the drawable twice. The second should be a no-op
    131         mViewOverlay.remove(redDrawable);
    132         mViewOverlay.remove(redDrawable);
    133         DrawingUtils.assertAllPixelsOfColor("Back to default fill", mViewWithOverlay,
    134                 Color.WHITE, null);
    135     }
    136 
    137     @UiThreadTest
    138     @Test
    139     public void testOverlayWithNonOverlappingDrawables() {
    140         // Add three color drawables to the overlay
    141         final Drawable redDrawable = new ColorDrawable(Color.RED);
    142         redDrawable.setBounds(10, 20, 30, 40);
    143         final Drawable greenDrawable = new ColorDrawable(Color.GREEN);
    144         greenDrawable.setBounds(60, 30, 90, 50);
    145         final Drawable blueDrawable = new ColorDrawable(Color.BLUE);
    146         blueDrawable.setBounds(40, 60, 80, 90);
    147 
    148         mViewOverlay.add(redDrawable);
    149         mViewOverlay.add(greenDrawable);
    150         mViewOverlay.add(blueDrawable);
    151 
    152         final List<Pair<Rect, Integer>> colorRectangles = new ArrayList<>();
    153         colorRectangles.add(new Pair<>(new Rect(10, 20, 30, 40), Color.RED));
    154         colorRectangles.add(new Pair<>(new Rect(60, 30, 90, 50), Color.GREEN));
    155         colorRectangles.add(new Pair<>(new Rect(40, 60, 80, 90), Color.BLUE));
    156         DrawingUtils.assertAllPixelsOfColor("Overlay with three drawables", mViewWithOverlay,
    157                 Color.WHITE, colorRectangles);
    158 
    159         // Remove one of the drawables from the overlay
    160         mViewOverlay.remove(greenDrawable);
    161         colorRectangles.clear();
    162         colorRectangles.add(new Pair<>(new Rect(10, 20, 30, 40), Color.RED));
    163         colorRectangles.add(new Pair<>(new Rect(40, 60, 80, 90), Color.BLUE));
    164         DrawingUtils.assertAllPixelsOfColor("Overlay with two drawables", mViewWithOverlay,
    165                 Color.WHITE, colorRectangles);
    166 
    167         // Clear all drawables from the overlay and test that we're back to pure white fill
    168         mViewOverlay.clear();
    169         DrawingUtils.assertAllPixelsOfColor("Back to default fill", mViewWithOverlay,
    170                 Color.WHITE, null);
    171     }
    172 
    173     @UiThreadTest
    174     @Test
    175     public void testOverlayWithOverlappingDrawables() {
    176         // Add two overlapping color drawables to the overlay
    177         final Drawable redDrawable = new ColorDrawable(Color.RED);
    178         redDrawable.setBounds(10, 20, 60, 40);
    179         final Drawable greenDrawable = new ColorDrawable(Color.GREEN);
    180         greenDrawable.setBounds(30, 20, 80, 40);
    181 
    182         mViewOverlay.add(redDrawable);
    183         mViewOverlay.add(greenDrawable);
    184 
    185         // Our overlay drawables overlap in horizontal 30-60 range. Here we test that the
    186         // second drawable is the one that is drawn last in that range.
    187         final List<Pair<Rect, Integer>> colorRectangles = new ArrayList<>();
    188         colorRectangles.add(new Pair<>(new Rect(10, 20, 30, 40), Color.RED));
    189         colorRectangles.add(new Pair<>(new Rect(30, 20, 80, 40), Color.GREEN));
    190         DrawingUtils.assertAllPixelsOfColor("Overlay with two drawables", mViewWithOverlay,
    191                 Color.WHITE, colorRectangles);
    192 
    193         // Remove the second from the overlay
    194         mViewOverlay.remove(greenDrawable);
    195         colorRectangles.clear();
    196         colorRectangles.add(new Pair<>(new Rect(10, 20, 60, 40), Color.RED));
    197         DrawingUtils.assertAllPixelsOfColor("Overlay with one drawable", mViewWithOverlay,
    198                 Color.WHITE, colorRectangles);
    199 
    200         // Clear all drawables from the overlay and test that we're back to pure white fill
    201         mViewOverlay.clear();
    202         DrawingUtils.assertAllPixelsOfColor("Back to default fill", mViewWithOverlay,
    203                 Color.WHITE, null);
    204     }
    205 
    206     @UiThreadTest
    207     @Test
    208     public void testOverlayDynamicChangesToDrawable() {
    209         // Add one colored drawable to the overlay
    210         final ColorDrawable drawable = new ColorDrawable(Color.RED);
    211         drawable.setBounds(20, 30, 40, 50);
    212         mViewOverlay.add(drawable);
    213 
    214         final List<Pair<Rect, Integer>> colorRectangles = new ArrayList<>();
    215         colorRectangles.add(new Pair<>(new Rect(20, 30, 40, 50), Color.RED));
    216         DrawingUtils.assertAllPixelsOfColor("Overlay with one red drawable", mViewWithOverlay,
    217                 Color.WHITE, colorRectangles);
    218 
    219         // Update the bounds of our red drawable. Note that ideally we want to verify that
    220         // ViewOverlay's internal implementation tracks the changes to the drawables and kicks
    221         // off a redraw pass at some point. Here we are testing a subset of that - that the
    222         // next time a redraw of View / ViewOverlay happens, it catches the new state of our
    223         // original drawable.
    224         drawable.setBounds(50, 10, 80, 90);
    225         colorRectangles.clear();
    226         colorRectangles.add(new Pair<>(new Rect(50, 10, 80, 90), Color.RED));
    227         DrawingUtils.assertAllPixelsOfColor("Red drawable moved", mViewWithOverlay,
    228                 Color.WHITE, colorRectangles);
    229 
    230         // Update the color of our drawable. Same (partial) testing as before.
    231         drawable.setColor(Color.GREEN);
    232         colorRectangles.clear();
    233         colorRectangles.add(new Pair<>(new Rect(50, 10, 80, 90), Color.GREEN));
    234         DrawingUtils.assertAllPixelsOfColor("Drawable is green now", mViewWithOverlay,
    235                 Color.WHITE, colorRectangles);
    236     }
    237 
    238     @UiThreadTest
    239     @Test
    240     public void testOverlayDynamicChangesToOverlappingDrawables() {
    241         // Add two overlapping color drawables to the overlay
    242         final ColorDrawable redDrawable = new ColorDrawable(Color.RED);
    243         redDrawable.setBounds(10, 20, 60, 40);
    244         final ColorDrawable greenDrawable = new ColorDrawable(Color.GREEN);
    245         greenDrawable.setBounds(30, 20, 80, 40);
    246 
    247         mViewOverlay.add(redDrawable);
    248         mViewOverlay.add(greenDrawable);
    249 
    250         // Our overlay drawables overlap in horizontal 30-60 range. This is the same test as
    251         // in testOverlayWithOverlappingDrawables
    252         final List<Pair<Rect, Integer>> colorRectangles = new ArrayList<>();
    253         colorRectangles.add(new Pair<>(new Rect(10, 20, 30, 40), Color.RED));
    254         colorRectangles.add(new Pair<>(new Rect(30, 20, 80, 40), Color.GREEN));
    255         DrawingUtils.assertAllPixelsOfColor("Overlay with two drawables", mViewWithOverlay,
    256                 Color.WHITE, colorRectangles);
    257 
    258         // Now change the color of the first drawable and verify that it didn't "bump" it up
    259         // in the drawing order.
    260         redDrawable.setColor(Color.BLUE);
    261         colorRectangles.add(new Pair<>(new Rect(10, 20, 30, 40), Color.BLUE));
    262         colorRectangles.add(new Pair<>(new Rect(30, 20, 80, 40), Color.GREEN));
    263         DrawingUtils.assertAllPixelsOfColor("Overlay with two drawables", mViewWithOverlay,
    264                 Color.WHITE, colorRectangles);
    265     }
    266 }