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