Home | History | Annotate | Download | only in gestures
      1 /*
      2  * Copyright (C) 2016 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.gestures;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 
     21 import static junit.framework.Assert.assertEquals;
     22 
     23 import static org.mockito.Matchers.anyString;
     24 import static org.mockito.Mockito.mock;
     25 import static org.mockito.Mockito.reset;
     26 import static org.mockito.Mockito.verify;
     27 import static org.mockito.Mockito.when;
     28 
     29 import android.content.Context;
     30 import android.os.Bundle;
     31 import android.support.v7.preference.Preference;
     32 import android.support.v7.preference.PreferenceScreen;
     33 import android.support.v7.preference.TwoStatePreference;
     34 
     35 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     36 import com.android.settings.widget.VideoPreference;
     37 
     38 import org.junit.Before;
     39 import org.junit.Test;
     40 import org.junit.runner.RunWith;
     41 import org.mockito.Answers;
     42 import org.mockito.Mock;
     43 import org.mockito.MockitoAnnotations;
     44 import org.robolectric.RuntimeEnvironment;
     45 
     46 @RunWith(SettingsRobolectricTestRunner.class)
     47 public class GesturePreferenceControllerTest {
     48 
     49     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     50     private Context mContext;
     51     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     52     private PreferenceScreen mScreen;
     53     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     54 
     55     private TestPrefController mController;
     56     private Preference mPreference;
     57 
     58     @Before
     59     public void setUp() {
     60         MockitoAnnotations.initMocks(this);
     61         mController = new TestPrefController(mContext, "testKey");
     62         mPreference = new Preference(RuntimeEnvironment.application);
     63         mPreference.setKey(mController.getPreferenceKey());
     64         when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
     65     }
     66 
     67     @Test
     68     public void display_configIsTrue_shouldDisplay() {
     69         mController.mIsPrefAvailable = true;
     70         when(mScreen.findPreference(anyString())).thenReturn(mock(VideoPreference.class));
     71 
     72         mController.displayPreference(mScreen);
     73 
     74         assertThat(mPreference.isVisible()).isTrue();
     75     }
     76 
     77     @Test
     78     public void display_configIsFalse_shouldNotDisplay() {
     79         mController.mIsPrefAvailable = false;
     80 
     81         mController.displayPreference(mScreen);
     82 
     83         assertThat(mPreference.isVisible()).isFalse();
     84     }
     85 
     86     @Test
     87     public void onResume_shouldStartVideoPreferenceWithVideoPauseState() {
     88         final VideoPreference videoPreference = mock(VideoPreference.class);
     89         when(mScreen.findPreference(mController.getVideoPrefKey())).thenReturn(videoPreference);
     90         mController.mIsPrefAvailable = true;
     91 
     92         mController.displayPreference(mScreen);
     93         final Bundle savedState = new Bundle();
     94 
     95         mController.onCreate(null);
     96         mController.onResume();
     97         verify(videoPreference).onViewVisible(false);
     98 
     99         reset(videoPreference);
    100         savedState.putBoolean(GesturePreferenceController.KEY_VIDEO_PAUSED, true);
    101         mController.onCreate(savedState);
    102         mController.onResume();
    103         verify(videoPreference).onViewVisible(true);
    104 
    105         reset(videoPreference);
    106         savedState.putBoolean(GesturePreferenceController.KEY_VIDEO_PAUSED, false);
    107         mController.onCreate(savedState);
    108         mController.onResume();
    109         verify(videoPreference).onViewVisible(false);
    110     }
    111 
    112     @Test
    113     public void onPause_shouldStopVideoPreference() {
    114         final VideoPreference videoPreference = mock(VideoPreference.class);
    115         when(mScreen.findPreference(mController.getVideoPrefKey())).thenReturn(videoPreference);
    116         mController.mIsPrefAvailable = true;
    117 
    118         mController.displayPreference(mScreen);
    119         mController.onPause();
    120 
    121         verify(videoPreference).onViewInvisible();
    122     }
    123 
    124     @Test
    125     public void onPause_shouldUpdateVideoPauseState() {
    126         final VideoPreference videoPreference = mock(VideoPreference.class);
    127         when(mScreen.findPreference(mController.getVideoPrefKey())).thenReturn(videoPreference);
    128         mController.mIsPrefAvailable = true;
    129         mController.displayPreference(mScreen);
    130 
    131         when(videoPreference.isVideoPaused()).thenReturn(true);
    132         mController.onPause();
    133         assertThat(mController.mVideoPaused).isTrue();
    134 
    135         when(videoPreference.isVideoPaused()).thenReturn(false);
    136         mController.onPause();
    137         assertThat(mController.mVideoPaused).isFalse();
    138     }
    139 
    140     @Test
    141     public void onSaveInstanceState_shouldSaveVideoPauseState() {
    142         final Bundle outState = mock(Bundle.class);
    143 
    144         mController.mVideoPaused = true;
    145         mController.onSaveInstanceState(outState);
    146         verify(outState).putBoolean(GesturePreferenceController.KEY_VIDEO_PAUSED, true);
    147 
    148         mController.mVideoPaused = false;
    149         mController.onSaveInstanceState(outState);
    150         verify(outState).putBoolean(GesturePreferenceController.KEY_VIDEO_PAUSED, false);
    151     }
    152 
    153     @Test
    154     public void updateState_preferenceSetCheckedWhenSettingIsOn() {
    155         // Mock a TwoStatePreference
    156         final TwoStatePreference preference = mock(TwoStatePreference.class);
    157         // Set the setting to be enabled.
    158         mController.mIsPrefEnabled = true;
    159         // Run through updateState
    160         mController.updateState(preference);
    161 
    162         // Verify pref is checked (as setting is enabled).
    163         verify(preference).setChecked(true);
    164     }
    165 
    166     @Test
    167     public void updateState_preferenceSetUncheckedWhenSettingIsOff() {
    168         // Mock a TwoStatePreference
    169         final TwoStatePreference preference = mock(TwoStatePreference.class);
    170         // Set the setting to be disabled.
    171         mController.mIsPrefEnabled = false;
    172 
    173         // Run through updateState
    174         mController.updateState(preference);
    175 
    176         // Verify pref is unchecked (as setting is disabled).
    177         verify(preference).setChecked(false);
    178     }
    179 
    180     @Test
    181     public void updateState_notTwoStatePreference_setSummary() {
    182         // Mock a regular preference
    183         final Preference preference = mock(Preference.class);
    184         // Set the setting to be disabled.
    185         mController.mIsPrefEnabled = false;
    186 
    187         // Run through updateState
    188         mController.updateState(preference);
    189 
    190         // Verify summary is set to off (as setting is disabled).
    191         assertThat(preference.getSummary()).isEqualTo(
    192                 mContext.getString(com.android.settings.R.string.gesture_setting_off));
    193     }
    194 
    195     private class TestPrefController extends GesturePreferenceController {
    196 
    197         boolean mIsPrefAvailable;
    198         boolean mIsPrefEnabled;
    199 
    200         private TestPrefController(Context context,
    201                 String key) {
    202             super(context, key);
    203         }
    204 
    205         @Override
    206         public int getAvailabilityStatus() {
    207             return mIsPrefAvailable ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
    208         }
    209 
    210         @Override
    211         protected String getVideoPrefKey() {
    212             return "videoKey";
    213         }
    214 
    215         @Override
    216         public boolean isChecked() {
    217             return mIsPrefEnabled;
    218         }
    219 
    220         @Override
    221         public boolean setChecked(boolean isChecked) {
    222             return false;
    223         }
    224     }
    225 }
    226