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