[Test Week] Add ScreenOnTrackerTest

Number of new tested methods: 5

Bug: 353303621
Test: This is the unit test
Flag: NONE - unit test
Change-Id: Ieae1ed9bb215a52a12fef919c8dd407291c2b0f9
This commit is contained in:
Fengjiang Li committed 2024-07-19 00:49:58 -07:00
1 parent 3a33017797
commit b7b3137261
2 files changed
+122 -4

No files matched your search

@@ -24,7 +24,10 @@ import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
import android.content.Context;
import android.content.Intent;
import androidx.annotation.VisibleForTesting;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Consumer;
/**
* Utility class for tracking if the screen is currently on or off
@@ -34,8 +37,7 @@ public class ScreenOnTracker implements SafeCloseable {
public static final MainThreadInitializedObject<ScreenOnTracker> INSTANCE =
new MainThreadInitializedObject<>(ScreenOnTracker::new);
private final SimpleBroadcastReceiver mReceiver =
new SimpleBroadcastReceiver(UI_HELPER_EXECUTOR, this::onReceive);
private final SimpleBroadcastReceiver mReceiver;
private final CopyOnWriteArrayList<ScreenOnListener> mListeners = new CopyOnWriteArrayList<>();
private final Context mContext;
@@ -44,8 +46,20 @@ public class ScreenOnTracker implements SafeCloseable {
private ScreenOnTracker(Context context) {
// Assume that the screen is on to begin with
mContext = context;
mReceiver = new SimpleBroadcastReceiver(UI_HELPER_EXECUTOR, this::onReceive);
init();
}
@VisibleForTesting
ScreenOnTracker(Context context, SimpleBroadcastReceiver receiver) {
mContext = context;
mReceiver = receiver;
init();
}
private void init() {
mIsScreenOn = true;
mReceiver.register(context, ACTION_SCREEN_ON, ACTION_SCREEN_OFF, ACTION_USER_PRESENT);
mReceiver.register(mContext, ACTION_SCREEN_ON, ACTION_SCREEN_OFF, ACTION_USER_PRESENT);
}
@Override
@@ -53,7 +67,8 @@ public class ScreenOnTracker implements SafeCloseable {
mReceiver.unregisterReceiverSafely(mContext);
}
private void onReceive(Intent intent) {
@VisibleForTesting
void onReceive(Intent intent) {
String action = intent.getAction();
if (ACTION_SCREEN_ON.equals(action)) {
mIsScreenOn = true;