• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.app.Activity;
20 import android.app.AlertDialog;
21 import android.app.DatePickerDialog;
22 import android.app.Dialog;
23 import android.app.TimePickerDialog;
24 import android.app.DatePickerDialog.OnDateSetListener;
25 import android.app.TimePickerDialog.OnTimeSetListener;
26 import android.content.Context;
27 import android.content.DialogInterface;
28 import android.content.DialogInterface.OnCancelListener;
29 import android.os.Bundle;
30 import android.os.Handler;
31 import android.os.Message;
32 import android.util.Log;
33 import android.view.KeyEvent;
34 import android.view.LayoutInflater;
35 import android.view.View;
36 import android.view.View.OnClickListener;
37 import android.widget.Button;
38 import android.widget.DatePicker;
39 import android.widget.TimePicker;
40 
41 import com.android.cts.stub.R;
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
onCreateDialog(int id)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 
getAlertDialogCancelablInstance(boolean cancelable)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")
getAlertDialogInstance(boolean deprecated)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 
getCustomAlertDialogInstance(boolean withSpacing)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 
getDialog()284     public Dialog getDialog() {
285         return mDialog;
286     }
287 
getDialogTitle()288     public String getDialogTitle() {
289         return (String) mDialog.getWindow().getAttributes().getTitle();
290     }
291 
292     @Override
onCreate(Bundle savedInstanceState)293     protected void onCreate(Bundle savedInstanceState) {
294         super.onCreate(savedInstanceState);
295 
296         setContentView(R.layout.dialog_stub_layout);
297 
298         findViewById(R.id.dialog_test_button_1).setOnClickListener(
299                 new MockClickListener(TEST_DIALOG_WITHOUT_THEME));
300         findViewById(R.id.dialog_test_button_2).setOnClickListener(
301                 new MockClickListener(TEST_DIALOG_WITH_THEME));
302         findViewById(R.id.dialog_test_button_3).setOnClickListener(
303                 new MockClickListener(TEST_ALERTDIALOG));
304         findViewById(R.id.dialog_test_button_4).setOnClickListener(
305                 new MockClickListener(TEST_CUSTOM_ALERTDIALOG));
306         final Button dialogTestButton5 = (Button) findViewById(R.id.dialog_test_button_5);
307         dialogTestButton5.setOnClickListener(new MockClickListener(TEST_DATEPICKERDIALOG));
308         findViewById(R.id.dialog_test_button_6).setOnClickListener(
309                 new MockClickListener(TEST_DATEPICKERDIALOG_WITH_THEME));
310         findViewById(R.id.dialog_test_button_7).setOnClickListener(
311                 new MockClickListener(TEST_TIMEPICKERDIALOG));
312         findViewById(R.id.dialog_test_button_8).setOnClickListener(
313                 new MockClickListener(TEST_TIMEPICKERDIALOG_WITH_THEME));
314         findViewById(R.id.dialog_test_button_9).setOnClickListener(
315                 new MockClickListener(TEST_ONSTART_AND_ONSTOP));
316         findViewById(R.id.dialog_test_button_10).setOnClickListener(
317                 new MockClickListener(TEST_ALERTDIALOG_DEPRECATED));
318         findViewById(R.id.dialog_test_button_11).setOnClickListener(
319                 new MockClickListener(TEST_ALERTDIALOG_CALLBACK));
320         findViewById(R.id.dialog_test_button_12).setOnClickListener(
321                 new MockClickListener(TEST_CUSTOM_ALERTDIALOG_VIEW));
322         findViewById(R.id.dialog_test_button_13).setOnClickListener(
323                 new MockClickListener(TEST_ALERTDIALOG_DEPRECATED_WITH_MESSAGE));
324 
325         findViewById(R.id.dialog_test_button_14).setOnClickListener(
326                 new MockClickListener(TEST_ALERTDIALOG_THEME));
327         findViewById(R.id.dialog_test_button_15).setOnClickListener(
328                 new MockClickListener(TEST_ALERTDIALOG_CANCELABLE));
329         findViewById(R.id.dialog_test_button_16).setOnClickListener(
330                 new MockClickListener(TEST_ALERTDIALOG_NOT_CANCELABLE));
331         findViewById(R.id.dialog_test_button_17).setOnClickListener(
332                 new MockClickListener(TEST_PROTECTED_CANCELABLE));
333         findViewById(R.id.dialog_test_button_18).setOnClickListener(
334                 new MockClickListener(TEST_PROTECTED_NOT_CANCELABLE));
335     }
336 
setUpTitle(final String title)337     public void setUpTitle(final String title) {
338         runOnUiThread(new Runnable() {
339             public void run() {
340                 getDialog().setTitle(title);
341             }
342         });
343     }
344 
setUpTitle(final int id)345     public void setUpTitle(final int id) {
346         runOnUiThread(new Runnable() {
347             public void run() {
348                 getDialog().setTitle(id);
349             }
350         });
351     }
352 
353     class MockAlertDialog extends AlertDialog {
MockAlertDialog(Context context)354         public MockAlertDialog(Context context) {
355             super(context);
356         }
357 
MockAlertDialog(Context context, int theme)358         public MockAlertDialog(Context context, int theme) {
359             super(context, theme);
360         }
361 
MockAlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener)362         public MockAlertDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
363             super(context, cancelable, cancelListener);
364         }
365 
366         @Override
onKeyDown(int keyCode, KeyEvent event)367         public boolean onKeyDown(int keyCode, KeyEvent event) {
368             onKeyDownCalled = true;
369             return super.onKeyDown(keyCode, event);
370         }
371 
372         @Override
onKeyUp(int keyCode, KeyEvent event)373         public boolean onKeyUp(int keyCode, KeyEvent event) {
374             onKeyUpCalled = true;
375             return super.onKeyUp(keyCode, event);
376         }
377 
378         @Override
onCreate(Bundle savedInstanceState)379         protected void onCreate(Bundle savedInstanceState) {
380             onCreateCalled = true;
381             super.onCreate(savedInstanceState);
382         }
383 
384     }
385 
386     private class MockClickListener implements OnClickListener {
387         private final int mId;
388 
MockClickListener(final int id)389         public MockClickListener(final int id) {
390             mId = id;
391         }
392 
onClick(View v)393         public void onClick(View v) {
394             showDialog(mId);
395         }
396     }
397 
398     class MockOnClickListener implements DialogInterface.OnClickListener {
399         private final int mId;
400 
MockOnClickListener(final int buttonId)401         public MockOnClickListener(final int buttonId) {
402             mId = buttonId;
403         }
404 
onClick(DialogInterface dialog, int which)405         public void onClick(DialogInterface dialog, int which) {
406             switch (mId) {
407                 case DialogInterface.BUTTON_POSITIVE:
408                     isPositiveButtonClicked = true;
409                     break;
410                 case DialogInterface.BUTTON_NEGATIVE:
411                     isNegativeButtonClicked = true;
412                     break;
413                 case DialogInterface.BUTTON_NEUTRAL:
414                     isNeutralButtonClicked = true;
415                     break;
416                 default:
417                     break;
418             }
419         }
420     }
421 
422     class MockDatePickerDialog extends DatePickerDialog {
MockDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)423         public MockDatePickerDialog(Context context, OnDateSetListener callBack, int year,
424                 int monthOfYear, int dayOfMonth) {
425             super(context, callBack, year, monthOfYear, dayOfMonth);
426         }
427 
MockDatePickerDialog(Context context, int theme, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)428         public MockDatePickerDialog(Context context, int theme, OnDateSetListener callBack,
429                 int year, int monthOfYear, int dayOfMonth) {
430             super(context, theme, callBack, year, monthOfYear, dayOfMonth);
431         }
432 
433         @Override
onClick(DialogInterface dialog, int which)434         public void onClick(DialogInterface dialog, int which) {
435             onClickCalled = true;
436             super.onClick(dialog, which);
437         }
438 
439         @Override
onDateChanged(DatePicker view, int year, int month, int day)440         public void onDateChanged(DatePicker view, int year, int month, int day) {
441             onDateChangedCalled = true;
442             super.onDateChanged(view, year, month, day);
443         }
444 
445         @Override
onRestoreInstanceState(Bundle savedInstanceState)446         public void onRestoreInstanceState(Bundle savedInstanceState) {
447             onRestoreInstanceStateCalled = true;
448             super.onRestoreInstanceState(savedInstanceState);
449         }
450 
451         @Override
onSaveInstanceState()452         public Bundle onSaveInstanceState() {
453             onSaveInstanceStateCalled = true;
454             return super.onSaveInstanceState();
455         }
456 
457     }
458 }
459