• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Mockito.when;
21 
22 import android.content.Context;
23 import android.support.v7.preference.Preference;
24 
25 import com.android.settings.R;
26 import com.android.settings.testutils.FakeFeatureFactory;
27 import com.android.settings.testutils.SettingsRobolectricTestRunner;
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 
36 @RunWith(SettingsRobolectricTestRunner.class)
37 public class EnterprisePrivacyPreferenceControllerTest {
38 
39     private static final String MANAGED_GENERIC = "managed by organization";
40     private static final String MANAGED_WITH_NAME = "managed by Foo, Inc.";
41     private static final String MANAGING_ORGANIZATION = "Foo, Inc.";
42     private static final String KEY_ENTERPRISE_PRIVACY = "enterprise_privacy";
43 
44     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
45     private Context mContext;
46     private FakeFeatureFactory mFeatureFactory;
47 
48     private EnterprisePrivacyPreferenceController mController;
49 
50     @Before
setUp()51     public void setUp() {
52         MockitoAnnotations.initMocks(this);
53         mFeatureFactory = FakeFeatureFactory.setupForTest();
54         mController = new EnterprisePrivacyPreferenceController(mContext);
55     }
56 
57     @Test
testUpdateState()58     public void testUpdateState() {
59         final Preference preference = new Preference(mContext, null, 0, 0);
60 
61         when(mContext.getString(R.string.enterprise_privacy_settings_summary_generic))
62                 .thenReturn(MANAGED_GENERIC);
63         when(mFeatureFactory.enterprisePrivacyFeatureProvider.getDeviceOwnerOrganizationName())
64                 .thenReturn(null);
65         mController.updateState(preference);
66         assertThat(preference.getSummary()).isEqualTo(MANAGED_GENERIC);
67 
68         when(mContext.getResources().getString(
69                 R.string.enterprise_privacy_settings_summary_with_name, MANAGING_ORGANIZATION))
70                 .thenReturn(MANAGED_WITH_NAME);
71         when(mFeatureFactory.enterprisePrivacyFeatureProvider.getDeviceOwnerOrganizationName())
72                 .thenReturn(MANAGING_ORGANIZATION);
73         mController.updateState(preference);
74         assertThat(preference.getSummary()).isEqualTo(MANAGED_WITH_NAME);
75     }
76 
77     @Test
testIsAvailable()78     public void testIsAvailable() {
79         when(mFeatureFactory.enterprisePrivacyFeatureProvider.hasDeviceOwner()).thenReturn(false);
80         assertThat(mController.isAvailable()).isFalse();
81 
82         when(mFeatureFactory.enterprisePrivacyFeatureProvider.hasDeviceOwner()).thenReturn(true);
83         assertThat(mController.isAvailable()).isTrue();
84     }
85 
86     @Test
testHandlePreferenceTreeClick()87     public void testHandlePreferenceTreeClick() {
88         assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
89                 .isFalse();
90     }
91 
92     @Test
testGetPreferenceKey()93     public void testGetPreferenceKey() {
94         assertThat(mController.getPreferenceKey()).isEqualTo(KEY_ENTERPRISE_PRIVACY);
95     }
96 }
97