Skip to content

Commit

Permalink
weblayer: fix bug where controls would show on rotation
Browse files Browse the repository at this point in the history
onOffsetsChanged() does nothing if fullscreen. This changes the
code to call to setControlsOffset() so that the offsets are
correctly updated.

BUG=1149335
TEST=testTopViewRemainsHiddenOnFullscreenRotation

Change-Id: I419f629292caf5f4454a04920c6a9778c8f16731
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2575898
Reviewed-by: Robbie McElrath <rmcelrath@chromium.org>
Commit-Queue: Scott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#834026}
  • Loading branch information
Scott Violet authored and Chromium LUCI CQ committed Dec 5, 2020
1 parent 80fe85e commit 88b825e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import androidx.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.base.test.util.CallbackHelper;
import org.chromium.base.test.util.DisabledTest;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.weblayer.Browser;
import org.chromium.weblayer.BrowserControlsOffsetCallback;
import org.chromium.weblayer.shell.InstrumentationActivity;

/**
Expand All @@ -28,8 +30,8 @@ public class FullscreenCallbackTest {
private InstrumentationActivity mActivity;
private TestFullscreenCallback mDelegate;

@Before
public void setUp() {
// Launch WL and triggers html fullscreen.
private void enterFullscren() {
String url = mActivityTestRule.getTestDataURL("fullscreen.html");
mActivity = mActivityTestRule.launchShellWithUrl(url);
Assert.assertNotNull(mActivity);
Expand All @@ -47,6 +49,7 @@ public void setUp() {
@SmallTest
@DisabledTest(message = "crbug.com/1133893")
public void testFullscreen() {
enterFullscren();
// Second touch exits.
EventUtils.simulateTouchCenterOfView(mActivity.getWindow().getDecorView());
mDelegate.waitForExitFullscreen();
Expand All @@ -56,6 +59,7 @@ public void testFullscreen() {
@Test
@SmallTest
public void testExitFullscreenWhenDelegateCleared() {
enterFullscren();
// Clearing the FullscreenCallback should exit fullscreen.
TestThreadUtils.runOnUiThreadBlocking(
() -> { mActivity.getTab().setFullscreenCallback(null); });
Expand All @@ -66,6 +70,7 @@ public void testExitFullscreenWhenDelegateCleared() {
@Test
@SmallTest
public void testExitFullscreenUsingRunnable() {
enterFullscren();
// Running the runnable supplied to the delegate should exit fullscreen.
TestThreadUtils.runOnUiThreadBlocking(mDelegate.mExitFullscreenRunnable);
mDelegate.waitForExitFullscreen();
Expand All @@ -75,6 +80,7 @@ public void testExitFullscreenUsingRunnable() {
@Test
@SmallTest
public void testExitFullscreenWhenTabDestroyed() {
enterFullscren();
// Destroying the tab should exit fullscreen.
TestThreadUtils.runOnUiThreadBlocking(
() -> { mActivity.getTab().getBrowser().destroyTab(mActivity.getTab()); });
Expand All @@ -88,6 +94,57 @@ public void testExitFullscreenWhenTabDestroyed() {
@Test
@SmallTest
public void testDestroyFragmentWhileFullscreen() {
enterFullscren();
TestThreadUtils.runOnUiThreadBlocking(() -> { mActivity.destroyFragment(); });
}

// Waits for the top offset to go to -height. This means the view is completely hidden.
private final class BrowserControlsOffsetCallbackImpl extends BrowserControlsOffsetCallback {
private final CallbackHelper mCallbackHelper;
BrowserControlsOffsetCallbackImpl(CallbackHelper callbackHelper) {
mCallbackHelper = callbackHelper;
}
@Override
public void onTopViewOffsetChanged(int offset) {
int height = mActivity.getTopContentsContainer().getHeight();
if (height != 0 && offset == -height) {
mCallbackHelper.notifyCalled();
}
}
}

@Test
@SmallTest
public void testTopViewRemainsHiddenOnFullscreenRotation() throws Exception {
String url = mActivityTestRule.getTestDataURL("rotation2.html");
mActivity = mActivityTestRule.launchShellWithUrl(url);
// Ensure the fragment is not recreated as otherwise things bounce around more.
mActivityTestRule.setRetainInstance(true);
Assert.assertNotNull(mActivity);
mDelegate = new TestFullscreenCallback();
CallbackHelper callbackHelper = new CallbackHelper();
// The offsets may move around during rotation. Wait for reattachment before installing
// the BrowserControlsOffsetCallback.
InstrumentationActivity.registerOnCreatedCallback(
new InstrumentationActivity.OnCreatedCallback() {
@Override
public void onCreated(Browser browser) {
browser.registerBrowserControlsOffsetCallback(
new BrowserControlsOffsetCallbackImpl(callbackHelper));
}
});
TestThreadUtils.runOnUiThreadBlocking(
() -> { mActivity.getTab().setFullscreenCallback(mDelegate); });

EventUtils.simulateTouchCenterOfView(mActivity.getWindow().getDecorView());
mDelegate.waitForFullscreen();
Assert.assertEquals(1, mDelegate.mEnterFullscreenCount);

// There should be a fullscreen element.
Assert.assertTrue(mActivityTestRule.executeScriptAndExtractBoolean(
"document.fullscreenElement != null"));

// Rotation should trigger the view being totally hidden.
callbackHelper.waitForFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,12 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
mLastHeight = height;
if (mLastWidth > 0 && mLastHeight > 0 && mViewResourceAdapter == null) {
createAdapterAndLayer();
if (mLastShownAmountWithView == DEFAULT_LAST_SHOWN_AMOUNT && mSavedState != null) {
if (mIsFullscreen) {
// This calls setControlsOffset() as onOffsetsChanged() does (mostly) nothing when
// fullscreen.
setControlsOffset(mIsTop ? -mLastHeight : mLastHeight, 0);
} else if (mLastShownAmountWithView == DEFAULT_LAST_SHOWN_AMOUNT
&& mSavedState != null) {
// If there wasn't a View before and we have non-empty saved state from a previous
// BrowserControlsContainerView instance, apply those saved offsets now. We can't
// rely on BrowserControlsOffsetManager to notify us of the correct location as we
Expand Down
16 changes: 16 additions & 0 deletions weblayer/test/data/rotation2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<html>
<body style="height:5000px">
<p>A page that will rotate on touch.</p>
</body>
<script>
async function toggleFullscreen() {
if (!document.fullscreenElement) {
await document.documentElement.requestFullscreen();
await screen.orientation.lock("landscape-primary");
} else {
document.exitFullscreen();
}
}
document.addEventListener('touchend', function(e) { toggleFullscreen(); }, false);
</script>
</html>

0 comments on commit 88b825e

Please sign in to comment.