| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 package org.chromium.chrome.browser.precache; |
| 5 |
| 6 import android.content.Intent; |
| 7 import android.test.ServiceTestCase; |
| 8 import android.test.suitebuilder.annotation.SmallTest; |
| 9 |
| 10 import org.chromium.base.ThreadUtils; |
| 11 import org.chromium.base.test.util.AdvancedMockContext; |
| 12 import org.chromium.base.test.util.Feature; |
| 13 import org.chromium.components.precache.MockDeviceState; |
| 14 |
| 15 /** |
| 16 * Tests of {@link PrecacheService}. |
| 17 */ |
| 18 public class PrecacheServiceTest extends ServiceTestCase<MockPrecacheService> { |
| 19 |
| 20 /** |
| 21 * Mock of the {@link PrecacheLauncher}. |
| 22 */ |
| 23 static class MockPrecacheLauncher extends PrecacheLauncher { |
| 24 |
| 25 private MockPrecacheService mService; |
| 26 |
| 27 public void setService(MockPrecacheService service) { |
| 28 mService = service; |
| 29 } |
| 30 |
| 31 @Override |
| 32 public void destroy() {} |
| 33 |
| 34 @Override |
| 35 public void start() {} |
| 36 |
| 37 @Override |
| 38 public void cancel() {} |
| 39 |
| 40 @Override |
| 41 protected void onPrecacheCompleted() { |
| 42 mService.handlePrecacheCompleted(); |
| 43 } |
| 44 } |
| 45 |
| 46 private final MockPrecacheLauncher mPrecacheLauncher = new MockPrecacheLaunc
her(); |
| 47 |
| 48 public PrecacheServiceTest() { |
| 49 super(MockPrecacheService.class); |
| 50 } |
| 51 |
| 52 @Override |
| 53 protected void setupService() { |
| 54 super.setupService(); |
| 55 mPrecacheLauncher.setService(getService()); |
| 56 getService().setPrecacheLauncher(mPrecacheLauncher); |
| 57 } |
| 58 |
| 59 private void startAndChangeDeviceState(MockPrecacheService service, boolean
powerIsConnected, |
| 60 boolean isInteractive, boolean wifiIsAvailable) { |
| 61 AdvancedMockContext context = new AdvancedMockContext(); |
| 62 getService().setDeviceState(new MockDeviceState(0, true, false, true)); |
| 63 assertFalse("Precaching should not be in progress initially", service.is
Precaching()); |
| 64 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 65 @Override |
| 66 public void run() { |
| 67 Intent intent = new Intent(getService(), MockPrecacheService.cla
ss); |
| 68 intent.setAction(PrecacheService.ACTION_START_PRECACHE); |
| 69 startService(intent); |
| 70 } |
| 71 }); |
| 72 assertTrue("Precaching should be in progress after start", service.isPre
caching()); |
| 73 getService().setDeviceState( |
| 74 new MockDeviceState(0, powerIsConnected, isInteractive, wifiIsAv
ailable)); |
| 75 service.getDeviceStateReceiver().onReceive(context, new Intent()); |
| 76 } |
| 77 |
| 78 /** Tests that disconnecting power stops a precache. */ |
| 79 @SmallTest |
| 80 @Feature({"Precache"}) |
| 81 public void testPrecacheWhenPowerDisconnects() { |
| 82 setupService(); |
| 83 startAndChangeDeviceState(getService(), false, false, true); |
| 84 assertFalse("Precaching should not be in progress when power is disconne
cted", |
| 85 getService().isPrecaching()); |
| 86 |
| 87 } |
| 88 |
| 89 /** Tests that the device becoming interactive stops a precache. */ |
| 90 @SmallTest |
| 91 @Feature({"Precache"}) |
| 92 public void testPrecacheWhenDeviceBecomesInteractive() { |
| 93 setupService(); |
| 94 startAndChangeDeviceState(getService(), true, true, true); |
| 95 assertFalse("Precaching should not be in progress when the device is int
eractive", |
| 96 getService().isPrecaching()); |
| 97 } |
| 98 |
| 99 /** Tests that going off of Wi-Fi stops a precache. */ |
| 100 @SmallTest |
| 101 @Feature({"Precache"}) |
| 102 public void testPrecacheWhenNoLongerWifi() { |
| 103 setupService(); |
| 104 startAndChangeDeviceState(getService(), true, true, true); |
| 105 assertFalse("Precaching should not be in progress when the network is no
t Wi-Fi", |
| 106 getService().isPrecaching()); |
| 107 } |
| 108 |
| 109 /** Tests that precaching continues if prerequisites are still met. */ |
| 110 @SmallTest |
| 111 @Feature({"Precache"}) |
| 112 public void testPrecacheWhenPrerequisitesStillMet() { |
| 113 setupService(); |
| 114 startAndChangeDeviceState(getService(), true, false, true); |
| 115 assertTrue("Precaching should be in progress", getService().isPrecaching
()); |
| 116 } |
| 117 |
| 118 /** |
| 119 * Tests that precaching continues until completion while the prerequisite d
evice states |
| 120 * (Wifi is in use, power is connected, and the device is non-interactive) c
ontinue to be met. |
| 121 */ |
| 122 @SmallTest |
| 123 @Feature({"Precache"}) |
| 124 public void testPrecacheCompleted() { |
| 125 setupService(); |
| 126 startAndChangeDeviceState(getService(), true, false, true); |
| 127 assertTrue("Precaching should be in progress", getService().isPrecaching
()); |
| 128 mPrecacheLauncher.onPrecacheCompleted(); |
| 129 assertFalse("Precaching should not be in progress after completion", |
| 130 getService().isPrecaching()); |
| 131 } |
| 132 } |
| OLD | NEW |