TestsJavaScriptEvalTest.java revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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
5package org.chromium.content.browser;
6
7import android.graphics.Rect;
8import android.test.suitebuilder.annotation.LargeTest;
9
10import org.chromium.base.test.util.Feature;
11import org.chromium.base.test.util.UrlUtils;
12import org.chromium.content.browser.test.util.Criteria;
13import org.chromium.content.browser.test.util.CriteriaHelper;
14import org.chromium.content.browser.test.util.DOMUtils;
15import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
16import org.chromium.content.browser.test.util.TestCallbackHelperContainer.OnPageFinishedHelper;
17import org.chromium.content_shell_apk.ContentShellTestBase;
18
19import java.util.concurrent.TimeUnit;
20
21public class TestsJavaScriptEvalTest extends ContentShellTestBase {
22    private static final int WAIT_TIMEOUT_SECONDS = 2;
23    private static final String JSTEST_URL = UrlUtils.encodeHtmlDataUri(
24            "<html><head><script>" +
25            "  function foobar() { return 'foobar'; }" +
26            "</script></head>" +
27            "<body><button id=\"test\">Test button</button></body></html>");
28
29    public TestsJavaScriptEvalTest() {
30    }
31
32    /**
33     * Tests that evaluation of JavaScript for test purposes (using JavaScriptUtils, DOMUtils etc)
34     * works even in presence of "background" (non-test-initiated) JavaScript evaluation activity.
35     */
36    @LargeTest
37    @Feature({"Browser"})
38    public void testJavaScriptEvalIsCorrectlyOrdered()
39            throws InterruptedException, Exception, Throwable {
40        launchContentShellWithUrl(JSTEST_URL);
41        assertTrue("Page failed to load", waitForActiveShellToBeDoneLoading());
42
43        final ContentView view = getActivity().getActiveContentView();
44        final TestCallbackHelperContainer viewClient =
45                new TestCallbackHelperContainer(view);
46
47        for(int i = 0; i < 30; ++i) {
48            for(int j = 0; j < 10; ++j) {
49                // Start evaluation of a JavaScript script -- we don't need a result.
50                view.evaluateJavaScript("foobar();");
51            }
52            // DOMUtils does need to evaluate a JavaScript and get its result to get DOM bounds.
53            assertNotNull("Failed to get bounds",
54                    DOMUtils.getNodeBounds(view, viewClient, "test"));
55        }
56    }
57}
58