OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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.chrome.browser; |
| 6 |
| 7 import android.test.suitebuilder.annotation.LargeTest; |
| 8 |
| 9 import org.chromium.base.test.util.Feature; |
| 10 import org.chromium.base.test.util.UrlUtils; |
| 11 import org.chromium.chrome.test.ChromeTabbedActivityTestBase; |
| 12 import org.chromium.chrome.test.util.ChromeTabUtils; |
| 13 import org.chromium.content.browser.test.util.JavaScriptUtils; |
| 14 |
| 15 import java.util.concurrent.TimeoutException; |
| 16 |
| 17 /** |
| 18 * Tests for evaluation of JavaScript. |
| 19 */ |
| 20 public class JavaScriptEvalChromeTest extends ChromeTabbedActivityTestBase { |
| 21 private static final String JSTEST_URL = UrlUtils.encodeHtmlDataUri( |
| 22 "<html><head><script>" |
| 23 + " var counter = 0;" |
| 24 + " function add2() { counter = counter + 2; return counter; }" |
| 25 + " function foobar() { return 'foobar'; }" |
| 26 + "</script></head>" |
| 27 + "<body><button id=\"test\">Test button</button></body></html>"); |
| 28 |
| 29 @Override |
| 30 public void startMainActivity() throws InterruptedException { |
| 31 startMainActivityWithURL(JSTEST_URL); |
| 32 } |
| 33 |
| 34 /** |
| 35 * Tests that evaluation of JavaScript for test purposes (using JavaScriptUt
ils, DOMUtils etc) |
| 36 * works even in presence of "background" (non-test-initiated) JavaScript ev
aluation activity. |
| 37 */ |
| 38 @LargeTest |
| 39 @Feature({"Browser"}) |
| 40 public void testJavaScriptEvalIsCorrectlyOrderedWithinOneTab() |
| 41 throws InterruptedException, TimeoutException { |
| 42 Tab tab1 = getActivity().getActivityTab(); |
| 43 ChromeTabUtils.newTabFromMenu(getInstrumentation(), getActivity()); |
| 44 Tab tab2 = getActivity().getActivityTab(); |
| 45 loadUrl(JSTEST_URL); |
| 46 |
| 47 ChromeTabUtils.switchTabInCurrentTabModel(getActivity(), tab1.getId()); |
| 48 assertFalse("Tab didn't open", tab1 == tab2); |
| 49 |
| 50 JavaScriptUtils.executeJavaScriptAndWaitForResult( |
| 51 tab1.getWebContents(), "counter = 0;"); |
| 52 JavaScriptUtils.executeJavaScriptAndWaitForResult( |
| 53 tab2.getWebContents(), "counter = 1;"); |
| 54 |
| 55 for (int i = 1; i <= 30; ++i) { |
| 56 for (int j = 0; j < 5; ++j) { |
| 57 // Start evaluation of a JavaScript script -- we don't need a re
sult. |
| 58 tab1.getWebContents().evaluateJavaScript("foobar();", null); |
| 59 tab2.getWebContents().evaluateJavaScript("foobar();", null); |
| 60 } |
| 61 assertEquals("Incorrect JavaScript evaluation result on tab1", |
| 62 i * 2, |
| 63 Integer.parseInt( |
| 64 JavaScriptUtils.executeJavaScriptAndWaitForResult( |
| 65 tab1.getWebContents(), "add2()"))); |
| 66 for (int j = 0; j < 5; ++j) { |
| 67 // Start evaluation of a JavaScript script -- we don't need a re
sult. |
| 68 tab1.getWebContents().evaluateJavaScript("foobar();", null); |
| 69 tab2.getWebContents().evaluateJavaScript("foobar();", null); |
| 70 } |
| 71 assertEquals("Incorrect JavaScript evaluation result on tab2", |
| 72 i * 2 + 1, |
| 73 Integer.parseInt( |
| 74 JavaScriptUtils.executeJavaScriptAndWaitForResult( |
| 75 tab2.getWebContents(), "add2()"))); |
| 76 } |
| 77 } |
| 78 } |
OLD | NEW |