1 /* 2 * Copyright (C) 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 com.android.deskclock.timer; 18 19 import android.content.Context; 20 import android.content.Intent; 21 22 import androidx.test.InstrumentationRegistry; 23 import androidx.test.core.app.ApplicationProvider; 24 import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner; 25 import androidx.test.rule.ActivityTestRule; 26 27 import com.android.deskclock.data.DataModel; 28 import com.android.deskclock.data.Timer; 29 30 import org.junit.Rule; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 import static org.junit.Assert.assertSame; 35 36 @RunWith(AndroidJUnit4ClassRunner.class) 37 public class ExpiredTimersActivityTest { 38 39 @Rule 40 public ActivityTestRule<ExpiredTimersActivity> rule = 41 new ActivityTestRule<>(ExpiredTimersActivity.class, true, false); 42 43 @Test configurationChanges_DoNotResetFiringTimer()44 public void configurationChanges_DoNotResetFiringTimer() { 45 // Construct an ExpiredTimersActivity to display the firing timer. 46 final Context context = ApplicationProvider.getApplicationContext(); 47 final Intent intent = new Intent() 48 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION); 49 final ExpiredTimersActivity activity = rule.launchActivity(intent); 50 51 Runnable fireTimerRunnable = () -> { 52 // Create a firing timer. 53 final DataModel dm = DataModel.getDataModel(); 54 Timer timer = dm.addTimer(60000L, "", false); 55 dm.startTimer(timer); 56 dm.expireTimer(null, dm.getTimer(timer.getId())); 57 timer = dm.getTimer(timer.getId()); 58 59 // Make the ExpiredTimersActivity believe it has been displayed to the user. 60 activity.getWindow().getCallback().onWindowFocusChanged(true); 61 62 // Simulate a configuration change by recreating the activity. 63 activity.recreate(); 64 65 // Verify that the recreation did not alter the firing timer. 66 assertSame(timer, dm.getTimer(timer.getId())); 67 }; 68 InstrumentationRegistry.getInstrumentation().runOnMainSync(fireTimerRunnable); 69 } 70 } 71