Home | History | Annotate | Download | only in display
      1 /*
      2  * Copyright (C) 2018 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.display;
     18 
     19 import static android.provider.Settings.Secure.THEME_MODE;
     20 
     21 import static com.google.common.truth.Truth.assertThat;
     22 
     23 import static org.mockito.ArgumentMatchers.eq;
     24 import static org.mockito.Matchers.anyString;
     25 import static org.mockito.Mockito.spy;
     26 import static org.mockito.Mockito.verify;
     27 import static org.mockito.Mockito.when;
     28 
     29 import android.content.Context;
     30 import android.provider.Settings;
     31 import android.support.v7.preference.ListPreference;
     32 import android.support.v7.preference.PreferenceScreen;
     33 
     34 import com.android.settings.R;
     35 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     36 
     37 import org.junit.Before;
     38 import org.junit.Test;
     39 import org.junit.runner.RunWith;
     40 import org.mockito.Mock;
     41 import org.mockito.MockitoAnnotations;
     42 import org.robolectric.RuntimeEnvironment;
     43 
     44 @RunWith(SettingsRobolectricTestRunner.class)
     45 public class SystemUiThemePreferenceControllerTest {
     46 
     47     @Mock
     48     private PreferenceScreen mPreferenceScreen;
     49     @Mock
     50     private ListPreference mListPreference;
     51     private Context mContext;
     52     private SystemUiThemePreferenceController mController;
     53 
     54     @Before
     55     public void setUp() {
     56         MockitoAnnotations.initMocks(this);
     57         mContext = RuntimeEnvironment.application;
     58         when(mPreferenceScreen.findPreference(anyString())).thenReturn(mListPreference);
     59         CharSequence[] entries = mContext.getResources().getStringArray(
     60                 R.array.systemui_theme_entries);
     61         when(mListPreference.getEntries()).thenReturn(entries);
     62         mController = spy(new SystemUiThemePreferenceController(mContext, "systemui_theme"));
     63     }
     64 
     65     @Test
     66     public void displayPreference_readsSetting() {
     67         Settings.Secure.putInt(mContext.getContentResolver(), THEME_MODE, 2);
     68         mController.displayPreference(mPreferenceScreen);
     69         verify(mListPreference).setValue(eq("2"));
     70     }
     71 
     72     @Test
     73     public void onPreferenceChange_writesSetting() {
     74         Settings.Secure.putInt(mContext.getContentResolver(), THEME_MODE, 2);
     75         mController.displayPreference(mPreferenceScreen);
     76         mController.onPreferenceChange(mListPreference, "0");
     77         int value = Settings.Secure.getInt(mContext.getContentResolver(), THEME_MODE, 2);
     78         assertThat(value).isEqualTo(0);
     79     }
     80 
     81     @Test
     82     public void onPreferenceChange_updatesSummary() {
     83         mController.displayPreference(mPreferenceScreen);
     84         mController.onPreferenceChange(mListPreference, "0");
     85         verify(mController).getSummary();
     86     }
     87 
     88 }