Commit 7dda812a authored by Scott Violet's avatar Scott Violet Committed by Commit Bot

weblayer: adds Tab.isDestroyed() and Browser.isDestroyed()

This way the embedder can readily detect when and if a Browser
or Tab has been destroyed.

BUG=1134085
TEST=covered by tests

Change-Id: I2e316e023d18c04d4aec6139306d1815e1ee65cb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2443402Reviewed-by: default avatarEvan Stade <estade@chromium.org>
Commit-Queue: Scott Violet <sky@chromium.org>
Cr-Commit-Position: refs/heads/master@{#813009}
parent 9a738068
......@@ -10,6 +10,7 @@ android_library("weblayer_java_tests") {
testonly = true
sources = [
"src/org/chromium/weblayer/test/BrowserFragmentLifecycleTest.java",
"src/org/chromium/weblayer/test/BrowserTest.java",
"src/org/chromium/weblayer/test/CookieManagerTest.java",
"src/org/chromium/weblayer/test/CrashReporterTest.java",
"src/org/chromium/weblayer/test/DataClearingTest.java",
......
......@@ -18,6 +18,7 @@ import org.junit.runner.RunWith;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import org.chromium.weblayer.Browser;
import org.chromium.weblayer.Navigation;
import org.chromium.weblayer.NavigationCallback;
import org.chromium.weblayer.NavigationController;
......@@ -282,4 +283,25 @@ public class BrowserFragmentLifecycleTest {
});
helper.waitForCallback(callCount, 1);
}
@Test
@SmallTest
public void browserAndTabIsDestroyedWhenFragmentDestroyed() throws Throwable {
mActivityTestRule.launchShellWithUrl(mActivityTestRule.getTestDataURL("simple_page.html"));
CallbackHelper helper = new CallbackHelper();
Browser browser = TestThreadUtils.runOnUiThreadBlocking(
() -> { return mActivityTestRule.getActivity().getBrowser(); });
Tab tab = TestThreadUtils.runOnUiThreadBlocking(() -> { return browser.getActiveTab(); });
TestThreadUtils.runOnUiThreadBlocking(() -> {
Assert.assertFalse(browser.isDestroyed());
Assert.assertFalse(tab.isDestroyed());
destroyFragment(helper);
});
helper.waitForFirst();
TestThreadUtils.runOnUiThreadBlocking(() -> {
Assert.assertTrue(browser.isDestroyed());
Assert.assertTrue(tab.isDestroyed());
});
}
}
// 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 androidx.test.filters.SmallTest;
import org.junit.Assert;
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.Browser;
import org.chromium.weblayer.Tab;
import org.chromium.weblayer.shell.InstrumentationActivity;
/**
* Tests for Browser.
*/
@RunWith(WebLayerJUnit4ClassRunner.class)
public class BrowserTest {
@Rule
public InstrumentationActivityTestRule mActivityTestRule =
new InstrumentationActivityTestRule();
private InstrumentationActivity mActivity;
@Test
@SmallTest
public void testDestroyTab() {
String url = mActivityTestRule.getTestDataURL("before_unload.html");
mActivity = mActivityTestRule.launchShellWithUrl(url);
Assert.assertNotNull(mActivity);
TestThreadUtils.runOnUiThreadBlocking(() -> {
Browser browser = mActivity.getBrowser();
Tab tab = browser.getActiveTab();
Assert.assertFalse(tab.isDestroyed());
browser.destroyTab(tab);
Assert.assertTrue(tab.isDestroyed());
});
}
}
......@@ -73,6 +73,14 @@ public class Browser {
: null;
}
/**
* Returns true if this Browser has been destroyed.
*/
public boolean isDestroyed() {
ThreadCheck.ensureOnUiThread();
return mImpl == null;
}
// Called prior to notifying IBrowser of destroy().
void prepareForDestroy() {
mFragment = null;
......
......@@ -128,6 +128,14 @@ public class Tab {
mBrowser = browser;
}
/**
* Returns true if this Tab has been destroyed.
*/
public boolean isDestroyed() {
ThreadCheck.ensureOnUiThread();
return mImpl == null;
}
@NonNull
public Browser getBrowser() {
ThreadCheck.ensureOnUiThread();
......
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