1 /*
2  * Copyright (C) 2022 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.development;
18 
19 import static android.util.FeatureFlagUtils.SETTINGS_ENABLE_MONITOR_PHANTOM_PROCS;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import android.content.Context;
24 import android.os.Looper;
25 import android.util.FeatureFlagUtils;
26 
27 import androidx.preference.PreferenceManager;
28 import androidx.preference.PreferenceScreen;
29 import androidx.preference.SwitchPreference;
30 import androidx.test.core.app.ApplicationProvider;
31 import androidx.test.ext.junit.runners.AndroidJUnit4;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 
37 @RunWith(AndroidJUnit4.class)
38 public class PhantomProcessPreferenceControllerTest {
39 
40     private Context mContext;
41     private PhantomProcessPreferenceController mController;
42     private SwitchPreference mPreference;
43 
44     @Before
setUp()45     public void setUp() {
46         mContext = ApplicationProvider.getApplicationContext();
47         mController = new PhantomProcessPreferenceController(mContext);
48         if (Looper.myLooper() == null) {
49             Looper.prepare();
50         }
51 
52         final PreferenceManager preferenceManager = new PreferenceManager(mContext);
53         final PreferenceScreen screen = preferenceManager.createPreferenceScreen(mContext);
54         mPreference = new SwitchPreference(mContext);
55         mPreference.setKey(mController.getPreferenceKey());
56         screen.addPreference(mPreference);
57         mController.displayPreference(screen);
58     }
59 
60     @Test
onPreferenceChanged_settingDisabled_shouldNotDisablePhantomProcessMonitor()61     public void onPreferenceChanged_settingDisabled_shouldNotDisablePhantomProcessMonitor() {
62         mController.onPreferenceChange(mPreference, false /* new value */);
63 
64         final boolean mode = !FeatureFlagUtils.isEnabled(mContext,
65                 SETTINGS_ENABLE_MONITOR_PHANTOM_PROCS);
66 
67         assertThat(mode).isFalse();
68     }
69 
70     @Test
onPreferenceChanged_settingEnabled_shouldDisablePhantomProcessMonitor()71     public void onPreferenceChanged_settingEnabled_shouldDisablePhantomProcessMonitor() {
72         mController.onPreferenceChange(mPreference, true /* new value */);
73 
74         final boolean mode = !FeatureFlagUtils.isEnabled(mContext,
75                 SETTINGS_ENABLE_MONITOR_PHANTOM_PROCS);
76 
77         assertThat(mode).isTrue();
78     }
79 
80     @Test
updateState_settingEnabled_preferenceShouldBeChecked()81     public void updateState_settingEnabled_preferenceShouldBeChecked() {
82         FeatureFlagUtils.setEnabled(mContext, SETTINGS_ENABLE_MONITOR_PHANTOM_PROCS, false);
83 
84         mController.updateState(mPreference);
85         assertThat(mPreference.isChecked()).isTrue();
86     }
87 
88     @Test
updateState_settingDisabled_preferenceShouldNotBeChecked()89     public void updateState_settingDisabled_preferenceShouldNotBeChecked() {
90         FeatureFlagUtils.setEnabled(mContext, SETTINGS_ENABLE_MONITOR_PHANTOM_PROCS, true);
91 
92         mController.updateState(mPreference);
93         assertThat(mPreference.isChecked()).isFalse();
94     }
95 
96     @Test
onDeveloperOptionsDisabled_shouldDisablePreference()97     public void onDeveloperOptionsDisabled_shouldDisablePreference() {
98         mController.onDeveloperOptionsSwitchDisabled();
99         final boolean mode = !FeatureFlagUtils.isEnabled(mContext,
100                 SETTINGS_ENABLE_MONITOR_PHANTOM_PROCS);
101 
102         mController.updateState(mPreference);
103 
104         assertThat(mode).isFalse();
105         assertThat(mPreference.isChecked()).isFalse();
106     }
107 }
108