• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.providers.downloads;
2 
3 import static org.mockito.ArgumentMatchers.any;
4 import static org.mockito.Mockito.mock;
5 import static org.mockito.Mockito.when;
6 
7 import android.content.Context;
8 import android.content.Intent;
9 import android.content.pm.PackageManager.NameNotFoundException;
10 import android.net.ConnectivityManager;
11 import android.net.Network;
12 import android.net.NetworkInfo;
13 import android.net.NetworkInfo.DetailedState;
14 
15 import org.mockito.invocation.InvocationOnMock;
16 import org.mockito.stubbing.Answer;
17 
18 import java.io.IOException;
19 import java.net.URL;
20 import java.net.URLConnection;
21 import java.security.GeneralSecurityException;
22 import java.util.ArrayList;
23 import java.util.List;
24 
25 import javax.net.ssl.SSLContext;
26 
27 public class FakeSystemFacade implements SystemFacade {
28     long mTimeMillis = 0;
29     Integer mActiveNetworkType = ConnectivityManager.TYPE_WIFI;
30     boolean mIsRoaming = false;
31     boolean mIsMetered = false;
32     long mMaxBytesOverMobile = Long.MAX_VALUE;
33     long mRecommendedMaxBytesOverMobile = Long.MAX_VALUE;
34     List<Intent> mBroadcastsSent = new ArrayList<Intent>();
35     boolean mCleartextTrafficPermitted = true;
36     private boolean mReturnActualTime = false;
37     private SSLContext mSSLContext = null;
38 
setUp()39     public void setUp() {
40         mTimeMillis = 0;
41         mActiveNetworkType = ConnectivityManager.TYPE_WIFI;
42         mIsRoaming = false;
43         mIsMetered = false;
44         mMaxBytesOverMobile = Long.MAX_VALUE;
45         mRecommendedMaxBytesOverMobile = Long.MAX_VALUE;
46         mBroadcastsSent.clear();
47         mReturnActualTime = false;
48         try {
49             mSSLContext = SSLContext.getDefault();
50         } catch (GeneralSecurityException e) {
51             throw new RuntimeException(e);
52         }
53     }
54 
incrementTimeMillis(long delta)55     void incrementTimeMillis(long delta) {
56         mTimeMillis += delta;
57     }
58 
59     @Override
currentTimeMillis()60     public long currentTimeMillis() {
61         if (mReturnActualTime) {
62             return System.currentTimeMillis();
63         }
64         return mTimeMillis;
65     }
66 
67     @Override
getActiveNetwork(int uid, boolean ignoreBlocked)68     public Network getActiveNetwork(int uid, boolean ignoreBlocked) {
69         if (mActiveNetworkType == null) {
70             return null;
71         } else {
72             final Network network = mock(Network.class);
73             try {
74                 when(network.openConnection(any())).then(new Answer<URLConnection>() {
75                     @Override
76                     public URLConnection answer(InvocationOnMock invocation) throws Throwable {
77                         final URL url = (URL) invocation.getArguments()[0];
78                         return url.openConnection();
79                     }
80                 });
81             } catch (IOException ignored) {
82             }
83             return network;
84         }
85     }
86 
87     @Override
getNetworkInfo(Network network, int uid, boolean ignoreBlocked)88     public NetworkInfo getNetworkInfo(Network network, int uid, boolean ignoreBlocked) {
89         if (mActiveNetworkType == null) {
90             return null;
91         } else {
92             final NetworkInfo info = new NetworkInfo(mActiveNetworkType, 0, null, null);
93             info.setDetailedState(DetailedState.CONNECTED, null, null);
94             info.setRoaming(mIsRoaming);
95             return info;
96         }
97     }
98 
99     @Override
isNetworkMetered(Network network)100     public boolean isNetworkMetered(Network network) {
101         return mIsMetered;
102     }
103 
104     @Override
isActiveNetworkMeteredForUid(int uid)105     public boolean isActiveNetworkMeteredForUid(int uid) {
106         return mIsMetered;
107     }
108 
109     @Override
getMaxBytesOverMobile()110     public long getMaxBytesOverMobile() {
111         return mMaxBytesOverMobile;
112     }
113 
114     @Override
getRecommendedMaxBytesOverMobile()115     public long getRecommendedMaxBytesOverMobile() {
116         return mRecommendedMaxBytesOverMobile;
117     }
118 
119     @Override
sendBroadcast(Intent intent)120     public void sendBroadcast(Intent intent) {
121         mBroadcastsSent.add(intent);
122     }
123 
124     @Override
userOwnsPackage(int uid, String pckg)125     public boolean userOwnsPackage(int uid, String pckg) throws NameNotFoundException {
126         return true;
127     }
128 
129     @Override
isCleartextTrafficPermitted(int uid)130     public boolean isCleartextTrafficPermitted(int uid) {
131         return mCleartextTrafficPermitted;
132     }
133 
134     @Override
getSSLContextForPackage(Context context, String pckg)135     public SSLContext getSSLContextForPackage(Context context, String pckg) {
136         return mSSLContext;
137     }
138 
setSSLContext(SSLContext context)139     public void setSSLContext(SSLContext context) {
140         mSSLContext = context;
141     }
142 
setReturnActualTime(boolean flag)143     public void setReturnActualTime(boolean flag) {
144         mReturnActualTime = flag;
145     }
146 }
147