Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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.content.Context; | 7 import android.content.Context; |
| 8 import android.os.Environment; | 8 import android.os.Environment; |
| 9 import android.test.suitebuilder.annotation.SmallTest; | 9 import android.test.suitebuilder.annotation.SmallTest; |
| 10 | 10 |
| 11 import org.chromium.base.Callback; | 11 import org.chromium.base.Callback; |
| 12 import org.chromium.base.ThreadUtils; | 12 import org.chromium.base.ThreadUtils; |
| 13 import org.chromium.base.test.util.CommandLineFlags; | 13 import org.chromium.base.test.util.CommandLineFlags; |
| 14 import org.chromium.chrome.browser.ChromeActivity; | 14 import org.chromium.chrome.browser.ChromeActivity; |
| 15 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge.OfflinePageMod elObserver; | 15 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge.OfflinePageMod elObserver; |
| 16 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge.SavePageCallba ck; | 16 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge.SavePageCallba ck; |
| 17 import org.chromium.chrome.browser.profiles.Profile; | 17 import org.chromium.chrome.browser.profiles.Profile; |
| 18 import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarController; | 18 import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarController; |
| 19 import org.chromium.chrome.browser.tab.Tab; | 19 import org.chromium.chrome.browser.tab.Tab; |
| 20 import org.chromium.chrome.test.ChromeActivityTestCaseBase; | 20 import org.chromium.chrome.test.ChromeActivityTestCaseBase; |
| 21 import org.chromium.components.offlinepages.SavePageResult; | 21 import org.chromium.components.offlinepages.SavePageResult; |
| 22 import org.chromium.content.browser.test.util.Criteria; | 22 import org.chromium.content.browser.test.util.Criteria; |
| 23 import org.chromium.content.browser.test.util.CriteriaHelper; | 23 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 24 import org.chromium.net.ConnectionType; | 24 import org.chromium.net.ConnectionType; |
| 25 import org.chromium.net.NetworkChangeNotifier; | 25 import org.chromium.net.NetworkChangeNotifier; |
| 26 import org.chromium.net.test.EmbeddedTestServer; | 26 import org.chromium.net.test.EmbeddedTestServer; |
| 27 | 27 |
| 28 import java.io.File; | |
| 28 import java.util.ArrayList; | 29 import java.util.ArrayList; |
| 29 import java.util.List; | 30 import java.util.List; |
| 31 import java.util.UUID; | |
| 30 import java.util.concurrent.Semaphore; | 32 import java.util.concurrent.Semaphore; |
| 31 import java.util.concurrent.TimeUnit; | 33 import java.util.concurrent.TimeUnit; |
| 32 | 34 |
| 33 /** Unit tests for {@link OfflinePageUtils}. */ | 35 /** Unit tests for {@link OfflinePageUtils}. */ |
| 34 @CommandLineFlags.Add("enable-features=OfflineBookmarks") | 36 @CommandLineFlags.Add("enable-features=OfflineBookmarks") |
| 35 public class OfflinePageUtilsTest extends ChromeActivityTestCaseBase<ChromeActiv ity> { | 37 public class OfflinePageUtilsTest extends ChromeActivityTestCaseBase<ChromeActiv ity> { |
| 36 private static final String TEST_PAGE = "/chrome/test/data/android/about.htm l"; | 38 private static final String TEST_PAGE = "/chrome/test/data/android/about.htm l"; |
| 37 private static final int TIMEOUT_MS = 5000; | 39 private static final int TIMEOUT_MS = 5000; |
| 38 private static final ClientId BOOKMARK_ID = | 40 private static final ClientId BOOKMARK_ID = |
| 39 new ClientId(OfflinePageBridge.BOOKMARK_NAMESPACE, "1234"); | 41 new ClientId(OfflinePageBridge.BOOKMARK_NAMESPACE, "1234"); |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 229 public void onResult(List<OfflinePageItem> allPages) { | 231 public void onResult(List<OfflinePageItem> allPages) { |
| 230 result.addAll(allPages); | 232 result.addAll(allPages); |
| 231 semaphore.release(); | 233 semaphore.release(); |
| 232 } | 234 } |
| 233 }); | 235 }); |
| 234 } | 236 } |
| 235 }); | 237 }); |
| 236 assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS)); | 238 assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS)); |
| 237 return result; | 239 return result; |
| 238 } | 240 } |
| 241 | |
| 242 @SmallTest | |
| 243 public void testCopyToShareableLocation() throws Exception { | |
| 244 // Save an offline page. | |
| 245 String testUrl = mTestServer.getURL(TEST_PAGE); | |
|
Theresa
2016/08/15 20:50:00
It looks like the code to save an offline page is
Vivian
2016/08/16 17:40:12
Done.
| |
| 246 loadUrl(testUrl); | |
| 247 savePage(SavePageResult.SUCCESS, testUrl); | |
| 248 | |
| 249 // Get an offline page from the list and obtain the file path. | |
| 250 List<OfflinePageItem> allPages = getAllPages(); | |
| 251 OfflinePageItem offlinePage = allPages.get(0); | |
| 252 String offlinePageFilePath = offlinePage.getFilePath(); | |
| 253 | |
| 254 File offlinePageOriginal = new File(offlinePageFilePath); | |
| 255 | |
| 256 // Clear the directory before perform file copying. | |
| 257 Context context = getActivity().getBaseContext(); | |
| 258 OfflinePageUtils.clearSharedOfflineFiles(context); | |
| 259 | |
| 260 File offlineCacheDir = | |
| 261 OfflinePageUtils.getDirectoryForOfflineSharing(getActivity().get BaseContext()); | |
| 262 | |
| 263 boolean success = (offlineCacheDir != null); | |
| 264 assertTrue("Should be able to create subdirectory in shareable directory .", success); | |
| 265 | |
| 266 // Use a random file name for test issue. | |
|
Theresa
2016/08/15 20:50:00
What is the test issue that requires using a rando
Vivian
2016/08/16 17:40:12
This is deprecated after adding clearing directory
| |
| 267 File offlinePageShareable = new File(offlineCacheDir, UUID.randomUUID(). toString()); | |
| 268 | |
| 269 assertFalse("Should not exist a file with same file name,", offlinePageS hareable.exists()); | |
|
Theresa
2016/08/15 20:50:00
"The shareable offline page file should lot alread
Vivian
2016/08/16 17:40:12
Done.
| |
| 270 assertTrue("Should be able to copy file to shareable location.", | |
| 271 OfflinePageUtils.copyToShareableLocation( | |
| 272 offlinePageOriginal, offlinePageShareable)); | |
| 273 assertEquals("File copy result incorrect", offlinePageOriginal.length(), | |
| 274 offlinePageShareable.length()); | |
| 275 } | |
| 276 | |
| 277 @SmallTest | |
| 278 public void testDeleteSharedOfflineFiles() throws Exception { | |
| 279 // Save an offline page. | |
| 280 String testUrl = mTestServer.getURL(TEST_PAGE); | |
| 281 loadUrl(testUrl); | |
| 282 savePage(SavePageResult.SUCCESS, testUrl); | |
| 283 | |
| 284 // Copies file to external cache directory. | |
| 285 List<OfflinePageItem> allPages = getAllPages(); | |
| 286 OfflinePageItem offlinePage = allPages.get(0); | |
| 287 String offlinePageFilePath = offlinePage.getFilePath(); | |
| 288 | |
| 289 File offlinePageOriginal = new File(offlinePageFilePath); | |
| 290 | |
| 291 File offlineCacheDir = | |
| 292 OfflinePageUtils.getDirectoryForOfflineSharing(getActivity().get BaseContext()); | |
| 293 | |
| 294 boolean success = (offlineCacheDir != null); | |
| 295 assertTrue("Should be able to create subdirectory in shareable directory .", success); | |
| 296 | |
| 297 File offlinePageShareable = new File(offlineCacheDir, offlinePageOrigina l.getName()); | |
| 298 if (!offlinePageShareable.exists()) { | |
| 299 assertTrue("Should be able to copy file to shareable location.", | |
| 300 OfflinePageUtils.copyToShareableLocation( | |
| 301 offlinePageOriginal, offlinePageShareable)); | |
| 302 } | |
| 303 | |
| 304 // Clear files. | |
| 305 Context context = getActivity().getBaseContext(); | |
| 306 OfflinePageUtils.clearSharedOfflineFiles(context); | |
| 307 | |
| 308 assertFalse("Chache directory should be deleted.", offlineCacheDir.exist s()); | |
| 309 } | |
| 239 } | 310 } |
| OLD | NEW |