Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2008 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.android.tests.libstest.app;
     18 
     19 import android.test.ActivityInstrumentationTestCase2;
     20 import android.test.suitebuilder.annotation.MediumTest;
     21 import android.widget.TextView;
     22 
     23 /**
     24  * An example of an {@link ActivityInstrumentationTestCase2} of a specific activity {@link Focus2}.
     25  * By virtue of extending {@link ActivityInstrumentationTestCase2}, the target activity is automatically
     26  * launched and finished before and after each test.  This also extends
     27  * {@link android.test.InstrumentationTestCase}, which provides
     28  * access to methods for sending events to the target activity, such as key and
     29  * touch events.  See {@link #sendKeys}.
     30  *
     31  * In general, {@link android.test.InstrumentationTestCase}s and {@link ActivityInstrumentationTestCase2}s
     32  * are heavier weight functional tests available for end to end testing of your
     33  * user interface.  When run via a {@link android.test.InstrumentationTestRunner},
     34  * the necessary {@link android.app.Instrumentation} will be injected for you to
     35  * user via {@link #getInstrumentation} in your tests.
     36  *
     37  * See {@link com.example.android.apis.AllTests} for documentation on running
     38  * all tests and individual tests in this application.
     39  */
     40 public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
     41 
     42     private TextView mAppTextView1;
     43     private TextView mAppTextView2;
     44     private TextView mLib1TextView1;
     45     private TextView mLib1TextView2;
     46     private TextView mLib2TextView1;
     47     private TextView mLib2TextView2;
     48 
     49     /**
     50      * Creates an {@link ActivityInstrumentationTestCase2} that tests the {@link Focus2} activity.
     51      */
     52     public MainActivityTest() {
     53         super(MainActivity.class);
     54     }
     55 
     56     @Override
     57     protected void setUp() throws Exception {
     58         super.setUp();
     59         final MainActivity a = getActivity();
     60         // ensure a valid handle to the activity has been returned
     61         assertNotNull(a);
     62 
     63         mAppTextView1 = (TextView) a.findViewById(R.id.app_text1);
     64         mAppTextView2 = (TextView) a.findViewById(R.id.app_text1);
     65         mLib1TextView1 = (TextView) a.findViewById(R.id.lib1_text1);
     66         mLib1TextView2 = (TextView) a.findViewById(R.id.lib1_text2);
     67         mLib2TextView1 = (TextView) a.findViewById(R.id.lib2_text1);
     68         mLib2TextView2 = (TextView) a.findViewById(R.id.lib2_text2);
     69     }
     70 
     71     /**
     72      * The name 'test preconditions' is a convention to signal that if this
     73      * test doesn't pass, the test case was not set up properly and it might
     74      * explain any and all failures in other tests.  This is not guaranteed
     75      * to run before other tests, as junit uses reflection to find the tests.
     76      */
     77     @MediumTest
     78     public void testPreconditions() {
     79         assertNotNull(mAppTextView1);
     80         assertNotNull(mAppTextView2);
     81         assertNotNull(mLib1TextView1);
     82         assertNotNull(mLib1TextView2);
     83         assertNotNull(mLib2TextView1);
     84         assertNotNull(mLib2TextView2);
     85     }
     86 
     87     @MediumTest
     88     public void testAndroidStrings() {
     89         assertEquals("SUCCESS-APP", mAppTextView1.getText());
     90         assertEquals("SUCCESS-LIB1", mLib1TextView1.getText());
     91         assertEquals("SUCCESS-LIB2", mLib2TextView1.getText());
     92     }
     93 
     94     @MediumTest
     95     public void testJavaStrings() {
     96         assertEquals("SUCCESS-APP", mAppTextView2.getText());
     97         assertEquals("SUCCESS-LIB1", mLib1TextView2.getText());
     98         assertEquals("SUCCESS-LIB2", mLib2TextView2.getText());
     99     }
    100 }
    101