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.content.ContentResolver;
     20 import android.content.Context;
     21 import android.provider.Settings;
     22 import android.support.v7.preference.Preference;
     23 import android.support.v7.preference.PreferenceScreen;
     24 import android.support.v7.preference.TwoStatePreference;
     25 
     26 import com.android.settings.TestConfig;
     27 import com.android.settings.search.InlinePayload;
     28 import com.android.settings.search.InlineSwitchPayload;
     29 import com.android.settings.testutils.shadow.ShadowSecureSettings;
     30 
     31 import org.junit.Before;
     32 import org.junit.Test;
     33 import org.junit.runner.RunWith;
     34 import org.mockito.Answers;
     35 import org.mockito.Mock;
     36 import org.mockito.MockitoAnnotations;
     37 import org.robolectric.RobolectricTestRunner;
     38 import org.robolectric.annotation.Config;
     39 import org.robolectric.shadows.ShadowApplication;
     40 
     41 import static android.provider.Settings.Secure.NOTIFICATION_BADGING;
     42 
     43 import static com.google.common.truth.Truth.assertThat;
     44 
     45 import static org.mockito.Mockito.any;
     46 import static org.mockito.Mockito.mock;
     47 import static org.mockito.Mockito.never;
     48 import static org.mockito.Mockito.verify;
     49 import static org.mockito.Mockito.when;
     50 
     51 @RunWith(RobolectricTestRunner.class)
     52 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
     53 public class BadgingNotificationPreferenceControllerTest {
     54 
     55     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     56     private Context mContext;
     57     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     58     private PreferenceScreen mScreen;
     59 
     60     private BadgingNotificationPreferenceController mController;
     61 
     62     @Before
     63     public void setUp() {
     64         MockitoAnnotations.initMocks(this);
     65         mController = new BadgingNotificationPreferenceController(mContext);
     66     }
     67 
     68     @Test
     69     public void display_configIsTrue_shouldDisplay() {
     70         when(mContext.getResources().
     71                 getBoolean(com.android.internal.R.bool.config_notificationBadging))
     72                 .thenReturn(true);
     73         mController.displayPreference(mScreen);
     74 
     75         verify(mScreen, never()).removePreference(any(Preference.class));
     76     }
     77 
     78     @Test
     79     public void display_configIsFalse_shouldNotDisplay() {
     80         when(mContext.getResources().
     81                 getBoolean(com.android.internal.R.bool.config_notificationBadging))
     82                 .thenReturn(false);
     83         final Preference preference = mock(Preference.class);
     84         when(mScreen.getPreferenceCount()).thenReturn(1);
     85         when(mScreen.getPreference(0)).thenReturn(preference);
     86         when(preference.getKey()).thenReturn(mController.getPreferenceKey());
     87 
     88         mController.displayPreference(mScreen);
     89 
     90         verify(mScreen).removePreference(any(Preference.class));
     91     }
     92 
     93     @Test
     94     public void updateState_preferenceSetCheckedWhenSettingIsOn() {
     95         final TwoStatePreference preference = mock(TwoStatePreference.class);
     96         final Context context = ShadowApplication.getInstance().getApplicationContext();
     97         Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_BADGING, 1);
     98 
     99         mController = new BadgingNotificationPreferenceController(context);
    100         mController.updateState(preference);
    101 
    102         verify(preference).setChecked(true);
    103     }
    104 
    105     @Test
    106     public void updateState_preferenceSetUncheckedWhenSettingIsOff() {
    107         final TwoStatePreference preference = mock(TwoStatePreference.class);
    108         final Context context = ShadowApplication.getInstance().getApplicationContext();
    109         Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_BADGING, 0);
    110 
    111         mController = new BadgingNotificationPreferenceController(context);
    112         mController.updateState(preference);
    113 
    114         verify(preference).setChecked(false);
    115     }
    116 
    117     @Test
    118     public void testPreferenceController_ProperResultPayloadType() {
    119         assertThat(mController.getResultPayload()).isInstanceOf(InlineSwitchPayload.class);
    120     }
    121 
    122     @Test
    123     @Config(shadows = ShadowSecureSettings.class)
    124     public void testSetValue_updatesCorrectly() {
    125         int newValue = 0;
    126         ContentResolver resolver = mContext.getContentResolver();
    127         Settings.Secure.putInt(resolver, Settings.Secure.NOTIFICATION_BADGING, 1);
    128 
    129         ((InlinePayload) mController.getResultPayload()).setValue(mContext, newValue);
    130         int updatedValue = Settings.Secure.getInt(resolver, Settings.Secure.NOTIFICATION_BADGING,
    131                 1);
    132 
    133         assertThat(updatedValue).isEqualTo(newValue);
    134     }
    135 
    136     @Test
    137     @Config(shadows = ShadowSecureSettings.class)
    138     public void testGetValue_correctValueReturned() {
    139         int currentValue = 1;
    140         ContentResolver resolver = mContext.getContentResolver();
    141         Settings.Secure.putInt(resolver, Settings.Secure.NOTIFICATION_BADGING, currentValue);
    142 
    143         int newValue = ((InlinePayload) mController.getResultPayload()).getValue(mContext);
    144 
    145         assertThat(newValue).isEqualTo(currentValue);
    146     }
    147 }
    148