Chromium Code Reviews| Index: chrome/android/javatests/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtilsTest.java |
| diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtilsTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtilsTest.java |
| index 239cb671d4533550a43dfa5713ef0e490e59ddcf..a5a30944bd4acb5b989903230a19fd1b03eadb6d 100644 |
| --- a/chrome/android/javatests/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtilsTest.java |
| +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/offlinepages/OfflinePageUtilsTest.java |
| @@ -26,8 +26,10 @@ import org.chromium.net.ConnectionType; |
| import org.chromium.net.NetworkChangeNotifier; |
| import org.chromium.net.test.EmbeddedTestServer; |
| +import java.io.File; |
| import java.util.ArrayList; |
| import java.util.List; |
| +import java.util.UUID; |
| import java.util.concurrent.Semaphore; |
| import java.util.concurrent.TimeUnit; |
| @@ -36,6 +38,8 @@ import java.util.concurrent.TimeUnit; |
| public class OfflinePageUtilsTest extends ChromeActivityTestCaseBase<ChromeActivity> { |
| private static final String TAG = "OfflinePageUtilsTest"; |
| private static final String TEST_PAGE = "/chrome/test/data/android/about.html"; |
| + private static final String CACHE_DIRECTORY = |
| + "/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
|
| private static final int TIMEOUT_MS = 5000; |
| private static final ClientId BOOKMARK_ID = |
| new ClientId(OfflinePageBridge.BOOKMARK_NAMESPACE, "1234"); |
| @@ -242,4 +246,69 @@ public class OfflinePageUtilsTest extends ChromeActivityTestCaseBase<ChromeActiv |
| assertTrue(semaphore.tryAcquire(TIMEOUT_MS, TimeUnit.MILLISECONDS)); |
| return result; |
| } |
| + |
| + @SmallTest |
| + public void testCopyToShareableLocation() throws Exception { |
| + // Save an offline page. |
| + String testUrl = mTestServer.getURL(TEST_PAGE); |
| + loadUrl(testUrl); |
| + savePage(SavePageResult.SUCCESS, testUrl); |
| + |
| + // Get an offline page from the list and obtain the file path. |
| + List<OfflinePageItem> allPages = getAllPages(); |
| + OfflinePageItem offlinePage = allPages.get(0); |
| + String offlinePageFilePath = offlinePage.getFilePath(); |
| + |
| + File offlinePageOriginal = new File(offlinePageFilePath); |
| + |
| + File offlineCacheDir = new File(CACHE_DIRECTORY); |
| + |
| + boolean success = offlineCacheDir.exists() || offlineCacheDir.mkdir(); |
| + assertTrue("Should be able to create subdirectory in shareable directory.", success); |
| + |
| + // Use a random file name for test issue. |
| + File offlinePageShareable = new File(CACHE_DIRECTORY, UUID.randomUUID().toString()); |
| + 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.
|
| + assertTrue("Should be able to copy file to shareable location.", |
| + OfflinePageUtils.copyToShareableLocation( |
| + offlinePageOriginal, offlinePageShareable)); |
| + assertEquals("File copy result incorrect", offlinePageOriginal.length(), |
| + offlinePageShareable.length()); |
| + } |
| + } |
| + |
| + @SmallTest |
| + public void testDeleteSharedOfflineFiles() throws Exception { |
| + // Save an offline page. |
| + String testUrl = mTestServer.getURL(TEST_PAGE); |
| + loadUrl(testUrl); |
| + savePage(SavePageResult.SUCCESS, testUrl); |
| + |
| + // Copies file to external cache directory. |
| + List<OfflinePageItem> allPages = getAllPages(); |
| + OfflinePageItem offlinePage = allPages.get(0); |
| + String offlinePageFilePath = offlinePage.getFilePath(); |
| + |
| + File offlinePageOriginal = new File(offlinePageFilePath); |
| + |
| + File offlineCacheDir = new File(CACHE_DIRECTORY); |
| + boolean success = true; |
| + if (!offlineCacheDir.exists() && !offlineCacheDir.mkdir()) { |
| + success = false; |
| + } |
| + assertTrue("Unable to create subdirectory in shareable directory.", success); |
| + |
| + File offlinePageShareable = new File(CACHE_DIRECTORY, offlinePageOriginal.getName()); |
| + if (!offlinePageShareable.exists()) { |
| + assertTrue("Should be able to copy file to shareable location.", |
| + OfflinePageUtils.copyToShareableLocation( |
| + offlinePageOriginal, offlinePageShareable)); |
| + } |
| + |
| + // Clear files. |
| + Context context = getActivity().getBaseContext(); |
| + OfflinePageUtils.deleteSharedOfflineFiles(offlineCacheDir); |
| + |
| + assertFalse("Chache directory should be deleted.", offlineCacheDir.exists()); |
| + } |
| } |