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 com.android.cts.stub.R;
     20 
     21 import android.app.Activity;
     22 import android.app.AlertDialog;
     23 import android.app.DatePickerDialog;
     24 import android.app.Dialog;
     25 import android.app.TimePickerDialog;
     26 import android.app.DatePickerDialog.OnDateSetListener;
     27 import android.app.TimePickerDialog.OnTimeSetListener;
     28 import android.content.Context;
     29 import android.content.DialogInterface;
     30 import android.content.Intent;
     31 import android.content.DialogInterface.OnCancelListener;
     32 import android.os.Bundle;
     33 import android.os.Handler;
     34 import android.os.Message;
     35 import android.test.ActivityInstrumentationTestCase2;
     36 import android.util.Log;
     37 import android.view.KeyEvent;
     38 import android.view.LayoutInflater;
     39 import android.view.View;
     40 import android.widget.DatePicker;
     41 import android.widget.TimePicker;
     42 
     43 /*
     44  * Stub class for  Dialog, AlertDialog, DatePickerDialog, TimePickerDialog etc.
     45  */
     46 public class DialogStubActivity extends Activity {
     47     public static final int TEST_DIALOG_WITHOUT_THEME = 0;
     48     public static final int TEST_DIALOG_WITH_THEME = 1;
     49     public static final int TEST_ALERTDIALOG = 2;
     50     public static final int TEST_CUSTOM_ALERTDIALOG = 3;
     51     public static final int TEST_DATEPICKERDIALOG = 4;
     52     public static final int TEST_DATEPICKERDIALOG_WITH_THEME = 5;
     53     public static final int TEST_TIMEPICKERDIALOG = 6;
     54     public static final int TEST_TIMEPICKERDIALOG_WITH_THEME = 7;
     55     public static final int TEST_ONSTART_AND_ONSTOP = 8;
     56     public static final int TEST_ALERTDIALOG_DEPRECATED = 9;
     57     public static final int TEST_ALERTDIALOG_CALLBACK = 10;
     58     public static final int TEST_CUSTOM_ALERTDIALOG_VIEW = 11;
     59     public static final int TEST_ALERTDIALOG_DEPRECATED_WITH_MESSAGE = 12;
     60     public static final int TEST_ALERTDIALOG_THEME = 13;
     61     public static final int TEST_ALERTDIALOG_CANCELABLE = 14;
     62     public static final int TEST_ALERTDIALOG_NOT_CANCELABLE = 15;
     63     public static final int TEST_PROTECTED_CANCELABLE = 16;
     64     public static final int TEST_PROTECTED_NOT_CANCELABLE = 17;
     65 
     66     public static final int SPACING_LEFT = 10;
     67     public static final int SPACING_TOP = 20;
     68     public static final int SPACING_RIGHT = 30;
     69     public static final int SPACING_BOTTOM = 40;
     70     public static int buttonIndex;
     71 
     72     public static final String DEFAULT_ALERTDIALOG_TITLE = "AlertDialog";
     73     public static final String DEFAULT_ALERTDIALOG_MESSAGE = "AlertDialog message";
     74     private static final String LOG_TAG = "DialogStubActivity";
     75 
     76     public boolean isPositiveButtonClicked = false;
     77     public boolean isNegativeButtonClicked = false;
     78     public boolean isNeutralButtonClicked = false;
     79     public boolean isCallBackCalled;
     80     public boolean onCancelCalled;
     81     public boolean onKeyDownCalled;
     82     public boolean onKeyUpCalled;
     83     public boolean onCreateCalled;
     84     public boolean onCancelListenerCalled;
     85     public boolean onClickCalled;
     86     public static boolean onDateChangedCalled;
     87     public static boolean onRestoreInstanceStateCalled;
     88     public boolean onSaveInstanceStateCalled;
     89     public int updatedYear;
     90     public int updatedMonth;
     91     public int updatedDay;
     92 
     93     public final int INITIAL_YEAR = 2008;
     94     public final int INITIAL_MONTH = 7;
     95     public final int INITIAL_DAY_OF_MONTH = 27;
     96     private final int INITIAL_HOUR = 10;
     97     private final int INITIAL_MINUTE = 35;
     98     private Dialog mDialog;
     99     private AlertDialog mAlertDialog;
    100     private OnDateSetListener mOnDateSetListener = new OnDateSetListener() {
    101         public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    102             updatedYear = year;
    103             updatedMonth = monthOfYear;
    104             updatedDay = dayOfMonth;
    105         }
    106     };
    107 
    108     @SuppressWarnings("deprecation")
    109     @Override
    110     protected Dialog onCreateDialog(int id) {
    111         switch (id) {
    112             case TEST_DIALOG_WITHOUT_THEME:
    113                 mDialog = new Dialog(this);
    114                 mDialog.setTitle("Hello, Dialog");
    115                 break;
    116 
    117             case TEST_DIALOG_WITH_THEME:
    118                 mDialog = new Dialog(this, 1);
    119                 break;
    120 
    121             case TEST_ALERTDIALOG:
    122                 mDialog = getAlertDialogInstance(false);
    123                 break;
    124 
    125             case TEST_CUSTOM_ALERTDIALOG:
    126                 mDialog = getCustomAlertDialogInstance(false);
    127                 break;
    128 
    129             case TEST_CUSTOM_ALERTDIALOG_VIEW:
    130                 mDialog = getCustomAlertDialogInstance(true);
    131                 break;
    132 
    133             case TEST_DATEPICKERDIALOG:
    134                 mDialog = new MockDatePickerDialog(this, mOnDateSetListener, INITIAL_YEAR,
    135                         INITIAL_MONTH, INITIAL_DAY_OF_MONTH);
    136                 break;
    137 
    138             case TEST_DATEPICKERDIALOG_WITH_THEME:
    139                 mDialog = new MockDatePickerDialog(this,
    140                         com.android.internal.R.style.Theme_Translucent, mOnDateSetListener,
    141                         INITIAL_YEAR, INITIAL_MONTH, INITIAL_DAY_OF_MONTH);
    142                 break;
    143 
    144             case TEST_TIMEPICKERDIALOG:
    145                 mDialog = new TimePickerDialog(this, new OnTimeSetListener() {
    146                     public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    147                         isCallBackCalled = true;
    148                     }
    149                 }, INITIAL_HOUR, INITIAL_MINUTE, true);
    150                 break;
    151 
    152             case TEST_TIMEPICKERDIALOG_WITH_THEME:
    153                 mDialog = new TimePickerDialog(this,
    154                         com.android.internal.R.style.Theme_Translucent, null, INITIAL_HOUR,
    155                         INITIAL_MINUTE, true);
    156                 break;
    157 
    158             case TEST_ONSTART_AND_ONSTOP:
    159                 mDialog = new TestDialog(this);
    160                 Log.i(LOG_TAG, "mTestDialog:" + mDialog);
    161                 return mDialog;
    162 
    163             case TEST_ALERTDIALOG_DEPRECATED:
    164                 mDialog = getAlertDialogInstance(true);
    165                 break;
    166 
    167             case TEST_ALERTDIALOG_DEPRECATED_WITH_MESSAGE:
    168                 final Handler handler = new Handler() {
    169                     @Override
    170                     public void handleMessage(Message msg) {
    171                         buttonIndex = msg.what;
    172                         super.handleMessage(msg);
    173                     }
    174                 };
    175                 final Message positiveMessage = Message.obtain();
    176                 positiveMessage.setTarget(handler);
    177                 positiveMessage.what = DialogInterface.BUTTON_POSITIVE;
    178 
    179                 final Message negativeMessage = Message.obtain();
    180                 negativeMessage.setTarget(handler);
    181                 negativeMessage.what = DialogInterface.BUTTON_NEGATIVE;
    182 
    183                 final Message neutralMessage = Message.obtain();
    184                 neutralMessage.setTarget(handler);
    185                 neutralMessage.what = DialogInterface.BUTTON_NEUTRAL;
    186                 mAlertDialog = getAlertDialogInstance(false);
    187                 mAlertDialog.setButton(getString(R.string.alert_dialog_positive), positiveMessage);
    188                 mAlertDialog.setButton2(getString(R.string.alert_dialog_negative), negativeMessage);
    189                 mAlertDialog.setButton3(getString(R.string.alert_dialog_neutral), neutralMessage);
    190                 mDialog = mAlertDialog;
    191                 break;
    192 
    193             case TEST_ALERTDIALOG_CALLBACK:
    194                 mDialog = new MockAlertDialog(this);
    195                 break;
    196             case TEST_ALERTDIALOG_THEME:
    197                 mDialog = new MockAlertDialog(this, R.style.Theme_AlertDialog);
    198                 break;
    199             case TEST_ALERTDIALOG_CANCELABLE:
    200                 mDialog = getAlertDialogCancelablInstance(true);
    201                 break;
    202             case TEST_ALERTDIALOG_NOT_CANCELABLE:
    203                 mDialog = getAlertDialogCancelablInstance(false);
    204                 break;
    205             case TEST_PROTECTED_CANCELABLE:
    206                 mDialog = new TestDialog(this, true, new OnCancelListener() {
    207                     public void onCancel(DialogInterface dialog) {
    208                         onCancelListenerCalled = true;
    209                     }
    210                 });
    211                 break;
    212             case TEST_PROTECTED_NOT_CANCELABLE:
    213                 mDialog = new TestDialog(this, false, new OnCancelListener() {
    214                     public void onCancel(DialogInterface dialog) {
    215                         onCancelListenerCalled = true;
    216                     }
    217                 });
    218                 break;
    219             default:
    220                 break;
    221         }
    222 
    223         Log.i(LOG_TAG, "mDialog:" + mDialog);
    224         return mDialog;
    225     }
    226 
    227     private AlertDialog getAlertDialogCancelablInstance(boolean cancelable) {
    228         OnCancelListener cancelListener = new OnCancelListener() {
    229             public void onCancel(DialogInterface dialog) {
    230                 onCancelCalled = true;
    231             }
    232         };
    233         return new MockAlertDialog(this, cancelable, cancelListener);
    234     }
    235 
    236     @SuppressWarnings("deprecation")
    237     private AlertDialog getAlertDialogInstance(boolean deprecated) {
    238         mAlertDialog = new AlertDialog.Builder(DialogStubActivity.this).create();
    239         mAlertDialog.setIcon(com.android.cts.stub.R.drawable.pass);
    240         mAlertDialog.setTitle(DEFAULT_ALERTDIALOG_TITLE);
    241         mAlertDialog.setMessage(DEFAULT_ALERTDIALOG_MESSAGE);
    242         mAlertDialog.setInverseBackgroundForced(true);
    243         final DialogInterface.OnClickListener positiveListener = new MockOnClickListener(
    244                 DialogInterface.BUTTON_POSITIVE);
    245         final DialogInterface.OnClickListener netativeListener = new MockOnClickListener(
    246                 DialogInterface.BUTTON_NEGATIVE);
    247         final DialogInterface.OnClickListener neutralListener = new MockOnClickListener(
    248                 DialogInterface.BUTTON_NEUTRAL);
    249 
    250         if (deprecated) {
    251             mAlertDialog.setButton(getString(R.string.alert_dialog_positive), positiveListener);
    252             mAlertDialog.setButton2(getString(R.string.alert_dialog_negative), netativeListener);
    253             mAlertDialog.setButton3(getString(R.string.alert_dialog_neutral), neutralListener);
    254         } else {
    255             mAlertDialog.setButton(DialogInterface.BUTTON_POSITIVE,
    256                     getString(R.string.alert_dialog_positive), positiveListener);
    257             mAlertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
    258                     getString(R.string.alert_dialog_negative), netativeListener);
    259             mAlertDialog.setButton(DialogInterface.BUTTON_NEUTRAL,
    260                     getString(R.string.alert_dialog_neutral), neutralListener);
    261         }
    262         return mAlertDialog;
    263 
    264     }
    265 
    266     private AlertDialog getCustomAlertDialogInstance(boolean withSpacing) {
    267         final LayoutInflater inflate = getLayoutInflater();
    268         final View customTitleViewCustom = inflate.inflate(R.layout.alertdialog_custom_title, null);
    269         final View textEntryView = inflate.inflate(R.layout.alert_dialog_text_entry_2, null);
    270         mAlertDialog = new AlertDialog.Builder(DialogStubActivity.this).create();
    271         mAlertDialog.setCustomTitle(customTitleViewCustom);
    272         mAlertDialog.setMessage(DEFAULT_ALERTDIALOG_MESSAGE);
    273         if (withSpacing) {
    274             mAlertDialog.setView(textEntryView, SPACING_LEFT, SPACING_TOP, SPACING_RIGHT,
    275                     SPACING_BOTTOM);
    276         } else {
    277             mAlertDialog.setView(textEntryView);
    278         }
    279 
    280         return mAlertDialog;
    281 
    282     }
    283 
    284     public Dialog getDialog() {
    285         return mDialog;
    286     }
    287 
    288     public String getDialogTitle() {
    289         return (String) mDialog.getWindow().getAttributes().getTitle();
    290     }
    291 
    292     private static final String TEST_DIALOG_NUMBER_EXTRA = "testDialogNumber";
    293 
    294     public static <T extends Activity> T startDialogActivity(
    295             ActivityInstrumentationTestCase2<T> testCase, int dialogNumber) {
    296         Intent intent = new Intent(Intent.ACTION_MAIN);
    297         intent.putExtra(TEST_DIALOG_NUMBER_EXTRA, dialogNumber);
    298         testCase.setActivityIntent(intent);
    299         return testCase.getActivity();
    300     }
    301 
    302     @Override
    303     protected void onCreate(Bundle savedInstanceState) {
    304         super.onCreate(savedInstanceState);
    305 
    306         setContentView(R.layout.dialog_stub_layout);
    307 
    308         Intent intent = getIntent();
    309         int dialogNum = intent.getIntExtra(TEST_DIALOG_NUMBER_EXTRA, -1);
    310         if (dialogNum != -1) {
    311             showDialog(dialogNum);
    312         }
    313     }
    314 
    315     public void setUpTitle(final String title) {
    316         runOnUiThread(new Runnable() {
    317             public void run() {
    318                 getDialog().setTitle(title);
    319             }
    320         });
    321     }
    322 
    323     public void setUpTitle(final int id) {
    324         runOnUiThread(new Runnable() {
    325             public void run() {
    326                 getDialog().setTitle(id);
    327             }
    328         });
    329     }
    330 
    331     class MockAlertDialog extends AlertDialog {
    332         public MockAlertDialog(Context context) {
    333             super(context);
    334         }
    335 
    336         public MockAlertDialog(Context context, int theme) {
    337             super(context, theme);
    338         }
    339 
    340         public MockAlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
    341             super(context, cancelable, cancelListener);
    342         }
    343 
    344         @Override
    345         public boolean onKeyDown(int keyCode, KeyEvent event) {
    346             onKeyDownCalled = true;
    347             return super.onKeyDown(keyCode, event);
    348         }
    349 
    350         @Override
    351         public boolean onKeyUp(int keyCode, KeyEvent event) {
    352             onKeyUpCalled = true;
    353             return super.onKeyUp(keyCode, event);
    354         }
    355 
    356         @Override
    357         protected void onCreate(Bundle savedInstanceState) {
    358             onCreateCalled = true;
    359             super.onCreate(savedInstanceState);
    360         }
    361 
    362     }
    363 
    364     class MockOnClickListener implements DialogInterface.OnClickListener {
    365         private final int mId;
    366 
    367         public MockOnClickListener(final int buttonId) {
    368             mId = buttonId;
    369         }
    370 
    371         public void onClick(DialogInterface dialog, int which) {
    372             switch (mId) {
    373                 case DialogInterface.BUTTON_POSITIVE:
    374                     isPositiveButtonClicked = true;
    375                     break;
    376                 case DialogInterface.BUTTON_NEGATIVE:
    377                     isNegativeButtonClicked = true;
    378                     break;
    379                 case DialogInterface.BUTTON_NEUTRAL:
    380                     isNeutralButtonClicked = true;
    381                     break;
    382                 default:
    383                     break;
    384             }
    385         }
    386     }
    387 
    388     class MockDatePickerDialog extends DatePickerDialog {
    389         public MockDatePickerDialog(Context context, OnDateSetListener callBack, int year,
    390                 int monthOfYear, int dayOfMonth) {
    391             super(context, callBack, year, monthOfYear, dayOfMonth);
    392         }
    393 
    394         public MockDatePickerDialog(Context context, int theme, OnDateSetListener callBack,
    395                 int year, int monthOfYear, int dayOfMonth) {
    396             super(context, theme, callBack, year, monthOfYear, dayOfMonth);
    397         }
    398 
    399         @Override
    400         public void onClick(DialogInterface dialog, int which) {
    401             onClickCalled = true;
    402             super.onClick(dialog, which);
    403         }
    404 
    405         @Override
    406         public void onDateChanged(DatePicker view, int year, int month, int day) {
    407             onDateChangedCalled = true;
    408             super.onDateChanged(view, year, month, day);
    409         }
    410 
    411         @Override
    412         public void onRestoreInstanceState(Bundle savedInstanceState) {
    413             onRestoreInstanceStateCalled = true;
    414             super.onRestoreInstanceState(savedInstanceState);
    415         }
    416 
    417         @Override
    418         public Bundle onSaveInstanceState() {
    419             onSaveInstanceStateCalled = true;
    420             return super.onSaveInstanceState();
    421         }
    422 
    423     }
    424 }
    425