Home | History | Annotate | Download | only in focus
      1 /*
      2  * Copyright (C) 2007 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 android.widget.focus;
     18 
     19 import android.widget.focus.RequestFocus;
     20 import com.android.frameworks.coretests.R;
     21 
     22 import android.os.Handler;
     23 import android.test.ActivityInstrumentationTestCase;
     24 import android.test.suitebuilder.annotation.LargeTest;
     25 import android.test.suitebuilder.annotation.MediumTest;
     26 import android.widget.Button;
     27 import android.util.AndroidRuntimeException;
     28 
     29 /**
     30  * {@link RequestFocusTest} is set up to exercise cases where the views that
     31  * have focus become invisible or GONE.
     32  */
     33 public class RequestFocusTest extends ActivityInstrumentationTestCase<RequestFocus> {
     34 
     35     private Button mTopLeftButton;
     36     private Button mBottomLeftButton;
     37     private Button mTopRightButton;
     38     private Button mBottomRightButton;
     39     private Handler mHandler;
     40 
     41     public RequestFocusTest() {
     42         super("com.android.frameworks.coretests", RequestFocus.class);
     43     }
     44 
     45     @Override
     46     public void setUp() throws Exception {
     47         super.setUp();
     48 
     49         final RequestFocus a = getActivity();
     50         mHandler = a.getHandler();
     51         mTopLeftButton = (Button) a.findViewById(R.id.topLeftButton);
     52         mBottomLeftButton = (Button) a.findViewById(R.id.bottomLeftButton);
     53         mTopRightButton = (Button) a.findViewById(R.id.topRightButton);
     54         mBottomRightButton = (Button) a.findViewById(R.id.bottomRightButton);
     55     }
     56 
     57     // Test that setUp did what we expect it to do.  These asserts
     58     // can't go in SetUp, or the test will hang.
     59     @MediumTest
     60     public void testSetUpConditions() throws Exception {
     61         assertNotNull(mHandler);
     62         assertNotNull(mTopLeftButton);
     63         assertNotNull(mTopRightButton);
     64         assertNotNull(mBottomLeftButton);
     65         assertNotNull(mBottomRightButton);
     66         assertTrue("requestFocus() should work from onCreate.", mBottomRightButton.hasFocus());
     67     }
     68 
     69     // Test that a posted requestFocus works.
     70     @LargeTest
     71     public void testPostedRequestFocus() throws Exception {
     72         mHandler.post(new Runnable() { public void run() {
     73             mBottomLeftButton.requestFocus();
     74         }});
     75         synchronized(this) {
     76             try {
     77                 wait(500);
     78             } catch (InterruptedException e) {
     79                 // Don't care.
     80             }
     81         }
     82         assertTrue("Focus should move to bottom left", mBottomLeftButton.hasFocus());
     83     }
     84 
     85     // Test that a requestFocus from the wrong thread fails.
     86     @MediumTest
     87     public void testWrongThreadRequestFocusFails() throws Exception {
     88         try {
     89             mTopRightButton.requestFocus();
     90             fail("requestFocus from wrong thread should raise exception.");
     91         } catch (AndroidRuntimeException e) {
     92             // Expected.  The actual exception is not public, so we can't catch it.
     93             assertEquals("android.view.ViewAncestor$CalledFromWrongThreadException",
     94                          e.getClass().getName());
     95         }
     96     }
     97 }
     98