Skip to content

Commit

Permalink
Add a test that ensures the java bridge is called from a fenced frame.
Browse files Browse the repository at this point in the history
Add two tests to check usage of the java bridge behaves correctly
across multiple RenderViewHosts. The first simple test checks
that the object is indeed injected into a fenced frame. The second
test ensures that assigning a value in the main frame is observed
in the fenced frame.

BUG=1322158

Change-Id: I677e3706dd989d04e9b03000fb69fc8f0d892e45
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3748144
Reviewed-by: Bo Liu <boliu@chromium.org>
Commit-Queue: Dave Tapuska <dtapuska@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1022289}
  • Loading branch information
dtapuska authored and Chromium LUCI CQ committed Jul 8, 2022
1 parent 68621e5 commit 27bee8b
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
// Copyright 2022 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.android_webview.test;

import static org.chromium.android_webview.test.AwActivityTestRule.WAIT_TIMEOUT_MS;

import android.support.test.InstrumentationRegistry;
import android.util.Pair;
import android.webkit.JavascriptInterface;

import androidx.test.filters.SmallTest;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import org.chromium.android_webview.AwContents;
import org.chromium.android_webview.test.util.JavascriptEventObserver;
import org.chromium.base.test.util.Batch;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.Feature;
import org.chromium.net.test.util.TestWebServer;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

/**
* Test for creating fenced frames in Android WebView.
*/
@Batch(Batch.PER_CLASS)
@CommandLineFlags.Add("webview-mparch-fenced-frames")
@RunWith(AwJUnit4ClassRunner.class)
public class FencedFrameTest {
@Rule
public AwActivityTestRule mActivityTestRule = new AwActivityTestRule();

private TestAwContentsClient mContentsClient;
private AwTestContainerView mTestView;
private AwContents mAwContents;
private TestWebServer mWebServer;

@Before
public void setUp() throws Exception {
mContentsClient = new TestAwContentsClient();
mTestView = mActivityTestRule.createAwTestContainerViewOnMainSync(mContentsClient);
mAwContents = mTestView.getAwContents();
mWebServer = TestWebServer.startSsl();
}

@After
public void tearDown() {
if (mWebServer != null) {
mWebServer.shutdown();
}
}

/**
* Allocate an URL from the webserver that stores a main document and a fenced frame
* resource to be returned. The result should then be loaded in the WebContents.
*/
private String generateFencedFrame(String fencedFrameHtml) {
String path = "/fenced_frame.html";
final List<Pair<String, String>> headers = new ArrayList<Pair<String, String>>();
headers.add(new Pair("Supports-Loading-Mode", " fenced-frame"));
String fencedFrameUrl = mWebServer.setResponse(path, fencedFrameHtml, headers);

String mainPath = "/main_document.html";
String mainResponseStr =
"<html><body><fencedframe src=" + fencedFrameUrl + "></fencedframe></body></html>";
return mWebServer.setResponse(mainPath, mainResponseStr, null);
}

/**
* Test that a java object is mirrored in a fenced frame.
**/
@Test
@SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"})
public void testInjectedObjectPresentInFencedFrame() throws Throwable {
String fencedFrameSource = "<script>fencedFrameObserver.notifyJava();</script>";
String mainUrl = generateFencedFrame(fencedFrameSource);

final JavascriptEventObserver fencedFrameObserver = new JavascriptEventObserver();
final String fencedFrameObserverName = "fencedFrameObserver";
AwActivityTestRule.enableJavaScriptOnUiThread(mAwContents);

InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
fencedFrameObserver.register(mTestView.getWebContents(), fencedFrameObserverName);
});
mActivityTestRule.loadUrlSync(
mAwContents, mContentsClient.getOnPageFinishedHelper(), mainUrl);
Assert.assertTrue(fencedFrameObserver.waitForEvent(WAIT_TIMEOUT_MS));
}

/**
* Test that an object that is mutated in the main frame can be observed in
* the fenced frame.
*/
@Test
@SmallTest
@Feature({"AndroidWebView", "Android-JavaBridge"})
public void testCommunicationBetweenFrames() throws Throwable {
String fencedFrameSource = "<script>"
+ " function step() {"
+ " if (testObserver.getValue() == 'SET') {"
+ " testObserver.notifyJava(); "
+ " } else {"
+ " requestAnimationFrame(step);"
+ " }"
+ " }"
+ " requestAnimationFrame(step);"
+ "</script>";
String mainUrl = generateFencedFrame(fencedFrameSource);

class TestObserver {
private String mValue = "UNSET";
private CallbackHelper mCallbackHelper = new CallbackHelper();

@JavascriptInterface
public String getValue() {
return mValue;
}

@JavascriptInterface
public void setString(String value) {
mValue = value;
}

@JavascriptInterface
public void notifyJava() {
mCallbackHelper.notifyCalled();
}

public void waitForEvent() throws TimeoutException {
mCallbackHelper.waitForNext();
}
}
AwActivityTestRule.enableJavaScriptOnUiThread(mAwContents);
TestObserver testObserver = new TestObserver();
AwActivityTestRule.addJavascriptInterfaceOnUiThread(
mAwContents, testObserver, "testObserver");

mActivityTestRule.loadUrlSync(
mAwContents, mContentsClient.getOnPageFinishedHelper(), mainUrl);

mActivityTestRule.runOnUiThread(
() -> { mAwContents.evaluateJavaScript("testObserver.setString('SET');", null); });
testObserver.waitForEvent();
}
}
1 change: 1 addition & 0 deletions android_webview/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ instrumentation_test_apk("webview_instrumentation_test_apk") {
"../javatests/src/org/chromium/android_webview/test/CookieManagerTest.java",
"../javatests/src/org/chromium/android_webview/test/DarkModeTest.java",
"../javatests/src/org/chromium/android_webview/test/DisableHardwareAccelerationForTest.java",
"../javatests/src/org/chromium/android_webview/test/FencedFrameTest.java",
"../javatests/src/org/chromium/android_webview/test/FullScreenVideoTestAwContentsClient.java",
"../javatests/src/org/chromium/android_webview/test/GeolocationTest.java",
"../javatests/src/org/chromium/android_webview/test/GetTitleTest.java",
Expand Down

0 comments on commit 27bee8b

Please sign in to comment.