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.enterprise;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.support.v7.preference.Preference;
22 
23 import com.android.settings.R;
24 import com.android.settings.SettingsRobolectricTestRunner;
25 import com.android.settings.TestConfig;
26 import com.android.settings.core.PreferenceAvailabilityObserver;
27 import com.android.settings.testutils.FakeFeatureFactory;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Answers;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.robolectric.annotation.Config;
36 
37 import static com.google.common.truth.Truth.assertThat;
38 import static org.mockito.Mockito.verify;
39 import static org.mockito.Mockito.when;
40 
41 /**
42  * Tests for {@link EnterprisePrivacyPreferenceController}.
43  */
44 @RunWith(SettingsRobolectricTestRunner.class)
45 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
46 public final class EnterprisePrivacyPreferenceControllerTest {
47 
48     private static final String MANAGED_GENERIC = "managed by organization";
49     private static final String MANAGED_WITH_NAME = "managed by Foo, Inc.";
50     private static final String MANAGING_ORGANIZATION = "Foo, Inc.";
51     private static final String KEY_ENTERPRISE_PRIVACY = "enterprise_privacy";
52 
53     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
54     private Context mContext;
55     private FakeFeatureFactory mFeatureFactory;
56     @Mock private PreferenceAvailabilityObserver mObserver;
57 
58     private EnterprisePrivacyPreferenceController mController;
59 
60     @Before
setUp()61     public void setUp() {
62         MockitoAnnotations.initMocks(this);
63         FakeFeatureFactory.setupForTest(mContext);
64         mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
65         mController = new EnterprisePrivacyPreferenceController(mContext, null /* lifecycle */);
66         mController.setAvailabilityObserver(mObserver);
67     }
68 
69     @Test
testGetAvailabilityObserver()70     public void testGetAvailabilityObserver() {
71         assertThat(mController.getAvailabilityObserver()).isEqualTo(mObserver);
72     }
73 
74     @Test
testUpdateState()75     public void testUpdateState() {
76         final Preference preference = new Preference(mContext, null, 0, 0);
77 
78         when(mContext.getString(R.string.enterprise_privacy_settings_summary_generic))
79                 .thenReturn(MANAGED_GENERIC);
80         when(mFeatureFactory.enterprisePrivacyFeatureProvider.getDeviceOwnerOrganizationName())
81                 .thenReturn(null);
82         mController.updateState(preference);
83         assertThat(preference.getSummary()).isEqualTo(MANAGED_GENERIC);
84 
85         when(mContext.getResources().getString(
86                 R.string.enterprise_privacy_settings_summary_with_name, MANAGING_ORGANIZATION))
87                 .thenReturn(MANAGED_WITH_NAME);
88         when(mFeatureFactory.enterprisePrivacyFeatureProvider.getDeviceOwnerOrganizationName())
89                 .thenReturn(MANAGING_ORGANIZATION);
90         mController.updateState(preference);
91         assertThat(preference.getSummary()).isEqualTo(MANAGED_WITH_NAME);
92       }
93 
94     @Test
testIsAvailable()95     public void testIsAvailable() {
96         when(mFeatureFactory.enterprisePrivacyFeatureProvider.hasDeviceOwner()).thenReturn(false);
97         assertThat(mController.isAvailable()).isFalse();
98         verify(mObserver).onPreferenceAvailabilityUpdated(KEY_ENTERPRISE_PRIVACY, false);
99 
100         when(mFeatureFactory.enterprisePrivacyFeatureProvider.hasDeviceOwner()).thenReturn(true);
101         assertThat(mController.isAvailable()).isTrue();
102         verify(mObserver).onPreferenceAvailabilityUpdated(KEY_ENTERPRISE_PRIVACY, true);
103     }
104 
105     @Test
testHandlePreferenceTreeClick()106     public void testHandlePreferenceTreeClick() {
107         assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
108                 .isFalse();
109     }
110 
111     @Test
testGetPreferenceKey()112     public void testGetPreferenceKey() {
113         assertThat(mController.getPreferenceKey()).isEqualTo(KEY_ENTERPRISE_PRIVACY);
114     }
115 }
116