|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// This code is licensed under the MIT License. |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files(the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions : |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in |
| 14 | +// all copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | +// THE SOFTWARE. |
| 23 | +package com.microsoft.identity.common.internal.broker; |
| 24 | + |
| 25 | +import android.content.Intent; |
| 26 | + |
| 27 | +import androidx.test.core.app.ApplicationProvider; |
| 28 | + |
| 29 | +import com.microsoft.identity.common.java.constants.SharedDeviceModeConstants; |
| 30 | + |
| 31 | +import org.junit.Assert; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.Test; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | +import org.junit.runners.JUnit4; |
| 36 | + |
| 37 | +@RunWith(JUnit4.class) |
| 38 | +public class SDMBroadcastReceiverTests { |
| 39 | + |
| 40 | + String actualCallbackReceived; |
| 41 | + |
| 42 | + @Before |
| 43 | + public void setup() { |
| 44 | + SDMBroadcastReceiver.initialize(ApplicationProvider.getApplicationContext(), new SDMBroadcastReceiver.SharedDeviceModeCallback() { |
| 45 | + @Override |
| 46 | + public void onSharedDeviceModeRegistrationStarted() { |
| 47 | + actualCallbackReceived = SharedDeviceModeConstants.BROADCAST_TYPE_SDM_REGISTRATION_START; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void onSharedDeviceModeRegistered() { |
| 52 | + actualCallbackReceived = SharedDeviceModeConstants.BROADCAST_TYPE_SDM_REGISTERED; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void onGlobalSignOut() { |
| 57 | + actualCallbackReceived = SharedDeviceModeConstants.BROADCAST_TYPE_GLOBAL_SIGN_OUT; |
| 58 | + } |
| 59 | + }); |
| 60 | + } |
| 61 | + @Test |
| 62 | + public void testSDMBroadcast() throws InterruptedException { |
| 63 | + sendBroadcast(SharedDeviceModeConstants.BROADCAST_TYPE_SDM_REGISTRATION_START); |
| 64 | + Thread.sleep(100); |
| 65 | + Assert.assertEquals(SharedDeviceModeConstants.BROADCAST_TYPE_SDM_REGISTRATION_START, actualCallbackReceived); |
| 66 | + |
| 67 | + sendBroadcast(SharedDeviceModeConstants.BROADCAST_TYPE_GLOBAL_SIGN_OUT); |
| 68 | + Thread.sleep(100); |
| 69 | + Assert.assertEquals(SharedDeviceModeConstants.BROADCAST_TYPE_GLOBAL_SIGN_OUT, actualCallbackReceived); |
| 70 | + } |
| 71 | + |
| 72 | + private void sendBroadcast(String broadcastType) { |
| 73 | + final Intent intent = new Intent(); |
| 74 | + intent.setAction(SharedDeviceModeConstants.CURRENT_ACCOUNT_CHANGED_BROADCAST_IDENTIFIER); |
| 75 | + intent.putExtra(SharedDeviceModeConstants.BROADCAST_TYPE_KEY, broadcastType); |
| 76 | + ApplicationProvider.getApplicationContext().sendBroadcast(intent); |
| 77 | + } |
| 78 | +} |
0 commit comments