OLD | NEW |
(Empty) | |
| 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.Feature; |
| 13 |
| 14 import java.io.File; |
| 15 import java.util.concurrent.Semaphore; |
| 16 import java.util.concurrent.TimeUnit; |
| 17 import java.util.concurrent.atomic.AtomicReference; |
| 18 |
| 19 public class ArchiveTest extends AndroidWebViewTestBase { |
| 20 |
| 21 private static final long TEST_TIMEOUT = 20000L; |
| 22 |
| 23 private static final String TEST_PAGE = |
| 24 "data:text/html;utf-8,<html><head></head><body>test</body></html>"; |
| 25 |
| 26 private TestAwContentsClient mContentsClient = new TestAwContentsClient(); |
| 27 private AwTestContainerView mTestContainerView; |
| 28 |
| 29 @Override |
| 30 protected void setUp() throws Exception { |
| 31 mTestContainerView = createAwTestContainerViewOnMainSync(mContentsClient
); |
| 32 } |
| 33 |
| 34 private void doArchiveTest(final AwContents contents, final String path, |
| 35 final boolean autoName, String expectedPath) throws InterruptedExcep
tion { |
| 36 if (expectedPath != null) { |
| 37 File file = new File(expectedPath); |
| 38 file.delete(); |
| 39 } |
| 40 |
| 41 // Set up a handler to handle the completion callback |
| 42 final Semaphore s = new Semaphore(0); |
| 43 final AtomicReference<String> msgPath = new AtomicReference<String>(); |
| 44 final ValueCallback<String> callback = new ValueCallback<String>() { |
| 45 @Override |
| 46 public void onReceiveValue(String path) { |
| 47 msgPath.set(path); |
| 48 s.release(); |
| 49 } |
| 50 }; |
| 51 |
| 52 // Generate MHTML and wait for completion |
| 53 ThreadUtils.runOnUiThread(new Runnable() { |
| 54 @Override |
| 55 public void run() { |
| 56 contents.saveWebArchive(path, autoName, callback); |
| 57 } |
| 58 }); |
| 59 assertTrue(s.tryAcquire(TEST_TIMEOUT, TimeUnit.MILLISECONDS)); |
| 60 |
| 61 assertEquals(expectedPath, msgPath.get()); |
| 62 if (expectedPath != null) { |
| 63 File file = new File(expectedPath); |
| 64 assertTrue(file.exists()); |
| 65 assertTrue(file.length() > 0); |
| 66 } else { |
| 67 // A path was provided, but the expected path was null. This means t
he save should have |
| 68 // failed, and so there shouldn't be a file path path. |
| 69 if (path != null) { |
| 70 assertFalse(new File(path).exists()); |
| 71 } |
| 72 } |
| 73 } |
| 74 |
| 75 @SmallTest |
| 76 @Feature({"Android-WebView"}) |
| 77 public void testExplicitGoodPath() throws Throwable { |
| 78 final String path = new File(getActivity().getFilesDir(), "test.mht").ge
tAbsolutePath(); |
| 79 File file = new File(path); |
| 80 file.delete(); |
| 81 assertFalse(file.exists()); |
| 82 |
| 83 loadUrlSync(mTestContainerView.getContentViewCore(), |
| 84 mContentsClient.getOnPageFinishedHelper(), TEST_PAGE); |
| 85 |
| 86 doArchiveTest(mTestContainerView.getAwContents(), path, false, path); |
| 87 } |
| 88 |
| 89 @SmallTest |
| 90 @Feature({"Android-WebView"}) |
| 91 public void testAutoGoodPath() throws Throwable { |
| 92 final String path = getActivity().getFilesDir().getAbsolutePath() + "/"; |
| 93 |
| 94 loadUrlSync(mTestContainerView.getContentViewCore(), |
| 95 mContentsClient.getOnPageFinishedHelper(), TEST_PAGE); |
| 96 |
| 97 // Create the first archive |
| 98 { |
| 99 String expectedPath = path + "index.mht"; |
| 100 doArchiveTest(mTestContainerView.getAwContents(), path, true, expect
edPath); |
| 101 } |
| 102 |
| 103 // Create a second archive, making sure that the second archive's name i
s auto incremented. |
| 104 { |
| 105 String expectedPath = path + "index-1.mht"; |
| 106 doArchiveTest(mTestContainerView.getAwContents(), path, true, expect
edPath); |
| 107 } |
| 108 } |
| 109 |
| 110 @SmallTest |
| 111 @Feature({"Android-WebView"}) |
| 112 public void testExplicitBadPath() throws Throwable { |
| 113 final String path = new File("/foo/bar/baz.mht").getAbsolutePath(); |
| 114 File file = new File(path); |
| 115 file.delete(); |
| 116 assertFalse(file.exists()); |
| 117 |
| 118 loadUrlSync(mTestContainerView.getContentViewCore(), |
| 119 mContentsClient.getOnPageFinishedHelper(), TEST_PAGE); |
| 120 |
| 121 doArchiveTest(mTestContainerView.getAwContents(), path, false, null); |
| 122 } |
| 123 |
| 124 @SmallTest |
| 125 @Feature({"Android-WebView"}) |
| 126 public void testAutoBadPath() throws Throwable { |
| 127 final String path = new File("/foo/bar/").getAbsolutePath(); |
| 128 File file = new File(path); |
| 129 file.delete(); |
| 130 assertFalse(file.exists()); |
| 131 |
| 132 loadUrlSync(mTestContainerView.getContentViewCore(), |
| 133 mContentsClient.getOnPageFinishedHelper(), TEST_PAGE); |
| 134 |
| 135 doArchiveTest(mTestContainerView.getAwContents(), path, true, null); |
| 136 } |
| 137 |
| 138 } |
OLD | NEW |