Home | History | Annotate | Download | only in featureflags
      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.featureflags;
     18 
     19 import static android.arch.lifecycle.Lifecycle.Event.ON_START;
     20 import static com.google.common.truth.Truth.assertThat;
     21 import static org.mockito.Matchers.any;
     22 import static org.mockito.Mockito.atLeastOnce;
     23 import static org.mockito.Mockito.verify;
     24 import static org.mockito.Mockito.when;
     25 
     26 import android.arch.lifecycle.LifecycleOwner;
     27 import android.content.Context;
     28 import android.support.v7.preference.PreferenceScreen;
     29 
     30 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     31 import com.android.settingslib.core.lifecycle.Lifecycle;
     32 
     33 import org.junit.Before;
     34 import org.junit.Test;
     35 import org.junit.runner.RunWith;
     36 import org.mockito.Mock;
     37 import org.mockito.MockitoAnnotations;
     38 import org.robolectric.RuntimeEnvironment;
     39 
     40 @RunWith(SettingsRobolectricTestRunner.class)
     41 public class FeatureFlagPreferenceControllerTest {
     42 
     43     @Mock
     44     private PreferenceScreen mScreen;
     45     private Context mContext;
     46     private LifecycleOwner mLifecycleOwner;
     47     private Lifecycle mLifecycle;
     48     private FeatureFlagsPreferenceController mController;
     49 
     50     @Before
     51     public void setUp() {
     52         MockitoAnnotations.initMocks(this);
     53         mContext = RuntimeEnvironment.application;
     54         mLifecycleOwner = () -> mLifecycle;
     55         mLifecycle = new Lifecycle(mLifecycleOwner);
     56         mController = new FeatureFlagsPreferenceController(mContext, mLifecycle);
     57         when(mScreen.getContext()).thenReturn(mContext);
     58         mController.displayPreference(mScreen);
     59     }
     60 
     61     @Test
     62     public void verifyConstants() {
     63         assertThat(mController.isAvailable()).isTrue();
     64         assertThat(mController.getPreferenceKey()).isNull();
     65     }
     66 
     67     @Test
     68     public void onStart_shouldRefreshFeatureFlags() {
     69         mLifecycle.handleLifecycleEvent(ON_START);
     70 
     71         verify(mScreen).removeAll();
     72         verify(mScreen, atLeastOnce()).addPreference(any(FeatureFlagPreference.class));
     73     }
     74 }
     75