VrDisplayPreferencePickerTest.java revision a0c2c11bbfe0c4643b2f645bbf883b918f4ba857
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
17package com.android.settings.display;
18
19import static com.google.common.truth.Truth.assertThat;
20import static org.mockito.Mockito.doReturn;
21import static org.mockito.Mockito.spy;
22
23import android.content.ContentResolver;
24import android.os.UserHandle;
25import android.provider.Settings;
26
27import com.android.internal.logging.nano.MetricsProto;
28import com.android.settings.SettingsRobolectricTestRunner;
29import com.android.settings.TestConfig;
30import com.android.settings.testutils.shadow.ShadowSecureSettings;
31
32import org.junit.Before;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35import org.mockito.MockitoAnnotations;
36import org.robolectric.RuntimeEnvironment;
37import org.robolectric.annotation.Config;
38
39import java.util.List;
40
41@RunWith(SettingsRobolectricTestRunner.class)
42@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
43public class VrDisplayPreferencePickerTest {
44
45    private VrDisplayPreferencePicker mPicker;
46
47    @Before
48    public void setUp() {
49        MockitoAnnotations.initMocks(this);
50
51        mPicker = spy(new VrDisplayPreferencePicker());
52        doReturn(RuntimeEnvironment.application).when(mPicker).getContext();
53    }
54
55    @Test
56    public void verifyMetricsConstant() {
57        assertThat(mPicker.getMetricsCategory())
58                .isEqualTo(MetricsProto.MetricsEvent.VR_DISPLAY_PREFERENCE);
59    }
60
61    @Test
62    public void getCandidates_shouldReturnTwoCandidates() {
63        List<VrDisplayPreferencePicker.VrCandidateInfo> candidates = mPicker.getCandidates();
64
65        assertThat(candidates.size()).isEqualTo(2);
66        assertThat(candidates.get(0).getKey())
67                .isEqualTo(VrDisplayPreferencePicker.PREF_KEY_PREFIX + 0);
68        assertThat(candidates.get(1).getKey())
69                .isEqualTo(VrDisplayPreferencePicker.PREF_KEY_PREFIX + 1);
70    }
71
72    @Test
73    @Config(shadows = ShadowSecureSettings.class)
74    public void getKey_shouldGetFromSettingsProvider() {
75        final ContentResolver cr = RuntimeEnvironment.application.getContentResolver();
76        Settings.Secure.putIntForUser(cr, Settings.Secure.VR_DISPLAY_MODE, 1,
77                UserHandle.myUserId());
78
79        assertThat(mPicker.getDefaultKey())
80                .isEqualTo(VrDisplayPreferencePicker.PREF_KEY_PREFIX + 1);
81    }
82}
83