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