Commit ef1a5655 authored by Evan Stade's avatar Evan Stade Committed by Commit Bot

WebLayer: add tests for FindInPage

Bug: 1038415
Change-Id: I62834ba8ad4b7f7be5ce53eb273551126f1f3a4e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2042759Reviewed-by: default avatarScott Violet <sky@chromium.org>
Commit-Queue: Evan Stade <estade@chromium.org>
Cr-Commit-Position: refs/heads/master@{#739770}
parent 945f92b3
......@@ -15,6 +15,7 @@ android_library("weblayer_java_tests") {
"src/org/chromium/weblayer/test/ErrorPageCallbackTest.java",
"src/org/chromium/weblayer/test/EventUtils.java",
"src/org/chromium/weblayer/test/ExecuteScriptTest.java",
"src/org/chromium/weblayer/test/FindInPageTest.java",
"src/org/chromium/weblayer/test/FullscreenCallbackTest.java",
"src/org/chromium/weblayer/test/InputTypesTest.java",
"src/org/chromium/weblayer/test/InstrumentationActivityTestRule.java",
......@@ -43,6 +44,7 @@ android_library("weblayer_java_tests") {
"//third_party/android_support_test_runner:rules_java",
"//third_party/android_support_test_runner:runner_java",
"//third_party/junit:junit",
"//weblayer/browser/java:interfaces_java",
"//weblayer/public/java",
"//weblayer/public/javatests:weblayer_public_javatests",
"//weblayer/shell/android:weblayer_shell_java",
......
// Copyright 2020 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.weblayer.test;
import android.net.Uri;
import android.support.test.filters.SmallTest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.weblayer.FindInPageCallback;
import org.chromium.weblayer.Tab;
import org.chromium.weblayer.shell.InstrumentationActivity;
import java.util.concurrent.CountDownLatch;
/**
* Tests the behavior of FindInPageController and FindInPageCallback.
*/
@RunWith(WebLayerJUnit4ClassRunner.class)
public class FindInPageTest {
@Rule
public InstrumentationActivityTestRule mActivityTestRule =
new InstrumentationActivityTestRule();
private InstrumentationActivity mActivity;
private CallbackImpl mCallback;
private static class CallbackImpl extends FindInPageCallback {
public int mNumberOfMatches;
public int mActiveMatchIndex;
public CountDownLatch mResultCountDown;
public CountDownLatch mEndedCountDown;
@Override
public void onFindResult(int numberOfMatches, int activeMatchIndex, boolean finalUpdate) {
mNumberOfMatches = numberOfMatches;
mActiveMatchIndex = activeMatchIndex;
if (finalUpdate) mResultCountDown.countDown();
}
@Override
public void onFindEnded() {
if (mEndedCountDown != null) mEndedCountDown.countDown();
}
}
private void searchFor(String text, boolean forward) {
mCallback.mResultCountDown = new CountDownLatch(1);
TestThreadUtils.runOnUiThreadBlocking(
() -> { mActivity.getTab().getFindInPageController().find(text, forward); });
try {
mCallback.mResultCountDown.await();
} catch (InterruptedException e) {
Assert.fail(e.toString());
}
}
private void searchFor(String text) {
searchFor(text, true);
}
private void verifyFindSessionInactive() {
Tab tab = mActivity.getTab();
// This verification only works on the active tab; if inactive setFindInPageCallback always
// fails.
Assert.assertTrue(TestThreadUtils.runOnUiThreadBlockingNoException(
() -> { return tab == tab.getBrowser().getActiveTab(); }));
Assert.assertTrue(TestThreadUtils.runOnUiThreadBlockingNoException(() -> {
// This call will return false if there's already a find session active.
boolean settingCallbackWorked =
tab.getFindInPageController().setFindInPageCallback(new CallbackImpl());
// Remove the new callback.
tab.getFindInPageController().setFindInPageCallback(null);
return settingCallbackWorked;
}));
}
@Before
public void setUp() {
String url = mActivityTestRule.getTestDataURL("shakespeare.html");
mActivity = mActivityTestRule.launchShellWithUrl(url);
Assert.assertNotNull(mActivity);
mCallback = new CallbackImpl();
TestThreadUtils.runOnUiThreadBlocking(() -> {
mActivity.getTab().getFindInPageController().setFindInPageCallback(mCallback);
});
}
@Test
@SmallTest
public void testBasic() {
// Search.
searchFor("de");
Assert.assertEquals(mCallback.mNumberOfMatches, 5);
Assert.assertEquals(mCallback.mActiveMatchIndex, 0);
// Search again to activate the next match.
searchFor("de");
Assert.assertEquals(mCallback.mNumberOfMatches, 5);
Assert.assertEquals(mCallback.mActiveMatchIndex, 1);
// Search backwards to activate the previous match.
searchFor("de", false);
Assert.assertEquals(mCallback.mNumberOfMatches, 5);
Assert.assertEquals(mCallback.mActiveMatchIndex, 0);
// Add a character to narrow the search.
searchFor("des");
Assert.assertEquals(mCallback.mNumberOfMatches, 2);
Assert.assertEquals(mCallback.mActiveMatchIndex, 0);
searchFor("des");
Assert.assertEquals(mCallback.mActiveMatchIndex, 1);
// Simulate a backspace; the active match shouldn't change (although the number of results
// and therefore indexing does change).
searchFor("de");
Assert.assertEquals(mCallback.mNumberOfMatches, 5);
Assert.assertEquals(mCallback.mActiveMatchIndex, 4);
TestThreadUtils.runOnUiThreadBlocking(() -> {
mActivity.getTab().getFindInPageController().setFindInPageCallback(null);
});
verifyFindSessionInactive();
}
@Test
@SmallTest
public void testHideOnNavigate() throws InterruptedException {
mCallback.mEndedCountDown = new CountDownLatch(1);
TestThreadUtils.runOnUiThreadBlocking(() -> {
mActivity.getTab().getNavigationController().navigate(Uri.parse("simple_page.html"));
});
mCallback.mEndedCountDown.await();
verifyFindSessionInactive();
}
@Test
@SmallTest
public void testHideOnNewTab() throws InterruptedException {
TestThreadUtils.runOnUiThreadBlocking(() -> {
mActivity.getBrowser().getActiveTab().setNewTabCallback(new NewTabCallbackImpl());
});
mCallback.mEndedCountDown = new CountDownLatch(1);
// This touch creates a new tab.
EventUtils.simulateTouchCenterOfView(mActivity.getWindow().getDecorView());
mCallback.mEndedCountDown.await();
}
}
......@@ -364,6 +364,7 @@ public final class TabImpl extends ITab.Stub {
public void findInPage(String searchText, boolean forward) {
StrictModeWorkaround.apply();
if (mFindInPageBridge == null) return;
if (searchText.length() > 0) {
mFindInPageBridge.startFinding(searchText, forward, false);
} else {
......@@ -373,7 +374,6 @@ public final class TabImpl extends ITab.Stub {
private void hideFindInPageUiAndNotifyClient() {
if (mFindInPageBridge == null) return;
mFindInPageBridge.stopFinding(true);
mFindResultBar.dismiss();
......
......@@ -2,9 +2,5 @@
<body>
<p id='x'>XXXX</p>
</body>
<script>
document.addEventListener('touchend',
function(e) {
window.open('about:blank', ''); }, false);
</script>
<script src='new_browser_on_touch.js'/></script>
</html>
// Copyright 2020 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.
document.addEventListener('touchend', function(e) {
window.open('about:blank', '');
}, false);
<html>
<body>
<p>
No, Time, thou shalt not boast that I do change:
Thy pyramids built up with newer might
To me are nothing novel, nothing strange;
They are but dressings of a former sight.
Our dates are brief, and therefore we admire
What thou dost foist upon us that is old,
And rather make them born to our desire
Than think that we before have heard them told.
Thy registers and thee I both defy,
Not wondering at the present nor the past,
For thy records and what we see doth lie,
Made more or less by thy continual haste.
This I do vow and this shall ever be;
I will be true, despite thy scythe and thee.
</p>
</body>
<script src='new_browser_on_touch.js'/></script>
</html>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment