Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2016 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.toast.cts;
     18 
     19 import android.app.Instrumentation;
     20 import android.app.UiAutomation;
     21 import android.content.Context;
     22 import android.graphics.PixelFormat;
     23 import android.os.SystemClock;
     24 import android.support.test.InstrumentationRegistry;
     25 import android.view.WindowManager;
     26 import android.widget.TextView;
     27 import android.widget.Toast;
     28 import org.junit.Before;
     29 
     30 /**
     31  * Base class for toast tests.
     32  */
     33 public abstract class BaseToastTest {
     34     protected static final long TOAST_TIMEOUT_MILLIS = 5000; // 5 sec
     35     protected static final long EVENT_TIMEOUT_MILLIS = 5000; // 5 sec
     36 
     37     protected Context mContext;
     38     protected Instrumentation mInstrumentation;
     39     protected UiAutomation mUiAutomation;
     40 
     41     @Before
     42     public void setUp() {
     43         mContext = InstrumentationRegistry.getContext();
     44         mInstrumentation = InstrumentationRegistry.getInstrumentation();
     45         mUiAutomation = mInstrumentation.getUiAutomation();
     46         waitForToastTimeout();
     47     }
     48 
     49     protected void waitForToastTimeout() {
     50         SystemClock.sleep(TOAST_TIMEOUT_MILLIS);
     51     }
     52 
     53     protected void showToastsViaToastApis(int count) throws Exception {
     54         Exception[] exceptions = new Exception[1];
     55         mInstrumentation.runOnMainSync(
     56                 () -> {
     57                     try {
     58                         for (int i = 0; i < count; i++) {
     59                             Toast.makeText(mContext, getClass().getName(),
     60                                     Toast.LENGTH_LONG).show();
     61                         }
     62                     } catch (Exception e) {
     63                         exceptions[0] = e;
     64                     }
     65                 });
     66         if (exceptions[0] != null) {
     67             throw exceptions[0];
     68         }
     69     }
     70 
     71     protected void showToastsViaAddingWindow(int count, boolean focusable) throws Exception {
     72         Exception[] exceptions = new Exception[1];
     73         mInstrumentation.runOnMainSync(() -> {
     74             try {
     75                 for (int i = 0; i < count; i++) {
     76                     WindowManager.LayoutParams params = new WindowManager.LayoutParams();
     77                     params.height = WindowManager.LayoutParams.WRAP_CONTENT;
     78                     params.width = WindowManager.LayoutParams.WRAP_CONTENT;
     79                     params.format = PixelFormat.TRANSLUCENT;
     80                     params.type = WindowManager.LayoutParams.TYPE_TOAST;
     81                     params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
     82                     if (!focusable) {
     83                         params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
     84                     }
     85 
     86                     TextView textView = new TextView(mContext);
     87                     textView.setText(BaseToastTest.class.getName());
     88 
     89                     WindowManager windowManager = mContext
     90                             .getSystemService(WindowManager.class);
     91                     windowManager.addView(textView, params);
     92                 }
     93             } catch (Exception e) {
     94                 exceptions[0] = e;
     95             }
     96         });
     97         if (exceptions[0] != null) {
     98             throw exceptions[0];
     99         }
    100     }
    101 }
    102