Merge "Use FakeTimer in test to avoid flakiness."
This commit is contained in:
committed by
Android (Google) Code Review
commit
1d070021c0
@@ -119,7 +119,7 @@ public class ScreenFlashNotificationColorDialogFragment extends DialogFragment i
|
|||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
if (mTimer != null) mTimer.cancel();
|
if (mTimer != null) mTimer.cancel();
|
||||||
|
|
||||||
mTimer = new Timer();
|
mTimer = createTimer();
|
||||||
if (mIsPreview) {
|
if (mIsPreview) {
|
||||||
mTimer.schedule(getStopTask(), 0);
|
mTimer.schedule(getStopTask(), 0);
|
||||||
startDelay = BETWEEN_STOP_AND_START_DELAY_MS;
|
startDelay = BETWEEN_STOP_AND_START_DELAY_MS;
|
||||||
@@ -176,4 +176,8 @@ public class ScreenFlashNotificationColorDialogFragment extends DialogFragment i
|
|||||||
getContext().sendBroadcast(stopIntent);
|
getContext().sendBroadcast(stopIntent);
|
||||||
mIsPreview = false;
|
mIsPreview = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Timer createTimer() {
|
||||||
|
return new Timer();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -42,6 +42,7 @@ import androidx.appcompat.app.AlertDialog;
|
|||||||
import androidx.fragment.app.FragmentActivity;
|
import androidx.fragment.app.FragmentActivity;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.testutils.FakeTimer;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -49,9 +50,12 @@ import org.junit.runner.RunWith;
|
|||||||
import org.robolectric.Robolectric;
|
import org.robolectric.Robolectric;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
import org.robolectric.shadows.ShadowContextWrapper;
|
import org.robolectric.shadows.ShadowContextWrapper;
|
||||||
|
import org.robolectric.util.ReflectionHelpers;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
@RunWith(RobolectricTestRunner.class)
|
@RunWith(RobolectricTestRunner.class)
|
||||||
public class ScreenFlashNotificationColorDialogFragmentTest {
|
public class ScreenFlashNotificationColorDialogFragmentTest {
|
||||||
@@ -68,9 +72,8 @@ public class ScreenFlashNotificationColorDialogFragmentTest {
|
|||||||
mShadowContextWrapper = shadowOf(fragmentActivity);
|
mShadowContextWrapper = shadowOf(fragmentActivity);
|
||||||
|
|
||||||
mCurrentColor = ROSE.mColorInt;
|
mCurrentColor = ROSE.mColorInt;
|
||||||
mDialogFragment = ScreenFlashNotificationColorDialogFragment.getInstance(
|
mDialogFragment = createFragment();
|
||||||
mCurrentColor, selectedColor -> mCurrentColor = selectedColor
|
|
||||||
);
|
|
||||||
mDialogFragment.show(fragmentActivity.getSupportFragmentManager(), "test");
|
mDialogFragment.show(fragmentActivity.getSupportFragmentManager(), "test");
|
||||||
|
|
||||||
mAlertDialog = (AlertDialog) mDialogFragment.getDialog();
|
mAlertDialog = (AlertDialog) mDialogFragment.getDialog();
|
||||||
@@ -91,16 +94,19 @@ public class ScreenFlashNotificationColorDialogFragmentTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void clickNeutral_assertStartPreview() throws InterruptedException {
|
public void clickNeutral_assertStartPreview() {
|
||||||
performClickOnDialog(BUTTON_NEUTRAL);
|
performClickOnDialog(BUTTON_NEUTRAL);
|
||||||
Thread.sleep(100);
|
getTimerFromFragment().runOneTask();
|
||||||
|
|
||||||
Intent captured = getLastCapturedIntent();
|
assertStartPreview(ROSE.mColorInt);
|
||||||
assertThat(captured.getAction()).isEqualTo(ACTION_FLASH_NOTIFICATION_START_PREVIEW);
|
}
|
||||||
assertThat(captured.getIntExtra(EXTRA_FLASH_NOTIFICATION_PREVIEW_TYPE, TYPE_SHORT_PREVIEW))
|
|
||||||
.isEqualTo(TYPE_LONG_PREVIEW);
|
@Test
|
||||||
assertThat(captured.getIntExtra(EXTRA_FLASH_NOTIFICATION_PREVIEW_COLOR, Color.TRANSPARENT))
|
public void clickNeutral_flushAllScheduledTasks_assertStopPreview() {
|
||||||
.isEqualTo(ROSE.mColorInt);
|
performClickOnDialog(BUTTON_NEUTRAL);
|
||||||
|
getTimerFromFragment().runAllTasks();
|
||||||
|
|
||||||
|
assertStopPreview();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -116,51 +122,47 @@ public class ScreenFlashNotificationColorDialogFragmentTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void clickNeutralAndPause_assertStopPreview() throws InterruptedException {
|
public void clickNeutralAndPause_assertStopPreview() {
|
||||||
performClickOnDialog(BUTTON_NEUTRAL);
|
performClickOnDialog(BUTTON_NEUTRAL);
|
||||||
Thread.sleep(100);
|
getTimerFromFragment().runOneTask();
|
||||||
mDialogFragment.onPause();
|
mDialogFragment.onPause();
|
||||||
Thread.sleep(100);
|
|
||||||
|
|
||||||
assertThat(getLastCapturedIntent().getAction())
|
assertStopPreview();
|
||||||
.isEqualTo(ACTION_FLASH_NOTIFICATION_STOP_PREVIEW);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void clickNeutralAndClickNegative_assertStopPreview() throws InterruptedException {
|
public void clickNeutralAndClickNegative_assertStopPreview() {
|
||||||
performClickOnDialog(BUTTON_NEUTRAL);
|
performClickOnDialog(BUTTON_NEUTRAL);
|
||||||
Thread.sleep(100);
|
getTimerFromFragment().runOneTask();
|
||||||
performClickOnDialog(BUTTON_NEGATIVE);
|
performClickOnDialog(BUTTON_NEGATIVE);
|
||||||
Thread.sleep(100);
|
|
||||||
|
|
||||||
assertThat(getLastCapturedIntent().getAction())
|
assertStopPreview();
|
||||||
.isEqualTo(ACTION_FLASH_NOTIFICATION_STOP_PREVIEW);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void clickNeutralAndClickPositive_assertStopPreview() throws InterruptedException {
|
public void clickNeutralAndClickPositive_assertStopPreview() {
|
||||||
performClickOnDialog(BUTTON_NEUTRAL);
|
performClickOnDialog(BUTTON_NEUTRAL);
|
||||||
Thread.sleep(100);
|
getTimerFromFragment().runOneTask();
|
||||||
performClickOnDialog(BUTTON_POSITIVE);
|
performClickOnDialog(BUTTON_POSITIVE);
|
||||||
Thread.sleep(100);
|
|
||||||
|
|
||||||
assertThat(getLastCapturedIntent().getAction())
|
assertStopPreview();
|
||||||
.isEqualTo(ACTION_FLASH_NOTIFICATION_STOP_PREVIEW);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void clickNeutralAndClickColor_assertStartPreview() throws InterruptedException {
|
public void clickNeutralAndClickColor_assertStartPreview() {
|
||||||
performClickOnDialog(BUTTON_NEUTRAL);
|
performClickOnDialog(BUTTON_NEUTRAL);
|
||||||
Thread.sleep(100);
|
getTimerFromFragment().runOneTask();
|
||||||
checkColorButton(CYAN);
|
checkColorButton(CYAN);
|
||||||
Thread.sleep(500);
|
// When changing the color while the preview is running, the fragment will schedule three
|
||||||
|
// tasks: stop the current preview, start the new preview, stop the new preview
|
||||||
|
int numOfPendingTasks = getTimerFromFragment().numOfPendingTasks();
|
||||||
|
// Run all the pending tasks except the last one
|
||||||
|
while (numOfPendingTasks > 1) {
|
||||||
|
getTimerFromFragment().runOneTask();
|
||||||
|
numOfPendingTasks--;
|
||||||
|
}
|
||||||
|
|
||||||
Intent captured = getLastCapturedIntent();
|
assertStartPreview(CYAN.mColorInt);
|
||||||
assertThat(captured.getAction()).isEqualTo(ACTION_FLASH_NOTIFICATION_START_PREVIEW);
|
|
||||||
assertThat(captured.getIntExtra(EXTRA_FLASH_NOTIFICATION_PREVIEW_TYPE, TYPE_SHORT_PREVIEW))
|
|
||||||
.isEqualTo(TYPE_LONG_PREVIEW);
|
|
||||||
assertThat(captured.getIntExtra(EXTRA_FLASH_NOTIFICATION_PREVIEW_COLOR, Color.TRANSPARENT))
|
|
||||||
.isEqualTo(CYAN.mColorInt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -168,6 +170,7 @@ public class ScreenFlashNotificationColorDialogFragmentTest {
|
|||||||
checkColorButton(AZURE);
|
checkColorButton(AZURE);
|
||||||
performClickOnDialog(BUTTON_NEGATIVE);
|
performClickOnDialog(BUTTON_NEGATIVE);
|
||||||
|
|
||||||
|
assertThat(getTimerFromFragment()).isNull();
|
||||||
assertThat(mCurrentColor).isEqualTo(ROSE.mColorInt);
|
assertThat(mCurrentColor).isEqualTo(ROSE.mColorInt);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,4 +196,46 @@ public class ScreenFlashNotificationColorDialogFragmentTest {
|
|||||||
final int size = capturedIntents.size();
|
final int size = capturedIntents.size();
|
||||||
return capturedIntents.get(size - 1);
|
return capturedIntents.get(size - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ScreenFlashNotificationColorDialogFragment createFragment() {
|
||||||
|
ScreenFlashNotificationColorDialogFragmentWithFakeTimer fragment =
|
||||||
|
new ScreenFlashNotificationColorDialogFragmentWithFakeTimer();
|
||||||
|
ReflectionHelpers.setField(fragment, "mCurrentColor", mCurrentColor);
|
||||||
|
ReflectionHelpers.setField(fragment, "mConsumer",
|
||||||
|
(Consumer<Integer>) selectedColor -> mCurrentColor = selectedColor);
|
||||||
|
|
||||||
|
return fragment;
|
||||||
|
}
|
||||||
|
|
||||||
|
private FakeTimer getTimerFromFragment() {
|
||||||
|
return (FakeTimer) ReflectionHelpers.getField(mDialogFragment, "mTimer");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertStartPreview(int color) {
|
||||||
|
Intent captured = getLastCapturedIntent();
|
||||||
|
assertThat(captured.getAction()).isEqualTo(ACTION_FLASH_NOTIFICATION_START_PREVIEW);
|
||||||
|
assertThat(captured.getIntExtra(EXTRA_FLASH_NOTIFICATION_PREVIEW_TYPE, TYPE_SHORT_PREVIEW))
|
||||||
|
.isEqualTo(TYPE_LONG_PREVIEW);
|
||||||
|
assertThat(captured.getIntExtra(EXTRA_FLASH_NOTIFICATION_PREVIEW_COLOR, Color.TRANSPARENT))
|
||||||
|
.isEqualTo(color);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertStopPreview() {
|
||||||
|
assertThat(getTimerFromFragment().numOfPendingTasks()).isEqualTo(0);
|
||||||
|
assertThat(getLastCapturedIntent().getAction())
|
||||||
|
.isEqualTo(ACTION_FLASH_NOTIFICATION_STOP_PREVIEW);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A {@link ScreenFlashNotificationColorDialogFragment} that uses a fake timer so that it won't
|
||||||
|
* create unmanageable timer threads during test.
|
||||||
|
*/
|
||||||
|
public static class ScreenFlashNotificationColorDialogFragmentWithFakeTimer extends
|
||||||
|
ScreenFlashNotificationColorDialogFragment {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
Timer createTimer() {
|
||||||
|
return new FakeTimer();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,81 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2023 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.android.settings.testutils;
|
||||||
|
|
||||||
|
import java.util.PriorityQueue;
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A fake {@link Timer} that doesn't create a TimerThread which is hard to manage in test.
|
||||||
|
*/
|
||||||
|
public class FakeTimer extends Timer {
|
||||||
|
private final PriorityQueue<ScheduledTimerTask> mQueue = new PriorityQueue<>();
|
||||||
|
|
||||||
|
public FakeTimer() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cancel() {
|
||||||
|
mQueue.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void schedule(TimerTask task, long delay) {
|
||||||
|
mQueue.offer(new ScheduledTimerTask(System.currentTimeMillis() + delay, task));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs the first task in the queue if there's any.
|
||||||
|
*/
|
||||||
|
public void runOneTask() {
|
||||||
|
if (mQueue.size() > 0) {
|
||||||
|
mQueue.poll().mTask.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs all the queued tasks in order.
|
||||||
|
*/
|
||||||
|
public void runAllTasks() {
|
||||||
|
while (mQueue.size() > 0) {
|
||||||
|
mQueue.poll().mTask.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns number of pending tasks in the timer
|
||||||
|
*/
|
||||||
|
public int numOfPendingTasks() {
|
||||||
|
return mQueue.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ScheduledTimerTask implements Comparable<ScheduledTimerTask> {
|
||||||
|
final long mTimeToRunInMillisSeconds;
|
||||||
|
final TimerTask mTask;
|
||||||
|
|
||||||
|
ScheduledTimerTask(long timeToRunInMilliSeconds, TimerTask task) {
|
||||||
|
this.mTimeToRunInMillisSeconds = timeToRunInMilliSeconds;
|
||||||
|
this.mTask = task;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(ScheduledTimerTask other) {
|
||||||
|
return Long.compare(this.mTimeToRunInMillisSeconds, other.mTimeToRunInMillisSeconds);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user