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.example.android.apis.view; 18 19 import com.example.android.apis.R; 20 21 import android.test.ActivityInstrumentationTestCase2; 22 import android.test.suitebuilder.annotation.MediumTest; 23 import android.view.KeyEvent; 24 import android.widget.Button; 25 26 /** 27 * An example of an {@link ActivityInstrumentationTestCase} of a specific activity {@link Focus2}. 28 * By virtue of extending {@link ActivityInstrumentationTestCase}, the target activity is automatically 29 * launched and finished before and after each test. This also extends 30 * {@link android.test.InstrumentationTestCase}, which provides 31 * access to methods for sending events to the target activity, such as key and 32 * touch events. See {@link #sendKeys}. 33 * 34 * In general, {@link android.test.InstrumentationTestCase}s and {@link ActivityInstrumentationTestCase}s 35 * are heavier weight functional tests available for end to end testing of your 36 * user interface. When run via a {@link android.test.InstrumentationTestRunner}, 37 * the necessary {@link android.app.Instrumentation} will be injected for you to 38 * user via {@link #getInstrumentation} in your tests. 39 * 40 * See {@link com.example.android.apis.app.ForwardingTest} for an example of an Activity unit test. 41 * 42 * See {@link com.example.android.apis.AllTests} for documentation on running 43 * all tests and individual tests in this application. 44 */ 45 public class Focus2ActivityTest extends ActivityInstrumentationTestCase2<Focus2> { 46 47 private Button mLeftButton; 48 private Button mCenterButton; 49 private Button mRightButton; 50 51 /** 52 * Creates an {@link ActivityInstrumentationTestCase2} that tests the {@link Focus2} activity. 53 */ Focus2ActivityTest()54 public Focus2ActivityTest() { 55 super(Focus2.class); 56 } 57 58 @Override setUp()59 protected void setUp() throws Exception { 60 super.setUp(); 61 final Focus2 a = getActivity(); 62 // ensure a valid handle to the activity has been returned 63 assertNotNull(a); 64 mLeftButton = (Button) a.findViewById(R.id.leftButton); 65 mCenterButton = (Button) a.findViewById(R.id.centerButton); 66 mRightButton = (Button) a.findViewById(R.id.rightButton); 67 } 68 69 /** 70 * The name 'test preconditions' is a convention to signal that if this 71 * test doesn't pass, the test case was not set up properly and it might 72 * explain any and all failures in other tests. This is not guaranteed 73 * to run before other tests, as junit uses reflection to find the tests. 74 */ 75 @MediumTest testPreconditions()76 public void testPreconditions() { 77 assertTrue("center button should be right of left button", 78 mLeftButton.getRight() < mCenterButton.getLeft()); 79 assertTrue("right button should be right of center button", 80 mCenterButton.getRight() < mRightButton.getLeft()); 81 assertTrue("left button should be focused", mLeftButton.isFocused()); 82 } 83 84 @MediumTest testGoingRightFromLeftButtonJumpsOverCenterToRight()85 public void testGoingRightFromLeftButtonJumpsOverCenterToRight() { 86 sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT); 87 assertTrue("right button should be focused", mRightButton.isFocused()); 88 } 89 90 @MediumTest testGoingLeftFromRightButtonGoesToCenter()91 public void testGoingLeftFromRightButtonGoesToCenter() { 92 // Give right button focus by having it request focus. We post it 93 // to the UI thread because we are not running on the same thread, and 94 // any direct api calls that change state must be made from the UI thread. 95 // This is in contrast to instrumentation calls that send events that are 96 // processed through the framework and eventually find their way to 97 // affecting the ui thread. 98 getActivity().runOnUiThread(new Runnable() { 99 public void run() { 100 mRightButton.requestFocus(); 101 } 102 }); 103 // wait for the request to go through 104 getInstrumentation().waitForIdleSync(); 105 106 assertTrue(mRightButton.isFocused()); 107 108 sendKeys(KeyEvent.KEYCODE_DPAD_LEFT); 109 assertTrue("center button should be focused", mCenterButton.isFocused()); 110 } 111 } 112