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