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 static org.junit.Assert.fail;
20 
21 import android.view.WindowManager;
22 
23 import androidx.test.runner.AndroidJUnit4;
24 
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 
28 /**
29  * Test whether toasts are properly shown. For apps targeting API 25+
30  * like this app the only way to add toast windows is via the dedicated
31  * toast APIs.
32  */
33 @RunWith(AndroidJUnit4.class)
34 public class LegacyToastTest extends BaseToastTest {
35     @Test
testAddSingleToastViaTestApisWhenUidFocused()36     public void testAddSingleToastViaTestApisWhenUidFocused() throws Exception {
37         // Normal toast windows cannot be obtained vie the accessibility APIs because
38         // they are not touchable. In this case not crashing is good enough.
39         showToastsViaToastApis(1);
40     }
41 
42     @Test
testAddTwoToastViaTestApisWhenUidFocused()43     public void testAddTwoToastViaTestApisWhenUidFocused() throws Exception {
44         // Normal toast windows cannot be obtained vie the accessibility APIs because
45         // they are not touchable. In this case not crashing is good enough.
46         showToastsViaToastApis(2);
47 
48         // Wait for the first one to expire
49         waitForToastTimeout();
50     }
51 
52     @Test
testAddSingleToastViaAddingWindowApisWhenUidFocused()53     public void testAddSingleToastViaAddingWindowApisWhenUidFocused() throws Exception {
54         try {
55             showToastsViaAddingWindow(1, false);
56             fail("Shouldn't be able to add toast windows directly");
57         } catch (WindowManager.BadTokenException e) {
58             /* expected */
59         }
60     }
61 }
62