Home | History | Annotate | Download | only in settings
      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;
     20 import android.support.test.filters.SmallTest;
     21 
     22 import com.android.systemui.SysuiTestCase;
     23 
     24 import org.junit.Before;
     25 import org.junit.Test;
     26 
     27 /**
     28  * Testing functionality of the current user tracker
     29  */
     30 @SmallTest
     31 public class CurrentUserTrackerTest extends SysuiTestCase {
     32 
     33     private CurrentUserTracker mTracker;
     34     private CurrentUserTracker.UserReceiver mReceiver;
     35 
     36     @Before
     37     public void setUp() {
     38         mReceiver = new CurrentUserTracker.UserReceiver(getContext());
     39         mTracker = new CurrentUserTracker(mReceiver) {
     40             @Override
     41             public void onUserSwitched(int newUserId) {
     42                 stopTracking();
     43             }
     44         };
     45     }
     46 
     47     @Test
     48     public void testBroadCastDoesntCrashOnConcurrentModification() {
     49         mTracker.startTracking();
     50         CurrentUserTracker secondTracker = new CurrentUserTracker(mReceiver) {
     51             @Override
     52             public void onUserSwitched(int newUserId) {
     53                 stopTracking();
     54             }
     55         };
     56         secondTracker.startTracking();
     57         triggerUserSwitch();
     58     }
     59     /**
     60      * Simulates a user switch event.
     61      */
     62     private void triggerUserSwitch() {
     63         Intent intent = new Intent(Intent.ACTION_USER_SWITCHED);
     64         intent.putExtra(Intent.EXTRA_USER_HANDLE, 1);
     65         mReceiver.onReceive(getContext(), intent);
     66     }
     67 }
     68