1 /* 2 * Copyright (C) 2024 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.launcher3.util 18 19 import android.content.Context 20 import android.content.Intent 21 import android.content.Intent.ACTION_SCREEN_OFF 22 import android.content.Intent.ACTION_SCREEN_ON 23 import android.content.Intent.ACTION_USER_PRESENT 24 import androidx.test.ext.junit.runners.AndroidJUnit4 25 import androidx.test.filters.SmallTest 26 import com.google.common.truth.Truth.assertThat 27 import org.junit.Before 28 import org.junit.Test 29 import org.junit.runner.RunWith 30 import org.mockito.Mock 31 import org.mockito.Mockito.verify 32 import org.mockito.MockitoAnnotations 33 import org.mockito.kotlin.verifyNoMoreInteractions 34 35 @SmallTest 36 @RunWith(AndroidJUnit4::class) 37 class ScreenOnTrackerTest { 38 39 @Mock private lateinit var receiver: SimpleBroadcastReceiver 40 @Mock private lateinit var context: Context 41 @Mock private lateinit var listener: ScreenOnTracker.ScreenOnListener 42 @Mock private lateinit var tracker: DaggerSingletonTracker 43 44 private lateinit var underTest: ScreenOnTracker 45 46 @Before setupnull47 fun setup() { 48 MockitoAnnotations.initMocks(this) 49 underTest = ScreenOnTracker(context, receiver, tracker) 50 } 51 52 @Test test_default_statenull53 fun test_default_state() { 54 verify(receiver).register(ACTION_SCREEN_ON, ACTION_SCREEN_OFF, ACTION_USER_PRESENT) 55 assertThat(underTest.isScreenOn).isTrue() 56 } 57 58 @Test close_unregister_receivernull59 fun close_unregister_receiver() { 60 underTest.close() 61 62 verify(receiver).unregisterReceiverSafely() 63 } 64 65 @Test add_listener_then_receive_screen_on_intent_notify_listenernull66 fun add_listener_then_receive_screen_on_intent_notify_listener() { 67 underTest.addListener(listener) 68 69 underTest.onReceive(Intent(ACTION_SCREEN_ON)) 70 71 verify(listener).onScreenOnChanged(true) 72 assertThat(underTest.isScreenOn).isTrue() 73 } 74 75 @Test add_listener_then_receive_screen_off_intent_notify_listenernull76 fun add_listener_then_receive_screen_off_intent_notify_listener() { 77 underTest.addListener(listener) 78 79 underTest.onReceive(Intent(ACTION_SCREEN_OFF)) 80 81 verify(listener).onScreenOnChanged(false) 82 assertThat(underTest.isScreenOn).isFalse() 83 } 84 85 @Test add_listener_then_receive_user_present_intent_notify_listenernull86 fun add_listener_then_receive_user_present_intent_notify_listener() { 87 underTest.addListener(listener) 88 89 underTest.onReceive(Intent(ACTION_USER_PRESENT)) 90 91 verify(listener).onUserPresent() 92 assertThat(underTest.isScreenOn).isTrue() 93 } 94 95 @Test remove_listener_then_receive_intent_noOpnull96 fun remove_listener_then_receive_intent_noOp() { 97 underTest.addListener(listener) 98 99 underTest.removeListener(listener) 100 101 underTest.onReceive(Intent(ACTION_USER_PRESENT)) 102 verifyNoMoreInteractions(listener) 103 } 104 } 105