1 /**
2  * Copyright (C) 2018 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 package com.android.settings.wifi;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import static org.mockito.ArgumentMatchers.eq;
21 import static org.mockito.ArgumentMatchers.nullable;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 
27 import com.android.internal.logging.nano.MetricsProto;
28 import com.android.settings.R;
29 import com.android.settings.testutils.FakeFeatureFactory;
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 ChangeWifiStateDetailsTest {
41 
42     private static final String PACKAGE_NAME = "app";
43     private FakeFeatureFactory mFeatureFactory;
44     private ChangeWifiStateDetails mFragment;
45     private Context mContext;
46 
47     @Mock
48     private AppStateChangeWifiStateBridge.WifiSettingsState mState;
49 
50     @Before
setUp()51     public void setUp() {
52         MockitoAnnotations.initMocks(this);
53 
54         mContext = RuntimeEnvironment.application;
55         mFeatureFactory = FakeFeatureFactory.setupForTest();
56         mFragment = new ChangeWifiStateDetails();
57     }
58 
59     @Test
testLogSpecialPermissionChange_setTrue_returnAllow()60     public void testLogSpecialPermissionChange_setTrue_returnAllow() {
61         mFragment.logSpecialPermissionChange(true /*newState*/, PACKAGE_NAME);
62         verify(mFeatureFactory.metricsFeatureProvider).action(nullable(Context.class),
63                 eq(MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_SETTINGS_CHANGE_ALLOW),
64                 eq(PACKAGE_NAME));
65     }
66 
67     @Test
testLogSpecialPermissionChange_setFalse_returnDeny()68     public void testLogSpecialPermissionChange_setFalse_returnDeny() {
69         mFragment.logSpecialPermissionChange(false /*newState*/, PACKAGE_NAME);
70         verify(mFeatureFactory.metricsFeatureProvider).action(nullable(Context.class),
71                 eq(MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_SETTINGS_CHANGE_DENY),
72                 eq(PACKAGE_NAME));
73     }
74 
75     @Test
testGetSummary_permissibleTrue_returnAllowed()76     public void testGetSummary_permissibleTrue_returnAllowed() {
77         when(mState.isPermissible()).thenReturn(true);
78         assertThat(ChangeWifiStateDetails.getSummary(mContext, mState))
79             .isEqualTo(mContext.getString(R.string.app_permission_summary_allowed));
80     }
81 
82     @Test
testGetSummary_permissibleFalse_returnNotAllowed()83     public void testGetSummary_permissibleFalse_returnNotAllowed() {
84         when(mState.isPermissible()).thenReturn(false);
85         assertThat(ChangeWifiStateDetails.getSummary(mContext, mState))
86             .isEqualTo(mContext.getString(R.string.app_permission_summary_not_allowed));
87     }
88 }
89