blob: 3f0eca549b40e03c6c04f7b9cf88c91353ae60f3 [file] [log] [blame]
Tony Mantlerc48ba6a2017-07-31 10:58:53 -07001/*
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.settingslib.development;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import static org.mockito.Mockito.atLeastOnce;
22import static org.mockito.Mockito.doReturn;
23import static org.mockito.Mockito.verify;
24
25import android.os.SystemProperties;
26import android.support.v7.preference.ListPreference;
Tony Mantler265e7fb2017-08-09 14:18:19 -070027import android.support.v7.preference.Preference;
Tony Mantlerc48ba6a2017-07-31 10:58:53 -070028import android.support.v7.preference.PreferenceScreen;
29
30import com.android.settingslib.SettingsLibRobolectricTestRunner;
31import com.android.settingslib.TestConfig;
32import com.android.settingslib.core.lifecycle.Lifecycle;
33
34import org.junit.Before;
35import org.junit.Test;
36import org.junit.runner.RunWith;
37import org.mockito.Mock;
38import org.mockito.MockitoAnnotations;
39import org.robolectric.RuntimeEnvironment;
40import org.robolectric.annotation.Config;
41
42@RunWith(SettingsLibRobolectricTestRunner.class)
43@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION,
44 shadows = SystemPropertiesTestImpl.class)
45public class LogpersistPreferenceControllerTest {
46
47 private Lifecycle mLifecycle = new Lifecycle();
48
49 @Mock
50 private ListPreference mListPreference;
51 @Mock
52 private PreferenceScreen mPreferenceScreen;
53
54 private AbstractLogpersistPreferenceController mController;
55
56 @Before
57 public void setUp() {
58 MockitoAnnotations.initMocks(this);
59 SystemProperties.set("ro.debuggable", "1");
60 mController = new AbstractLogpersistPreferenceController(RuntimeEnvironment.application,
61 mLifecycle) {
62 @Override
Tony Mantler265e7fb2017-08-09 14:18:19 -070063 public void showConfirmationDialog(Preference preference) {}
Tony Mantlerc48ba6a2017-07-31 10:58:53 -070064
Tony Mantler265e7fb2017-08-09 14:18:19 -070065 @Override
66 public void dismissConfirmationDialog() {}
67
68 @Override
69 public boolean isConfirmationDialogShowing() {
70 return false;
Tony Mantlerc48ba6a2017-07-31 10:58:53 -070071 }
72 };
73
74 doReturn(mListPreference).when(mPreferenceScreen)
75 .findPreference(mController.getPreferenceKey());
76
77 mController.displayPreference(mPreferenceScreen);
78 }
79
80 @Test
81 public void testAvailable() {
82 SystemProperties.set("ro.debuggable", "");
83 assertThat(mController.isAvailable()).isFalse();
84 SystemProperties.set("ro.debuggable", "1");
85 assertThat(mController.isAvailable()).isTrue();
86 SystemProperties.set("ro.debuggable", "0");
87 assertThat(mController.isAvailable()).isFalse();
88 }
89
90 @Test
91 public void testUpdateLogpersistValues_null() {
92 SystemProperties.set(
93 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY,
94 AbstractLogpersistPreferenceController.SELECT_LOGPERSIST_PROPERTY_SERVICE);
95 mController.updateLogpersistValues();
96 verify(mListPreference, atLeastOnce()).setValue("all");
97 }
98
99 @Test
100 public void testUpdateLogpersistValues_all() {
101 SystemProperties.set(
102 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY,
103 AbstractLogpersistPreferenceController.SELECT_LOGPERSIST_PROPERTY_SERVICE);
104 SystemProperties.set(
105 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY_BUFFER,
106 "all");
107 mController.updateLogpersistValues();
108 verify(mListPreference, atLeastOnce()).setValue("all");
109 }
110
111 @Test
112 public void testUpdateLogpersistValues_defaultSecurityKernel() {
113 SystemProperties.set(
114 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY,
115 AbstractLogpersistPreferenceController.SELECT_LOGPERSIST_PROPERTY_SERVICE);
116 SystemProperties.set(
117 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY_BUFFER,
118 "default,security,kernel");
119 mController.updateLogpersistValues();
120 verify(mListPreference, atLeastOnce()).setValue("default,security,kernel");
121 }
122
123 @Test
124 public void testUpdateLogpersistValues_kernel() {
125 SystemProperties.set(
126 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY,
127 AbstractLogpersistPreferenceController.SELECT_LOGPERSIST_PROPERTY_SERVICE);
128 SystemProperties.set(
129 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY_BUFFER,
130 "kernel");
131 mController.updateLogpersistValues();
132 verify(mListPreference, atLeastOnce()).setValue("kernel");
133 }
134
135 @Test
136 public void testUpdateLogpersistValues_mainSecuritykernel() {
137 SystemProperties.set(
138 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY,
139 AbstractLogpersistPreferenceController.SELECT_LOGPERSIST_PROPERTY_SERVICE);
140 SystemProperties.set(
141 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY_BUFFER,
142 "main,security,kernel");
143 mController.updateLogpersistValues();
144 verify(mListPreference, atLeastOnce()).setValue("all");
145 }
146}
147