1 /*
2  * Copyright (C) 2019 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 static android.provider.Settings.Secure.NOTIFICATION_DISMISS_RTL;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.provider.Settings;
27 
28 import com.android.settings.R;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Answers;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.robolectric.RobolectricTestRunner;
37 import org.robolectric.RuntimeEnvironment;
38 
39 import androidx.preference.ListPreference;
40 import androidx.preference.PreferenceScreen;
41 
42 @RunWith(RobolectricTestRunner.class)
43 public class SwipeDirectionPreferenceControllerTest {
44 
45     private Context mContext;
46     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
47     private PreferenceScreen mScreen;
48 
49     private SwipeDirectionPreferenceController mController;
50     private ListPreference mPreference;
51 
52     private static final String KEY = "swipe";
53 
54     @Before
setUp()55     public void setUp() {
56         MockitoAnnotations.initMocks(this);
57         mContext = RuntimeEnvironment.application;
58         mController = new SwipeDirectionPreferenceController(mContext, KEY);
59         mPreference = new ListPreference(RuntimeEnvironment.application);
60         mPreference.setKey(mController.getPreferenceKey());
61         when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
62     }
63 
64     @Test
display_shouldDisplay()65     public void display_shouldDisplay() {
66         assertThat(mPreference.isVisible()).isTrue();
67     }
68 
69     @Test
updateState_SettingIsOn()70     public void updateState_SettingIsOn() {
71         Settings.Secure.putInt(mContext.getContentResolver(),
72                 NOTIFICATION_DISMISS_RTL,
73                 1);
74 
75         mController.updateState(mPreference);
76 
77         assertThat(mPreference.getValue()).isEqualTo("1");
78     }
79 
80     @Test
updateState_SettingIsOff()81     public void updateState_SettingIsOff() {
82         Settings.Secure.putInt(mContext.getContentResolver(),
83                 NOTIFICATION_DISMISS_RTL,
84                 0);
85 
86         mController.updateState(mPreference);
87 
88         assertThat(mPreference.getValue()).isEqualTo("0");
89     }
90 
91     @Test
onPreferenceChange_LTR()92     public void onPreferenceChange_LTR() {
93         Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_DISMISS_RTL, 1);
94 
95         mController.onPreferenceChange(mPreference, "0");
96 
97         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
98                 NOTIFICATION_DISMISS_RTL, 1)).isEqualTo(0);
99 
100         assertThat(mPreference.getSummary()).isEqualTo(
101                 mContext.getResources().getStringArray(R.array.swipe_direction_titles)[1]);
102     }
103 
104     @Test
onPreferenceChange_On()105     public void onPreferenceChange_On() {
106         Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_DISMISS_RTL, 0);
107 
108         mController.onPreferenceChange(mPreference, "1");
109 
110         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
111                 NOTIFICATION_DISMISS_RTL, 0)).isEqualTo(1);
112 
113         assertThat(mPreference.getSummary()).isEqualTo(
114                 mContext.getResources().getStringArray(R.array.swipe_direction_titles)[0]);
115     }
116 }
117