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.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.when;
22 
23 import android.content.Context;
24 import android.net.ConnectivitySettingsManager;
25 
26 import androidx.preference.ListPreference;
27 import androidx.preference.PreferenceScreen;
28 
29 import com.android.settings.R;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.robolectric.RobolectricTestRunner;
37 import org.robolectric.RuntimeEnvironment;
38 
39 @RunWith(RobolectricTestRunner.class)
40 public class IngressRateLimitPreferenceControllerTest {
41     private Context mContext = RuntimeEnvironment.application;
42     private ListPreference mPreference;
43     private IngressRateLimitPreferenceController mController;
44 
45     @Mock
46     private PreferenceScreen mPreferenceScreen;
47 
48     @Before
setUp()49     public void setUp() {
50         MockitoAnnotations.initMocks(this);
51 
52         mPreference = new ListPreference(mContext);
53         mPreference.setEntries(R.array.ingress_rate_limit_entries);
54         mPreference.setEntryValues(R.array.ingress_rate_limit_values);
55 
56         mController = new IngressRateLimitPreferenceController(mContext);
57         when(mPreferenceScreen.findPreference(mController.getPreferenceKey())).thenReturn(
58                 mPreference);
59         mController.displayPreference(mPreferenceScreen);
60     }
61 
62     @Test
onPreferenceChanged_select5Mbits_shouldEnableIngressRateLimit()63     public void onPreferenceChanged_select5Mbits_shouldEnableIngressRateLimit() {
64         final long newRateLimit = 625000; // 5mbit == 625000 B/s
65         assertThat(mController.onPreferenceChange(mPreference, newRateLimit)).isTrue();
66 
67         final long configuredRateLimit =
68                 ConnectivitySettingsManager.getIngressRateLimitInBytesPerSecond(mContext);
69         assertThat(configuredRateLimit).isEqualTo(newRateLimit);
70     }
71 
72     @Test
onPreferenceChanged_selectDisabled_shouldDisableIngressRateLimit()73     public void onPreferenceChanged_selectDisabled_shouldDisableIngressRateLimit() {
74         final long disabledRateLimit = -1; // -1 == disabled
75         assertThat(mController.onPreferenceChange(mPreference, disabledRateLimit)).isTrue();
76 
77         final long configuredRateLimit =
78                 ConnectivitySettingsManager.getIngressRateLimitInBytesPerSecond(mContext);
79         assertThat(configuredRateLimit).isEqualTo(disabledRateLimit);
80     }
81 
82     @Test
onPreferenceChanged_invalidValue_returnsFalse()83     public void onPreferenceChanged_invalidValue_returnsFalse() {
84         final long invalidRateLimit = -123;
85         assertThat(mController.onPreferenceChange(mPreference, invalidRateLimit)).isFalse();
86     }
87 
88     @Test
updateState_preferenceShouldBeSelected()89     public void updateState_preferenceShouldBeSelected() {
90         final long newRateLimit = 625000; // 5mbit == 625000 B/s
91         ConnectivitySettingsManager.setIngressRateLimitInBytesPerSecond(mContext, newRateLimit);
92         mController.updateState(mPreference);
93         assertThat(Long.parseLong(mPreference.getValue())).isEqualTo(newRateLimit);
94     }
95 
96     @Test
onDeveloperOptionsSwitchDisabled_shouldDisablePreference()97     public void onDeveloperOptionsSwitchDisabled_shouldDisablePreference() {
98         final long newRateLimit = 625000; // 5mbit == 625000 B/s
99         ConnectivitySettingsManager.setIngressRateLimitInBytesPerSecond(mContext, newRateLimit);
100         mController.updateState(mPreference);
101 
102         mController.onDeveloperOptionsSwitchDisabled();
103         assertThat(Long.parseLong(mPreference.getValue())).isEqualTo(-1);
104         assertThat(ConnectivitySettingsManager.getIngressRateLimitInBytesPerSecond(
105                 mContext)).isEqualTo(-1);
106     }
107 }
108