1 // Copyright 2019 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.weblayer.test; 6 7 import androidx.test.filters.SmallTest; 8 9 import org.json.JSONObject; 10 import org.junit.Assert; 11 import org.junit.Rule; 12 import org.junit.Test; 13 import org.junit.runner.RunWith; 14 15 import org.chromium.base.test.util.UrlUtils; 16 import org.chromium.content_public.browser.test.util.TestThreadUtils; 17 import org.chromium.weblayer.Tab; 18 import org.chromium.weblayer.shell.InstrumentationActivity; 19 20 /** 21 * Tests that script execution works as expected. 22 */ 23 @RunWith(WebLayerJUnit4ClassRunner.class) 24 public class ExecuteScriptTest { 25 @Rule 26 public InstrumentationActivityTestRule mActivityTestRule = 27 new InstrumentationActivityTestRule(); 28 29 private static final String DATA_URL = UrlUtils.encodeHtmlDataUri( 30 "<html><head><script>var bar = 10;</script></head><body>foo</body></html>"); 31 32 @Test 33 @SmallTest testBasicScript()34 public void testBasicScript() throws Exception { 35 InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(DATA_URL); 36 JSONObject result = mActivityTestRule.executeScriptSync( 37 "document.body.innerHTML", true /* useSeparateIsolate */); 38 Assert.assertEquals(result.getString(Tab.SCRIPT_RESULT_KEY), "foo"); 39 } 40 41 @Test 42 @SmallTest testScriptIsolatedFromPage()43 public void testScriptIsolatedFromPage() throws Exception { 44 InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(DATA_URL); 45 JSONObject result = 46 mActivityTestRule.executeScriptSync("bar", true /* useSeparateIsolate */); 47 Assert.assertTrue(result.isNull(Tab.SCRIPT_RESULT_KEY)); 48 } 49 50 @Test 51 @SmallTest testMainWorldScriptNotIsolatedFromPage()52 public void testMainWorldScriptNotIsolatedFromPage() throws Exception { 53 InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(DATA_URL); 54 JSONObject result = 55 mActivityTestRule.executeScriptSync("bar", false /* useSeparateIsolate */); 56 Assert.assertEquals(result.getInt(Tab.SCRIPT_RESULT_KEY), 10); 57 } 58 59 @Test 60 @SmallTest testScriptNotIsolatedFromOtherScript()61 public void testScriptNotIsolatedFromOtherScript() throws Exception { 62 InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(DATA_URL); 63 mActivityTestRule.executeScriptSync("var foo = 20;", true /* useSeparateIsolate */); 64 JSONObject result = 65 mActivityTestRule.executeScriptSync("foo", true /* useSeparateIsolate */); 66 Assert.assertEquals(result.getInt(Tab.SCRIPT_RESULT_KEY), 20); 67 } 68 69 @Test 70 @SmallTest testClearedOnNavigate()71 public void testClearedOnNavigate() throws Exception { 72 InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(DATA_URL); 73 mActivityTestRule.executeScriptSync("var foo = 20;", true /* useSeparateIsolate */); 74 75 String newUrl = UrlUtils.encodeHtmlDataUri("<html></html>"); 76 mActivityTestRule.navigateAndWait(newUrl); 77 JSONObject result = 78 mActivityTestRule.executeScriptSync("foo", true /* useSeparateIsolate */); 79 Assert.assertTrue(result.isNull(Tab.SCRIPT_RESULT_KEY)); 80 } 81 82 @Test 83 @SmallTest testNullCallback()84 public void testNullCallback() throws Exception { 85 InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl(DATA_URL); 86 TestThreadUtils.runOnUiThreadBlocking(() -> { 87 // Null callback should not crash. 88 activity.getTab().executeScript("null", true /* useSeparateIsolate */, null); 89 }); 90 // Execute a sync script to make sure the other script finishes. 91 mActivityTestRule.executeScriptSync("null", true /* useSeparateIsolate */); 92 } 93 } 94