Selim Cinek | fc9af34 | 2017-05-05 14:45:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.systemui.settings; |
| 18 | |
| 19 | import android.content.Intent; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 20 | |
| 21 | import androidx.test.filters.SmallTest; |
Selim Cinek | fc9af34 | 2017-05-05 14:45:11 -0700 | [diff] [blame] | 22 | |
| 23 | import com.android.systemui.SysuiTestCase; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 24 | import com.android.systemui.broadcast.BroadcastDispatcher; |
Selim Cinek | fc9af34 | 2017-05-05 14:45:11 -0700 | [diff] [blame] | 25 | |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 28 | import org.mockito.Mock; |
| 29 | import org.mockito.MockitoAnnotations; |
Selim Cinek | fc9af34 | 2017-05-05 14:45:11 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Testing functionality of the current user tracker |
| 33 | */ |
Jason Monk | fba8faf | 2017-05-23 10:42:59 -0400 | [diff] [blame] | 34 | @SmallTest |
Selim Cinek | fc9af34 | 2017-05-05 14:45:11 -0700 | [diff] [blame] | 35 | public class CurrentUserTrackerTest extends SysuiTestCase { |
| 36 | |
| 37 | private CurrentUserTracker mTracker; |
| 38 | private CurrentUserTracker.UserReceiver mReceiver; |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 39 | @Mock |
| 40 | private BroadcastDispatcher mBroadcastDispatcher; |
Selim Cinek | fc9af34 | 2017-05-05 14:45:11 -0700 | [diff] [blame] | 41 | |
| 42 | @Before |
| 43 | public void setUp() { |
Fabian Kozynski | 5ca7a51 | 2019-10-16 19:56:11 +0000 | [diff] [blame] | 44 | MockitoAnnotations.initMocks(this); |
| 45 | mReceiver = new CurrentUserTracker.UserReceiver(mBroadcastDispatcher); |
Selim Cinek | fc9af34 | 2017-05-05 14:45:11 -0700 | [diff] [blame] | 46 | mTracker = new CurrentUserTracker(mReceiver) { |
| 47 | @Override |
| 48 | public void onUserSwitched(int newUserId) { |
| 49 | stopTracking(); |
| 50 | } |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | @Test |
| 55 | public void testBroadCastDoesntCrashOnConcurrentModification() { |
| 56 | mTracker.startTracking(); |
| 57 | CurrentUserTracker secondTracker = new CurrentUserTracker(mReceiver) { |
| 58 | @Override |
| 59 | public void onUserSwitched(int newUserId) { |
| 60 | stopTracking(); |
| 61 | } |
| 62 | }; |
| 63 | secondTracker.startTracking(); |
| 64 | triggerUserSwitch(); |
| 65 | } |
| 66 | /** |
| 67 | * Simulates a user switch event. |
| 68 | */ |
| 69 | private void triggerUserSwitch() { |
| 70 | Intent intent = new Intent(Intent.ACTION_USER_SWITCHED); |
| 71 | intent.putExtra(Intent.EXTRA_USER_HANDLE, 1); |
| 72 | mReceiver.onReceive(getContext(), intent); |
| 73 | } |
| 74 | } |