• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.cts.net.hostside;
18 
19 import static com.android.cts.net.hostside.NetworkPolicyTestUtils.clearSnoozeTimestamps;
20 
21 import android.content.pm.PackageManager;
22 import android.support.test.uiautomator.By;
23 import android.support.test.uiautomator.Direction;
24 import android.support.test.uiautomator.UiObject2;
25 import android.support.test.uiautomator.Until;
26 import android.telephony.SubscriptionManager;
27 import android.telephony.SubscriptionPlan;
28 
29 import androidx.test.platform.app.InstrumentationRegistry;
30 import androidx.test.uiautomator.UiDevice;
31 
32 import com.android.compatibility.common.util.SystemUtil;
33 import com.android.compatibility.common.util.UiAutomatorUtils;
34 
35 import org.junit.After;
36 import org.junit.Assume;
37 import org.junit.Before;
38 import org.junit.Test;
39 
40 import java.time.Period;
41 import java.time.ZonedDateTime;
42 import java.util.Arrays;
43 import java.util.List;
44 
45 public class DataWarningReceiverTest extends AbstractRestrictBackgroundNetworkTestCase {
46 
47     @Before
setUp()48     public void setUp() throws Exception {
49         super.setUp();
50 
51         clearSnoozeTimestamps();
52         registerBroadcastReceiver();
53         turnScreenOn();
54     }
55 
56     @After
tearDown()57     public void tearDown() throws Exception {
58         super.tearDown();
59     }
60 
61     @Test
testSnoozeWarningNotReceived()62     public void testSnoozeWarningNotReceived() throws Exception {
63         Assume.assumeTrue("Feature not supported: " + PackageManager.FEATURE_TELEPHONY,
64                 mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY));
65         final SubscriptionManager sm = mContext.getSystemService(SubscriptionManager.class);
66         final int subId = SubscriptionManager.getDefaultDataSubscriptionId();
67         Assume.assumeTrue("Valid subId not found",
68                 subId != SubscriptionManager.INVALID_SUBSCRIPTION_ID);
69 
70         setSubPlanOwner(subId, TEST_PKG);
71         final List<SubscriptionPlan> originalPlans = sm.getSubscriptionPlans(subId);
72         try {
73             // In NetworkPolicyManagerService class, we set the data warning bytes to 90% of
74             // data limit bytes. So, create the subscription plan in such a way this data warning
75             // threshold is already reached.
76             final SubscriptionPlan plan = SubscriptionPlan.Builder
77                     .createRecurring(ZonedDateTime.parse("2007-03-14T00:00:00.000Z"),
78                             Period.ofMonths(1))
79                     .setTitle("CTS")
80                     .setDataLimit(1_000_000_000, SubscriptionPlan.LIMIT_BEHAVIOR_THROTTLED)
81                     .setDataUsage(999_000_000, System.currentTimeMillis())
82                     .build();
83             sm.setSubscriptionPlans(subId, Arrays.asList(plan));
84             final UiDevice uiDevice = UiDevice.getInstance(mInstrumentation);
85             uiDevice.openNotification();
86             try {
87                 final UiObject2 uiObject = UiAutomatorUtils.waitFindObject(
88                         By.text("Data warning"));
89                 Assume.assumeNotNull(uiObject);
90                 uiObject.wait(Until.clickable(true), 10_000L);
91                 uiObject.getParent().swipe(Direction.RIGHT, 1.0f);
92             } catch (Throwable t) {
93                 Assume.assumeNoException(
94                         "Error occurred while finding and swiping the notification", t);
95             }
96             assertSnoozeWarningNotReceived();
97             uiDevice.pressHome();
98         } finally {
99             sm.setSubscriptionPlans(subId, originalPlans);
100             setSubPlanOwner(subId, "");
101         }
102     }
103 
setSubPlanOwner(int subId, String packageName)104     private static void setSubPlanOwner(int subId, String packageName) throws Exception {
105         SystemUtil.runShellCommand(InstrumentationRegistry.getInstrumentation(),
106                 "cmd netpolicy set sub-plan-owner " + subId + " " + packageName);
107     }
108 }
109