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 com.android.settings.development.NfcSnoopLogPreferenceController
20         .NFCSNOOP_MODE_FILTERED;
21 import static com.android.settings.development.NfcSnoopLogPreferenceController
22         .NFCSNOOP_MODE_FULL;
23 import static com.android.settings.development.NfcSnoopLogPreferenceController
24         .NFC_NFCSNOOP_LOG_MODE_PROPERTY;
25 
26 import static com.google.common.truth.Truth.assertThat;
27 
28 import static org.mockito.Mockito.spy;
29 import static org.mockito.Mockito.when;
30 
31 import android.content.Context;
32 import android.os.SystemProperties;
33 
34 import androidx.preference.PreferenceScreen;
35 import androidx.preference.SwitchPreference;
36 
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.robolectric.RobolectricTestRunner;
43 import org.robolectric.RuntimeEnvironment;
44 
45 @RunWith(RobolectricTestRunner.class)
46 public class NfcSnoopLogPreferenceControllerTest {
47     @Mock
48     private PreferenceScreen mPreferenceScreen;
49     @Mock
50     private DevelopmentSettingsDashboardFragment mFragment;
51 
52     private Context mContext;
53     private SwitchPreference mPreference;
54     private NfcSnoopLogPreferenceController mController;
55 
56     @Before
setup()57     public void setup() {
58         MockitoAnnotations.initMocks(this);
59         mContext = RuntimeEnvironment.application;
60         mPreference = new SwitchPreference(mContext);
61         mController = spy(new NfcSnoopLogPreferenceController(mContext, mFragment));
62         when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
63             .thenReturn(mPreference);
64         mController.displayPreference(mPreferenceScreen);
65     }
66 
67     @Test
onNfcRebootDialogConfirmed_nfcSnoopLogDisabled_shouldChangeProperty()68     public void onNfcRebootDialogConfirmed_nfcSnoopLogDisabled_shouldChangeProperty() {
69         SystemProperties.set(NFC_NFCSNOOP_LOG_MODE_PROPERTY, NFCSNOOP_MODE_FILTERED);
70         mController.mChanged = true;
71 
72         mController.onNfcRebootDialogConfirmed();
73 
74         final String currentValue = SystemProperties.get(NFC_NFCSNOOP_LOG_MODE_PROPERTY);
75         assertThat(currentValue.equals(NFCSNOOP_MODE_FULL)).isTrue();
76     }
77 
78     @Test
onNfcRebootDialogConfirmed_nfcSnoopLogEnabled_shouldChangeProperty()79     public void onNfcRebootDialogConfirmed_nfcSnoopLogEnabled_shouldChangeProperty() {
80         SystemProperties.set(NFC_NFCSNOOP_LOG_MODE_PROPERTY, NFCSNOOP_MODE_FULL);
81         mController.mChanged = true;
82 
83         mController.onNfcRebootDialogConfirmed();
84 
85         final String currentValue = SystemProperties.get(NFC_NFCSNOOP_LOG_MODE_PROPERTY);
86         assertThat(currentValue.equals(NFCSNOOP_MODE_FILTERED)).isTrue();
87     }
88 
89     @Test
onNfcRebootDialogCanceled_shouldNotChangeProperty()90     public void onNfcRebootDialogCanceled_shouldNotChangeProperty() {
91         SystemProperties.set(NFC_NFCSNOOP_LOG_MODE_PROPERTY, NFCSNOOP_MODE_FILTERED);
92         mController.mChanged = true;
93 
94         mController.onNfcRebootDialogCanceled();
95 
96         final String currentValue = SystemProperties.get(NFC_NFCSNOOP_LOG_MODE_PROPERTY);
97         assertThat(currentValue.equals(NFCSNOOP_MODE_FILTERED)).isTrue();
98     }
99 }
100