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