Commit 8ff47bc0 authored by Bo Liu's avatar Bo Liu Committed by Commit Bot

weblayer: Fix crash on recreate on rotation

There is a crash if activity is recreated on rotation and the weblayer
fragment is also destroyed and recreated. This is introduced in the CL
that that keeps visibility across activities when fragment is retained:
https://chromium-review.googlesource.com/c/chromium/src/+/2216311

Add a test to catch this.

Reported in b/158864549

Change-Id: I51c43b102ea995fa5387df420bf0a7844670af71
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2252952Reviewed-by: default avatarScott Violet <sky@chromium.org>
Commit-Queue: Bo <boliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#780113}
parent ec0871e3
......@@ -8,6 +8,7 @@ import android.app.Activity;
import android.app.Instrumentation.ActivityMonitor;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.test.InstrumentationRegistry;
......@@ -137,18 +138,13 @@ public class InstrumentationActivityTestRule
(new NavigationWaiter(url, tab, true /* expectFailure */, waitForPaint)).navigateAndWait();
}
/**
* Recreates the Activity, blocking until finished.
* After calling this, getActivity() returns the new Activity.
*/
public void recreateActivity() {
private void recreateActivityHelper(Runnable recreate) {
Activity activity = getActivity();
ActivityMonitor monitor =
new ActivityMonitor(InstrumentationActivity.class.getName(), null, false);
InstrumentationRegistry.getInstrumentation().addMonitor(monitor);
TestThreadUtils.runOnUiThreadBlocking(activity::recreate);
recreate.run();
CriteriaHelper.pollUiThread(
() -> monitor.getLastActivity() != null && monitor.getLastActivity() != activity);
......@@ -165,6 +161,26 @@ public class InstrumentationActivityTestRule
}
}
/**
* Recreates the Activity, blocking until finished.
* After calling this, getActivity() returns the new Activity.
*/
public void recreateActivity() {
recreateActivityHelper(() -> {
Activity activity = getActivity();
TestThreadUtils.runOnUiThreadBlocking(activity::recreate);
});
}
public void recreateByRotatingToLandscape() {
recreateActivityHelper(() -> {
Activity activity = getActivity();
TestThreadUtils.runOnUiThreadBlocking(() -> {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
});
});
}
/**
* Executes the script passed in and waits for the result.
*/
......
......@@ -4,6 +4,9 @@
package org.chromium.weblayer.test;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import androidx.fragment.app.Fragment;
import androidx.test.filters.SmallTest;
......@@ -80,6 +83,32 @@ public class SmokeTest {
});
}
@Test
@SmallTest
public void testRecreateInstance() {
try {
InstrumentationActivity activity = mActivityTestRule.launchShellWithUrl("about:blank");
TestThreadUtils.runOnUiThreadBlocking(() -> {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
});
mActivityTestRule.setRetainInstance(false);
Fragment firstFragment = mActivityTestRule.getFragment();
mActivityTestRule.recreateByRotatingToLandscape();
boolean destroyed =
TestThreadUtils.runOnUiThreadBlockingNoException(() -> activity.isDestroyed());
Assert.assertTrue(destroyed);
Fragment secondFragment = mActivityTestRule.getFragment();
Assert.assertNotSame(firstFragment, secondFragment);
} finally {
Activity activity = mActivityTestRule.getActivity();
TestThreadUtils.runOnUiThreadBlocking(() -> {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
});
}
}
@Test
@SmallTest
public void testSetRetainInstance() {
......
......@@ -323,7 +323,7 @@ public class BrowserImpl extends IBrowser.Stub {
@CalledByNative
private void onActiveTabChanged(TabImpl tab) {
mViewController.setActiveTab(tab);
if (mViewController != null) mViewController.setActiveTab(tab);
if (mInDestroy) return;
try {
if (mClient != null) {
......
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