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; 32 import android.support.test.filters.SmallTest; 33 import android.testing.AndroidTestingRunner; 34 35 import com.android.settingslib.SliceBroadcastRelay; 36 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.ArgumentCaptor; 40 41 @RunWith(AndroidTestingRunner.class) 42 @SmallTest 43 public class SliceBroadcastRelayHandlerTest extends SysuiTestCase { 44 45 private static final String TEST_ACTION = "com.android.systemui.action.TEST_ACTION"; 46 47 @Test testRegister()48 public void testRegister() { 49 Uri testUri = new Uri.Builder() 50 .scheme(ContentResolver.SCHEME_CONTENT) 51 .authority("something") 52 .path("test") 53 .build(); 54 SliceBroadcastRelayHandler relayHandler = new SliceBroadcastRelayHandler(); 55 relayHandler.mContext = spy(mContext); 56 57 Intent intent = new Intent(SliceBroadcastRelay.ACTION_REGISTER); 58 intent.putExtra(SliceBroadcastRelay.EXTRA_URI, ContentProvider.maybeAddUserId(testUri, 0)); 59 intent.putExtra(SliceBroadcastRelay.EXTRA_RECEIVER, 60 new ComponentName(mContext.getPackageName(), Receiver.class.getName())); 61 IntentFilter value = new IntentFilter(TEST_ACTION); 62 intent.putExtra(SliceBroadcastRelay.EXTRA_FILTER, value); 63 intent.putExtra(SliceBroadcastRelay.EXTRA_URI, testUri); 64 65 relayHandler.handleIntent(intent); 66 verify(relayHandler.mContext).registerReceiver(any(), eq(value)); 67 } 68 69 @Test testUnregister()70 public void testUnregister() { 71 Uri testUri = new Uri.Builder() 72 .scheme(ContentResolver.SCHEME_CONTENT) 73 .authority("something") 74 .path("test") 75 .build(); 76 SliceBroadcastRelayHandler relayHandler = new SliceBroadcastRelayHandler(); 77 relayHandler.mContext = spy(mContext); 78 79 Intent intent = new Intent(SliceBroadcastRelay.ACTION_REGISTER); 80 intent.putExtra(SliceBroadcastRelay.EXTRA_URI, ContentProvider.maybeAddUserId(testUri, 0)); 81 intent.putExtra(SliceBroadcastRelay.EXTRA_RECEIVER, 82 new ComponentName(mContext.getPackageName(), Receiver.class.getName())); 83 IntentFilter value = new IntentFilter(TEST_ACTION); 84 intent.putExtra(SliceBroadcastRelay.EXTRA_FILTER, value); 85 86 relayHandler.handleIntent(intent); 87 ArgumentCaptor<BroadcastReceiver> relay = ArgumentCaptor.forClass(BroadcastReceiver.class); 88 verify(relayHandler.mContext).registerReceiver(relay.capture(), eq(value)); 89 90 intent = new Intent(SliceBroadcastRelay.ACTION_UNREGISTER); 91 intent.putExtra(SliceBroadcastRelay.EXTRA_URI, ContentProvider.maybeAddUserId(testUri, 0)); 92 relayHandler.handleIntent(intent); 93 verify(relayHandler.mContext).unregisterReceiver(eq(relay.getValue())); 94 } 95 96 @Test testRelay()97 public void testRelay() { 98 Receiver.sReceiver = mock(BroadcastReceiver.class); 99 Uri testUri = new Uri.Builder() 100 .scheme(ContentResolver.SCHEME_CONTENT) 101 .authority("something") 102 .path("test") 103 .build(); 104 SliceBroadcastRelayHandler relayHandler = new SliceBroadcastRelayHandler(); 105 relayHandler.mContext = spy(mContext); 106 107 Intent intent = new Intent(SliceBroadcastRelay.ACTION_REGISTER); 108 intent.putExtra(SliceBroadcastRelay.EXTRA_URI, ContentProvider.maybeAddUserId(testUri, 0)); 109 intent.putExtra(SliceBroadcastRelay.EXTRA_RECEIVER, 110 new ComponentName(mContext.getPackageName(), Receiver.class.getName())); 111 IntentFilter value = new IntentFilter(TEST_ACTION); 112 intent.putExtra(SliceBroadcastRelay.EXTRA_FILTER, value); 113 114 relayHandler.handleIntent(intent); 115 ArgumentCaptor<BroadcastReceiver> relay = ArgumentCaptor.forClass(BroadcastReceiver.class); 116 verify(relayHandler.mContext).registerReceiver(relay.capture(), eq(value)); 117 relay.getValue().onReceive(relayHandler.mContext, new Intent(TEST_ACTION)); 118 119 verify(Receiver.sReceiver, timeout(2000)).onReceive(any(), any()); 120 } 121 122 public static class Receiver extends BroadcastReceiver { 123 private static BroadcastReceiver sReceiver; 124 125 @Override onReceive(Context context, Intent intent)126 public void onReceive(Context context, Intent intent) { 127 if (sReceiver != null) sReceiver.onReceive(context, intent); 128 } 129 } 130 131 }