Home | History | Annotate | Download | only in notification
      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.notification;
     18 
     19 import android.app.Activity;
     20 import android.content.ContentResolver;
     21 import android.content.Context;
     22 import android.provider.Settings.Global;
     23 import android.support.v7.preference.PreferenceScreen;
     24 
     25 import com.android.settings.SettingsPreferenceFragment;
     26 import com.android.settings.SettingsRobolectricTestRunner;
     27 import com.android.settings.TestConfig;
     28 import com.android.settings.core.lifecycle.Lifecycle;
     29 
     30 import java.util.ArrayList;
     31 import java.util.List;
     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.annotation.Config;
     39 import org.robolectric.shadows.ShadowApplication;
     40 
     41 import static com.android.settings.notification.SettingPref.TYPE_GLOBAL;
     42 import static com.google.common.truth.Truth.assertThat;
     43 import static org.mockito.Answers.RETURNS_DEEP_STUBS;
     44 import static org.mockito.Mockito.doReturn;
     45 import static org.mockito.Mockito.spy;
     46 import static org.mockito.Mockito.verify;
     47 import static org.mockito.Mockito.when;
     48 
     49 @RunWith(SettingsRobolectricTestRunner.class)
     50 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
     51 public class SettingPrefControllerTest {
     52 
     53     @Mock
     54     private PreferenceScreen mScreen;
     55     @Mock
     56     private SoundSettings mSetting;
     57     @Mock
     58     private Activity mActivity;
     59     @Mock
     60     private ContentResolver mContentResolver;
     61 
     62     private Context mContext;
     63     private PreferenceControllerTestable mController;
     64     private SettingPref mPreference;
     65 
     66     @Before
     67     public void setUp() {
     68         MockitoAnnotations.initMocks(this);
     69         mContext = spy(ShadowApplication.getInstance().getApplicationContext());
     70         when(mContext.getContentResolver()).thenReturn(mContentResolver);
     71         when(mSetting.getActivity()).thenReturn(mActivity);
     72         doReturn(mScreen).when(mSetting).getPreferenceScreen();
     73         mController = new PreferenceControllerTestable(mContext, mSetting, null);
     74         mPreference = mController.getPref();
     75     }
     76 
     77     @Test
     78     public void displayPreference_shouldInitPreference() {
     79         mController.displayPreference(mScreen);
     80 
     81         verify(mPreference).init(mSetting);
     82     }
     83 
     84     @Test
     85     public void isAvailable_shouldCallisApplicable() {
     86         mController.isAvailable();
     87 
     88         verify(mPreference).isApplicable(mContext);
     89     }
     90 
     91     @Test
     92     public void getPreferenceKey_shouldReturnPrefKey() {
     93         assertThat(mController.getPreferenceKey()).isEqualTo(mController.KEY_TEST);
     94     }
     95 
     96     @Test
     97     public void updateState_shouldUpdatePreference() {
     98         mController.updateState(null);
     99 
    100         verify(mPreference).update(mContext);
    101     }
    102 
    103     @Test
    104     public void onResume_shouldRegisterContentObserver() {
    105         mController.displayPreference(mScreen);
    106         mController.onResume();
    107 
    108         verify(mContentResolver).registerContentObserver(
    109             Global.getUriFor("Setting1"), false, mController.getObserver());
    110     }
    111 
    112     @Test
    113     public void onPause_shouldUnregisterContentObserver() {
    114         mController.displayPreference(mScreen);
    115         mController.onPause();
    116 
    117         verify(mContentResolver).unregisterContentObserver(mController.getObserver());
    118     }
    119 
    120     @Test
    121     public void onContentChange_shouldUpdatePreference() {
    122         mController.displayPreference(mScreen);
    123         mController.onResume();
    124         mController.getObserver().onChange(false, Global.getUriFor("Setting1"));
    125 
    126         verify(mPreference).update(mContext);
    127     }
    128 
    129     @Test
    130     public void updateNonIndexableKeys_applicable_shouldNotUpdate() {
    131         final List<String> keys = new ArrayList<>();
    132 
    133         mController.updateNonIndexableKeys(keys);
    134 
    135         assertThat(keys).isEmpty();
    136     }
    137 
    138     @Test
    139     public void updateNonIndexableKeys_notApplicable_shouldUpdate() {
    140         mController.setApplicable(false);
    141         final List<String> keys = new ArrayList<>();
    142 
    143         mController.updateNonIndexableKeys(keys);
    144 
    145         assertThat(keys).isNotEmpty();
    146     }
    147 
    148     private class PreferenceControllerTestable extends SettingPrefController {
    149 
    150         private static final String KEY_TEST = "key1";
    151         private boolean mApplicable = true;
    152 
    153         public PreferenceControllerTestable(Context context, SettingsPreferenceFragment parent,
    154             Lifecycle lifecycle) {
    155             super(context, parent, lifecycle);
    156             mPreference = spy(new SettingPref(
    157                 TYPE_GLOBAL, KEY_TEST, "Setting1", 1) {
    158                 @Override
    159                 public boolean isApplicable(Context context) {
    160                     return mApplicable;
    161                 }
    162             });
    163         }
    164 
    165         SettingPref getPref() {
    166             return mPreference;
    167         }
    168 
    169         PreferenceControllerTestable.SettingsObserver getObserver() {
    170             return mSettingsObserver;
    171         }
    172 
    173         void setApplicable(boolean applicable) {
    174             mApplicable = applicable;
    175         }
    176 
    177     }
    178 
    179 }
    180