1 /* 2 * Copyright 2020 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 androidx.wear.activity; 18 19 import static org.junit.Assert.assertFalse; 20 import static org.junit.Assert.assertTrue; 21 22 import android.widget.Button; 23 24 import androidx.test.ext.junit.runners.AndroidJUnit4; 25 import androidx.test.filters.LargeTest; 26 import androidx.test.platform.app.InstrumentationRegistry; 27 import androidx.test.rule.ActivityTestRule; 28 import androidx.wear.test.R; 29 import androidx.wear.widget.util.WakeLockRule; 30 31 import org.jspecify.annotations.Nullable; 32 import org.junit.Ignore; 33 import org.junit.Rule; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 @LargeTest 38 @RunWith(AndroidJUnit4.class) 39 public class ConfirmationActivityTest { 40 41 // Number of milliseconds before the end of the duration period that we should check that the 42 // ConfirmationActivity still has focus 43 private static final int MILLIS_BEFORE_END_OF_DURATION = 450; 44 45 // Number of milliseconds after the end of the duration period that we should wait before the 46 // ConfirmationActivity still lost focus 47 private static final int MILLIS_AFTER_END_OF_DURATION = 500; 48 49 // Number of milliseconds to wait for the confirmation activity to display initially 50 private static final int MILLIS_TO_WAIT_FOR_ACTIVITY_TO_BE_DRAWN = 500; 51 52 @Rule 53 public final WakeLockRule wakeLock = new WakeLockRule(); 54 55 @Rule 56 public final ActivityTestRule<ConfirmationActivityTestActivity> mActivityRule = 57 new ActivityTestRule<>(ConfirmationActivityTestActivity.class, true, true); 58 59 @Test 60 @Ignore("b/272346886") testConfirmationDialogShownForDefaultDuration()61 public void testConfirmationDialogShownForDefaultDuration() throws Throwable { 62 int testDuration = ConfirmationActivity.DEFAULT_ANIMATION_DURATION_MILLIS; 63 // Check that the structure of the test is still valid 64 assertTrue(testDuration 65 > (MILLIS_BEFORE_END_OF_DURATION + MILLIS_TO_WAIT_FOR_ACTIVITY_TO_BE_DRAWN)); 66 testConfirmationDialogShownForConfiguredDuration( 67 ConfirmationActivity.DEFAULT_ANIMATION_DURATION_MILLIS, "A message"); 68 } 69 70 @Test 71 @Ignore("b/272346886") testConfirmationDialogShownForLongerDuration()72 public void testConfirmationDialogShownForLongerDuration() throws Throwable { 73 testConfirmationDialogShownForConfiguredDuration( 74 ConfirmationActivity.DEFAULT_ANIMATION_DURATION_MILLIS * 2, "A message"); 75 } 76 77 @Test 78 @Ignore("b/272346886") testConfirmationDialogWithMissingMessage()79 public void testConfirmationDialogWithMissingMessage() throws Throwable { 80 testConfirmationDialogShownForConfiguredDuration( 81 ConfirmationActivity.DEFAULT_ANIMATION_DURATION_MILLIS * 2, /* message= */null); 82 } 83 testConfirmationDialogShownForConfiguredDuration(int duration, @Nullable String message)84 private void testConfirmationDialogShownForConfiguredDuration(int duration, 85 @Nullable String message) throws Throwable { 86 // Wait for the test activity to be visible 87 InstrumentationRegistry.getInstrumentation().waitForIdleSync(); 88 Thread.sleep(MILLIS_TO_WAIT_FOR_ACTIVITY_TO_BE_DRAWN); 89 90 // Check that the test activity currently hasWindowFocus() 91 assertTrue(mActivityRule.getActivity().hasWindowFocus()); 92 Button button = 93 mActivityRule.getActivity().findViewById(R.id.show_confirmation_activity_button); 94 95 // GIVEN a display duration in milliseconds, and message 96 mActivityRule.getActivity().setDuration(duration); 97 mActivityRule.getActivity().setMessage(message); 98 // WHEN we click on the button 99 mActivityRule.runOnUiThread(button::performClick); 100 // THEN wait for the activity to be drawn 101 Thread.sleep(MILLIS_TO_WAIT_FOR_ACTIVITY_TO_BE_DRAWN); 102 // AND lose window focus to the confirmation activity on top 103 assertFalse(mActivityRule.getActivity().hasWindowFocus()); 104 // AND wait until a short while before the confirmation activity is due to expire 105 Thread.sleep(duration - MILLIS_BEFORE_END_OF_DURATION 106 - MILLIS_TO_WAIT_FOR_ACTIVITY_TO_BE_DRAWN); 107 // AND confirm that the confirmation activity still has focus 108 assertFalse(mActivityRule.getActivity().hasWindowFocus()); 109 // AND wait for until the confirmation activity should be gone 110 Thread.sleep(MILLIS_AFTER_END_OF_DURATION + MILLIS_BEFORE_END_OF_DURATION 111 + MILLIS_TO_WAIT_FOR_ACTIVITY_TO_BE_DRAWN); 112 // AND confirm that the test activity has focus again 113 assertTrue(mActivityRule.getActivity().hasWindowFocus()); 114 } 115 116 } 117