Home | History | Annotate | Download | only in tests
      1 /*
      2 * Copyright 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 
     17 package com.example.android.interpolatorplayground.tests;
     18 
     19 import com.example.android.interpolator.InterpolatorFragment;
     20 import com.example.android.interpolator.MainActivity;
     21 import com.example.android.interpolator.R;
     22 
     23 import android.animation.ObjectAnimator;
     24 import android.test.ActivityInstrumentationTestCase2;
     25 import android.test.UiThreadTest;
     26 import android.view.animation.Interpolator;
     27 import android.widget.SeekBar;
     28 import android.widget.Spinner;
     29 import android.widget.TextView;
     30 
     31 /**
     32  * Tests for interpolatorplayground sample.
     33  */
     34 public class SampleTests extends ActivityInstrumentationTestCase2<MainActivity> {
     35 
     36     private MainActivity mTestActivity;
     37     private InterpolatorFragment mTestFragment;
     38 
     39     public SampleTests() {
     40         super(MainActivity.class);
     41     }
     42 
     43     @Override
     44     protected void setUp() throws Exception {
     45         super.setUp();
     46 
     47         setActivityInitialTouchMode(true);
     48 
     49         // Starts the activity under test using the default Intent with:
     50         // action = {@link Intent#ACTION_MAIN}
     51         // flags = {@link Intent#FLAG_ACTIVITY_NEW_TASK}
     52         // All other fields are null or empty.
     53         mTestActivity = getActivity();
     54         mTestFragment = (InterpolatorFragment)
     55                 mTestActivity.getSupportFragmentManager().getFragments().get(1);
     56     }
     57 
     58     /**
     59      * Test if the test fixture has been set up correctly.
     60      */
     61     public void testPreconditions() {
     62         //Try to add a message to add context to your assertions. These messages will be shown if
     63         //a tests fails and make it easy to understand why a test failed
     64         assertNotNull("mTestActivity is null", mTestActivity);
     65         assertNotNull("mTestFragment is null", mTestFragment);
     66     }
     67 
     68     /**
     69      * Test if all UI elements have been set up correctly.
     70      */
     71     public void testInitialisation() {
     72         final int initialDuration = 750;
     73         final String initialInterpolator = "Linear";
     74 
     75         SeekBar durationBar = (SeekBar) getActivity().findViewById(R.id.durationSeek);
     76         TextView durationLabel = (TextView) getActivity().findViewById(R.id.durationLabel);
     77         Spinner interpolateSpinner = (Spinner) getActivity().findViewById(R.id.interpolatorSpinner);
     78         Interpolator[] interpolators = mTestFragment.getInterpolators();
     79 
     80         // Duration in progress bar
     81         assertEquals(durationBar.getProgress(), initialDuration);
     82         // Duration label
     83         assertEquals(durationLabel.getText().toString(), getActivity().getResources().getString(R.string.animation_duration, initialDuration));
     84         // Initial Interpolator
     85         assertEquals((String) interpolateSpinner.getSelectedItem(), initialInterpolator);
     86 
     87         // The number of loaded interpolators has to match the number of entries in the spinner
     88         assertEquals(interpolators.length, interpolateSpinner.getCount());
     89         // Test that all interpolators have been loaded
     90         for (Interpolator i : interpolators) {
     91             assertNotNull(i);
     92         }
     93 
     94     }
     95 
     96     /**
     97      * Test if all Interpolators can be used to start an animation.
     98      */
     99     @UiThreadTest
    100     public void testStartInterpolators() {
    101 
    102         // Start an animation for each interpolator
    103         final Interpolator[] interpolators = mTestFragment.getInterpolators();
    104 
    105         for (final Interpolator i : interpolators) {
    106             // Start the animation
    107             ObjectAnimator animator = mTestFragment.startAnimation(i, 1000L, mTestFragment.getPathIn());
    108             // Check that the correct interpolator is used for the animation
    109             assertEquals(i, animator.getInterpolator());
    110             // Verify the animation has started
    111             assertTrue(animator.isStarted());
    112             // Cancel before starting the next animation
    113             animator.cancel();
    114         }
    115     }
    116 }
    117