• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.settings.accounts;
17 
18 import static android.provider.Settings.Secure.MANAGED_PROFILE_CONTACT_REMOTE_SEARCH;
19 
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.verify;
25 
26 import android.content.Context;
27 import android.os.UserHandle;
28 import android.provider.Settings;
29 
30 import com.android.settingslib.RestrictedSwitchPreference;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.robolectric.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 
40 @RunWith(RobolectricTestRunner.class)
41 public class ContactSearchPreferenceControllerTest {
42 
43     private static final String PREF_KEY = "contacts_search";
44 
45     @Mock
46     private UserHandle mManagedUser;
47 
48     private Context mContext;
49     private ContactSearchPreferenceController mController;
50     private RestrictedSwitchPreference mPreference;
51 
52     @Before
setUp()53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55         mContext = RuntimeEnvironment.application;
56         mController = new ContactSearchPreferenceController(mContext, PREF_KEY);
57         mController.setManagedUser(mManagedUser);
58         mPreference = spy(new RestrictedSwitchPreference(mContext));
59     }
60 
61     @Test
getAvailabilityStatus_noManagedUser_DISABLED()62     public void getAvailabilityStatus_noManagedUser_DISABLED() {
63         mController.setManagedUser(null);
64         assertThat(mController.getAvailabilityStatus())
65                 .isNotEqualTo(ContactSearchPreferenceController.AVAILABLE);
66     }
67 
68     @Test
getAvailabilityStatus_hasManagedUser_AVAILABLE()69     public void getAvailabilityStatus_hasManagedUser_AVAILABLE() {
70         mController.setManagedUser(mManagedUser);
71         assertThat(mController.getAvailabilityStatus())
72                 .isEqualTo(ContactSearchPreferenceController.AVAILABLE);
73     }
74 
75     @Test
updateState_shouldRefreshContent()76     public void updateState_shouldRefreshContent() {
77         Settings.Secure.putIntForUser(mContext.getContentResolver(),
78                 MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 0, mManagedUser.getIdentifier());
79         mController.updateState(mPreference);
80         assertThat(mPreference.isChecked()).isFalse();
81 
82         Settings.Secure.putIntForUser(mContext.getContentResolver(),
83                 MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 1, mManagedUser.getIdentifier());
84         mController.updateState(mPreference);
85         assertThat(mPreference.isChecked()).isTrue();
86     }
87 
88     @Test
updateState_preferenceShouldBeDisabled()89     public void updateState_preferenceShouldBeDisabled() {
90         mController.updateState(mPreference);
91         verify(mPreference).setDisabledByAdmin(any());
92     }
93 
94     @Test
onPreferenceChange_shouldUpdateProviderValue()95     public void onPreferenceChange_shouldUpdateProviderValue() {
96         mController.onPreferenceChange(mPreference, false);
97         assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(),
98                 MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 1, mManagedUser.getIdentifier()))
99                 .isEqualTo(0);
100 
101         mController.onPreferenceChange(mPreference, true);
102         assertThat(Settings.Secure.getIntForUser(mContext.getContentResolver(),
103                 MANAGED_PROFILE_CONTACT_REMOTE_SEARCH, 0, mManagedUser.getIdentifier()))
104                 .isEqualTo(1);
105     }
106 }