Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
| 13 | */ |
| 14 | |
| 15 | package com.android.systemui; |
| 16 | |
| 17 | import static org.mockito.ArgumentMatchers.any; |
| 18 | import static org.mockito.ArgumentMatchers.eq; |
| 19 | import static org.mockito.Mockito.mock; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 20 | import static org.mockito.Mockito.never; |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 21 | import static org.mockito.Mockito.spy; |
| 22 | import static org.mockito.Mockito.timeout; |
| 23 | import static org.mockito.Mockito.verify; |
| 24 | |
| 25 | import android.content.BroadcastReceiver; |
| 26 | import android.content.ComponentName; |
| 27 | import android.content.ContentProvider; |
| 28 | import android.content.ContentResolver; |
| 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
| 31 | import android.content.IntentFilter; |
| 32 | import android.net.Uri; |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 33 | import android.testing.AndroidTestingRunner; |
| 34 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 35 | import androidx.test.filters.SmallTest; |
| 36 | |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 37 | import com.android.settingslib.SliceBroadcastRelay; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 38 | import com.android.systemui.broadcast.BroadcastDispatcher; |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 39 | |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 40 | import org.junit.Before; |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 41 | import org.junit.Test; |
| 42 | import org.junit.runner.RunWith; |
| 43 | import org.mockito.ArgumentCaptor; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 44 | import org.mockito.Mock; |
| 45 | import org.mockito.MockitoAnnotations; |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 46 | |
| 47 | @RunWith(AndroidTestingRunner.class) |
| 48 | @SmallTest |
| 49 | public class SliceBroadcastRelayHandlerTest extends SysuiTestCase { |
| 50 | |
| 51 | private static final String TEST_ACTION = "com.android.systemui.action.TEST_ACTION"; |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 52 | private SliceBroadcastRelayHandler mRelayHandler; |
| 53 | private Context mSpyContext; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 54 | @Mock |
| 55 | private BroadcastDispatcher mBroadcastDispatcher; |
| 56 | |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 57 | @Before |
| 58 | public void setup() { |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 59 | MockitoAnnotations.initMocks(this); |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 60 | mSpyContext = spy(mContext); |
| 61 | |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 62 | mRelayHandler = new SliceBroadcastRelayHandler(mSpyContext, mBroadcastDispatcher); |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 63 | } |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 64 | |
| 65 | @Test |
| 66 | public void testRegister() { |
| 67 | Uri testUri = new Uri.Builder() |
| 68 | .scheme(ContentResolver.SCHEME_CONTENT) |
| 69 | .authority("something") |
| 70 | .path("test") |
| 71 | .build(); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 72 | |
| 73 | Intent intent = new Intent(SliceBroadcastRelay.ACTION_REGISTER); |
| 74 | intent.putExtra(SliceBroadcastRelay.EXTRA_URI, ContentProvider.maybeAddUserId(testUri, 0)); |
| 75 | intent.putExtra(SliceBroadcastRelay.EXTRA_RECEIVER, |
| 76 | new ComponentName(mContext.getPackageName(), Receiver.class.getName())); |
| 77 | IntentFilter value = new IntentFilter(TEST_ACTION); |
| 78 | intent.putExtra(SliceBroadcastRelay.EXTRA_FILTER, value); |
Matthew Fritze | 429d27d | 2018-05-02 19:43:39 -0700 | [diff] [blame] | 79 | intent.putExtra(SliceBroadcastRelay.EXTRA_URI, testUri); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 80 | |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 81 | mRelayHandler.handleIntent(intent); |
| 82 | verify(mSpyContext).registerReceiver(any(), eq(value)); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testUnregister() { |
| 87 | Uri testUri = new Uri.Builder() |
| 88 | .scheme(ContentResolver.SCHEME_CONTENT) |
| 89 | .authority("something") |
| 90 | .path("test") |
| 91 | .build(); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 92 | |
| 93 | Intent intent = new Intent(SliceBroadcastRelay.ACTION_REGISTER); |
| 94 | intent.putExtra(SliceBroadcastRelay.EXTRA_URI, ContentProvider.maybeAddUserId(testUri, 0)); |
| 95 | intent.putExtra(SliceBroadcastRelay.EXTRA_RECEIVER, |
| 96 | new ComponentName(mContext.getPackageName(), Receiver.class.getName())); |
| 97 | IntentFilter value = new IntentFilter(TEST_ACTION); |
| 98 | intent.putExtra(SliceBroadcastRelay.EXTRA_FILTER, value); |
| 99 | |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 100 | mRelayHandler.handleIntent(intent); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 101 | ArgumentCaptor<BroadcastReceiver> relay = ArgumentCaptor.forClass(BroadcastReceiver.class); |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 102 | verify(mSpyContext).registerReceiver(relay.capture(), eq(value)); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 103 | |
| 104 | intent = new Intent(SliceBroadcastRelay.ACTION_UNREGISTER); |
| 105 | intent.putExtra(SliceBroadcastRelay.EXTRA_URI, ContentProvider.maybeAddUserId(testUri, 0)); |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 106 | mRelayHandler.handleIntent(intent); |
| 107 | verify(mSpyContext).unregisterReceiver(eq(relay.getValue())); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Test |
Sunny Goyal | a21570c7 | 2019-04-19 15:05:41 -0700 | [diff] [blame] | 111 | public void testUnregisterWithoutRegister() { |
| 112 | Uri testUri = new Uri.Builder() |
| 113 | .scheme(ContentResolver.SCHEME_CONTENT) |
| 114 | .authority("something") |
| 115 | .path("test") |
| 116 | .build(); |
Sunny Goyal | a21570c7 | 2019-04-19 15:05:41 -0700 | [diff] [blame] | 117 | |
| 118 | Intent intent = new Intent(SliceBroadcastRelay.ACTION_UNREGISTER); |
| 119 | intent.putExtra(SliceBroadcastRelay.EXTRA_URI, ContentProvider.maybeAddUserId(testUri, 0)); |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 120 | mRelayHandler.handleIntent(intent); |
Sunny Goyal | a21570c7 | 2019-04-19 15:05:41 -0700 | [diff] [blame] | 121 | // No crash |
| 122 | } |
| 123 | |
| 124 | @Test |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 125 | public void testRelay() { |
| 126 | Receiver.sReceiver = mock(BroadcastReceiver.class); |
| 127 | Uri testUri = new Uri.Builder() |
| 128 | .scheme(ContentResolver.SCHEME_CONTENT) |
| 129 | .authority("something") |
| 130 | .path("test") |
| 131 | .build(); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 132 | Intent intent = new Intent(SliceBroadcastRelay.ACTION_REGISTER); |
| 133 | intent.putExtra(SliceBroadcastRelay.EXTRA_URI, ContentProvider.maybeAddUserId(testUri, 0)); |
| 134 | intent.putExtra(SliceBroadcastRelay.EXTRA_RECEIVER, |
| 135 | new ComponentName(mContext.getPackageName(), Receiver.class.getName())); |
| 136 | IntentFilter value = new IntentFilter(TEST_ACTION); |
| 137 | intent.putExtra(SliceBroadcastRelay.EXTRA_FILTER, value); |
| 138 | |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 139 | mRelayHandler.handleIntent(intent); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 140 | ArgumentCaptor<BroadcastReceiver> relay = ArgumentCaptor.forClass(BroadcastReceiver.class); |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 141 | verify(mSpyContext).registerReceiver(relay.capture(), eq(value)); |
| 142 | relay.getValue().onReceive(mSpyContext, new Intent(TEST_ACTION)); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 143 | |
| 144 | verify(Receiver.sReceiver, timeout(2000)).onReceive(any(), any()); |
| 145 | } |
| 146 | |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 147 | @Test |
| 148 | public void testRegisteredWithDispatcher() { |
| 149 | mRelayHandler.start(); |
| 150 | |
| 151 | verify(mBroadcastDispatcher) |
| 152 | .registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class)); |
| 153 | verify(mSpyContext, never()) |
| 154 | .registerReceiver(any(BroadcastReceiver.class), any(IntentFilter.class)); |
| 155 | } |
| 156 | |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 157 | public static class Receiver extends BroadcastReceiver { |
| 158 | private static BroadcastReceiver sReceiver; |
| 159 | |
| 160 | @Override |
| 161 | public void onReceive(Context context, Intent intent) { |
| 162 | if (sReceiver != null) sReceiver.onReceive(context, intent); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | } |