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 b1274372a4c6015019895988983421589f4d70d0..2d1dcb9fbdf76c1e81924956330b11ce06942684 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 |
| @@ -25,8 +25,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; |
| @@ -236,4 +238,73 @@ 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); |
|
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.
|
| + 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); |
| + |
| + // Clear the directory before perform file copying. |
| + Context context = getActivity().getBaseContext(); |
| + OfflinePageUtils.clearSharedOfflineFiles(context); |
| + |
| + File offlineCacheDir = |
| + OfflinePageUtils.getDirectoryForOfflineSharing(getActivity().getBaseContext()); |
| + |
| + boolean success = (offlineCacheDir != null); |
| + assertTrue("Should be able to create subdirectory in shareable directory.", success); |
| + |
| + // 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
|
| + File offlinePageShareable = new File(offlineCacheDir, UUID.randomUUID().toString()); |
| + |
| + assertFalse("Should not exist a file with same file name,", offlinePageShareable.exists()); |
|
Theresa
2016/08/15 20:50:00
"The shareable offline page file should lot alread
Vivian
2016/08/16 17:40:12
Done.
|
| + 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 = |
| + OfflinePageUtils.getDirectoryForOfflineSharing(getActivity().getBaseContext()); |
| + |
| + boolean success = (offlineCacheDir != null); |
| + assertTrue("Should be able to create subdirectory in shareable directory.", success); |
| + |
| + File offlinePageShareable = new File(offlineCacheDir, 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.clearSharedOfflineFiles(context); |
| + |
| + assertFalse("Chache directory should be deleted.", offlineCacheDir.exists()); |
| + } |
| } |