1 // Copyright 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.
5 package org.chromium.android_webview.test;
7 import android.test.suitebuilder.annotation.SmallTest;
8 import android.webkit.ValueCallback;
10 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
12 import org.chromium.android_webview.AwContents;
13 import org.chromium.base.ThreadUtils;
14 import org.chromium.base.test.util.Feature;
15 import org.chromium.base.test.util.UrlUtils;
18 import java.util.concurrent.Semaphore;
19 import java.util.concurrent.TimeUnit;
20 import java.util.concurrent.atomic.AtomicReference;
23 * Test suite for the WebView.saveWebArchive feature.
25 public class ArchiveTest extends AwTestBase {
27 private static final long TEST_TIMEOUT = scaleTimeout(20000L);
29 private static final String TEST_PAGE = UrlUtils.encodeHtmlDataUri(
30 "<html><head></head><body>test</body></html>");
32 private TestAwContentsClient mContentsClient = new TestAwContentsClient();
33 private AwTestContainerView mTestContainerView;
36 protected void setUp() throws Exception {
38 mTestContainerView = createAwTestContainerViewOnMainSync(mContentsClient);
41 private void deleteFile(String path) {
42 File file = new File(path);
44 assertTrue(file.delete());
45 assertFalse(file.exists());
48 private void doArchiveTest(final AwContents contents, final String path,
49 final boolean autoName, String expectedPath) throws InterruptedException {
50 if (expectedPath != null) {
51 deleteFile(expectedPath);
54 // Set up a handler to handle the completion callback
55 final Semaphore s = new Semaphore(0);
56 final AtomicReference<String> msgPath = new AtomicReference<String>();
57 final ValueCallback<String> callback = new ValueCallback<String>() {
59 public void onReceiveValue(String path) {
65 // Generate MHTML and wait for completion
66 ThreadUtils.runOnUiThread(new Runnable() {
69 contents.saveWebArchive(path, autoName, callback);
72 assertTrue(s.tryAcquire(TEST_TIMEOUT, TimeUnit.MILLISECONDS));
74 assertEquals(expectedPath, msgPath.get());
75 if (expectedPath != null) {
76 File file = new File(expectedPath);
77 assertTrue(file.exists());
78 assertTrue(file.length() > 0);
80 // A path was provided, but the expected path was null. This means the save should have
81 // failed, and so there shouldn't be a file path path.
83 assertFalse(new File(path).exists());
89 @Feature({"AndroidWebView"})
90 public void testExplicitGoodPath() throws Throwable {
91 final String path = new File(getActivity().getFilesDir(), "test.mht").getAbsolutePath();
94 loadUrlSync(mTestContainerView.getAwContents(),
95 mContentsClient.getOnPageFinishedHelper(), TEST_PAGE);
97 doArchiveTest(mTestContainerView.getAwContents(), path, false, path);
101 @Feature({"AndroidWebView"})
102 public void testAutoGoodPath() throws Throwable {
103 final String path = getActivity().getFilesDir().getAbsolutePath() + "/";
105 loadUrlSync(mTestContainerView.getAwContents(),
106 mContentsClient.getOnPageFinishedHelper(), TEST_PAGE);
108 // Create the first archive
110 String expectedPath = path + "index.mht";
111 doArchiveTest(mTestContainerView.getAwContents(), path, true, expectedPath);
114 // Create a second archive, making sure that the second archive's name is auto incremented.
116 String expectedPath = path + "index-1.mht";
117 doArchiveTest(mTestContainerView.getAwContents(), path, true, expectedPath);
122 @Feature({"AndroidWebView"})
123 public void testExplicitBadPath() throws Throwable {
124 final String path = new File("/foo/bar/baz.mht").getAbsolutePath();
127 loadUrlSync(mTestContainerView.getAwContents(),
128 mContentsClient.getOnPageFinishedHelper(), TEST_PAGE);
130 doArchiveTest(mTestContainerView.getAwContents(), path, false, null);
134 @Feature({"AndroidWebView"})
135 public void testAutoBadPath() throws Throwable {
136 final String path = new File("/foo/bar/").getAbsolutePath();
139 loadUrlSync(mTestContainerView.getAwContents(),
140 mContentsClient.getOnPageFinishedHelper(), TEST_PAGE);
142 doArchiveTest(mTestContainerView.getAwContents(), path, true, null);