| 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.android_webview.test; |
| 6 | |
| 7 | import android.test.suitebuilder.annotation.SmallTest; |
| 8 | import android.webkit.ValueCallback; |
| 9 | |
| 10 | import org.chromium.android_webview.AwContents; |
| 11 | import org.chromium.base.ThreadUtils; |
| 12 | import org.chromium.base.test.util.Feature; |
| 13 | import org.chromium.base.test.util.UrlUtils; |
| 14 | |
| 15 | import java.io.File; |
| 16 | import java.util.concurrent.Semaphore; |
| 17 | import java.util.concurrent.TimeUnit; |
| 18 | import java.util.concurrent.atomic.AtomicReference; |
| 19 | |
| 20 | public class ArchiveTest extends AwTestBase { |
| 21 | |
| 22 | private static final long TEST_TIMEOUT = 20000L; |
| 23 | |
| 24 | private static final String TEST_PAGE = UrlUtils.encodeHtmlDataUri( |
| 25 | "<html><head></head><body>test</body></html>"); |
| 26 | |
| 27 | private TestAwContentsClient mContentsClient = new TestAwContentsClient(); |
| 28 | private AwTestContainerView mTestContainerView; |
| 29 | |
| 30 | @Override |
| 31 | protected void setUp() throws Exception { |
| 32 | super.setUp(); |
| 33 | mTestContainerView = createAwTestContainerViewOnMainSync(mContentsClient); |
| 34 | } |
| 35 | |
| 36 | private void doArchiveTest(final AwContents contents, final String path, |
| 37 | final boolean autoName, String expectedPath) throws InterruptedException { |
| 38 | if (expectedPath != null) { |
| 39 | File file = new File(expectedPath); |
| 40 | file.delete(); |
| 41 | } |
| 42 | |
| 43 | // Set up a handler to handle the completion callback |
| 44 | final Semaphore s = new Semaphore(0); |
| 45 | final AtomicReference<String> msgPath = new AtomicReference<String>(); |
| 46 | final ValueCallback<String> callback = new ValueCallback<String>() { |
| 47 | @Override |
| 48 | public void onReceiveValue(String path) { |
| 49 | msgPath.set(path); |
| 50 | s.release(); |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | // Generate MHTML and wait for completion |
| 55 | ThreadUtils.runOnUiThread(new Runnable() { |
| 56 | @Override |
| 57 | public void run() { |
| 58 | contents.saveWebArchive(path, autoName, callback); |
| 59 | } |
| 60 | }); |
| 61 | assertTrue(s.tryAcquire(TEST_TIMEOUT, TimeUnit.MILLISECONDS)); |
| 62 | |
| 63 | assertEquals(expectedPath, msgPath.get()); |
| 64 | if (expectedPath != null) { |
| 65 | File file = new File(expectedPath); |
| 66 | assertTrue(file.exists()); |
| 67 | assertTrue(file.length() > 0); |
| 68 | } else { |
| 69 | // A path was provided, but the expected path was null. This means the save should have |
| 70 | // failed, and so there shouldn't be a file path path. |
| 71 | if (path != null) { |
| 72 | assertFalse(new File(path).exists()); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | @SmallTest |
| 78 | @Feature({"AndroidWebView"}) |
| 79 | public void testExplicitGoodPath() throws Throwable { |
| 80 | final String path = new File(getActivity().getFilesDir(), "test.mht").getAbsolutePath(); |
| 81 | File file = new File(path); |
| 82 | file.delete(); |
| 83 | assertFalse(file.exists()); |
| 84 | |
| 85 | loadUrlSync(mTestContainerView.getAwContents(), |
| 86 | mContentsClient.getOnPageFinishedHelper(), TEST_PAGE); |
| 87 | |
| 88 | doArchiveTest(mTestContainerView.getAwContents(), path, false, path); |
| 89 | } |
| 90 | |
| 91 | @SmallTest |
| 92 | @Feature({"AndroidWebView"}) |
| 93 | public void testAutoGoodPath() throws Throwable { |
| 94 | final String path = getActivity().getFilesDir().getAbsolutePath() + "/"; |
| 95 | |
| 96 | loadUrlSync(mTestContainerView.getAwContents(), |
| 97 | mContentsClient.getOnPageFinishedHelper(), TEST_PAGE); |
| 98 | |
| 99 | // Create the first archive |
| 100 | { |
| 101 | String expectedPath = path + "index.mht"; |
| 102 | doArchiveTest(mTestContainerView.getAwContents(), path, true, expectedPath); |
| 103 | } |
| 104 | |
| 105 | // Create a second archive, making sure that the second archive's name is auto incremented. |
| 106 | { |
| 107 | String expectedPath = path + "index-1.mht"; |
| 108 | doArchiveTest(mTestContainerView.getAwContents(), path, true, expectedPath); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | @SmallTest |
| 113 | @Feature({"AndroidWebView"}) |
| 114 | public void testExplicitBadPath() throws Throwable { |
| 115 | final String path = new File("/foo/bar/baz.mht").getAbsolutePath(); |
| 116 | File file = new File(path); |
| 117 | file.delete(); |
| 118 | assertFalse(file.exists()); |
| 119 | |
| 120 | loadUrlSync(mTestContainerView.getAwContents(), |
| 121 | mContentsClient.getOnPageFinishedHelper(), TEST_PAGE); |
| 122 | |
| 123 | doArchiveTest(mTestContainerView.getAwContents(), path, false, null); |
| 124 | } |
| 125 | |
| 126 | @SmallTest |
| 127 | @Feature({"AndroidWebView"}) |
| 128 | public void testAutoBadPath() throws Throwable { |
| 129 | final String path = new File("/foo/bar/").getAbsolutePath(); |
| 130 | File file = new File(path); |
| 131 | file.delete(); |
| 132 | assertFalse(file.exists()); |
| 133 | |
| 134 | loadUrlSync(mTestContainerView.getAwContents(), |
| 135 | mContentsClient.getOnPageFinishedHelper(), TEST_PAGE); |
| 136 | |
| 137 | doArchiveTest(mTestContainerView.getAwContents(), path, true, null); |
| 138 | } |
| 139 | |
| 140 | } |