• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 android.provider.cts;
18 
19 import static android.content.pm.PackageManager.PERMISSION_GRANTED;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import android.app.slice.Slice;
24 import android.app.slice.SliceManager;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.pm.PackageManager;
28 import android.content.pm.PackageManager.NameNotFoundException;
29 import android.content.pm.ResolveInfo;
30 import android.net.Uri;
31 import android.os.Process;
32 
33 import android.net.wifi.WifiManager;
34 import android.util.Log;
35 
36 import androidx.slice.SliceConvert;
37 import androidx.slice.SliceMetadata;
38 import androidx.slice.core.SliceAction;
39 import androidx.test.InstrumentationRegistry;
40 import androidx.test.runner.AndroidJUnit4;
41 
42 import java.util.Collections;
43 import java.util.List;
44 
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 
49 @RunWith(AndroidJUnit4.class)
50 public class WifiSliceTest {
51   private static final String TAG = "WifiSliceTest";
52 
53   private static final Uri WIFI_SLICE_URI =
54           Uri.parse("content://android.settings.slices/action/wifi");
55 
56   private static final String ACTION_ASSIST = "android.intent.action.ASSIST";
57   private static final String ACTION_VOICE_ASSIST = "android.intent.action.VOICE_ASSIST";
58   private static final String CATEGORY_DEFAULT = "android.intent.category.DEFAULT";
59   private static final String FEATURE_VOICE_RECOGNIZERS = "android.software.voice_recognizers";
60 
61   private final Context mContext = InstrumentationRegistry.getContext();
62   private final SliceManager mSliceManager = mContext.getSystemService(SliceManager.class);
63   private final boolean mHasVoiceRecognizersFeature =
64           mContext.getPackageManager().hasSystemFeature(FEATURE_VOICE_RECOGNIZERS);
65 
66   private Slice mWifiSlice;
67 
68   @Before
setUp()69   public void setUp() throws Exception {
70     mWifiSlice = mSliceManager.bindSlice(WIFI_SLICE_URI, Collections.emptySet());
71   }
72 
73   @Test
wifiSliceToggle_changeWifiState()74   public void wifiSliceToggle_changeWifiState() {
75     SliceMetadata mWifiSliceMetadata =
76             SliceMetadata.from(mContext, SliceConvert.wrap(mWifiSlice, mContext));
77     List<SliceAction> wifiSliceActions = mWifiSliceMetadata.getToggles();
78     if (wifiSliceActions.size() != 0) {
79       SliceAction toggleAction = wifiSliceActions.get(0);
80 
81       toggleAction.setChecked(true);
82       assertThat(toggleAction.isChecked()).isEqualTo(isWifiEnabled());
83 
84       toggleAction.setChecked(false);
85       assertThat(toggleAction.isChecked()).isEqualTo(isWifiEnabled());
86     }
87   }
88 
89   @Test
wifiSlice_hasCorrectUri()90   public void wifiSlice_hasCorrectUri() {
91     assertThat(mWifiSlice.getUri()).isEqualTo(WIFI_SLICE_URI);
92   }
93 
94   @Test
wifiSlice_grantedPermissionToDefaultAssistant()95   public void wifiSlice_grantedPermissionToDefaultAssistant() throws NameNotFoundException {
96     if (!mHasVoiceRecognizersFeature) {
97       Log.i(TAG, "The device doesn't support feature: " + FEATURE_VOICE_RECOGNIZERS);
98       return;
99     }
100     final PackageManager pm = mContext.getPackageManager();
101     final Intent requestDefaultAssistant =
102             new Intent(ACTION_ASSIST).addCategory(CATEGORY_DEFAULT);
103 
104     final ResolveInfo info = pm.resolveActivity(requestDefaultAssistant, 0);
105 
106     if (info != null) {
107       final int testPid = Process.myPid();
108       final int testUid = pm.getPackageUid(info.activityInfo.packageName,  0);
109 
110       assertThat(mSliceManager.checkSlicePermission(WIFI_SLICE_URI, testPid, testUid))
111               .isEqualTo(PERMISSION_GRANTED);
112     }
113   }
114 
115   @Test
wifiSlice_grantedPermissionToDefaultVoiceAssistant()116   public void wifiSlice_grantedPermissionToDefaultVoiceAssistant() throws NameNotFoundException {
117     if (!mHasVoiceRecognizersFeature) {
118       Log.i(TAG, "The device doesn't support feature: " + FEATURE_VOICE_RECOGNIZERS);
119       return;
120     }
121     final PackageManager pm = mContext.getPackageManager();
122     final Intent requestDefaultAssistant =
123             new Intent(ACTION_VOICE_ASSIST).addCategory(CATEGORY_DEFAULT);
124 
125     final ResolveInfo info = pm.resolveActivity(requestDefaultAssistant, 0);
126 
127     if (info != null) {
128       final int testPid = Process.myPid();
129       final int testUid = pm.getPackageUid(info.activityInfo.packageName,  0);
130 
131       assertThat(mSliceManager.checkSlicePermission(WIFI_SLICE_URI, testPid, testUid))
132               .isEqualTo(PERMISSION_GRANTED);
133     }
134   }
135 
isWifiEnabled()136   private boolean isWifiEnabled() {
137     final WifiManager wifiManager = mContext.getSystemService(WifiManager.class);
138     return wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED
139             || wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLING;
140   }
141 
142 }
143