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.users;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.when;
22 
23 import android.content.Context;
24 import android.os.UserHandle;
25 import android.view.View;
26 
27 import com.android.settings.SettingsRobolectricTestRunner;
28 import com.android.settings.TestConfig;
29 import com.android.settingslib.RestrictedPreferenceHelper;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.robolectric.RuntimeEnvironment;
37 import org.robolectric.annotation.Config;
38 import org.robolectric.util.ReflectionHelpers;
39 
40 @RunWith(SettingsRobolectricTestRunner.class)
41 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
42 public class UserPreferenceTest {
43 
44     @Mock
45     private RestrictedPreferenceHelper mRestrictedPreferenceHelper;
46     private Context mContext;
47     private UserPreference mUserPreference;
48 
49     @Before
setUp()50     public void setUp() {
51         MockitoAnnotations.initMocks(this);
52         mContext = RuntimeEnvironment.application;
53         mUserPreference = new UserPreference(mContext, null /* attrs */, UserHandle.USER_CURRENT,
54                 null /* settingsListener */, null /* deleteListener */);
55         ReflectionHelpers.setField(mUserPreference, "mHelper", mRestrictedPreferenceHelper);
56     }
57 
58     @Test
testShouldHideSecondTarget_noListener_shouldHide()59     public void testShouldHideSecondTarget_noListener_shouldHide() {
60         assertThat(mUserPreference.shouldHideSecondTarget()).isTrue();
61     }
62 
63     @Test
testShouldHideSecondTarget_disabledByAdmin_shouldHide()64     public void testShouldHideSecondTarget_disabledByAdmin_shouldHide() {
65         when(mRestrictedPreferenceHelper.isDisabledByAdmin()).thenReturn(true);
66 
67         assertThat(mUserPreference.shouldHideSecondTarget()).isTrue();
68     }
69 
70     @Test
testShouldHideSecondTarget_hasSettingListener_shouldNotHide()71     public void testShouldHideSecondTarget_hasSettingListener_shouldNotHide() {
72         ReflectionHelpers.setField(mUserPreference, "mSettingsClickListener",
73                 mock(View.OnClickListener.class));
74 
75         assertThat(mUserPreference.shouldHideSecondTarget()).isFalse();
76     }
77 }
78