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 
17 package com.android.documentsui.bots;
18 
19 import android.app.Activity;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.support.test.uiautomator.UiDevice;
23 
24 import androidx.test.InstrumentationRegistry;
25 
26 import com.android.documentsui.services.TestNotificationService;
27 
28 import java.io.IOException;
29 
30 /**
31  * A test helper class for controlling notification items.
32  */
33 public class NotificationsBot extends Bots.BaseBot {
34     private final ComponentName mComponent;
35 
NotificationsBot(UiDevice device, Context context, int timeout)36     public NotificationsBot(UiDevice device, Context context, int timeout) {
37         super(device, context, timeout);
38         mComponent = new ComponentName(InstrumentationRegistry.getContext(),
39                 TestNotificationService.class);
40     }
41 
setNotificationAccess(Activity activity, boolean enabled)42     public void setNotificationAccess(Activity activity, boolean enabled) throws IOException {
43         if (enabled) {
44             mDevice.executeShellCommand(
45                     "cmd notification allow_listener " + mComponent.flattenToString());
46         } else {
47             mDevice.executeShellCommand(
48                     "cmd notification disallow_listener " + mComponent.flattenToString());
49         }
50     }
51 }
52