blob: b0aaa256fdab2bdc97b3bf1231e2228dc5d28d0b [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;
Tony Mantlerc48ba6a2017-07-31 10:58:53 -070020import static org.mockito.Mockito.atLeastOnce;
21import static org.mockito.Mockito.doReturn;
22import static org.mockito.Mockito.verify;
23
Aurimas Liutikasfd52c142018-04-17 09:50:46 -070024import androidx.lifecycle.LifecycleOwner;
Tony Mantlerc48ba6a2017-07-31 10:58:53 -070025import android.os.SystemProperties;
Aurimas Liutikasfd52c142018-04-17 09:50:46 -070026import androidx.preference.ListPreference;
27import androidx.preference.Preference;
28import androidx.preference.PreferenceScreen;
Tony Mantlerc48ba6a2017-07-31 10:58:53 -070029
30import com.android.settingslib.SettingsLibRobolectricTestRunner;
Tony Mantlerc48ba6a2017-07-31 10:58:53 -070031import com.android.settingslib.core.lifecycle.Lifecycle;
32
33import org.junit.Before;
34import org.junit.Test;
35import org.junit.runner.RunWith;
36import org.mockito.Mock;
37import org.mockito.MockitoAnnotations;
38import org.robolectric.RuntimeEnvironment;
Tony Mantlerc48ba6a2017-07-31 10:58:53 -070039
40@RunWith(SettingsLibRobolectricTestRunner.class)
Tony Mantlerc48ba6a2017-07-31 10:58:53 -070041public class LogpersistPreferenceControllerTest {
42
Fan Zhang0cc61642018-01-16 14:31:51 -080043 private LifecycleOwner mLifecycleOwner;
Tony Mantlerece840b2017-11-10 13:16:59 -080044 private Lifecycle mLifecycle;
Tony Mantlerc48ba6a2017-07-31 10:58:53 -070045
46 @Mock
47 private ListPreference mListPreference;
48 @Mock
49 private PreferenceScreen mPreferenceScreen;
50
51 private AbstractLogpersistPreferenceController mController;
52
53 @Before
54 public void setUp() {
55 MockitoAnnotations.initMocks(this);
56 SystemProperties.set("ro.debuggable", "1");
Fan Zhang0cc61642018-01-16 14:31:51 -080057 mLifecycleOwner = () -> mLifecycle;
58 mLifecycle = new Lifecycle(mLifecycleOwner);
Tony Mantlerc48ba6a2017-07-31 10:58:53 -070059 mController = new AbstractLogpersistPreferenceController(RuntimeEnvironment.application,
60 mLifecycle) {
61 @Override
Tony Mantler265e7fb2017-08-09 14:18:19 -070062 public void showConfirmationDialog(Preference preference) {}
Tony Mantlerc48ba6a2017-07-31 10:58:53 -070063
Tony Mantler265e7fb2017-08-09 14:18:19 -070064 @Override
65 public void dismissConfirmationDialog() {}
66
67 @Override
68 public boolean isConfirmationDialogShowing() {
69 return false;
Tony Mantlerc48ba6a2017-07-31 10:58:53 -070070 }
71 };
72
73 doReturn(mListPreference).when(mPreferenceScreen)
74 .findPreference(mController.getPreferenceKey());
75
76 mController.displayPreference(mPreferenceScreen);
77 }
78
79 @Test
80 public void testAvailable() {
81 SystemProperties.set("ro.debuggable", "");
82 assertThat(mController.isAvailable()).isFalse();
83 SystemProperties.set("ro.debuggable", "1");
84 assertThat(mController.isAvailable()).isTrue();
85 SystemProperties.set("ro.debuggable", "0");
86 assertThat(mController.isAvailable()).isFalse();
87 }
88
89 @Test
90 public void testUpdateLogpersistValues_null() {
91 SystemProperties.set(
92 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY,
93 AbstractLogpersistPreferenceController.SELECT_LOGPERSIST_PROPERTY_SERVICE);
94 mController.updateLogpersistValues();
95 verify(mListPreference, atLeastOnce()).setValue("all");
96 }
97
98 @Test
99 public void testUpdateLogpersistValues_all() {
100 SystemProperties.set(
101 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY,
102 AbstractLogpersistPreferenceController.SELECT_LOGPERSIST_PROPERTY_SERVICE);
103 SystemProperties.set(
104 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY_BUFFER,
105 "all");
106 mController.updateLogpersistValues();
107 verify(mListPreference, atLeastOnce()).setValue("all");
108 }
109
110 @Test
111 public void testUpdateLogpersistValues_defaultSecurityKernel() {
112 SystemProperties.set(
113 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY,
114 AbstractLogpersistPreferenceController.SELECT_LOGPERSIST_PROPERTY_SERVICE);
115 SystemProperties.set(
116 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY_BUFFER,
117 "default,security,kernel");
118 mController.updateLogpersistValues();
119 verify(mListPreference, atLeastOnce()).setValue("default,security,kernel");
120 }
121
122 @Test
123 public void testUpdateLogpersistValues_kernel() {
124 SystemProperties.set(
125 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY,
126 AbstractLogpersistPreferenceController.SELECT_LOGPERSIST_PROPERTY_SERVICE);
127 SystemProperties.set(
128 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY_BUFFER,
129 "kernel");
130 mController.updateLogpersistValues();
131 verify(mListPreference, atLeastOnce()).setValue("kernel");
132 }
133
134 @Test
135 public void testUpdateLogpersistValues_mainSecuritykernel() {
136 SystemProperties.set(
137 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY,
138 AbstractLogpersistPreferenceController.SELECT_LOGPERSIST_PROPERTY_SERVICE);
139 SystemProperties.set(
140 AbstractLogpersistPreferenceController.ACTUAL_LOGPERSIST_PROPERTY_BUFFER,
141 "main,security,kernel");
142 mController.updateLogpersistValues();
143 verify(mListPreference, atLeastOnce()).setValue("all");
144 }
145}
146