Commit d3d369ea authored by Wei-Yin Chen (陳威尹)'s avatar Wei-Yin Chen (陳威尹) Committed by Commit Bot

Enhance thumbnail-related test coverage

- All thumbnails shown can be released after GTS is gone.
- No thumbnails are loaded before GTS becomes visible.

Bug: 983170
Change-Id: Ib7b38909625033c82213f728b6d2e9d4a33e7b53
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1868844
Commit-Queue: Wei-Yin Chen (陳威尹) <wychen@chromium.org>
Reviewed-by: default avatarYusuf Ozuysal <yusufo@chromium.org>
Reviewed-by: default avatarYue Zhang <yuezhanggg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#708774}
parent 1ae3555f
...@@ -32,7 +32,7 @@ import android.support.v7.widget.RecyclerView; ...@@ -32,7 +32,7 @@ import android.support.v7.widget.RecyclerView;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.View; import android.view.View;
import org.junit.Assert; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
...@@ -100,6 +100,7 @@ public class StartSurfaceLayoutTest { ...@@ -100,6 +100,7 @@ public class StartSurfaceLayoutTest {
private Callback<Bitmap> mBitmapListener = private Callback<Bitmap> mBitmapListener =
(bitmap) -> mAllBitmaps.add(new WeakReference<>(bitmap)); (bitmap) -> mAllBitmaps.add(new WeakReference<>(bitmap));
private TabSwitcher.TabListDelegate mTabListDelegate; private TabSwitcher.TabListDelegate mTabListDelegate;
private boolean mSkipAssertThumbnailsAreReleased;
@Before @Before
public void setUp() { public void setUp() {
...@@ -118,7 +119,7 @@ public class StartSurfaceLayoutTest { ...@@ -118,7 +119,7 @@ public class StartSurfaceLayoutTest {
mTabListDelegate = mStartSurfaceLayout.getStartSurfaceForTesting().getTabListDelegate(); mTabListDelegate = mStartSurfaceLayout.getStartSurfaceForTesting().getTabListDelegate();
mTabListDelegate.setBitmapCallbackForTesting(mBitmapListener); mTabListDelegate.setBitmapCallbackForTesting(mBitmapListener);
Assert.assertEquals(0, mTabListDelegate.getBitmapFetchCountForTesting()); assertEquals(0, mTabListDelegate.getBitmapFetchCountForTesting());
mActivityTestRule.getActivity().getTabContentManager().setCaptureMinRequestTimeForTesting( mActivityTestRule.getActivity().getTabContentManager().setCaptureMinRequestTimeForTesting(
0); 0);
...@@ -128,6 +129,15 @@ public class StartSurfaceLayoutTest { ...@@ -128,6 +129,15 @@ public class StartSurfaceLayoutTest {
.getTabModelSelector() .getTabModelSelector()
.getTabModelFilterProvider() .getTabModelFilterProvider()
.getCurrentTabModelFilter()::isTabModelRestored)); .getCurrentTabModelFilter()::isTabModelRestored));
assertEquals(0, mTabListDelegate.getBitmapFetchCountForTesting());
// Only skip thumbnail releasing assertion when "warm" (large soft-cleanup-delay).
mSkipAssertThumbnailsAreReleased = false;
}
@After
public void tearDown() {
if (!mSkipAssertThumbnailsAreReleased) assertThumbnailsAreReleased();
} }
@Test @Test
...@@ -142,7 +152,6 @@ public class StartSurfaceLayoutTest { ...@@ -142,7 +152,6 @@ public class StartSurfaceLayoutTest {
prepareTabs(2, 0, NTP_URL); prepareTabs(2, 0, NTP_URL);
testTabToGrid(mUrl); testTabToGrid(mUrl);
assertThumbnailsAreReleased();
} }
@Test @Test
...@@ -158,7 +167,6 @@ public class StartSurfaceLayoutTest { ...@@ -158,7 +167,6 @@ public class StartSurfaceLayoutTest {
prepareTabs(2, 0, NTP_URL); prepareTabs(2, 0, NTP_URL);
testTabToGrid(mUrl); testTabToGrid(mUrl);
assertThumbnailsAreReleased();
} }
@Test @Test
...@@ -173,6 +181,7 @@ public class StartSurfaceLayoutTest { ...@@ -173,6 +181,7 @@ public class StartSurfaceLayoutTest {
prepareTabs(2, 0, NTP_URL); prepareTabs(2, 0, NTP_URL);
testTabToGrid(mUrl); testTabToGrid(mUrl);
mSkipAssertThumbnailsAreReleased = true;
} }
@Test @Test
...@@ -185,6 +194,7 @@ public class StartSurfaceLayoutTest { ...@@ -185,6 +194,7 @@ public class StartSurfaceLayoutTest {
// clang-format on // clang-format on
prepareTabs(2, 0, NTP_URL); prepareTabs(2, 0, NTP_URL);
testTabToGrid(mUrl); testTabToGrid(mUrl);
mSkipAssertThumbnailsAreReleased = true;
} }
@Test @Test
...@@ -216,7 +226,6 @@ public class StartSurfaceLayoutTest { ...@@ -216,7 +226,6 @@ public class StartSurfaceLayoutTest {
public void testTabToGridFromNtp() throws InterruptedException { public void testTabToGridFromNtp() throws InterruptedException {
prepareTabs(2, 0, NTP_URL); prepareTabs(2, 0, NTP_URL);
testTabToGrid(NTP_URL); testTabToGrid(NTP_URL);
assertThumbnailsAreReleased();
} }
/** /**
...@@ -231,6 +240,7 @@ public class StartSurfaceLayoutTest { ...@@ -231,6 +240,7 @@ public class StartSurfaceLayoutTest {
assertTrue(numTabs >= 1); assertTrue(numTabs >= 1);
assertTrue(numIncognitoTabs >= 0); assertTrue(numIncognitoTabs >= 0);
int oldCount = mTabListDelegate.getBitmapFetchCountForTesting();
assertEquals(1, assertEquals(1,
mActivityTestRule.getActivity().getTabModelSelector().getModel(false).getCount()); mActivityTestRule.getActivity().getTabModelSelector().getModel(false).getCount());
assertEquals( assertEquals(
...@@ -248,6 +258,7 @@ public class StartSurfaceLayoutTest { ...@@ -248,6 +258,7 @@ public class StartSurfaceLayoutTest {
mActivityTestRule.getActivity().getTabModelSelector().getModel(false).getCount()); mActivityTestRule.getActivity().getTabModelSelector().getModel(false).getCount());
assertEquals(numIncognitoTabs, assertEquals(numIncognitoTabs,
mActivityTestRule.getActivity().getTabModelSelector().getModel(true).getCount()); mActivityTestRule.getActivity().getTabModelSelector().getModel(true).getCount());
assertEquals(0, mTabListDelegate.getBitmapFetchCountForTesting() - oldCount);
} }
/** /**
...@@ -337,18 +348,9 @@ public class StartSurfaceLayoutTest { ...@@ -337,18 +348,9 @@ public class StartSurfaceLayoutTest {
final int initCount = getCaptureCount(); final int initCount = getCaptureCount();
StartSurface startSurface = mStartSurfaceLayout.getStartSurfaceForTesting();
for (int i = 0; i < mRepeat; i++) { for (int i = 0; i < mRepeat; i++) {
enterGTS(); enterGTS();
leaveGTS();
TestThreadUtils.runOnUiThreadBlocking(
() -> { startSurface.getController().onBackPressed(); });
// clang-format off
CriteriaHelper.pollInstrumentationThread(
() -> !mActivityTestRule.getActivity().getLayoutManager().overviewVisible(),
"Overview not hidden yet", DEFAULT_MAX_TIME_TO_POLL * 10,
DEFAULT_POLLING_INTERVAL);
// clang-format on
} }
checkFinalCaptureCount(false, initCount); checkFinalCaptureCount(false, initCount);
} }
...@@ -413,7 +415,7 @@ public class StartSurfaceLayoutTest { ...@@ -413,7 +415,7 @@ public class StartSurfaceLayoutTest {
() -> mActivityTestRule.getActivity().getTabModelSelector().closeAllTabs()); () -> mActivityTestRule.getActivity().getTabModelSelector().closeAllTabs());
ApplicationTestUtils.finishActivity(mActivityTestRule.getActivity()); ApplicationTestUtils.finishActivity(mActivityTestRule.getActivity());
mActivityTestRule.startMainActivityOnBlankPage(); mActivityTestRule.startMainActivityOnBlankPage();
Assert.assertEquals(1, mActivityTestRule.tabsCount(false)); assertEquals(1, mActivityTestRule.tabsCount(false));
} }
assertNotEquals(0, assertNotEquals(0,
...@@ -487,7 +489,7 @@ public class StartSurfaceLayoutTest { ...@@ -487,7 +489,7 @@ public class StartSurfaceLayoutTest {
!mActivityTestRule.getActivity().getLayoutManager().overviewVisible(); !mActivityTestRule.getActivity().getLayoutManager().overviewVisible();
if (!doneHiding) { if (!doneHiding) {
// Before overview hiding animation is done, the tab index should not change. // Before overview hiding animation is done, the tab index should not change.
Assert.assertEquals( assertEquals(
index, mActivityTestRule.getActivity().getCurrentTabModel().index()); index, mActivityTestRule.getActivity().getCurrentTabModel().index());
} }
return doneHiding; return doneHiding;
...@@ -506,7 +508,6 @@ public class StartSurfaceLayoutTest { ...@@ -506,7 +508,6 @@ public class StartSurfaceLayoutTest {
checkCaptureCount(delta, count); checkCaptureCount(delta, count);
} }
checkFinalCaptureCount(switchToAnotherTab, initCount); checkFinalCaptureCount(switchToAnotherTab, initCount);
assertThumbnailsAreReleased();
} }
@Test @Test
...@@ -521,12 +522,12 @@ public class StartSurfaceLayoutTest { ...@@ -521,12 +522,12 @@ public class StartSurfaceLayoutTest {
// the same process as this test. // the same process as this test.
ApplicationTestUtils.finishActivity(mActivityTestRule.getActivity()); ApplicationTestUtils.finishActivity(mActivityTestRule.getActivity());
mActivityTestRule.startMainActivityOnBlankPage(); mActivityTestRule.startMainActivityOnBlankPage();
Assert.assertEquals(3, mActivityTestRule.tabsCount(false)); assertEquals(3, mActivityTestRule.tabsCount(false));
Layout layout = mActivityTestRule.getActivity().getLayoutManager().getOverviewLayout(); Layout layout = mActivityTestRule.getActivity().getLayoutManager().getOverviewLayout();
assertTrue(layout instanceof StartSurfaceLayout); assertTrue(layout instanceof StartSurfaceLayout);
mStartSurfaceLayout = (StartSurfaceLayout) layout; mStartSurfaceLayout = (StartSurfaceLayout) layout;
Assert.assertEquals(0, mTabListDelegate.getBitmapFetchCountForTesting() - oldCount); assertEquals(0, mTabListDelegate.getBitmapFetchCountForTesting() - oldCount);
} }
@Test @Test
...@@ -534,7 +535,7 @@ public class StartSurfaceLayoutTest { ...@@ -534,7 +535,7 @@ public class StartSurfaceLayoutTest {
@CommandLineFlags.Add({BASE_PARAMS}) @CommandLineFlags.Add({BASE_PARAMS})
public void testInvisibleTabsDontFetch() throws InterruptedException { public void testInvisibleTabsDontFetch() throws InterruptedException {
// Open a few new tabs. // Open a few new tabs.
final int count = mAllBitmaps.size(); final int count = mTabListDelegate.getBitmapFetchCountForTesting();
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
MenuUtils.invokeCustomMenuActionSync(InstrumentationRegistry.getInstrumentation(), MenuUtils.invokeCustomMenuActionSync(InstrumentationRegistry.getInstrumentation(),
mActivityTestRule.getActivity(), org.chromium.chrome.R.id.new_tab_menu_id); mActivityTestRule.getActivity(), org.chromium.chrome.R.id.new_tab_menu_id);
...@@ -543,7 +544,7 @@ public class StartSurfaceLayoutTest { ...@@ -543,7 +544,7 @@ public class StartSurfaceLayoutTest {
Thread.sleep(1000); Thread.sleep(1000);
// No fetching should happen. // No fetching should happen.
Assert.assertEquals(0, mAllBitmaps.size() - count); assertEquals(0, mTabListDelegate.getBitmapFetchCountForTesting() - count);
} }
@Test @Test
...@@ -558,7 +559,7 @@ public class StartSurfaceLayoutTest { ...@@ -558,7 +559,7 @@ public class StartSurfaceLayoutTest {
Thread.sleep(1000); Thread.sleep(1000);
// Open a few new tabs. // Open a few new tabs.
final int count = mAllBitmaps.size(); final int count = mTabListDelegate.getBitmapFetchCountForTesting();
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
MenuUtils.invokeCustomMenuActionSync(InstrumentationRegistry.getInstrumentation(), MenuUtils.invokeCustomMenuActionSync(InstrumentationRegistry.getInstrumentation(),
mActivityTestRule.getActivity(), org.chromium.chrome.R.id.new_tab_menu_id); mActivityTestRule.getActivity(), org.chromium.chrome.R.id.new_tab_menu_id);
...@@ -567,7 +568,8 @@ public class StartSurfaceLayoutTest { ...@@ -567,7 +568,8 @@ public class StartSurfaceLayoutTest {
Thread.sleep(1000); Thread.sleep(1000);
// No fetching should happen. // No fetching should happen.
Assert.assertEquals(0, mAllBitmaps.size() - count); assertEquals(0, mTabListDelegate.getBitmapFetchCountForTesting() - count);
mSkipAssertThumbnailsAreReleased = true;
} }
@Test @Test
...@@ -582,7 +584,7 @@ public class StartSurfaceLayoutTest { ...@@ -582,7 +584,7 @@ public class StartSurfaceLayoutTest {
Thread.sleep(1000); Thread.sleep(1000);
// Open a few new tabs. // Open a few new tabs.
final int count = mAllBitmaps.size(); final int count = mTabListDelegate.getBitmapFetchCountForTesting();
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
MenuUtils.invokeCustomMenuActionSync(InstrumentationRegistry.getInstrumentation(), MenuUtils.invokeCustomMenuActionSync(InstrumentationRegistry.getInstrumentation(),
mActivityTestRule.getActivity(), org.chromium.chrome.R.id.new_tab_menu_id); mActivityTestRule.getActivity(), org.chromium.chrome.R.id.new_tab_menu_id);
...@@ -591,7 +593,7 @@ public class StartSurfaceLayoutTest { ...@@ -591,7 +593,7 @@ public class StartSurfaceLayoutTest {
Thread.sleep(1000); Thread.sleep(1000);
// No fetching should happen. // No fetching should happen.
Assert.assertEquals(0, mAllBitmaps.size() - count); assertEquals(0, mTabListDelegate.getBitmapFetchCountForTesting() - count);
} }
@Test @Test
...@@ -637,6 +639,7 @@ public class StartSurfaceLayoutTest { ...@@ -637,6 +639,7 @@ public class StartSurfaceLayoutTest {
onView(withId(org.chromium.chrome.tab_ui.R.id.tab_list_view)) onView(withId(org.chromium.chrome.tab_ui.R.id.tab_list_view))
.check(TabCountAssertion.havingTabCount(2)); .check(TabCountAssertion.havingTabCount(2));
} }
leaveGTS();
} }
@Test @Test
...@@ -651,20 +654,21 @@ public class StartSurfaceLayoutTest { ...@@ -651,20 +654,21 @@ public class StartSurfaceLayoutTest {
enterGTS(); enterGTS();
int currentFetchCount = mTabListDelegate.getBitmapFetchCountForTesting(); int currentFetchCount = mTabListDelegate.getBitmapFetchCountForTesting();
Assert.assertEquals(2, currentFetchCount - oldFetchCount); assertEquals(2, currentFetchCount - oldFetchCount);
oldFetchCount = currentFetchCount; oldFetchCount = currentFetchCount;
for (int i = 0; i < mRepeat; i++) { for (int i = 0; i < mRepeat; i++) {
switchTabModel(false); switchTabModel(false);
currentFetchCount = mTabListDelegate.getBitmapFetchCountForTesting(); currentFetchCount = mTabListDelegate.getBitmapFetchCountForTesting();
Assert.assertEquals(1, currentFetchCount - oldFetchCount); assertEquals(1, currentFetchCount - oldFetchCount);
oldFetchCount = currentFetchCount; oldFetchCount = currentFetchCount;
switchTabModel(true); switchTabModel(true);
currentFetchCount = mTabListDelegate.getBitmapFetchCountForTesting(); currentFetchCount = mTabListDelegate.getBitmapFetchCountForTesting();
Assert.assertEquals(2, currentFetchCount - oldFetchCount); assertEquals(2, currentFetchCount - oldFetchCount);
oldFetchCount = currentFetchCount; oldFetchCount = currentFetchCount;
} }
leaveGTS();
} }
private static class TabCountAssertion implements ViewAssertion { private static class TabCountAssertion implements ViewAssertion {
...@@ -738,6 +742,18 @@ public class StartSurfaceLayoutTest { ...@@ -738,6 +742,18 @@ public class StartSurfaceLayoutTest {
if (checkThumbnail) checkThumbnailsExist(currentTab); if (checkThumbnail) checkThumbnailsExist(currentTab);
} }
private void leaveGTS() {
assertTrue(mActivityTestRule.getActivity().getLayoutManager().overviewVisible());
StartSurface startSurface = mStartSurfaceLayout.getStartSurfaceForTesting();
TestThreadUtils.runOnUiThreadBlocking(
() -> { startSurface.getController().onBackPressed(); });
CriteriaHelper.pollInstrumentationThread(
() -> !mActivityTestRule.getActivity().getLayoutManager().overviewVisible(),
"Overview not hidden yet", DEFAULT_MAX_TIME_TO_POLL * 10,
DEFAULT_POLLING_INTERVAL);
}
private void checkFinalCaptureCount(boolean switchToAnotherTab, int initCount) { private void checkFinalCaptureCount(boolean switchToAnotherTab, int initCount) {
int expected; int expected;
if (TextUtils.equals( if (TextUtils.equals(
...@@ -793,7 +809,9 @@ public class StartSurfaceLayoutTest { ...@@ -793,7 +809,9 @@ public class StartSurfaceLayoutTest {
private boolean canAllBeGarbageCollected(List<WeakReference<Bitmap>> bitmaps) { private boolean canAllBeGarbageCollected(List<WeakReference<Bitmap>> bitmaps) {
for (WeakReference<Bitmap> bitmap : bitmaps) { for (WeakReference<Bitmap> bitmap : bitmaps) {
if (!GarbageCollectionTestUtils.canBeGarbageCollected(bitmap)) return false; if (!GarbageCollectionTestUtils.canBeGarbageCollected(bitmap)) {
return false;
}
} }
return true; return true;
} }
......
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