1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17 package com.android.storagemanager.automatic; 18 19 import android.content.Context; 20 import android.net.ConnectivityManager; 21 import android.net.Network; 22 import android.net.NetworkInfo; 23 import android.os.BatteryManager; 24 import com.android.storagemanager.testing.TestingConstants; 25 import org.junit.Before; 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.mockito.Mock; 29 import org.mockito.MockitoAnnotations; 30 import org.mockito.invocation.InvocationOnMock; 31 import org.mockito.stubbing.Answer; 32 import org.robolectric.RobolectricTestRunner; 33 import org.robolectric.RuntimeEnvironment; 34 import org.robolectric.annotation.Config; 35 36 import java.util.ArrayList; 37 import java.util.HashMap; 38 39 import static com.google.common.truth.Truth.assertThat; 40 import static org.mockito.Matchers.any; 41 import static org.mockito.Mockito.doReturn; 42 import static org.mockito.Mockito.when; 43 import static org.robolectric.Shadows.shadowOf; 44 45 @RunWith(RobolectricTestRunner.class) 46 @Config(manifest=TestingConstants.MANIFEST, sdk=23) 47 public class JobPreconditionsTest { 48 // TODO: Instead of mocking, use ShadowConnectivityManager. Right now, using it causes a crash. 49 // Use the shadow once we get it working. 50 @Mock ConnectivityManager mConnectivityManager; 51 @Mock BatteryManager mBatteryManager; 52 @Mock Network mWifiNetwork; 53 @Mock NetworkInfo mWifiNetworkInfo; 54 private Context mContext; 55 private ArrayList<Network> mNetworkList; 56 private HashMap<Network, NetworkInfo> mNetworkMap; 57 58 @Before 59 public void setUp() throws Exception { 60 MockitoAnnotations.initMocks(this); 61 mNetworkList = new ArrayList<>(); 62 mNetworkMap = new HashMap<>(); 63 shadowOf(RuntimeEnvironment.application).setSystemService(Context.CONNECTIVITY_SERVICE, 64 mConnectivityManager); 65 shadowOf(RuntimeEnvironment.application).setSystemService(Context.BATTERY_SERVICE, 66 mBatteryManager); 67 mContext = RuntimeEnvironment.application; 68 } 69 70 @Test 71 public void testNoNetworks() { 72 hookUpMocks(); 73 74 assertThat(JobPreconditions.isWifiConnected(mContext)).isFalse(); 75 } 76 77 @Test 78 public void testValidWifiNetworkConnected() { 79 // Connect the fake Wi-Fi. 80 mNetworkList.add(mWifiNetwork); 81 mNetworkMap.put(mWifiNetwork, mWifiNetworkInfo); 82 when(mWifiNetworkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI); 83 when(mWifiNetworkInfo.isConnected()).thenReturn(true); 84 hookUpMocks(); 85 86 assertThat(JobPreconditions.isWifiConnected(mContext)).isTrue(); 87 assertThat(JobPreconditions.isNetworkMetered(mContext)).isFalse(); 88 } 89 90 @Test 91 public void testBatteryConnected() { 92 when(mBatteryManager.isCharging()).thenReturn(true); 93 94 assertThat(JobPreconditions.isCharging(mContext)).isTrue(); 95 } 96 97 @Test 98 public void testBatteryDisconnected() { 99 when(mBatteryManager.isCharging()).thenReturn(false); 100 101 assertThat(JobPreconditions.isCharging(mContext)).isFalse(); 102 } 103 104 // TODO(b/31224380): Checking if a network is metered relies on a hidden API which we cannot 105 // mock or shadow in the current API. Find a way to test the metered check. 106 107 /** 108 * hookUpMocks takes the local list of networks and hooks it up to the mock ConnectivityManager. 109 * It mocks the getAllNetworks() method to return the list of Networks as an array and the 110 * getNetworkInfo(Network) method to return a lookup in our local network -> network info map. 111 */ 112 private void hookUpMocks() { 113 doReturn(mNetworkList.toArray(new Network[mNetworkList.size()])) 114 .when(mConnectivityManager).getAllNetworks(); 115 when(mConnectivityManager.getNetworkInfo(any(Network.class))).thenAnswer( 116 new Answer<NetworkInfo>() { 117 @Override 118 public NetworkInfo answer(InvocationOnMock invocation) { 119 Network network = (Network) invocation.getArguments()[0]; 120 return mNetworkMap.get(network); 121 } 122 } 123 ); 124 } 125 } 126