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.Log; | 12 import org.chromium.base.Log; |
| 13 import org.chromium.base.ThreadUtils; | 13 import org.chromium.base.ThreadUtils; |
| 14 import org.chromium.base.test.util.CommandLineFlags; | 14 import org.chromium.base.test.util.CommandLineFlags; |
| 15 import org.chromium.chrome.browser.ChromeActivity; | 15 import org.chromium.chrome.browser.ChromeActivity; |
| 16 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge.OfflinePageMod elObserver; | 16 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge.OfflinePageMod elObserver; |
| 17 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge.SavePageCallba ck; | 17 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge.SavePageCallba ck; |
| 18 import org.chromium.chrome.browser.profiles.Profile; | 18 import org.chromium.chrome.browser.profiles.Profile; |
| 19 import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarController; | 19 import org.chromium.chrome.browser.snackbar.SnackbarManager.SnackbarController; |
| 20 import org.chromium.chrome.browser.tab.Tab; | 20 import org.chromium.chrome.browser.tab.Tab; |
| 21 import org.chromium.chrome.test.ChromeActivityTestCaseBase; | 21 import org.chromium.chrome.test.ChromeActivityTestCaseBase; |
| 22 import org.chromium.components.offlinepages.SavePageResult; | 22 import org.chromium.components.offlinepages.SavePageResult; |
| 23 import org.chromium.content.browser.test.util.Criteria; | 23 import org.chromium.content.browser.test.util.Criteria; |
| 24 import org.chromium.content.browser.test.util.CriteriaHelper; | 24 import org.chromium.content.browser.test.util.CriteriaHelper; |
| 25 import org.chromium.net.ConnectionType; | 25 import org.chromium.net.ConnectionType; |
| 26 import org.chromium.net.NetworkChangeNotifier; | 26 import org.chromium.net.NetworkChangeNotifier; |
| 27 import org.chromium.net.test.EmbeddedTestServer; | 27 import org.chromium.net.test.EmbeddedTestServer; |
| 28 | 28 |
| 29 import java.io.File; | |
| 29 import java.util.ArrayList; | 30 import java.util.ArrayList; |
| 30 import java.util.List; | 31 import java.util.List; |
| 32 import java.util.UUID; | |
| 31 import java.util.concurrent.Semaphore; | 33 import java.util.concurrent.Semaphore; |
| 32 import java.util.concurrent.TimeUnit; | 34 import java.util.concurrent.TimeUnit; |
| 33 | 35 |
| 34 /** Unit tests for {@link OfflinePageUtils}. */ | 36 /** Unit tests for {@link OfflinePageUtils}. */ |
| 35 @CommandLineFlags.Add("enable-features=OfflineBookmarks") | 37 @CommandLineFlags.Add("enable-features=OfflineBookmarks") |
| 36 public class OfflinePageUtilsTest extends ChromeActivityTestCaseBase<ChromeActiv ity> { | 38 public class OfflinePageUtilsTest extends ChromeActivityTestCaseBase<ChromeActiv ity> { |
| 37 private static final String TAG = "OfflinePageUtilsTest"; | 39 private static final String TAG = "OfflinePageUtilsTest"; |
| 38 private static final String TEST_PAGE = "/chrome/test/data/android/about.htm l"; | 40 private static final String TEST_PAGE = "/chrome/test/data/android/about.htm l"; |
| 41 private static final String CACHE_DIRECTORY = | |
| 42 "/storage/emulated/0/Android/data/org.chromium.chrome/cache/offline- pages"; | |
|
gone
2016/08/12 01:26:13
1) This directory only applies to local builds, wh
Vivian
2016/08/12 23:07:13
Removed this line and used pull directory from the
| |
| 39 private static final int TIMEOUT_MS = 5000; | 43 private static final int TIMEOUT_MS = 5000; |
| 40 private static final ClientId BOOKMARK_ID = | 44 private static final ClientId BOOKMARK_ID = |
| 41 new ClientId(OfflinePageBridge.BOOKMARK_NAMESPACE, "1234"); | 45 new ClientId(OfflinePageBridge.BOOKMARK_NAMESPACE, "1234"); |
| 42 | 46 |
| 43 private OfflinePageBridge mOfflinePageBridge; | 47 private OfflinePageBridge mOfflinePageBridge; |
| 44 private EmbeddedTestServer mTestServer; | 48 private EmbeddedTestServer mTestServer; |
| 45 | 49 |
| 46 public OfflinePageUtilsTest() { | 50 public OfflinePageUtilsTest() { |
| 47 super(ChromeActivity.class); | 51 super(ChromeActivity.class); |
| 48 } | 52 } |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 public void onResult(List<OfflinePageItem> allPages) { | 239 public void onResult(List<OfflinePageItem> allPages) { |
| 236 result.addAll(allPages); | 240 result.addAll(allPages); |
| 237 semaphore.release(); | 241 semaphore.release(); |
| 238 } | 242 } |
| 239 }); | 243 }); |
| 240 } | 244 } |
| 241 }); | 245 }); |
| 242 assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS)); | 246 assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS)); |
| 243 return result; | 247 return result; |
| 244 } | 248 } |
| 249 | |
| 250 @SmallTest | |
| 251 public void testCopyToShareableLocation() throws Exception { | |
| 252 // Save an offline page. | |
| 253 String testUrl = mTestServer.getURL(TEST_PAGE); | |
| 254 loadUrl(testUrl); | |
| 255 savePage(SavePageResult.SUCCESS, testUrl); | |
| 256 | |
| 257 // Get an offline page from the list and obtain the file path. | |
| 258 List<OfflinePageItem> allPages = getAllPages(); | |
| 259 OfflinePageItem offlinePage = allPages.get(0); | |
| 260 String offlinePageFilePath = offlinePage.getFilePath(); | |
| 261 | |
| 262 File offlinePageOriginal = new File(offlinePageFilePath); | |
| 263 | |
| 264 File offlineCacheDir = new File(CACHE_DIRECTORY); | |
| 265 | |
| 266 boolean success = offlineCacheDir.exists() || offlineCacheDir.mkdir(); | |
| 267 assertTrue("Should be able to create subdirectory in shareable directory .", success); | |
| 268 | |
| 269 // Use a random file name for test issue. | |
| 270 File offlinePageShareable = new File(CACHE_DIRECTORY, UUID.randomUUID(). toString()); | |
| 271 if (!offlinePageShareable.exists()) { | |
|
gone
2016/08/12 01:26:13
What happens if the file doesn't exist? Should th
Vivian
2016/08/12 23:07:13
Added check for file existence.
| |
| 272 assertTrue("Should be able to copy file to shareable location.", | |
| 273 OfflinePageUtils.copyToShareableLocation( | |
| 274 offlinePageOriginal, offlinePageShareable)); | |
| 275 assertEquals("File copy result incorrect", offlinePageOriginal.lengt h(), | |
| 276 offlinePageShareable.length()); | |
| 277 } | |
| 278 } | |
| 279 | |
| 280 @SmallTest | |
| 281 public void testDeleteSharedOfflineFiles() throws Exception { | |
| 282 // Save an offline page. | |
| 283 String testUrl = mTestServer.getURL(TEST_PAGE); | |
| 284 loadUrl(testUrl); | |
| 285 savePage(SavePageResult.SUCCESS, testUrl); | |
| 286 | |
| 287 // Copies file to external cache directory. | |
| 288 List<OfflinePageItem> allPages = getAllPages(); | |
| 289 OfflinePageItem offlinePage = allPages.get(0); | |
| 290 String offlinePageFilePath = offlinePage.getFilePath(); | |
| 291 | |
| 292 File offlinePageOriginal = new File(offlinePageFilePath); | |
| 293 | |
| 294 File offlineCacheDir = new File(CACHE_DIRECTORY); | |
| 295 boolean success = true; | |
| 296 if (!offlineCacheDir.exists() && !offlineCacheDir.mkdir()) { | |
| 297 success = false; | |
| 298 } | |
| 299 assertTrue("Unable to create subdirectory in shareable directory.", succ ess); | |
| 300 | |
| 301 File offlinePageShareable = new File(CACHE_DIRECTORY, offlinePageOrigina l.getName()); | |
| 302 if (!offlinePageShareable.exists()) { | |
| 303 assertTrue("Should be able to copy file to shareable location.", | |
| 304 OfflinePageUtils.copyToShareableLocation( | |
| 305 offlinePageOriginal, offlinePageShareable)); | |
| 306 } | |
| 307 | |
| 308 // Clear files. | |
| 309 Context context = getActivity().getBaseContext(); | |
| 310 OfflinePageUtils.deleteSharedOfflineFiles(offlineCacheDir); | |
| 311 | |
| 312 assertFalse("Chache directory should be deleted.", offlineCacheDir.exist s()); | |
| 313 } | |
| 245 } | 314 } |
| OLD | NEW |