Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 74861537 authored by Fengjiang Li's avatar Fengjiang Li Committed by Android (Google) Code Review
Browse files

Merge "[Test Week] Add ScreenOnTrackerTest" into main

parents 5217ae7f b7b31372
Loading
Loading
Loading
Loading
+19 −4
Original line number Diff line number Diff line
@@ -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;
+103 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.launcher3.util

import android.content.Context
import android.content.Intent
import android.content.Intent.ACTION_SCREEN_OFF
import android.content.Intent.ACTION_SCREEN_ON
import android.content.Intent.ACTION_USER_PRESENT
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.verify
import org.mockito.MockitoAnnotations
import org.mockito.kotlin.verifyNoMoreInteractions

@SmallTest
@RunWith(AndroidJUnit4::class)
class ScreenOnTrackerTest {

    @Mock private lateinit var receiver: SimpleBroadcastReceiver
    @Mock private lateinit var context: Context
    @Mock private lateinit var listener: ScreenOnTracker.ScreenOnListener

    private lateinit var underTest: ScreenOnTracker

    @Before
    fun setup() {
        MockitoAnnotations.initMocks(this)
        underTest = ScreenOnTracker(context, receiver)
    }

    @Test
    fun test_default_state() {
        verify(receiver).register(context, ACTION_SCREEN_ON, ACTION_SCREEN_OFF, ACTION_USER_PRESENT)
        assertThat(underTest.isScreenOn).isTrue()
    }

    @Test
    fun close_unregister_receiver() {
        underTest.close()

        verify(receiver).unregisterReceiverSafely(context)
    }

    @Test
    fun add_listener_then_receive_screen_on_intent_notify_listener() {
        underTest.addListener(listener)

        underTest.onReceive(Intent(ACTION_SCREEN_ON))

        verify(listener).onScreenOnChanged(true)
        assertThat(underTest.isScreenOn).isTrue()
    }

    @Test
    fun add_listener_then_receive_screen_off_intent_notify_listener() {
        underTest.addListener(listener)

        underTest.onReceive(Intent(ACTION_SCREEN_OFF))

        verify(listener).onScreenOnChanged(false)
        assertThat(underTest.isScreenOn).isFalse()
    }

    @Test
    fun add_listener_then_receive_user_present_intent_notify_listener() {
        underTest.addListener(listener)

        underTest.onReceive(Intent(ACTION_USER_PRESENT))

        verify(listener).onUserPresent()
        assertThat(underTest.isScreenOn).isTrue()
    }

    @Test
    fun remove_listener_then_receive_intent_noOp() {
        underTest.addListener(listener)

        underTest.removeListener(listener)

        underTest.onReceive(Intent(ACTION_USER_PRESENT))
        verifyNoMoreInteractions(listener)
    }
}