Home | History | Annotate | Download | only in test
      1 /*
      2 * Copyright (C) 2014 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 package android.ui.DrawableTinting.Application.src.test;
     17 
     18 import android.graphics.Color;
     19 import android.graphics.PorterDuff;
     20 import android.graphics.PorterDuffColorFilter;
     21 import android.test.ActivityInstrumentationTestCase2;
     22 import android.test.UiThreadTest;
     23 import android.ui.DrawableTinting.Application.src.main.java.com.example.android.drawabletinting.DrawableTintingFragment;
     24 import android.view.View;
     25 import android.widget.ImageView;
     26 import android.widget.SeekBar;
     27 import android.widget.Spinner;
     28 
     29 import com.example.android.drawabletinting.MainActivity;
     30 import com.example.android.drawabletinting.R;
     31 
     32 /**
     33  * Tests for drawabletinting sample.
     34  */
     35 public class SampleTests extends ActivityInstrumentationTestCase2<MainActivity> {
     36 
     37     private MainActivity mTestActivity;
     38     private DrawableTintingFragment mTestFragment;
     39 
     40     View mFragmentView;
     41     SeekBar mAlpha;
     42     SeekBar mRed;
     43     SeekBar mGreen;
     44     SeekBar mBlue;
     45     Spinner mBlendMode;
     46     ImageView mImage;
     47 
     48     public SampleTests() {
     49         super(MainActivity.class);
     50     }
     51 
     52     @Override
     53     protected void setUp() throws Exception {
     54         super.setUp();
     55 
     56         // Starts the activity under test using the default Intent with:
     57         // action = {@link Intent#ACTION_MAIN}
     58         // flags = {@link Intent#FLAG_ACTIVITY_NEW_TASK}
     59         // All other fields are null or empty.
     60         mTestActivity = getActivity();
     61         mTestFragment = (DrawableTintingFragment)
     62                 mTestActivity.getSupportFragmentManager().getFragments().get(1);
     63 
     64         final View fragmentView = mTestFragment.getView();
     65         mAlpha = (SeekBar) fragmentView.findViewById(R.id.alphaSeek);
     66         mRed = (SeekBar) fragmentView.findViewById(R.id.redSeek);
     67         mGreen = (SeekBar) fragmentView.findViewById(R.id.greenSeek);
     68         mBlue = (SeekBar) fragmentView.findViewById(R.id.blueSeek);
     69         mBlendMode = (Spinner) fragmentView.findViewById(R.id.blendSpinner);
     70         mImage = (ImageView) fragmentView.findViewById(R.id.image);
     71     }
     72 
     73     /**
     74      * Test if the test fixture has been set up correctly.
     75      */
     76     public void testPreconditions() {
     77         //Try to add a message to add context to your assertions. These messages will be shown if
     78         //a tests fails and make it easy to understand why a test failed
     79         assertNotNull("mTestActivity is null", mTestActivity);
     80         assertNotNull("mTestFragment is null", mTestFragment);
     81     }
     82 
     83     /**
     84      * Test the initial state of all UI elements, color and blend mode.
     85      */
     86     public void testInitialState() {
     87         assertEquals(255, mAlpha.getProgress());
     88         assertEquals(0, mRed.getProgress());
     89         assertEquals(0, mGreen.getProgress());
     90         assertEquals(0, mBlue.getProgress());
     91         assertEquals("Add", (String) mBlendMode.getSelectedItem());
     92 
     93         PorterDuffColorFilter colorFilter = (PorterDuffColorFilter) mImage.getColorFilter();
     94         assertNotNull(colorFilter);
     95         int initialColor = Color.BLACK;
     96         assertEquals(initialColor, colorFilter.getColor());
     97         assertEquals(PorterDuff.Mode.ADD, colorFilter.getMode());
     98     }
     99 
    100     /**
    101      * Test application of blend modes.
    102      */
    103     @UiThreadTest
    104     public void testModes() {
    105         final int testColor = Color.GREEN;
    106         // Test that each tint mode can be successfully applied and matches a valid PorterDuff mode
    107         final PorterDuff.Mode[] modes = PorterDuff.Mode.values();
    108 
    109         // Test that each blend mode can be applied
    110         for (PorterDuff.Mode m : modes) {
    111             mTestFragment.updateTint(testColor, m);
    112             final PorterDuffColorFilter filter = (PorterDuffColorFilter) mImage.getColorFilter();
    113             assertEquals(m, filter.getMode());
    114             assertEquals(testColor, filter.getColor());
    115         }
    116     }
    117 
    118     /**
    119      * Test the color computation from ARGB Seekbars.
    120      */
    121     public void testColor() {
    122         // Red
    123         mAlpha.setProgress(255);
    124         mRed.setProgress(255);
    125         mBlue.setProgress(0);
    126         mGreen.setProgress(0);
    127         assertEquals(Color.RED, mTestFragment.getColor());
    128 
    129         // Blue
    130         mAlpha.setProgress(255);
    131         mRed.setProgress(0);
    132         mBlue.setProgress(255);
    133         mGreen.setProgress(0);
    134         assertEquals(Color.BLUE, mTestFragment.getColor());
    135 
    136         // Green
    137         mAlpha.setProgress(255);
    138         mRed.setProgress(0);
    139         mBlue.setProgress(0);
    140         mGreen.setProgress(255);
    141         assertEquals(Color.GREEN, mTestFragment.getColor());
    142 
    143         // Black
    144         mAlpha.setProgress(255);
    145         mRed.setProgress(0);
    146         mBlue.setProgress(0);
    147         mGreen.setProgress(0);
    148         assertEquals(Color.BLACK, mTestFragment.getColor());
    149 
    150         // Transparent
    151         mAlpha.setProgress(0);
    152         mRed.setProgress(0);
    153         mBlue.setProgress(0);
    154         mGreen.setProgress(0);
    155         assertEquals(Color.TRANSPARENT, mTestFragment.getColor());
    156     }
    157 
    158 }
    159