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.IntentFilter 22 import android.os.Handler 23 import android.os.Looper 24 import androidx.test.ext.junit.runners.AndroidJUnit4 25 import androidx.test.filters.SmallTest 26 import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation 27 import com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR 28 import com.google.common.truth.Truth.assertThat 29 import java.util.function.Consumer 30 import org.junit.Before 31 import org.junit.Test 32 import org.junit.runner.RunWith 33 import org.mockito.ArgumentCaptor 34 import org.mockito.ArgumentMatchers.eq 35 import org.mockito.ArgumentMatchers.same 36 import org.mockito.Captor 37 import org.mockito.Mock 38 import org.mockito.MockitoAnnotations 39 import org.mockito.kotlin.verify 40 41 @SmallTest 42 @RunWith(AndroidJUnit4::class) 43 class SimpleBroadcastReceiverTest { 44 45 private lateinit var underTest: SimpleBroadcastReceiver 46 47 @Mock private lateinit var intentConsumer: Consumer<Intent> 48 @Mock private lateinit var context: Context 49 @Mock private lateinit var completionRunnable: Runnable 50 @Captor private lateinit var intentFilterCaptor: ArgumentCaptor<IntentFilter> 51 52 @Before setUpnull53 fun setUp() { 54 MockitoAnnotations.initMocks(this) 55 underTest = SimpleBroadcastReceiver(context, UI_HELPER_EXECUTOR, intentConsumer) 56 if (Looper.getMainLooper() == null) { 57 Looper.prepareMainLooper() 58 } 59 } 60 61 @Test async_registernull62 fun async_register() { 63 underTest.register("test_action_1", "test_action_2") 64 awaitTasksCompleted() 65 66 verify(context).registerReceiver(same(underTest), intentFilterCaptor.capture()) 67 val intentFilter = intentFilterCaptor.value 68 assertThat(intentFilter.countActions()).isEqualTo(2) 69 assertThat(intentFilter.getAction(0)).isEqualTo("test_action_1") 70 assertThat(intentFilter.getAction(1)).isEqualTo("test_action_2") 71 } 72 73 @Test async_register_withCompletionRunnablenull74 fun async_register_withCompletionRunnable() { 75 underTest.register(completionRunnable, "test_action_1", "test_action_2") 76 awaitTasksCompleted() 77 78 verify(context).registerReceiver(same(underTest), intentFilterCaptor.capture()) 79 verify(completionRunnable).run() 80 val intentFilter = intentFilterCaptor.value 81 assertThat(intentFilter.countActions()).isEqualTo(2) 82 assertThat(intentFilter.getAction(0)).isEqualTo("test_action_1") 83 assertThat(intentFilter.getAction(1)).isEqualTo("test_action_2") 84 } 85 86 @Test async_register_withCompletionRunnable_and_flagnull87 fun async_register_withCompletionRunnable_and_flag() { 88 underTest.register(completionRunnable, 1, "test_action_1", "test_action_2") 89 awaitTasksCompleted() 90 91 verify(context).registerReceiver(same(underTest), intentFilterCaptor.capture(), eq(1)) 92 verify(completionRunnable).run() 93 val intentFilter = intentFilterCaptor.value 94 assertThat(intentFilter.countActions()).isEqualTo(2) 95 assertThat(intentFilter.getAction(0)).isEqualTo("test_action_1") 96 assertThat(intentFilter.getAction(1)).isEqualTo("test_action_2") 97 } 98 99 @Test async_register_with_packagenull100 fun async_register_with_package() { 101 underTest.registerPkgActions("pkg", "test_action_1", "test_action_2") 102 103 awaitTasksCompleted() 104 verify(context).registerReceiver(same(underTest), intentFilterCaptor.capture()) 105 val intentFilter = intentFilterCaptor.value 106 assertThat(intentFilter.getDataScheme(0)).isEqualTo("package") 107 assertThat(intentFilter.getDataSchemeSpecificPart(0).path).isEqualTo("pkg") 108 assertThat(intentFilter.countActions()).isEqualTo(2) 109 assertThat(intentFilter.getAction(0)).isEqualTo("test_action_1") 110 assertThat(intentFilter.getAction(1)).isEqualTo("test_action_2") 111 } 112 113 @Test sync_register_withCompletionRunnable_and_flagnull114 fun sync_register_withCompletionRunnable_and_flag() { 115 underTest = 116 SimpleBroadcastReceiver(context, Handler(Looper.getMainLooper()), intentConsumer) 117 118 underTest.register(completionRunnable, 1, "test_action_1", "test_action_2") 119 getInstrumentation().waitForIdleSync() 120 121 verify(context).registerReceiver(same(underTest), intentFilterCaptor.capture(), eq(1)) 122 verify(completionRunnable).run() 123 val intentFilter = intentFilterCaptor.value 124 assertThat(intentFilter.countActions()).isEqualTo(2) 125 assertThat(intentFilter.getAction(0)).isEqualTo("test_action_1") 126 assertThat(intentFilter.getAction(1)).isEqualTo("test_action_2") 127 } 128 129 @Test sync_register_withCompletionRunnable_and_permission_and_flagnull130 fun sync_register_withCompletionRunnable_and_permission_and_flag() { 131 underTest = 132 SimpleBroadcastReceiver(context, Handler(Looper.getMainLooper()), intentConsumer) 133 134 underTest.register(completionRunnable, "permission", 1, "test_action_1", "test_action_2") 135 getInstrumentation().waitForIdleSync() 136 137 verify(context) 138 .registerReceiver( 139 same(underTest), 140 intentFilterCaptor.capture(), 141 eq("permission"), 142 eq(null), 143 eq(1), 144 ) 145 verify(completionRunnable).run() 146 val intentFilter = intentFilterCaptor.value 147 assertThat(intentFilter.countActions()).isEqualTo(2) 148 assertThat(intentFilter.getAction(0)).isEqualTo("test_action_1") 149 assertThat(intentFilter.getAction(1)).isEqualTo("test_action_2") 150 } 151 152 @Test async_unregisternull153 fun async_unregister() { 154 underTest.unregisterReceiverSafely() 155 156 awaitTasksCompleted() 157 verify(context).unregisterReceiver(same(underTest)) 158 } 159 160 @Test sync_unregisternull161 fun sync_unregister() { 162 underTest = 163 SimpleBroadcastReceiver(context, Handler(Looper.getMainLooper()), intentConsumer) 164 165 underTest.unregisterReceiverSafely() 166 getInstrumentation().waitForIdleSync() 167 168 verify(context).unregisterReceiver(same(underTest)) 169 } 170 171 @Test getPackageFilternull172 fun getPackageFilter() { 173 val intentFilter = 174 SimpleBroadcastReceiver.getPackageFilter("pkg", "test_action_1", "test_action_2") 175 176 assertThat(intentFilter.getDataScheme(0)).isEqualTo("package") 177 assertThat(intentFilter.getDataSchemeSpecificPart(0).path).isEqualTo("pkg") 178 assertThat(intentFilter.countActions()).isEqualTo(2) 179 assertThat(intentFilter.getAction(0)).isEqualTo("test_action_1") 180 assertThat(intentFilter.getAction(1)).isEqualTo("test_action_2") 181 } 182 awaitTasksCompletednull183 private fun awaitTasksCompleted() { 184 UI_HELPER_EXECUTOR.submit<Any> { null }.get() 185 } 186 } 187