• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.datausage;
17 
18 import com.android.internal.logging.nano.MetricsProto;
19 import android.content.Context;
20 import android.content.pm.ApplicationInfo;
21 import android.os.Process;
22 import com.android.settings.SettingsRobolectricTestRunner;
23 import com.android.settings.TestConfig;
24 import com.android.settings.testutils.FakeFeatureFactory;
25 import com.android.settingslib.applications.ApplicationsState;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Answers;
30 import org.mockito.Mock;
31 import org.mockito.MockitoAnnotations;
32 import org.robolectric.annotation.Config;
33 
34 import static com.google.common.truth.Truth.assertThat;
35 import static org.mockito.Matchers.any;
36 import static org.mockito.Matchers.eq;
37 import static org.mockito.Mockito.verify;
38 
39 @RunWith(SettingsRobolectricTestRunner.class)
40 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
41 public class UnrestrictedDataAccessTest {
42 
43     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
44     private Context mContext;
45 
46     @Mock
47     private ApplicationsState.AppEntry mAppEntry;
48     private UnrestrictedDataAccess mFragment;
49     private FakeFeatureFactory mFeatureFactory;
50 
51     @Before
setUp()52     public void setUp() {
53         MockitoAnnotations.initMocks(this);
54         FakeFeatureFactory.setupForTest(mContext);
55         mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
56         mFragment = new UnrestrictedDataAccess();
57     }
58 
59     @Test
testShouldAddPreferenceForApps()60     public void testShouldAddPreferenceForApps() {
61         mAppEntry.info = new ApplicationInfo();
62         mAppEntry.info.uid = Process.FIRST_APPLICATION_UID + 10;
63 
64         assertThat(mFragment.shouldAddPreference(mAppEntry)).isTrue();
65     }
66 
67     @Test
testShouldNotAddPreferenceForNonApps()68     public void testShouldNotAddPreferenceForNonApps() {
69         mAppEntry.info = new ApplicationInfo();
70         mAppEntry.info.uid = Process.FIRST_APPLICATION_UID - 10;
71 
72         assertThat(mFragment.shouldAddPreference(mAppEntry)).isFalse();
73     }
74 
75     @Test
logSpecialPermissionChange()76     public void logSpecialPermissionChange() {
77         mFragment.logSpecialPermissionChange(true, "app");
78         verify(mFeatureFactory.metricsFeatureProvider).action(any(Context.class),
79                 eq(MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_UNL_DATA_ALLOW), eq("app"));
80 
81         mFragment.logSpecialPermissionChange(false, "app");
82         verify(mFeatureFactory.metricsFeatureProvider).action(any(Context.class),
83                 eq(MetricsProto.MetricsEvent.APP_SPECIAL_PERMISSION_UNL_DATA_DENY), eq("app"));
84     }
85 
86 }
87