OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.offlinepages; | 5 package org.chromium.chrome.browser.offlinepages; |
6 | 6 |
7 import android.support.test.InstrumentationRegistry; | |
8 import android.support.test.filters.SmallTest; | 7 import android.support.test.filters.SmallTest; |
9 | 8 |
10 import org.junit.Assert; | |
11 import org.junit.Before; | |
12 import org.junit.Rule; | |
13 import org.junit.Test; | |
14 import org.junit.runner.RunWith; | |
15 | |
16 import org.chromium.base.ThreadUtils; | 9 import org.chromium.base.ThreadUtils; |
17 import org.chromium.base.test.util.CommandLineFlags; | 10 import org.chromium.base.test.util.CommandLineFlags; |
18 import org.chromium.base.test.util.RetryOnFailure; | 11 import org.chromium.base.test.util.RetryOnFailure; |
19 import org.chromium.chrome.browser.ChromeActivity; | 12 import org.chromium.chrome.browser.ChromeActivity; |
20 import org.chromium.chrome.browser.ChromeSwitches; | |
21 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge.OfflinePageMod
elObserver; | 13 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge.OfflinePageMod
elObserver; |
22 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge.SavePageCallba
ck; | 14 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge.SavePageCallba
ck; |
23 import org.chromium.chrome.browser.profiles.Profile; | 15 import org.chromium.chrome.browser.profiles.Profile; |
24 import org.chromium.chrome.browser.tab.Tab; | 16 import org.chromium.chrome.browser.tab.Tab; |
25 import org.chromium.chrome.test.ChromeActivityTestRule; | 17 import org.chromium.chrome.test.ChromeActivityTestCaseBase; |
26 import org.chromium.chrome.test.ChromeJUnit4ClassRunner; | |
27 import org.chromium.components.offlinepages.SavePageResult; | 18 import org.chromium.components.offlinepages.SavePageResult; |
28 import org.chromium.net.NetworkChangeNotifier; | 19 import org.chromium.net.NetworkChangeNotifier; |
29 import org.chromium.net.test.EmbeddedTestServer; | 20 import org.chromium.net.test.EmbeddedTestServer; |
30 | 21 |
31 import java.util.concurrent.Semaphore; | 22 import java.util.concurrent.Semaphore; |
32 import java.util.concurrent.TimeUnit; | 23 import java.util.concurrent.TimeUnit; |
33 | 24 |
34 /** Unit tests for offline page request handling. */ | 25 /** Unit tests for offline page request handling. */ |
35 @RunWith(ChromeJUnit4ClassRunner.class) | 26 @CommandLineFlags.Add("enable-features=OfflineBookmarks") |
36 @CommandLineFlags.Add({"enable-features=OfflineBookmarks", | 27 public class OfflinePageRequestTest extends ChromeActivityTestCaseBase<ChromeAct
ivity> { |
37 ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE, | |
38 ChromeActivityTestRule.DISABLE_NETWORK_PREDICTION_FLAG}) | |
39 public class OfflinePageRequestTest { | |
40 @Rule | |
41 public ChromeActivityTestRule<ChromeActivity> mActivityTestRule = | |
42 new ChromeActivityTestRule<>(ChromeActivity.class); | |
43 | |
44 private static final String TEST_PAGE = "/chrome/test/data/android/test.html
"; | 28 private static final String TEST_PAGE = "/chrome/test/data/android/test.html
"; |
45 private static final String ABOUT_PAGE = "/chrome/test/data/android/about.ht
ml"; | 29 private static final String ABOUT_PAGE = "/chrome/test/data/android/about.ht
ml"; |
46 private static final int TIMEOUT_MS = 5000; | 30 private static final int TIMEOUT_MS = 5000; |
47 private static final ClientId CLIENT_ID = | 31 private static final ClientId CLIENT_ID = |
48 new ClientId(OfflinePageBridge.BOOKMARK_NAMESPACE, "1234"); | 32 new ClientId(OfflinePageBridge.BOOKMARK_NAMESPACE, "1234"); |
49 | 33 |
50 private OfflinePageBridge mOfflinePageBridge; | 34 private OfflinePageBridge mOfflinePageBridge; |
51 | 35 |
52 @Before | 36 public OfflinePageRequestTest() { |
53 public void setUp() throws Exception { | 37 super(ChromeActivity.class); |
54 mActivityTestRule.startMainActivityOnBlankPage(); | 38 } |
| 39 |
| 40 @Override |
| 41 protected void setUp() throws Exception { |
| 42 super.setUp(); |
55 final Semaphore semaphore = new Semaphore(0); | 43 final Semaphore semaphore = new Semaphore(0); |
56 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 44 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
57 @Override | 45 @Override |
58 public void run() { | 46 public void run() { |
59 if (!NetworkChangeNotifier.isInitialized()) { | 47 if (!NetworkChangeNotifier.isInitialized()) { |
60 NetworkChangeNotifier.init(); | 48 NetworkChangeNotifier.init(); |
61 } | 49 } |
62 NetworkChangeNotifier.forceConnectivityState(true); | 50 NetworkChangeNotifier.forceConnectivityState(true); |
63 | 51 |
64 Profile profile = Profile.getLastUsedProfile(); | 52 Profile profile = Profile.getLastUsedProfile(); |
65 mOfflinePageBridge = OfflinePageBridge.getForProfile(profile); | 53 mOfflinePageBridge = OfflinePageBridge.getForProfile(profile); |
66 if (mOfflinePageBridge.isOfflinePageModelLoaded()) { | 54 if (mOfflinePageBridge.isOfflinePageModelLoaded()) { |
67 semaphore.release(); | 55 semaphore.release(); |
68 } else { | 56 } else { |
69 mOfflinePageBridge.addObserver(new OfflinePageModelObserver(
) { | 57 mOfflinePageBridge.addObserver(new OfflinePageModelObserver(
) { |
70 @Override | 58 @Override |
71 public void offlinePageModelLoaded() { | 59 public void offlinePageModelLoaded() { |
72 semaphore.release(); | 60 semaphore.release(); |
73 mOfflinePageBridge.removeObserver(this); | 61 mOfflinePageBridge.removeObserver(this); |
74 } | 62 } |
75 }); | 63 }); |
76 } | 64 } |
77 } | 65 } |
78 }); | 66 }); |
79 Assert.assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS
)); | 67 assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS)); |
80 } | 68 } |
81 | 69 |
82 @Test | 70 @Override |
| 71 protected void tearDown() throws Exception { |
| 72 super.tearDown(); |
| 73 } |
| 74 |
| 75 @Override |
| 76 public void startMainActivity() throws InterruptedException { |
| 77 startMainActivityOnBlankPage(); |
| 78 } |
| 79 |
83 @SmallTest | 80 @SmallTest |
84 @RetryOnFailure | 81 @RetryOnFailure |
85 public void testLoadOfflinePageOnDisconnectedNetwork() throws Exception { | 82 public void testLoadOfflinePageOnDisconnectedNetwork() throws Exception { |
86 EmbeddedTestServer testServer = EmbeddedTestServer.createAndStartServer( | 83 EmbeddedTestServer testServer = EmbeddedTestServer.createAndStartServer( |
87 InstrumentationRegistry.getInstrumentation().getContext()); | 84 getInstrumentation().getContext()); |
88 String testUrl = testServer.getURL(TEST_PAGE); | 85 String testUrl = testServer.getURL(TEST_PAGE); |
89 String aboutUrl = testServer.getURL(ABOUT_PAGE); | 86 String aboutUrl = testServer.getURL(ABOUT_PAGE); |
90 | 87 |
91 Tab tab = mActivityTestRule.getActivity().getActivityTab(); | 88 Tab tab = getActivity().getActivityTab(); |
92 | 89 |
93 // Load and save an offline page. | 90 // Load and save an offline page. |
94 savePage(testUrl); | 91 savePage(testUrl); |
95 Assert.assertFalse(isErrorPage(tab)); | 92 assertFalse(isErrorPage(tab)); |
96 Assert.assertFalse(isOfflinePage(tab)); | 93 assertFalse(isOfflinePage(tab)); |
97 | 94 |
98 // Load another page. | 95 // Load another page. |
99 mActivityTestRule.loadUrl(aboutUrl); | 96 loadUrl(aboutUrl); |
100 Assert.assertFalse(isErrorPage(tab)); | 97 assertFalse(isErrorPage(tab)); |
101 Assert.assertFalse(isOfflinePage(tab)); | 98 assertFalse(isOfflinePage(tab)); |
102 | 99 |
103 // Stop the server and also disconnect the network. | 100 // Stop the server and also disconnect the network. |
104 testServer.stopAndDestroyServer(); | 101 testServer.stopAndDestroyServer(); |
105 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 102 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
106 @Override | 103 @Override |
107 public void run() { | 104 public void run() { |
108 NetworkChangeNotifier.forceConnectivityState(false); | 105 NetworkChangeNotifier.forceConnectivityState(false); |
109 } | 106 } |
110 }); | 107 }); |
111 | 108 |
112 // Load the page that has an offline copy. The offline page should be sh
own. | 109 // Load the page that has an offline copy. The offline page should be sh
own. |
113 mActivityTestRule.loadUrl(testUrl); | 110 loadUrl(testUrl); |
114 Assert.assertFalse(isErrorPage(tab)); | 111 assertFalse(isErrorPage(tab)); |
115 Assert.assertTrue(isOfflinePage(tab)); | 112 assertTrue(isOfflinePage(tab)); |
116 } | 113 } |
117 | 114 |
118 @Test | |
119 @SmallTest | 115 @SmallTest |
120 @RetryOnFailure | 116 @RetryOnFailure |
121 public void testLoadOfflinePageWithFragmentOnDisconnectedNetwork() throws Ex
ception { | 117 public void testLoadOfflinePageWithFragmentOnDisconnectedNetwork() throws Ex
ception { |
122 EmbeddedTestServer testServer = EmbeddedTestServer.createAndStartServer( | 118 EmbeddedTestServer testServer = |
123 InstrumentationRegistry.getInstrumentation().getContext()); | 119 EmbeddedTestServer.createAndStartServer(getInstrumentation().get
Context()); |
124 String testUrl = testServer.getURL(TEST_PAGE); | 120 String testUrl = testServer.getURL(TEST_PAGE); |
125 String testUrlWithFragment = testUrl + "#ref"; | 121 String testUrlWithFragment = testUrl + "#ref"; |
126 | 122 |
127 Tab tab = mActivityTestRule.getActivity().getActivityTab(); | 123 Tab tab = getActivity().getActivityTab(); |
128 | 124 |
129 // Load and save an offline page for the url with a fragment. | 125 // Load and save an offline page for the url with a fragment. |
130 savePage(testUrlWithFragment); | 126 savePage(testUrlWithFragment); |
131 Assert.assertFalse(isErrorPage(tab)); | 127 assertFalse(isErrorPage(tab)); |
132 Assert.assertFalse(isOfflinePage(tab)); | 128 assertFalse(isOfflinePage(tab)); |
133 | 129 |
134 // Stop the server and also disconnect the network. | 130 // Stop the server and also disconnect the network. |
135 testServer.stopAndDestroyServer(); | 131 testServer.stopAndDestroyServer(); |
136 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 132 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
137 @Override | 133 @Override |
138 public void run() { | 134 public void run() { |
139 NetworkChangeNotifier.forceConnectivityState(false); | 135 NetworkChangeNotifier.forceConnectivityState(false); |
140 } | 136 } |
141 }); | 137 }); |
142 | 138 |
143 // Load the URL without the fragment. The offline page should be shown. | 139 // Load the URL without the fragment. The offline page should be shown. |
144 mActivityTestRule.loadUrl(testUrl); | 140 loadUrl(testUrl); |
145 Assert.assertFalse(isErrorPage(tab)); | 141 assertFalse(isErrorPage(tab)); |
146 Assert.assertTrue(isOfflinePage(tab)); | 142 assertTrue(isOfflinePage(tab)); |
147 } | 143 } |
148 | 144 |
149 private void savePage(String url) throws InterruptedException { | 145 private void savePage(String url) throws InterruptedException { |
150 mActivityTestRule.loadUrl(url); | 146 loadUrl(url); |
151 | 147 |
152 final Semaphore semaphore = new Semaphore(0); | 148 final Semaphore semaphore = new Semaphore(0); |
153 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 149 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
154 @Override | 150 @Override |
155 public void run() { | 151 public void run() { |
156 mOfflinePageBridge.savePage( | 152 mOfflinePageBridge.savePage(getActivity().getActivityTab().getWe
bContents(), |
157 mActivityTestRule.getActivity().getActivityTab().getWebC
ontents(), | |
158 CLIENT_ID, new SavePageCallback() { | 153 CLIENT_ID, new SavePageCallback() { |
159 @Override | 154 @Override |
160 public void onSavePageDone( | 155 public void onSavePageDone( |
161 int savePageResult, String url, long offline
Id) { | 156 int savePageResult, String url, long offline
Id) { |
162 Assert.assertEquals( | 157 assertEquals( |
163 "Save failed.", SavePageResult.SUCCESS,
savePageResult); | 158 "Save failed.", SavePageResult.SUCCESS,
savePageResult); |
164 semaphore.release(); | 159 semaphore.release(); |
165 } | 160 } |
166 }); | 161 }); |
167 } | 162 } |
168 }); | 163 }); |
169 Assert.assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS
)); | 164 assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS)); |
170 } | 165 } |
171 | 166 |
172 private boolean isOfflinePage(final Tab tab) { | 167 private boolean isOfflinePage(final Tab tab) { |
173 final boolean[] isOffline = new boolean[1]; | 168 final boolean[] isOffline = new boolean[1]; |
174 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 169 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
175 @Override | 170 @Override |
176 public void run() { | 171 public void run() { |
177 isOffline[0] = OfflinePageUtils.isOfflinePage(tab); | 172 isOffline[0] = OfflinePageUtils.isOfflinePage(tab); |
178 } | 173 } |
179 }); | 174 }); |
180 return isOffline[0]; | 175 return isOffline[0]; |
181 } | 176 } |
182 | 177 |
183 private boolean isErrorPage(final Tab tab) { | 178 private boolean isErrorPage(final Tab tab) { |
184 final boolean[] isShowingError = new boolean[1]; | 179 final boolean[] isShowingError = new boolean[1]; |
185 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | 180 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
186 @Override | 181 @Override |
187 public void run() { | 182 public void run() { |
188 isShowingError[0] = tab.isShowingErrorPage(); | 183 isShowingError[0] = tab.isShowingErrorPage(); |
189 } | 184 } |
190 }); | 185 }); |
191 return isShowingError[0]; | 186 return isShowingError[0]; |
192 } | 187 } |
193 } | 188 } |
OLD | NEW |