• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.car.settings.testutils;
18 
19 import android.content.Context;
20 import android.net.NetworkPolicy;
21 import android.net.NetworkPolicyManager;
22 import android.util.Pair;
23 
24 import org.robolectric.annotation.Implementation;
25 import org.robolectric.annotation.Implements;
26 import org.robolectric.annotation.Resetter;
27 
28 import java.time.ZonedDateTime;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.Map;
32 
33 @Implements(NetworkPolicyManager.class)
34 public class ShadowNetworkPolicyManager {
35 
36     private static NetworkPolicyManager sNetworkPolicyManager;
37     private static Iterator<Pair<ZonedDateTime, ZonedDateTime>> sCycleIterator;
38     private static Map<String, Integer> sResetCalledForSubscriberCount = new HashMap<>();
39 
verifyFactoryResetCalled(String subscriber, int numTimes)40     public static boolean verifyFactoryResetCalled(String subscriber, int numTimes) {
41         if (!sResetCalledForSubscriberCount.containsKey(subscriber)) return false;
42         return sResetCalledForSubscriberCount.get(subscriber) == numTimes;
43     }
44 
45     @Implementation
factoryReset(String subscriber)46     protected void factoryReset(String subscriber) {
47         sResetCalledForSubscriberCount.put(subscriber,
48                 sResetCalledForSubscriberCount.getOrDefault(subscriber, 0) + 1);
49     }
50 
51     @Implementation
getUidsWithPolicy(int policy)52     protected int[] getUidsWithPolicy(int policy) {
53         return sNetworkPolicyManager == null ? new int[0] : sNetworkPolicyManager
54                 .getUidsWithPolicy(policy);
55     }
56 
57     @Implementation
cycleIterator( NetworkPolicy policy)58     protected static Iterator<Pair<ZonedDateTime, ZonedDateTime>> cycleIterator(
59             NetworkPolicy policy) {
60         return sCycleIterator;
61     }
62 
setCycleIterator( Iterator<Pair<ZonedDateTime, ZonedDateTime>> cycleIterator)63     public static void setCycleIterator(
64             Iterator<Pair<ZonedDateTime, ZonedDateTime>> cycleIterator) {
65         sCycleIterator = cycleIterator;
66     }
67 
68     @Implementation
from(Context context)69     public static NetworkPolicyManager from(Context context) {
70         return sNetworkPolicyManager;
71     }
72 
setNetworkPolicyManager(NetworkPolicyManager networkPolicyManager)73     public static void setNetworkPolicyManager(NetworkPolicyManager networkPolicyManager) {
74         sNetworkPolicyManager = networkPolicyManager;
75     }
76 
77     @Resetter
reset()78     public static void reset() {
79         sResetCalledForSubscriberCount.clear();
80         sCycleIterator = null;
81         sNetworkPolicyManager = null;
82     }
83 }
84