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; |
| 20 | import static org.mockito.Mockito.spy; |
| 21 | import static org.mockito.Mockito.timeout; |
| 22 | import static org.mockito.Mockito.verify; |
| 23 | |
| 24 | import android.content.BroadcastReceiver; |
| 25 | import android.content.ComponentName; |
| 26 | import android.content.ContentProvider; |
| 27 | import android.content.ContentResolver; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
| 30 | import android.content.IntentFilter; |
| 31 | import android.net.Uri; |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 32 | import android.testing.AndroidTestingRunner; |
| 33 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 34 | import androidx.test.filters.SmallTest; |
| 35 | |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 36 | import com.android.settingslib.SliceBroadcastRelay; |
| 37 | |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 38 | import org.junit.Before; |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 39 | import org.junit.Test; |
| 40 | import org.junit.runner.RunWith; |
| 41 | import org.mockito.ArgumentCaptor; |
| 42 | |
| 43 | @RunWith(AndroidTestingRunner.class) |
| 44 | @SmallTest |
| 45 | public class SliceBroadcastRelayHandlerTest extends SysuiTestCase { |
| 46 | |
| 47 | private static final String TEST_ACTION = "com.android.systemui.action.TEST_ACTION"; |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 48 | private SliceBroadcastRelayHandler mRelayHandler; |
| 49 | private Context mSpyContext; |
| 50 | @Before |
| 51 | public void setup() { |
| 52 | mSpyContext = spy(mContext); |
| 53 | |
| 54 | mRelayHandler = new SliceBroadcastRelayHandler(mSpyContext); |
| 55 | } |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 56 | |
| 57 | @Test |
| 58 | public void testRegister() { |
| 59 | Uri testUri = new Uri.Builder() |
| 60 | .scheme(ContentResolver.SCHEME_CONTENT) |
| 61 | .authority("something") |
| 62 | .path("test") |
| 63 | .build(); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 64 | |
| 65 | Intent intent = new Intent(SliceBroadcastRelay.ACTION_REGISTER); |
| 66 | intent.putExtra(SliceBroadcastRelay.EXTRA_URI, ContentProvider.maybeAddUserId(testUri, 0)); |
| 67 | intent.putExtra(SliceBroadcastRelay.EXTRA_RECEIVER, |
| 68 | new ComponentName(mContext.getPackageName(), Receiver.class.getName())); |
| 69 | IntentFilter value = new IntentFilter(TEST_ACTION); |
| 70 | intent.putExtra(SliceBroadcastRelay.EXTRA_FILTER, value); |
Matthew Fritze | 429d27d | 2018-05-02 19:43:39 -0700 | [diff] [blame] | 71 | intent.putExtra(SliceBroadcastRelay.EXTRA_URI, testUri); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 72 | |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 73 | mRelayHandler.handleIntent(intent); |
| 74 | verify(mSpyContext).registerReceiver(any(), eq(value)); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void testUnregister() { |
| 79 | Uri testUri = new Uri.Builder() |
| 80 | .scheme(ContentResolver.SCHEME_CONTENT) |
| 81 | .authority("something") |
| 82 | .path("test") |
| 83 | .build(); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 84 | |
| 85 | Intent intent = new Intent(SliceBroadcastRelay.ACTION_REGISTER); |
| 86 | intent.putExtra(SliceBroadcastRelay.EXTRA_URI, ContentProvider.maybeAddUserId(testUri, 0)); |
| 87 | intent.putExtra(SliceBroadcastRelay.EXTRA_RECEIVER, |
| 88 | new ComponentName(mContext.getPackageName(), Receiver.class.getName())); |
| 89 | IntentFilter value = new IntentFilter(TEST_ACTION); |
| 90 | intent.putExtra(SliceBroadcastRelay.EXTRA_FILTER, value); |
| 91 | |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 92 | mRelayHandler.handleIntent(intent); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 93 | ArgumentCaptor<BroadcastReceiver> relay = ArgumentCaptor.forClass(BroadcastReceiver.class); |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 94 | verify(mSpyContext).registerReceiver(relay.capture(), eq(value)); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 95 | |
| 96 | intent = new Intent(SliceBroadcastRelay.ACTION_UNREGISTER); |
| 97 | intent.putExtra(SliceBroadcastRelay.EXTRA_URI, ContentProvider.maybeAddUserId(testUri, 0)); |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 98 | mRelayHandler.handleIntent(intent); |
| 99 | verify(mSpyContext).unregisterReceiver(eq(relay.getValue())); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | @Test |
Sunny Goyal | a21570c7 | 2019-04-19 15:05:41 -0700 | [diff] [blame] | 103 | public void testUnregisterWithoutRegister() { |
| 104 | Uri testUri = new Uri.Builder() |
| 105 | .scheme(ContentResolver.SCHEME_CONTENT) |
| 106 | .authority("something") |
| 107 | .path("test") |
| 108 | .build(); |
Sunny Goyal | a21570c7 | 2019-04-19 15:05:41 -0700 | [diff] [blame] | 109 | |
| 110 | Intent intent = new Intent(SliceBroadcastRelay.ACTION_UNREGISTER); |
| 111 | intent.putExtra(SliceBroadcastRelay.EXTRA_URI, ContentProvider.maybeAddUserId(testUri, 0)); |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 112 | mRelayHandler.handleIntent(intent); |
Sunny Goyal | a21570c7 | 2019-04-19 15:05:41 -0700 | [diff] [blame] | 113 | // No crash |
| 114 | } |
| 115 | |
| 116 | @Test |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 117 | public void testRelay() { |
| 118 | Receiver.sReceiver = mock(BroadcastReceiver.class); |
| 119 | Uri testUri = new Uri.Builder() |
| 120 | .scheme(ContentResolver.SCHEME_CONTENT) |
| 121 | .authority("something") |
| 122 | .path("test") |
| 123 | .build(); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 124 | Intent intent = new Intent(SliceBroadcastRelay.ACTION_REGISTER); |
| 125 | intent.putExtra(SliceBroadcastRelay.EXTRA_URI, ContentProvider.maybeAddUserId(testUri, 0)); |
| 126 | intent.putExtra(SliceBroadcastRelay.EXTRA_RECEIVER, |
| 127 | new ComponentName(mContext.getPackageName(), Receiver.class.getName())); |
| 128 | IntentFilter value = new IntentFilter(TEST_ACTION); |
| 129 | intent.putExtra(SliceBroadcastRelay.EXTRA_FILTER, value); |
| 130 | |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 131 | mRelayHandler.handleIntent(intent); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 132 | ArgumentCaptor<BroadcastReceiver> relay = ArgumentCaptor.forClass(BroadcastReceiver.class); |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 133 | verify(mSpyContext).registerReceiver(relay.capture(), eq(value)); |
| 134 | relay.getValue().onReceive(mSpyContext, new Intent(TEST_ACTION)); |
Jason Monk | 0b5e52c | 2018-04-19 22:14:46 -0400 | [diff] [blame] | 135 | |
| 136 | verify(Receiver.sReceiver, timeout(2000)).onReceive(any(), any()); |
| 137 | } |
| 138 | |
| 139 | public static class Receiver extends BroadcastReceiver { |
| 140 | private static BroadcastReceiver sReceiver; |
| 141 | |
| 142 | @Override |
| 143 | public void onReceive(Context context, Intent intent) { |
| 144 | if (sReceiver != null) sReceiver.onReceive(context, intent); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | } |