Home | History | Annotate | Download | only in development
      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.settings.development;
     18 
     19 import static android.arch.lifecycle.Lifecycle.Event.ON_START;
     20 import static android.arch.lifecycle.Lifecycle.Event.ON_STOP;
     21 import static com.google.common.truth.Truth.assertThat;
     22 import static org.mockito.Mockito.when;
     23 
     24 import android.app.Application;
     25 import android.arch.lifecycle.LifecycleOwner;
     26 import android.content.Context;
     27 import android.os.UserManager;
     28 
     29 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     30 import com.android.settings.testutils.shadow.ShadowUtils;
     31 import com.android.settings.widget.SwitchBar;
     32 import com.android.settings.widget.SwitchBar.OnSwitchChangeListener;
     33 import com.android.settingslib.core.lifecycle.Lifecycle;
     34 
     35 import org.junit.After;
     36 import org.junit.Before;
     37 import org.junit.Test;
     38 import org.junit.runner.RunWith;
     39 import org.mockito.Mock;
     40 import org.mockito.MockitoAnnotations;
     41 import org.robolectric.RuntimeEnvironment;
     42 import org.robolectric.Shadows;
     43 import org.robolectric.annotation.Config;
     44 import org.robolectric.util.ReflectionHelpers;
     45 
     46 import java.util.List;
     47 
     48 @RunWith(SettingsRobolectricTestRunner.class)
     49 @Config(shadows = ShadowUtils.class)
     50 public class DevelopmentSwitchBarControllerTest {
     51 
     52     @Mock
     53     private DevelopmentSettingsDashboardFragment mSettings;
     54     private LifecycleOwner mLifecycleOwner;
     55     private Lifecycle mLifecycle;
     56     private SwitchBar mSwitchBar;
     57 
     58     @Before
     59     public void setUp() {
     60         MockitoAnnotations.initMocks(this);
     61         final Context context = RuntimeEnvironment.application;
     62         UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
     63         Shadows.shadowOf(userManager).setIsAdminUser(true);
     64         mLifecycleOwner = () -> mLifecycle;
     65         mLifecycle = new Lifecycle(mLifecycleOwner);
     66         mSwitchBar = new SwitchBar(context);
     67         when(mSettings.getContext()).thenReturn(context);
     68     }
     69 
     70     @After
     71     public void tearDown() {
     72         ShadowUtils.reset();
     73     }
     74 
     75     @Test
     76     public void runThroughLifecycle_v2_isMonkeyRun_shouldNotRegisterListener() {
     77         ShadowUtils.setIsUserAMonkey(true);
     78         new DevelopmentSwitchBarController(mSettings, mSwitchBar,
     79                 true /* isAvailable */, mLifecycle);
     80         final List<SwitchBar.OnSwitchChangeListener> listeners =
     81                 ReflectionHelpers.getField(mSwitchBar, "mSwitchChangeListeners");
     82 
     83         mLifecycle.handleLifecycleEvent(ON_START);
     84         assertThat(listeners).doesNotContain(mSettings);
     85 
     86         mLifecycle.handleLifecycleEvent(ON_STOP);
     87         assertThat(listeners).doesNotContain(mSettings);
     88     }
     89 
     90     @Test
     91     public void runThroughLifecycle_isNotMonkeyRun_shouldRegisterAndRemoveListener() {
     92         ShadowUtils.setIsUserAMonkey(false);
     93         new DevelopmentSwitchBarController(mSettings, mSwitchBar,
     94                 true /* isAvailable */, mLifecycle);
     95         final List<OnSwitchChangeListener> listeners =
     96                 ReflectionHelpers.getField(mSwitchBar, "mSwitchChangeListeners");
     97 
     98         mLifecycle.handleLifecycleEvent(ON_START);
     99         assertThat(listeners).contains(mSettings);
    100 
    101         mLifecycle.handleLifecycleEvent(ON_STOP);
    102         assertThat(listeners).doesNotContain(mSettings);
    103     }
    104 
    105     @Test
    106     public void runThroughLifecycle_v2_isNotMonkeyRun_shouldRegisterAndRemoveListener() {
    107         when(mSettings.getContext()).thenReturn(RuntimeEnvironment.application);
    108         ShadowUtils.setIsUserAMonkey(false);
    109         new DevelopmentSwitchBarController(mSettings, mSwitchBar,
    110                 true /* isAvailable */, mLifecycle);
    111         final List<SwitchBar.OnSwitchChangeListener> listeners =
    112                 ReflectionHelpers.getField(mSwitchBar, "mSwitchChangeListeners");
    113 
    114         mLifecycle.handleLifecycleEvent(ON_START);
    115         assertThat(listeners).contains(mSettings);
    116 
    117         mLifecycle.handleLifecycleEvent(ON_STOP);
    118         assertThat(listeners).doesNotContain(mSettings);
    119     }
    120 
    121     @Test
    122     public void buildController_unavailable_shouldDisableSwitchBar() {
    123         ShadowUtils.setIsUserAMonkey(false);
    124         new DevelopmentSwitchBarController(mSettings, mSwitchBar,
    125                 false /* isAvailable */, mLifecycle);
    126 
    127         assertThat(mSwitchBar.isEnabled()).isFalse();
    128     }
    129 }
    130