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 android.app.Instrumentation;
     20 import android.app.ProgressDialog;
     21 import android.app.stubs.MockActivity;
     22 import android.content.Context;
     23 import android.content.DialogInterface;
     24 import android.content.DialogInterface.OnCancelListener;
     25 import android.graphics.drawable.Drawable;
     26 import android.os.Bundle;
     27 import android.test.ActivityInstrumentationTestCase2;
     28 import android.test.UiThreadTest;
     29 import android.view.Window;
     30 import android.widget.ProgressBar;
     31 
     32 import android.app.stubs.R;
     33 
     34 /**
     35  * Test {@link ProgressDialog}.
     36  */
     37 public class ProgressDialogTest extends ActivityInstrumentationTestCase2<MockActivity> {
     38     private final CharSequence TITLE = "title";
     39     private final CharSequence MESSAGE = "message";
     40 
     41     private boolean mCanceled;
     42     private Drawable mDrawable;
     43     private Drawable mActualDrawableNull;
     44     private Drawable mActualDrawable;
     45     private ProgressBar mProgressBar;
     46     private int mProgress1;
     47     private int mProgress2;
     48 
     49     private Context mContext;
     50     private Instrumentation mInstrumentation;
     51     private MockActivity mActivity;
     52 
     53     public ProgressDialogTest() {
     54         super("android.app.stubs", MockActivity.class);
     55     }
     56 
     57     @Override
     58     protected void setUp() throws Exception {
     59         super.setUp();
     60 
     61         mCanceled = false;
     62         mInstrumentation = getInstrumentation();
     63         mActivity = getActivity();
     64         mContext = mActivity;
     65         mDrawable = getActivity().getResources().getDrawable(
     66                 R.drawable.yellow);
     67     }
     68 
     69     @UiThreadTest
     70     public void testProgressDialog1(){
     71         new ProgressDialog(mContext);
     72     }
     73 
     74     @UiThreadTest
     75     public void testProgressDialog2(){
     76         new ProgressDialog(mContext, R.style.Theme_AlertDialog);
     77     }
     78 
     79     @UiThreadTest
     80     public void testOnStartCreateStop() {
     81         MockProgressDialog pd = new MockProgressDialog(mContext);
     82 
     83         assertFalse(pd.mIsOnCreateCalled);
     84         assertFalse(pd.mIsOnStartCalled);
     85         pd.show();
     86         assertTrue(pd.mIsOnCreateCalled);
     87         assertTrue(pd.mIsOnStartCalled);
     88 
     89         assertFalse(pd.mIsOnStopCalled);
     90         pd.dismiss();
     91         assertTrue(pd.mIsOnStopCalled);
     92     }
     93 
     94     @UiThreadTest
     95     public void testShow1() {
     96         ProgressDialog.show(mContext, TITLE, MESSAGE);
     97     }
     98 
     99     @UiThreadTest
    100     public void testShow2() {
    101         ProgressDialog dialog = ProgressDialog.show(mContext, TITLE, MESSAGE, false);
    102 
    103         /*
    104          * note: the progress bar's style only supports indeterminate mode,
    105          * so can't change indeterminate
    106          */
    107         assertTrue(dialog.isIndeterminate());
    108 
    109         dialog = ProgressDialog.show(mContext, TITLE, MESSAGE, true);
    110         assertTrue(dialog.isIndeterminate());
    111     }
    112 
    113     public void testShow3() throws Throwable {
    114         final OnCancelListener cL = new OnCancelListener(){
    115             public void onCancel(DialogInterface dialog) {
    116                 mCanceled = true;
    117             }
    118         };
    119 
    120         // cancelable is false
    121         runTestOnUiThread(new Runnable() {
    122             public void run() {
    123                 ProgressDialog dialog = ProgressDialog.show(mContext, TITLE, MESSAGE, true, false);
    124 
    125                 dialog.setOnCancelListener(cL);
    126                 dialog.onBackPressed();
    127                 dialog.dismiss();
    128             }
    129         });
    130         mInstrumentation.waitForIdleSync();
    131 
    132         assertFalse(mCanceled);
    133 
    134         // cancelable is true
    135         runTestOnUiThread(new Runnable() {
    136             public void run() {
    137                 ProgressDialog dialog = ProgressDialog.show(mContext, TITLE, MESSAGE, true, true);
    138                 assertFalse(mCanceled);
    139                 dialog.setOnCancelListener(cL);
    140                 dialog.onBackPressed();
    141             }
    142         });
    143         mInstrumentation.waitForIdleSync();
    144 
    145         assertTrue(mCanceled);
    146     }
    147 
    148     public void testShow4() throws Throwable {
    149         final OnCancelListener cL = new OnCancelListener(){
    150             public void onCancel(DialogInterface dialog) {
    151                 mCanceled = true;
    152             }
    153         };
    154 
    155         // cancelable is false
    156         runTestOnUiThread(new Runnable() {
    157             public void run() {
    158                 ProgressDialog dialog = ProgressDialog.show(
    159                         mContext, TITLE, MESSAGE, true, false, cL);
    160 
    161                 dialog.onBackPressed();
    162                 dialog.dismiss();;
    163             }
    164         });
    165         mInstrumentation.waitForIdleSync();
    166 
    167         assertFalse(mCanceled);
    168 
    169         // cancelable is true
    170         runTestOnUiThread(new Runnable() {
    171             public void run() {
    172                 ProgressDialog dialog = ProgressDialog.show(
    173                         mContext, TITLE, MESSAGE, true, true, cL);
    174 
    175                 assertFalse(mCanceled);
    176                 dialog.onBackPressed();
    177             }
    178         });
    179         mInstrumentation.waitForIdleSync();
    180 
    181         assertTrue(mCanceled);
    182     }
    183 
    184     @UiThreadTest
    185     public void testAccessMax() {
    186         // progressDialog is null
    187         ProgressDialog progressDialog = buildDialog();
    188         progressDialog.setMax(2008);
    189         assertEquals(2008, progressDialog.getMax());
    190 
    191         // progressDialog is not null
    192         progressDialog = ProgressDialog.show(mContext, TITLE, MESSAGE);
    193         progressDialog.setMax(2009);
    194         assertEquals(2009, progressDialog.getMax());
    195     }
    196 
    197     @UiThreadTest
    198     public void testAccessProgress() {
    199         // progressDialog is null
    200         ProgressDialog progressDialog = buildDialog();
    201         progressDialog.setProgress(11);
    202         assertEquals(11, progressDialog.getProgress());
    203 
    204         /* progressDialog is not null
    205          * note: the progress bar's style only supports indeterminate mode,
    206          * so can't change progress
    207          */
    208         progressDialog = ProgressDialog.show(mContext, TITLE, MESSAGE);
    209         progressDialog.setProgress(12);
    210         assertEquals(0, progressDialog.getProgress());
    211     }
    212 
    213     @UiThreadTest
    214     public void testAccessSecondaryProgress() {
    215         // dialog is null
    216         ProgressDialog dialog = buildDialog();
    217         dialog.setSecondaryProgress(17);
    218         assertEquals(17, dialog.getSecondaryProgress());
    219 
    220         /* mProgress is not null
    221          * note: the progress bar's style only supports indeterminate mode,
    222          * so can't change secondary progress
    223          */
    224         dialog = ProgressDialog.show(mContext, TITLE, MESSAGE);
    225         dialog.setSecondaryProgress(18);
    226         assertEquals(0, dialog.getSecondaryProgress());
    227     }
    228 
    229     @UiThreadTest
    230     public void testSetIndeterminate() {
    231         // progress is null
    232         ProgressDialog dialog = buildDialog();
    233         dialog.setIndeterminate(true);
    234         assertTrue(dialog.isIndeterminate());
    235         dialog.setIndeterminate(false);
    236         assertFalse(dialog.isIndeterminate());
    237 
    238         /* mProgress is not null
    239          * note: the progress bar's style only supports indeterminate mode,
    240          * so can't change indeterminate
    241          */
    242         dialog = ProgressDialog.show(mContext, TITLE, MESSAGE);
    243         dialog.setIndeterminate(true);
    244         assertTrue(dialog.isIndeterminate());
    245         dialog.setIndeterminate(false);
    246         assertTrue(dialog.isIndeterminate());
    247     }
    248 
    249     @UiThreadTest
    250     public void testIncrementProgressBy() throws Throwable {
    251         ProgressDialog dialog = new ProgressDialog(mContext);
    252         dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    253         dialog.show();
    254         dialog.setProgress(10);
    255         mProgress1 = dialog.getProgress();
    256         dialog.incrementProgressBy(60);
    257         mProgress2 = dialog.getProgress();
    258         dialog.cancel();
    259 
    260         assertEquals(10, mProgress1);
    261         assertEquals(70, mProgress2);
    262     }
    263 
    264     @UiThreadTest
    265     public void testIncrementSecondaryProgressBy() throws Throwable {
    266         ProgressDialog dialog = new ProgressDialog(mContext);
    267         dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    268         dialog.show();
    269         dialog.setSecondaryProgress(10);
    270         mProgress1 = dialog.getSecondaryProgress();
    271         dialog.incrementSecondaryProgressBy(60);
    272         mProgress2 = dialog.getSecondaryProgress();
    273 
    274         assertEquals(10, mProgress1);
    275         assertEquals(70, mProgress2);
    276     }
    277 
    278     @UiThreadTest
    279     public void testSetProgressDrawable() throws Throwable {
    280         ProgressDialog dialog = ProgressDialog.show(mContext, TITLE, MESSAGE);
    281         Window w = dialog.getWindow();
    282         ProgressBar progressBar = (ProgressBar) w.findViewById(android.R.id.progress);
    283 
    284         dialog.setProgressDrawable(mDrawable);
    285         mActualDrawable = progressBar.getProgressDrawable();
    286 
    287         dialog.setProgressDrawable(null);
    288         mActualDrawableNull = progressBar.getProgressDrawable();
    289         assertEquals(mDrawable, mActualDrawable);
    290         assertEquals(null, mActualDrawableNull);
    291     }
    292 
    293     @UiThreadTest
    294     public void testSetIndeterminateDrawable() throws Throwable {
    295         ProgressDialog dialog = ProgressDialog.show(mContext, TITLE, MESSAGE);
    296         Window w = dialog.getWindow();
    297         mProgressBar = (ProgressBar) w.findViewById(android.R.id.progress);
    298 
    299         dialog.setIndeterminateDrawable(mDrawable);
    300         mActualDrawable = mProgressBar.getIndeterminateDrawable();
    301         assertEquals(mDrawable, mActualDrawable);
    302 
    303         dialog.setIndeterminateDrawable(null);
    304         mActualDrawableNull = mProgressBar.getIndeterminateDrawable();
    305         assertEquals(null, mActualDrawableNull);
    306     }
    307 
    308     @UiThreadTest
    309     public void testSetMessage() throws Throwable {
    310         ProgressDialog dialog = new ProgressDialog(mContext);
    311         dialog = new ProgressDialog(mContext);
    312         dialog.setMessage(MESSAGE);
    313         dialog.show();
    314         // dialog is not null
    315         dialog = ProgressDialog.show(mContext, TITLE, MESSAGE);
    316         dialog.setMessage("Chuck Norris");
    317     }
    318 
    319     @UiThreadTest
    320     public void testSetProgressStyle() throws Throwable {
    321         ProgressDialog dialog = new ProgressDialog(mContext);
    322         setProgressStyle(dialog, ProgressDialog.STYLE_HORIZONTAL);
    323         setProgressStyle(dialog, ProgressDialog.STYLE_SPINNER);
    324         setProgressStyle(dialog, 100);
    325     }
    326 
    327     private void setProgressStyle(ProgressDialog dialog, int style) {
    328         dialog.setProgressStyle(style);
    329         dialog.show();
    330         dialog.setProgress(10);
    331         dialog.setMax(100);
    332     }
    333 
    334     private static class MockProgressDialog extends ProgressDialog {
    335         public boolean mIsOnStopCalled;
    336         public boolean mIsOnStartCalled;
    337         public boolean mIsOnCreateCalled;
    338 
    339         public MockProgressDialog(Context context) {
    340             super(context);
    341         }
    342 
    343         @Override
    344         protected void onCreate(Bundle savedInstanceState) {
    345             super.onCreate(savedInstanceState);
    346             mIsOnCreateCalled = true;
    347         }
    348 
    349         @Override
    350         public void onStart(){
    351             super.onStart();
    352             mIsOnStartCalled = true;
    353         }
    354 
    355         @Override
    356         public void onStop() {
    357             super.onStop();
    358             mIsOnStopCalled = true;
    359         }
    360     }
    361 
    362     private ProgressDialog buildDialog() {
    363         return new ProgressDialog(mContext);
    364     }
    365 }
    366