blob: 1b515c6bbdbd5158c652ecc14788074c0a20b7f1 [file] [log] [blame]
Selim Cinekfc9af342017-05-05 14:45:11 -07001/*
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
17package com.android.systemui.settings;
18
19import android.content.Intent;
Brett Chabot84151d92019-02-27 15:37:59 -080020
21import androidx.test.filters.SmallTest;
Selim Cinekfc9af342017-05-05 14:45:11 -070022
23import com.android.systemui.SysuiTestCase;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000024import com.android.systemui.broadcast.BroadcastDispatcher;
Selim Cinekfc9af342017-05-05 14:45:11 -070025
26import org.junit.Before;
27import org.junit.Test;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000028import org.mockito.Mock;
29import org.mockito.MockitoAnnotations;
Selim Cinekfc9af342017-05-05 14:45:11 -070030
31/**
32 * Testing functionality of the current user tracker
33 */
Jason Monkfba8faf2017-05-23 10:42:59 -040034@SmallTest
Selim Cinekfc9af342017-05-05 14:45:11 -070035public class CurrentUserTrackerTest extends SysuiTestCase {
36
37 private CurrentUserTracker mTracker;
38 private CurrentUserTracker.UserReceiver mReceiver;
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000039 @Mock
40 private BroadcastDispatcher mBroadcastDispatcher;
Selim Cinekfc9af342017-05-05 14:45:11 -070041
42 @Before
43 public void setUp() {
Fabian Kozynski5ca7a512019-10-16 19:56:11 +000044 MockitoAnnotations.initMocks(this);
45 mReceiver = new CurrentUserTracker.UserReceiver(mBroadcastDispatcher);
Selim Cinekfc9af342017-05-05 14:45:11 -070046 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}