1 package org.robolectric.shadows; 2 3 import static android.os.Build.VERSION_CODES.LOLLIPOP; 4 import static android.os.Build.VERSION_CODES.N; 5 import static android.os.Build.VERSION_CODES.N_MR1; 6 import static android.os.Build.VERSION_CODES.O; 7 import static android.os.Build.VERSION_CODES.Q; 8 import static org.robolectric.util.reflector.Reflector.reflector; 9 10 import android.net.NetworkCapabilities; 11 import android.net.NetworkSpecifier; 12 import android.net.TransportInfo; 13 import org.robolectric.annotation.HiddenApi; 14 import org.robolectric.annotation.Implementation; 15 import org.robolectric.annotation.Implements; 16 import org.robolectric.annotation.RealObject; 17 import org.robolectric.shadow.api.Shadow; 18 import org.robolectric.util.reflector.Direct; 19 import org.robolectric.util.reflector.ForType; 20 21 /** Robolectic provides overrides for fetching and updating transport. */ 22 @Implements(value = NetworkCapabilities.class, minSdk = LOLLIPOP, looseSignatures = true) 23 public class ShadowNetworkCapabilities { 24 25 @RealObject protected NetworkCapabilities realNetworkCapabilities; 26 newInstance()27 public static NetworkCapabilities newInstance() { 28 return Shadow.newInstanceOf(NetworkCapabilities.class); 29 } 30 31 /** Updates the transport types for this network capablities to include {@code transportType}. */ 32 @HiddenApi 33 @Implementation addTransportType(int transportType)34 public NetworkCapabilities addTransportType(int transportType) { 35 return reflector(NetworkCapabilitiesReflector.class, realNetworkCapabilities) 36 .addTransportType(transportType); 37 } 38 39 /** Updates the transport types for this network capablities to remove {@code transportType}. */ 40 @HiddenApi 41 @Implementation removeTransportType(int transportType)42 public NetworkCapabilities removeTransportType(int transportType) { 43 return reflector(NetworkCapabilitiesReflector.class, realNetworkCapabilities) 44 .removeTransportType(transportType); 45 } 46 47 /** Adds {@code capability} to the NetworkCapabilities. */ 48 @HiddenApi 49 @Implementation addCapability(int capability)50 public NetworkCapabilities addCapability(int capability) { 51 return reflector(NetworkCapabilitiesReflector.class, realNetworkCapabilities) 52 .addCapability(capability); 53 } 54 55 /** Removes {@code capability} from the NetworkCapabilities. */ 56 @HiddenApi 57 @Implementation removeCapability(int capability)58 public NetworkCapabilities removeCapability(int capability) { 59 return reflector(NetworkCapabilitiesReflector.class, realNetworkCapabilities) 60 .removeCapability(capability); 61 } 62 63 /** 64 * Changes {@link NetworkSpecifier} for this network capabilities. Works only on Android O and 65 * higher. For lower versions use {@link #setNetworkSpecifier(String)} 66 */ 67 @Implementation(minSdk = O) setNetworkSpecifier(NetworkSpecifier networkSpecifier)68 public NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier) { 69 return reflector(NetworkCapabilitiesReflector.class, realNetworkCapabilities) 70 .setNetworkSpecifier(networkSpecifier); 71 } 72 73 /** 74 * Changes {@link NetworkSpecifier} for this network capabilities. Works only on Android N_MR1 and 75 * lower. For higher versions use {@link #setNetworkSpecifier(NetworkSpecifier)} 76 */ 77 @Implementation(minSdk = N, maxSdk = N_MR1) setNetworkSpecifier(String networkSpecifier)78 public NetworkCapabilities setNetworkSpecifier(String networkSpecifier) { 79 return reflector(NetworkCapabilitiesReflector.class, realNetworkCapabilities) 80 .setNetworkSpecifier(networkSpecifier); 81 } 82 83 /** Sets the {@code transportInfo} of the NetworkCapabilities. */ 84 @HiddenApi 85 @Implementation(minSdk = Q) setTransportInfo(TransportInfo transportInfo)86 public NetworkCapabilities setTransportInfo(TransportInfo transportInfo) { 87 return reflector(NetworkCapabilitiesReflector.class, realNetworkCapabilities) 88 .setTransportInfo(transportInfo); 89 } 90 91 /** Sets the LinkDownstreamBandwidthKbps of the NetworkCapabilities. */ 92 @HiddenApi 93 @Implementation setLinkDownstreamBandwidthKbps(Object kbps)94 public Object setLinkDownstreamBandwidthKbps(Object kbps) { 95 // Loose signatures is necessary because the return type of setLinkDownstreamBandwidthKbps 96 // changed from void to NetworkCapabilities starting from API 28 (Pie) 97 return reflector(NetworkCapabilitiesReflector.class, realNetworkCapabilities) 98 .setLinkDownstreamBandwidthKbps((int) kbps); 99 } 100 101 @ForType(NetworkCapabilities.class) 102 interface NetworkCapabilitiesReflector { 103 104 @Direct addTransportType(int transportType)105 NetworkCapabilities addTransportType(int transportType); 106 107 @Direct removeTransportType(int transportType)108 NetworkCapabilities removeTransportType(int transportType); 109 110 @Direct addCapability(int capability)111 NetworkCapabilities addCapability(int capability); 112 113 @Direct removeCapability(int capability)114 NetworkCapabilities removeCapability(int capability); 115 116 @Direct setNetworkSpecifier(NetworkSpecifier networkSpecifier)117 NetworkCapabilities setNetworkSpecifier(NetworkSpecifier networkSpecifier); 118 119 @Direct setNetworkSpecifier(String networkSpecifier)120 NetworkCapabilities setNetworkSpecifier(String networkSpecifier); 121 122 @Direct setTransportInfo(TransportInfo transportInfo)123 NetworkCapabilities setTransportInfo(TransportInfo transportInfo); 124 125 @Direct setLinkDownstreamBandwidthKbps(int kbps)126 NetworkCapabilities setLinkDownstreamBandwidthKbps(int kbps); 127 } 128 } 129