Home | History | Annotate | Download | only in lesson3
      1 /*
      2  * Copyright (C) 2013 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 com.example.android.testingfun.tests.lesson3;
     17 
     18 
     19 import com.example.android.testingfun.R;
     20 import com.example.android.testingfun.lesson3.ClickFunActivity;
     21 
     22 import android.test.ActivityInstrumentationTestCase2;
     23 import android.test.TouchUtils;
     24 import android.test.ViewAsserts;
     25 import android.test.suitebuilder.annotation.MediumTest;
     26 import android.view.View;
     27 import android.view.ViewGroup;
     28 import android.view.WindowManager;
     29 import android.widget.Button;
     30 import android.widget.TextView;
     31 
     32 /**
     33  * Tests for ClickFunActivity. Introduces touch mode, test size annotations and TouchUtils.
     34  */
     35 public class ClickFunActivityTest extends ActivityInstrumentationTestCase2<ClickFunActivity> {
     36 
     37     private ClickFunActivity mClickFunActivity;
     38     private Button mClickMeButton;
     39     private TextView mInfoTextView;
     40 
     41     public ClickFunActivityTest() {
     42         super(ClickFunActivity.class);
     43     }
     44 
     45     /**
     46      * Sets up the test fixture for this test case. This method is always called before every test
     47      * run.
     48      */
     49     @Override
     50     protected void setUp() throws Exception {
     51         super.setUp();
     52 
     53         //Sets the initial touch mode for the Activity under test. This must be called before
     54         //getActivity()
     55         setActivityInitialTouchMode(true);
     56 
     57         //Get a reference to the Activity under test, starting it if necessary.
     58         mClickFunActivity = getActivity();
     59 
     60         //Get references to all views
     61         mClickMeButton = (Button) mClickFunActivity.findViewById(R.id.launch_next_activity_button);
     62         mInfoTextView = (TextView) mClickFunActivity.findViewById(R.id.info_text_view);
     63     }
     64 
     65     /**
     66      * Tests the preconditions of this test fixture.
     67      */
     68     @MediumTest
     69     public void testPreconditions() {
     70         assertNotNull("mClickFunActivity is null", mClickFunActivity);
     71         assertNotNull("mClickMeButton is null", mClickMeButton);
     72         assertNotNull("mInfoTextView is null", mInfoTextView);
     73     }
     74 
     75     @MediumTest
     76     public void testClickMeButton_layout() {
     77         //Retrieve the top-level window decor view
     78         final View decorView = mClickFunActivity.getWindow().getDecorView();
     79 
     80         //Verify that the mClickMeButton is on screen
     81         ViewAsserts.assertOnScreen(decorView, mClickMeButton);
     82 
     83         //Verify width and heights
     84         final ViewGroup.LayoutParams layoutParams = mClickMeButton.getLayoutParams();
     85         assertNotNull(layoutParams);
     86         assertEquals(layoutParams.width, WindowManager.LayoutParams.MATCH_PARENT);
     87         assertEquals(layoutParams.height, WindowManager.LayoutParams.WRAP_CONTENT);
     88     }
     89 
     90     @MediumTest
     91     public void testClickMeButton_labelText() {
     92         //Verify that mClickMeButton uses the correct string resource
     93         final String expectedNextButtonText = mClickFunActivity.getString(R.string.label_click_me);
     94         final String actualNextButtonText = mClickMeButton.getText().toString();
     95         assertEquals(expectedNextButtonText, actualNextButtonText);
     96     }
     97 
     98     @MediumTest
     99     public void testInfoTextView_layout() {
    100         //Retrieve the top-level window decor view
    101         final View decorView = mClickFunActivity.getWindow().getDecorView();
    102 
    103         //Verify that the mInfoTextView is on screen and is not visible
    104         ViewAsserts.assertOnScreen(decorView, mInfoTextView);
    105         assertTrue(View.GONE == mInfoTextView.getVisibility());
    106     }
    107 
    108     @MediumTest
    109     public void testInfoTextViewText_isEmpty() {
    110         //Verify that the mInfoTextView is initialized with the correct default value
    111         assertEquals("", mInfoTextView.getText());
    112     }
    113 
    114     @MediumTest
    115     public void testClickMeButton_clickButtonAndExpectInfoText() {
    116         String expectedInfoText = mClickFunActivity.getString(R.string.info_text);
    117         //Perform a click on mClickMeButton
    118         TouchUtils.clickView(this, mClickMeButton);
    119         //Verify the that mClickMeButton was clicked. mInfoTextView is visible and contains
    120         //the correct text.
    121         assertTrue(View.VISIBLE == mInfoTextView.getVisibility());
    122         assertEquals(expectedInfoText, mInfoTextView.getText());
    123     }
    124 }
    125