Home | History | Annotate | Download | only in cts
      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 android.app.cts;
     18 
     19 import dalvik.annotation.TestLevel;
     20 import dalvik.annotation.TestTargetClass;
     21 import dalvik.annotation.TestTargetNew;
     22 import dalvik.annotation.TestTargets;
     23 import dalvik.annotation.ToBeFixed;
     24 
     25 import android.app.Activity;
     26 import android.app.Instrumentation;
     27 import android.app.TabActivity;
     28 import android.content.ComponentName;
     29 import android.content.Context;
     30 import android.content.Intent;
     31 import android.content.pm.ActivityInfo;
     32 import android.test.InstrumentationTestCase;
     33 import android.view.KeyEvent;
     34 import android.widget.TabHost;
     35 
     36 @TestTargetClass(TabActivity.class)
     37 public class TabActivityTest extends InstrumentationTestCase {
     38     private Instrumentation mInstrumentation;
     39     private MockTabActivity mActivity;
     40     private Activity mChildActivity;
     41 
     42     @Override
     43     protected void setUp() throws Exception {
     44         super.setUp();
     45         mInstrumentation = super.getInstrumentation();
     46     }
     47 
     48     @Override
     49     protected void tearDown() throws Exception {
     50         if (mActivity != null) {
     51             if (!mActivity.isFinishing()) {
     52                 mActivity.finish();
     53             } else if (mChildActivity != null) {
     54                 if (!mChildActivity.isFinishing()) {
     55                     mChildActivity.finish();
     56                 }
     57             }
     58         }
     59         super.tearDown();
     60     }
     61 
     62     @TestTargets({
     63         @TestTargetNew(
     64             level = TestLevel.COMPLETE,
     65             method = "TabActivity",
     66             args = {}
     67         ),
     68         @TestTargetNew(
     69             level = TestLevel.COMPLETE,
     70             method = "setDefaultTab",
     71             args = {java.lang.String.class}
     72         ),
     73         @TestTargetNew(
     74             level = TestLevel.COMPLETE,
     75             method = "setDefaultTab",
     76             args = {int.class}
     77         ),
     78         @TestTargetNew(
     79             level = TestLevel.COMPLETE,
     80             method = "onContentChanged",
     81             args = {}
     82         ),
     83         @TestTargetNew(
     84             level = TestLevel.COMPLETE,
     85             method = "getTabHost",
     86             args = {}
     87         ),
     88         @TestTargetNew(
     89             level = TestLevel.COMPLETE,
     90             method = "getTabWidget",
     91             args = {}
     92         ),
     93         @TestTargetNew(
     94             level = TestLevel.COMPLETE,
     95             method = "onPostCreate",
     96             args = {android.os.Bundle.class}
     97         ),
     98         @TestTargetNew(
     99             level = TestLevel.COMPLETE,
    100             method = "onRestoreInstanceState",
    101             args = {android.os.Bundle.class}
    102         ),
    103         @TestTargetNew(
    104             level = TestLevel.COMPLETE,
    105             method = "onSaveInstanceState",
    106             args = {android.os.Bundle.class}
    107         )
    108     })
    109     @ToBeFixed(bug = "1701364", explanation = "When testing TabActivity#setDefaultTab(int index),"
    110             + " setDefaultTab(String tag), we find that the set values are hard to get, there"
    111             + " is no proper method or other way to obtain these two default values.")
    112     public void testTabActivity() throws Throwable {
    113         // Test constructor
    114         new TabActivity();
    115 
    116         final String packageName = "com.android.cts.stub";
    117         final Intent intent = new Intent(Intent.ACTION_MAIN);
    118         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    119         intent.setClassName(packageName, MockTabActivity.class.getName());
    120         mActivity = (MockTabActivity) mInstrumentation.startActivitySync(intent);
    121         // Test onPostCreate, onContentChanged. These two methods are invoked in starting
    122         // activity. Default values of isOnContentChangedCalled, isOnPostCreateCalled are false.
    123         assertTrue(mActivity.isOnContentChangedCalled);
    124         assertTrue(mActivity.isOnPostCreateCalled);
    125 
    126         // Can't get default value.
    127         final int defaultIndex = 1;
    128         mActivity.setDefaultTab(defaultIndex);
    129         final String defaultTab = "DefaultTab";
    130         mActivity.setDefaultTab(defaultTab);
    131         // Test getTabHost, getTabWidget
    132         final TabHost tabHost = mActivity.getTabHost();
    133         assertNotNull(tabHost);
    134         assertNotNull(tabHost.getTabWidget());
    135 
    136         // Test onSaveInstanceState
    137         assertFalse(mActivity.isOnSaveInstanceStateCalled);
    138         final Intent embedded = new Intent(mInstrumentation.getTargetContext(),
    139                 ChildTabActivity.class);
    140         mActivity.startActivity(embedded);
    141         mInstrumentation.waitForIdleSync();
    142         assertTrue(mActivity.isOnSaveInstanceStateCalled);
    143 
    144         // Test onRestoreInstanceState
    145         sendKeys(KeyEvent.KEYCODE_BACK);
    146         mInstrumentation.waitForIdleSync();
    147         assertFalse(MockTabActivity.isOnRestoreInstanceStateCalled);
    148         OrientationTestUtils.toggleOrientationSync(mActivity, mInstrumentation);
    149         assertTrue(MockTabActivity.isOnRestoreInstanceStateCalled);
    150     }
    151 
    152     @TestTargetNew(
    153         level = TestLevel.COMPLETE,
    154         method = "onChildTitleChanged",
    155         args = {android.app.Activity.class, java.lang.CharSequence.class}
    156     )
    157     public void testChildTitleCallback() throws Exception {
    158         final Context context = mInstrumentation.getTargetContext();
    159         final Intent intent = new Intent(context, MockTabActivity.class);
    160         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    161 
    162         final MockTabActivity father = new MockTabActivity();
    163         final ComponentName componentName = new ComponentName(context, MockTabActivity.class);
    164         final ActivityInfo info = context.getPackageManager().getActivityInfo(componentName, 0);
    165         mChildActivity = mInstrumentation.newActivity(MockTabActivity.class, mInstrumentation
    166                 .getTargetContext(), null, null, intent, info, MockTabActivity.class.getName(),
    167                 father, null, null);
    168 
    169         assertNotNull(mChildActivity);
    170         final String newTitle = "New Title";
    171         mChildActivity.setTitle(newTitle);
    172         assertTrue(father.isOnChildTitleChangedCalled);
    173     }
    174 }
    175