Commit ca34466d authored by Eric Willigers's avatar Eric Willigers Committed by Commit Bot

desktop-pwas: WebAppEngagementBrowserTest

Test helper functions are introduced in
web_app_browsertest_util.h

BookmarkAppTest is renamed to WebAppEngagementBrowserTest.

The test no longer references extensions.

TBR=calamity@chromium.org

Bug: 966290
Change-Id: I630fa2873766a5a2955fe96f91008fd3bb9bc84e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1787761Reviewed-by: default avatarEric Willigers <ericwilligers@chromium.org>
Reviewed-by: default avatarAlan Cutter <alancutter@chromium.org>
Commit-Queue: Eric Willigers <ericwilligers@chromium.org>
Cr-Commit-Position: refs/heads/master@{#698408}
parent f79a55bf
...@@ -37,6 +37,10 @@ namespace history { ...@@ -37,6 +37,10 @@ namespace history {
class HistoryService; class HistoryService;
} }
namespace web_app {
class WebAppEngagementBrowserTest;
}
class GURL; class GURL;
class HostContentSettingsMap; class HostContentSettingsMap;
class Profile; class Profile;
...@@ -185,9 +189,9 @@ class SiteEngagementService : public KeyedService, ...@@ -185,9 +189,9 @@ class SiteEngagementService : public KeyedService,
void AddPointsForTesting(const GURL& url, double points); void AddPointsForTesting(const GURL& url, double points);
private: private:
friend class BookmarkAppTest;
friend class SiteEngagementObserver; friend class SiteEngagementObserver;
friend class SiteEngagementServiceTest; friend class SiteEngagementServiceTest;
friend class web_app::WebAppEngagementBrowserTest;
FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CheckHistograms); FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CheckHistograms);
FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CleanupEngagementScores); FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, CleanupEngagementScores);
FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest, FRIEND_TEST_ALL_PREFIXES(SiteEngagementServiceTest,
......
// Copyright 2019 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.
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "base/run_loop.h"
#include "base/test/bind_test_util.h"
#include "chrome/browser/apps/app_service/app_launch_params.h"
#include "chrome/browser/apps/launch_service/launch_service.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/web_applications/components/external_install_options.h"
#include "chrome/browser/web_applications/components/pending_app_manager.h"
#include "chrome/browser/web_applications/components/web_app_helpers.h"
#include "chrome/browser/web_applications/components/web_app_provider_base.h"
#include "chrome/browser/web_applications/components/web_app_tab_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace web_app {
Browser* LaunchWebAppBrowser(Profile* profile, const AppId& app_id) {
EXPECT_TRUE(
apps::LaunchService::Get(profile)->OpenApplication(AppLaunchParams(
profile, app_id, apps::mojom::LaunchContainer::kLaunchContainerWindow,
WindowOpenDisposition::CURRENT_TAB,
apps::mojom::AppLaunchSource::kSourceTest)));
Browser* browser = chrome::FindLastActive();
bool is_correct_app_browser =
browser && GetAppIdFromApplicationName(browser->app_name()) == app_id;
EXPECT_TRUE(is_correct_app_browser);
return is_correct_app_browser ? browser : nullptr;
}
Browser* LaunchBrowserForWebAppInTab(Profile* profile, const AppId& app_id) {
content::WebContents* web_contents =
apps::LaunchService::Get(profile)->OpenApplication(AppLaunchParams(
profile, app_id, apps::mojom::LaunchContainer::kLaunchContainerTab,
WindowOpenDisposition::NEW_FOREGROUND_TAB,
apps::mojom::AppLaunchSource::kSourceTest));
DCHECK(web_contents);
WebAppTabHelper* tab_helper = WebAppTabHelper::FromWebContents(web_contents);
DCHECK(tab_helper);
EXPECT_EQ(app_id, tab_helper->app_id());
Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
EXPECT_EQ(browser, chrome::FindLastActive());
EXPECT_EQ(web_contents, browser->tab_strip_model()->GetActiveWebContents());
return browser;
}
ExternalInstallOptions CreateInstallOptions(const GURL& url) {
ExternalInstallOptions install_options(
url, LaunchContainer::kWindow, ExternalInstallSource::kInternalDefault);
// Avoid creating real shortcuts in tests.
install_options.add_to_applications_menu = false;
install_options.add_to_desktop = false;
install_options.add_to_quick_launch_bar = false;
return install_options;
}
InstallResultCode InstallApp(Profile* profile,
ExternalInstallOptions install_options) {
base::RunLoop run_loop;
InstallResultCode result_code;
WebAppProviderBase::GetProviderBase(profile)->pending_app_manager().Install(
std::move(install_options),
base::BindLambdaForTesting(
[&result_code, &run_loop](const GURL& provided_url,
InstallResultCode code) {
result_code = code;
run_loop.Quit();
}));
run_loop.Run();
return result_code;
}
} // namespace web_app
// Copyright 2019 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.
#ifndef CHROME_BROWSER_UI_WEB_APPLICATIONS_TEST_WEB_APP_BROWSERTEST_UTIL_H_
#define CHROME_BROWSER_UI_WEB_APPLICATIONS_TEST_WEB_APP_BROWSERTEST_UTIL_H_
#include "chrome/browser/web_applications/components/web_app_helpers.h"
class Browser;
class Profile;
namespace web_app {
struct ExternalInstallOptions;
enum class InstallResultCode;
// Launches a new app window for |app| in |profile|.
Browser* LaunchWebAppBrowser(Profile*, const AppId&);
// Launches a new tab for |app| in |profile|.
Browser* LaunchBrowserForWebAppInTab(Profile*, const AppId&);
ExternalInstallOptions CreateInstallOptions(const GURL& url);
InstallResultCode InstallApp(Profile*, ExternalInstallOptions);
} // namespace web_app
#endif // CHROME_BROWSER_UI_WEB_APPLICATIONS_TEST_WEB_APP_BROWSERTEST_UTIL_H_
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "chrome/browser/extensions/browsertest_util.h" #include "chrome/browser/extensions/browsertest_util.h"
#include "chrome/browser/installable/installable_metrics.h" #include "chrome/browser/installable/installable_metrics.h"
#include "chrome/browser/predictors/loading_predictor_config.h" #include "chrome/browser/predictors/loading_predictor_config.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/web_applications/components/install_manager.h" #include "chrome/browser/web_applications/components/install_manager.h"
#include "chrome/browser/web_applications/components/web_app_constants.h" #include "chrome/browser/web_applications/components/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_provider.h" #include "chrome/browser/web_applications/web_app_provider.h"
...@@ -38,21 +39,7 @@ WebAppControllerBrowserTestBase::WebAppControllerBrowserTestBase() { ...@@ -38,21 +39,7 @@ WebAppControllerBrowserTestBase::WebAppControllerBrowserTestBase() {
WebAppControllerBrowserTestBase::~WebAppControllerBrowserTestBase() = default; WebAppControllerBrowserTestBase::~WebAppControllerBrowserTestBase() = default;
WebAppControllerBrowserTest::WebAppControllerBrowserTest() AppId WebAppControllerBrowserTestBase::InstallPWA(const GURL& app_url) {
: https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {
scoped_feature_list_.InitWithFeatures(
{}, {predictors::kSpeculativePreconnectFeature});
}
WebAppControllerBrowserTest::~WebAppControllerBrowserTest() = default;
void WebAppControllerBrowserTest::SetUp() {
https_server_.AddDefaultHandlers(GetChromeTestDataDir());
extensions::ExtensionBrowserTest::SetUp();
}
AppId WebAppControllerBrowserTest::InstallPWA(const GURL& app_url) {
auto web_app_info = std::make_unique<WebApplicationInfo>(); auto web_app_info = std::make_unique<WebApplicationInfo>();
web_app_info->app_url = app_url; web_app_info->app_url = app_url;
web_app_info->scope = app_url.GetWithoutFilename(); web_app_info->scope = app_url.GetWithoutFilename();
...@@ -60,18 +47,18 @@ AppId WebAppControllerBrowserTest::InstallPWA(const GURL& app_url) { ...@@ -60,18 +47,18 @@ AppId WebAppControllerBrowserTest::InstallPWA(const GURL& app_url) {
return InstallWebApp(std::move(web_app_info)); return InstallWebApp(std::move(web_app_info));
} }
AppId WebAppControllerBrowserTest::InstallWebApp( AppId WebAppControllerBrowserTestBase::InstallWebApp(
std::unique_ptr<WebApplicationInfo>&& web_app_info) { std::unique_ptr<WebApplicationInfo>&& web_app_info) {
AppId app_id; AppId app_id;
base::RunLoop run_loop; base::RunLoop run_loop;
auto* provider = web_app::WebAppProvider::Get(profile()); auto* provider = WebAppProvider::Get(profile());
DCHECK(provider); DCHECK(provider);
provider->install_manager().InstallWebAppFromInfo( provider->install_manager().InstallWebAppFromInfo(
std::move(web_app_info), ForInstallableSite::kYes, std::move(web_app_info), ForInstallableSite::kYes,
WebappInstallSource::OMNIBOX_INSTALL_ICON, WebappInstallSource::OMNIBOX_INSTALL_ICON,
base::BindLambdaForTesting( base::BindLambdaForTesting(
[&](const AppId& installed_app_id, web_app::InstallResultCode code) { [&](const AppId& installed_app_id, InstallResultCode code) {
EXPECT_EQ(web_app::InstallResultCode::kSuccessNewInstall, code); EXPECT_EQ(InstallResultCode::kSuccessNewInstall, code);
app_id = installed_app_id; app_id = installed_app_id;
run_loop.Quit(); run_loop.Quit();
})); }));
...@@ -80,9 +67,40 @@ AppId WebAppControllerBrowserTest::InstallWebApp( ...@@ -80,9 +67,40 @@ AppId WebAppControllerBrowserTest::InstallWebApp(
return app_id; return app_id;
} }
Browser* WebAppControllerBrowserTestBase::LaunchWebAppBrowser(
const AppId& app_id) {
return web_app::LaunchWebAppBrowser(profile(), app_id);
}
Browser* WebAppControllerBrowserTestBase::LaunchBrowserForWebAppInTab(
const AppId& app_id) {
return web_app::LaunchBrowserForWebAppInTab(profile(), app_id);
}
base::Optional<AppId> WebAppControllerBrowserTestBase::FindAppWithUrlInScope(
const GURL& url) {
auto* provider = WebAppProvider::Get(profile());
DCHECK(provider);
return provider->registrar().FindAppWithUrlInScope(url);
}
WebAppControllerBrowserTest::WebAppControllerBrowserTest()
: https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {
scoped_feature_list_.InitWithFeatures(
{}, {predictors::kSpeculativePreconnectFeature});
}
WebAppControllerBrowserTest::~WebAppControllerBrowserTest() = default;
void WebAppControllerBrowserTest::SetUp() {
https_server_.AddDefaultHandlers(GetChromeTestDataDir());
extensions::ExtensionBrowserTest::SetUp();
}
content::WebContents* WebAppControllerBrowserTest::OpenApplication( content::WebContents* WebAppControllerBrowserTest::OpenApplication(
const AppId& app_id) { const AppId& app_id) {
auto* provider = web_app::WebAppProvider::Get(profile()); auto* provider = WebAppProvider::Get(profile());
DCHECK(provider); DCHECK(provider);
ui_test_utils::UrlLoadObserver url_observer( ui_test_utils::UrlLoadObserver url_observer(
provider->registrar().GetAppLaunchURL(app_id), provider->registrar().GetAppLaunchURL(app_id),
......
...@@ -40,6 +40,18 @@ class WebAppControllerBrowserTestBase ...@@ -40,6 +40,18 @@ class WebAppControllerBrowserTestBase
WebAppControllerBrowserTestBase(); WebAppControllerBrowserTestBase();
~WebAppControllerBrowserTestBase() = 0; ~WebAppControllerBrowserTestBase() = 0;
AppId InstallPWA(const GURL& app_url);
AppId InstallWebApp(std::unique_ptr<WebApplicationInfo>&& web_app_info);
// Launches the app as a window and returns the browser.
Browser* LaunchWebAppBrowser(const AppId&);
// Launches the app as a tab and returns the browser.
Browser* LaunchBrowserForWebAppInTab(const AppId&);
base::Optional<AppId> FindAppWithUrlInScope(const GURL& url);
private: private:
base::test::ScopedFeatureList scoped_feature_list_; base::test::ScopedFeatureList scoped_feature_list_;
...@@ -55,10 +67,6 @@ class WebAppControllerBrowserTest : public WebAppControllerBrowserTestBase { ...@@ -55,10 +67,6 @@ class WebAppControllerBrowserTest : public WebAppControllerBrowserTestBase {
void SetUp() override; void SetUp() override;
protected: protected:
AppId InstallPWA(const GURL& app_url);
AppId InstallWebApp(std::unique_ptr<WebApplicationInfo>&& web_app_info);
content::WebContents* OpenApplication(const AppId&); content::WebContents* OpenApplication(const AppId&);
net::EmbeddedTestServer* https_server() { return &https_server_; } net::EmbeddedTestServer* https_server() { return &https_server_; }
......
...@@ -12,7 +12,9 @@ ...@@ -12,7 +12,9 @@
#include "base/test/bind_test_util.h" #include "base/test/bind_test_util.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/web_applications/components/app_registrar.h" #include "chrome/browser/web_applications/components/app_registrar.h"
#include "chrome/browser/web_applications/components/external_install_options.h"
#include "chrome/browser/web_applications/components/externally_installed_web_app_prefs.h" #include "chrome/browser/web_applications/components/externally_installed_web_app_prefs.h"
#include "chrome/browser/web_applications/components/web_app_constants.h" #include "chrome/browser/web_applications/components/web_app_constants.h"
#include "chrome/browser/web_applications/components/web_app_provider_base.h" #include "chrome/browser/web_applications/components/web_app_provider_base.h"
...@@ -26,17 +28,6 @@ ...@@ -26,17 +28,6 @@
namespace web_app { namespace web_app {
ExternalInstallOptions CreateInstallOptions(const GURL& url) {
ExternalInstallOptions install_options(
url, LaunchContainer::kWindow, ExternalInstallSource::kInternalDefault);
// Avoid creating real shortcuts in tests.
install_options.add_to_applications_menu = false;
install_options.add_to_desktop = false;
install_options.add_to_quick_launch_bar = false;
return install_options;
}
class PendingAppManagerImplBrowserTest : public InProcessBrowserTest { class PendingAppManagerImplBrowserTest : public InProcessBrowserTest {
protected: protected:
AppRegistrar& registrar() { AppRegistrar& registrar() {
...@@ -50,19 +41,7 @@ class PendingAppManagerImplBrowserTest : public InProcessBrowserTest { ...@@ -50,19 +41,7 @@ class PendingAppManagerImplBrowserTest : public InProcessBrowserTest {
} }
void InstallApp(ExternalInstallOptions install_options) { void InstallApp(ExternalInstallOptions install_options) {
base::RunLoop run_loop; result_code_ = web_app::InstallApp(browser()->profile(), install_options);
WebAppProviderBase::GetProviderBase(browser()->profile())
->pending_app_manager()
.Install(std::move(install_options),
base::BindLambdaForTesting(
[this, &run_loop](const GURL& provided_url,
InstallResultCode code) {
result_code_ = code;
run_loop.Quit();
}));
run_loop.Run();
ASSERT_TRUE(result_code_.has_value());
} }
void CheckServiceWorkerStatus(const GURL& url, void CheckServiceWorkerStatus(const GURL& url,
......
...@@ -356,6 +356,8 @@ static_library("test_support") { ...@@ -356,6 +356,8 @@ static_library("test_support") {
"../browser/extensions/extension_browsertest.h", "../browser/extensions/extension_browsertest.h",
"../browser/extensions/permissions_test_util.cc", "../browser/extensions/permissions_test_util.cc",
"../browser/extensions/permissions_test_util.h", "../browser/extensions/permissions_test_util.h",
"../browser/ui/web_applications/test/web_app_browsertest_util.cc",
"../browser/ui/web_applications/test/web_app_browsertest_util.h",
"../browser/ui/web_applications/web_app_controller_browsertest.cc", "../browser/ui/web_applications/web_app_controller_browsertest.cc",
"../browser/ui/web_applications/web_app_controller_browsertest.h", "../browser/ui/web_applications/web_app_controller_browsertest.h",
] ]
...@@ -1267,8 +1269,8 @@ if (!is_android) { ...@@ -1267,8 +1269,8 @@ if (!is_android) {
"../browser/ui/views/webauthn/authenticator_qr_code_test.cc", "../browser/ui/views/webauthn/authenticator_qr_code_test.cc",
"../browser/ui/views/webview_accessibility_browsertest.cc", "../browser/ui/views/webview_accessibility_browsertest.cc",
"../browser/ui/web_applications/app_browser_controller_browsertest.cc", "../browser/ui/web_applications/app_browser_controller_browsertest.cc",
"../browser/ui/web_applications/bookmark_app_browsertest.cc",
"../browser/ui/web_applications/web_app_badging_browsertest.cc", "../browser/ui/web_applications/web_app_badging_browsertest.cc",
"../browser/ui/web_applications/web_app_engagement_browsertest.cc",
"../browser/ui/web_applications/web_app_file_handling_browsertest.cc", "../browser/ui/web_applications/web_app_file_handling_browsertest.cc",
"../browser/ui/web_applications/web_app_ui_manager_impl_browsertest.cc", "../browser/ui/web_applications/web_app_ui_manager_impl_browsertest.cc",
"../browser/ui/webauthn/authenticator_dialog_browsertest.cc", "../browser/ui/webauthn/authenticator_dialog_browsertest.cc",
......
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