Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(516)

Unified Diff: components/precache/android/javatests/src/org/chromium/components/precache/DeviceStateTest.java

Issue 1031003003: precache: Move the java files into //components/precache/android (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/precache/android/javatests/src/org/chromium/components/precache/DeviceStateTest.java
diff --git a/components/precache/android/javatests/src/org/chromium/components/precache/DeviceStateTest.java b/components/precache/android/javatests/src/org/chromium/components/precache/DeviceStateTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..30c79b39b62596cb48c02444998923d47a634b07
--- /dev/null
+++ b/components/precache/android/javatests/src/org/chromium/components/precache/DeviceStateTest.java
@@ -0,0 +1,184 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.components.precache;
+
+import android.content.Context;
+import android.net.ConnectivityManager;
+import android.os.BatteryManager;
+import android.test.InstrumentationTestCase;
+import android.test.suitebuilder.annotation.SmallTest;
+
+import org.chromium.base.test.util.AdvancedMockContext;
+import org.chromium.base.test.util.Feature;
+
+/**
+ * Tests for {@link DeviceState}.
+ */
+public class DeviceStateTest extends InstrumentationTestCase {
+
+ /**
+ * Factory to create {@link MockNetworkInfoDelegate} instances.
+ */
+ static class MockNetworkInfoDelegateFactory extends NetworkInfoDelegateFactory {
+ private final boolean mIsValid;
+ private final int mType;
+ private final boolean mIsAvailable;
+ private final boolean mIsConnected;
+ private final boolean mIsRoaming;
+ private final boolean mIsActiveNetworkMetered;
+
+ MockNetworkInfoDelegateFactory(boolean valid, int type, boolean available,
+ boolean connected, boolean roaming, boolean activeNetworkMetered) {
+ mIsValid = valid;
+ mType = type;
+ mIsAvailable = available;
+ mIsConnected = connected;
+ mIsRoaming = roaming;
+ mIsActiveNetworkMetered = activeNetworkMetered;
+ }
+
+ @Override
+ NetworkInfoDelegate getNetworkInfoDelegate(Context context) {
+ return new MockNetworkInfoDelegate(mIsValid, mType, mIsAvailable, mIsConnected,
+ mIsRoaming, mIsActiveNetworkMetered);
+ }
+ }
+
+ /**
+ * Mock of {@link NetworkInfoDelegate}.
+ */
+ static class MockNetworkInfoDelegate extends NetworkInfoDelegate {
+ private final boolean mIsValid;
+ private final int mType;
+ private final boolean mIsAvailable;
+ private final boolean mIsConnected;
+ private final boolean mIsRoaming;
+ private final boolean mIsActiveNetworkMetered;
+
+ MockNetworkInfoDelegate(boolean valid, int type, boolean available, boolean connected,
+ boolean roaming, boolean activeNetworkMetered) {
+ mIsValid = valid;
+ mType = type;
+ mIsAvailable = available;
+ mIsConnected = connected;
+ mIsRoaming = roaming;
+ mIsActiveNetworkMetered = activeNetworkMetered;
+ }
+
+ @Override
+ protected void getNetworkInfo(Context context) {}
+
+ @Override
+ protected boolean isValid() {
+ return mIsValid;
+ }
+
+ @Override
+ protected int getType() {
+ return mType;
+ }
+
+ @Override
+ protected boolean isAvailable() {
+ return mIsAvailable;
+ }
+
+ @Override
+ protected boolean isConnected() {
+ return mIsConnected;
+ }
+
+ @Override
+ protected boolean isRoaming() {
+ return mIsRoaming;
+ }
+
+ @Override
+ protected boolean isActiveNetworkMetered() {
+ return mIsActiveNetworkMetered;
+ }
+ }
+
+ /**
+ * Mock of {@link DeviceState}.
+ */
+ static class MockDeviceState extends DeviceState {
+ int mBatteryStatus;
+
+ @Override
+ int getStickyBatteryStatus(Context context) {
+ return mBatteryStatus;
+ }
+
+ public void setStickyBatteryStatus(int status) {
+ mBatteryStatus = status;
+ }
+ }
+
+ @SmallTest
+ @Feature({"Precache"})
+ public void testPowerConnected() {
+ AdvancedMockContext context = new AdvancedMockContext();
+ MockDeviceState deviceState = new MockDeviceState();
+
+ deviceState.setStickyBatteryStatus(BatteryManager.BATTERY_STATUS_NOT_CHARGING);
+ assertFalse(deviceState.isPowerConnected(context));
+
+ deviceState.setStickyBatteryStatus(BatteryManager.BATTERY_STATUS_CHARGING);
+ assertTrue(deviceState.isPowerConnected(context));
+
+ deviceState.setStickyBatteryStatus(BatteryManager.BATTERY_STATUS_FULL);
+ assertTrue(deviceState.isPowerConnected(context));
+ }
+
+ @SmallTest
+ @Feature({"Precache"})
+ public void testWifiAvailable() {
+ AdvancedMockContext context = new AdvancedMockContext();
+ DeviceState deviceState = DeviceState.getInstance();
+
+ // Expect WiFi to be reported as available because there is valid {@link NetworkInfo},
+ // the connection is WiFi, it's available and connected, and not roaming or metered.
+ deviceState.setNetworkInfoDelegateFactory(
+ new MockNetworkInfoDelegateFactory(
+ true, ConnectivityManager.TYPE_WIFI, true, true, false, false));
+ assertTrue(deviceState.isWifiAvailable(context));
+
+ // Expect WiFi to be reported as unavailable because one of the aforementioned required
+ // conditions is not met.
+ deviceState.setNetworkInfoDelegateFactory(
+ new MockNetworkInfoDelegateFactory(
+ false, ConnectivityManager.TYPE_WIFI, true, true, false, false));
+ assertFalse(deviceState.isWifiAvailable(context));
+
+ deviceState.setNetworkInfoDelegateFactory(
+ new MockNetworkInfoDelegateFactory(true, 0, false, true, false, false));
+ assertFalse(deviceState.isWifiAvailable(context));
+
+ deviceState.setNetworkInfoDelegateFactory(
+ new MockNetworkInfoDelegateFactory(
+ true, ConnectivityManager.TYPE_WIFI, false, true, false, false));
+ assertFalse(deviceState.isWifiAvailable(context));
+
+ deviceState.setNetworkInfoDelegateFactory(
+ new MockNetworkInfoDelegateFactory(
+ true, ConnectivityManager.TYPE_WIFI, true, false, false, false));
+ assertFalse(deviceState.isWifiAvailable(context));
+
+ deviceState.setNetworkInfoDelegateFactory(
+ new MockNetworkInfoDelegateFactory(
+ true, ConnectivityManager.TYPE_WIFI, true, true, true, false));
+ assertFalse(deviceState.isWifiAvailable(context));
+
+ deviceState.setNetworkInfoDelegateFactory(
+ new MockNetworkInfoDelegateFactory(
+ true, ConnectivityManager.TYPE_WIFI, true, true, false, true));
+ assertFalse(deviceState.isWifiAvailable(context));
+
+ deviceState.setNetworkInfoDelegateFactory(
+ new MockNetworkInfoDelegateFactory(true, 0, false, false, true, true));
+ assertFalse(deviceState.isWifiAvailable(context));
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698