Commit 7b056106 authored by Mehran Mahmoudi's avatar Mehran Mahmoudi Committed by Commit Bot

[Paint Preview] Add end-to-end browser integration tests

This adds a suite of new tests that verify capture on hide and display
on startup. These tests don't override any test only behavior and fully
rely on the prod behavior.

Bug: 1143722
Change-Id: Iccde9b097b8cd0217bd74588599c66063c7cec21
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2514725
Commit-Queue: Mehran Mahmoudi <mahmoudi@chromium.org>
Reviewed-by: default avatarCalder Kitagawa <ckitagawa@chromium.org>
Cr-Commit-Position: refs/heads/master@{#823634}
parent 8a1740bc
......@@ -352,6 +352,8 @@ chrome_test_java_sources = [
"javatests/src/org/chromium/chrome/browser/page_info/CookieControlsViewTest.java",
"javatests/src/org/chromium/chrome/browser/page_info/PageInfoControllerTest.java",
"javatests/src/org/chromium/chrome/browser/page_info/PageInfoViewTest.java",
"javatests/src/org/chromium/chrome/browser/paint_preview/StartupPaintPreviewHelperTest.java",
"javatests/src/org/chromium/chrome/browser/paint_preview/StartupPaintPreviewHelperTestRunner.java",
"javatests/src/org/chromium/chrome/browser/partnercustomizations/BasePartnerBrowserCustomizationIntegrationTestRule.java",
"javatests/src/org/chromium/chrome/browser/partnercustomizations/BasePartnerBrowserCustomizationUnitTestRule.java",
"javatests/src/org/chromium/chrome/browser/partnercustomizations/PartnerBrowserCustomizationsUnitTest.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.chrome.browser.paint_preview;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.UiDevice;
import androidx.test.filters.MediumTest;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.base.test.params.ParameterizedCommandLineFlags;
import org.chromium.base.test.params.ParameterizedCommandLineFlags.Switches;
import org.chromium.base.test.util.CommandLineFlags;
import org.chromium.base.test.util.Restriction;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.flags.ChromeSwitches;
import org.chromium.chrome.browser.paint_preview.services.PaintPreviewTabServiceFactory;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
import org.chromium.chrome.test.util.browser.Features;
import org.chromium.content_public.browser.test.util.CriteriaHelper;
import org.chromium.content_public.browser.test.util.TestThreadUtils;
import java.util.concurrent.ExecutionException;
/**
* Tests for the {@link StartupPaintPreviewHelper} class.
* This test suite cannot be batched because tests rely on the cold start behavior of
* {@link ChromeActivity}.
*/
@RunWith(StartupPaintPreviewHelperTestRunner.class)
@Features.EnableFeatures({ChromeFeatureList.PAINT_PREVIEW_SHOW_ON_STARTUP})
@CommandLineFlags.Add({ChromeSwitches.DISABLE_FIRST_RUN_EXPERIENCE})
@ParameterizedCommandLineFlags({
@Switches("disable-features=" + ChromeFeatureList.START_SURFACE_ANDROID + ","
+ ChromeFeatureList.INSTANT_START)
,
@Switches("enable-features=" + ChromeFeatureList.START_SURFACE_ANDROID),
@Switches("enable-features=" + ChromeFeatureList.INSTANT_START),
@Switches("enable-features=" + ChromeFeatureList.START_SURFACE_ANDROID + ","
+ ChromeFeatureList.INSTANT_START),
})
public class StartupPaintPreviewHelperTest {
@Rule
public ChromeTabbedActivityTestRule mActivityTestRule = new ChromeTabbedActivityTestRule();
private static final String TEST_URL = "/chrome/test/data/android/about.html";
/**
* Tests that paint preview is captured when Chrome is backgrounded and deleted when a tab is
* closed.
*/
@Test
@MediumTest
@Restriction(StartupPaintPreviewHelperTestRunner.RESTRICTION_TYPE_KEEP_ACTIVITIES)
public void testCaptureOnBackgrounded() throws ExecutionException {
mActivityTestRule.startMainActivityWithURL(
mActivityTestRule.getTestServer().getURL(TEST_URL));
Tab tab = mActivityTestRule.getActivity().getActivityTab();
// Verify no capture exists for this tab and no paint preview is showing.
assertHasCaptureForTab(tab, false);
TabbedPaintPreview tabbedPaintPreview =
TestThreadUtils.runOnUiThreadBlocking(() -> TabbedPaintPreview.get(tab));
Assert.assertFalse("No preview should be showing.", tabbedPaintPreview.isShowing());
Assert.assertFalse("No preview should be attached.", tabbedPaintPreview.isAttached());
// Send Chrome to background to trigger capture.
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).pressHome();
assertHasCaptureForTab(tab, true);
Assert.assertFalse("No preview should be showing.", tabbedPaintPreview.isShowing());
Assert.assertFalse("No preview should be attached.", tabbedPaintPreview.isAttached());
// Closing the tab should delete its captured paint preview.
TestThreadUtils.runOnUiThreadBlocking(()
-> mActivityTestRule.getActivity()
.getTabModelSelector()
.getCurrentModel()
.closeTab(tab));
assertHasCaptureForTab(tab, false);
}
/**
* Captures paint preview first, restarts Chrome and tests whether the preview is shown on
* startup.
*/
@Test
@MediumTest
@Restriction(StartupPaintPreviewHelperTestRunner.RESTRICTION_TYPE_KEEP_ACTIVITIES)
public void testDisplayOnStartup() throws ExecutionException {
mActivityTestRule.startMainActivityWithURL(
mActivityTestRule.getTestServer().getURL(TEST_URL));
// Send Chrome to background to trigger capture.
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).pressHome();
assertHasCaptureForTab(mActivityTestRule.getActivity().getActivityTab(), true);
// Restart Chrome. Paint preview should be shown on startup.
TestThreadUtils.runOnUiThreadBlocking(() -> mActivityTestRule.getActivity().finish());
mActivityTestRule.startMainActivityFromLauncher();
final Tab tab = mActivityTestRule.getActivity().getActivityTab();
TabbedPaintPreview tabbedPaintPreview =
TestThreadUtils.runOnUiThreadBlocking(() -> TabbedPaintPreview.get(tab));
TabbedPaintPreviewTest.assertAttachedAndShown(tabbedPaintPreview, true, true);
// Closing the tab should delete its captured paint preview.
TestThreadUtils.runOnUiThreadBlocking(()
-> mActivityTestRule.getActivity()
.getTabModelSelector()
.getCurrentModel()
.closeTab(tab));
assertHasCaptureForTab(tab, false);
}
private static void assertHasCaptureForTab(Tab tab, boolean shouldHaveCapture) {
String shownMessage = shouldHaveCapture ? "No paint preview capture found."
: "Paint preview capture should have not existed.";
CriteriaHelper.pollUiThread(
()
-> PaintPreviewTabServiceFactory.getServiceInstance().hasCaptureForTab(
tab.getId())
== shouldHaveCapture,
shownMessage);
}
}
// 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.chrome.browser.paint_preview;
import android.content.Context;
import android.provider.Settings;
import android.support.test.InstrumentationRegistry;
import android.text.TextUtils;
import org.junit.runners.model.InitializationError;
import org.chromium.base.test.util.RestrictionSkipCheck;
import org.chromium.base.test.util.SkipCheck;
import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
import java.util.List;
/**
* Test runner class that handles a custom {@link Restriction} type.
*/
public class StartupPaintPreviewHelperTestRunner extends ChromeJUnit4ClassRunner {
/**
* The test is only valid if Settings.Global.ALWAYS_FINISH_ACTIVITIES on device settings is
* disabled.
*/
public static final String RESTRICTION_TYPE_KEEP_ACTIVITIES = "Keep_Activities";
public StartupPaintPreviewHelperTestRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
protected List<SkipCheck> getSkipChecks() {
return addToList(super.getSkipChecks(),
new StartupPaintPreviewSkipCheck(InstrumentationRegistry.getTargetContext()));
}
private static class StartupPaintPreviewSkipCheck extends RestrictionSkipCheck {
public StartupPaintPreviewSkipCheck(Context targetContext) {
super(targetContext);
}
@Override
protected boolean restrictionApplies(String restriction) {
if (TextUtils.equals(restriction, RESTRICTION_TYPE_KEEP_ACTIVITIES)) {
int alwaysFinishActivities =
Settings.System.getInt(getTargetContext().getContentResolver(),
Settings.Global.ALWAYS_FINISH_ACTIVITIES, 0);
return alwaysFinishActivities != 0;
}
return super.restrictionApplies(restriction);
}
}
}
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