Commit 1114f7dd authored by Eric Willigers's avatar Eric Willigers Committed by Commit Bot

desktop-pwas: Test file handling with WebAppBrowserController

The file handling tests are now run with both WebAppBrowserController
and HostedAppBrowserController.

Bug: 966290
Change-Id: I3ec4abf6883dfb132a294c3707439607e3cb6233
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1727849
Commit-Queue: Eric Willigers <ericwilligers@chromium.org>
Reviewed-by: default avatarJay Harris <harrisjay@chromium.org>
Cr-Commit-Position: refs/heads/master@{#682595}
parent 9c158d86
...@@ -86,7 +86,6 @@ ...@@ -86,7 +86,6 @@
#include "net/test/embedded_test_server/embedded_test_server.h" #include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/embedded_test_server/request_handler_util.h" #include "net/test/embedded_test_server/request_handler_util.h"
#include "third_party/blink/public/common/features.h" #include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/manifest/manifest.h"
#include "third_party/blink/public/mojom/renderer_preferences.mojom.h" #include "third_party/blink/public/mojom/renderer_preferences.mojom.h"
#include "ui/base/clipboard/clipboard.h" #include "ui/base/clipboard/clipboard.h"
#include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect.h"
...@@ -413,7 +412,7 @@ class HostedAppTest : public extensions::ExtensionBrowserTest, ...@@ -413,7 +412,7 @@ class HostedAppTest : public extensions::ExtensionBrowserTest,
void SetUpOnMainThread() override { void SetUpOnMainThread() override {
extensions::ExtensionBrowserTest::SetUpOnMainThread(); extensions::ExtensionBrowserTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1"); host_resolver()->AddRule("*", "127.0.0.1");
// By default, all SSL cert checks are valid. Can be overriden in tests. // By default, all SSL cert checks are valid. Can be overridden in tests.
cert_verifier_.mock_cert_verifier()->set_default_result(net::OK); cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
} }
...@@ -869,94 +868,6 @@ IN_PROC_BROWSER_TEST_P(HostedAppTest, InScopeHttpUrlsDisplayAppTitle) { ...@@ -869,94 +868,6 @@ IN_PROC_BROWSER_TEST_P(HostedAppTest, InScopeHttpUrlsDisplayAppTitle) {
app_browser->GetWindowTitleForCurrentTab(false)); app_browser->GetWindowTitleForCurrentTab(false));
} }
class HostedAppFileHandlingTest : public HostedAppTest {
public:
HostedAppFileHandlingTest() {
scoped_feature_list_.InitWithFeatures(
{blink::features::kNativeFileSystemAPI,
blink::features::kFileHandlingAPI},
{});
}
base::FilePath NewTestFilePath() {
// CreateTemporaryFile blocks, temporarily allow blocking.
base::ScopedAllowBlockingForTesting allow_blocking;
base::FilePath test_file_path;
base::CreateTemporaryFile(&test_file_path);
return test_file_path;
}
std::string InstallFileHandlingPWA() {
DCHECK_EQ(web_app_info_.app_url, GURL());
GURL url = GetSecureAppURL();
web_app_info_.app_url = url;
web_app_info_.scope = url.GetWithoutFilename();
web_app_info_.title = base::ASCIIToUTF16("A Hosted App");
web_app_info_.file_handler = blink::Manifest::FileHandler();
web_app_info_.file_handler->action =
https_server()->GetURL("app.com", "/ssl/blank_page.html");
{
std::vector<blink::Manifest::FileFilter> filters;
blink::Manifest::FileFilter text = {
base::ASCIIToUTF16("text"),
{base::ASCIIToUTF16(".txt"), base::ASCIIToUTF16("text/*")}};
filters.push_back(text);
web_app_info_.file_handler->files = std::move(filters);
}
app_ = InstallBookmarkApp(web_app_info_);
return app_->id();
}
content::WebContents* LaunchWithFiles(
const std::string& app_id,
const std::vector<base::FilePath>& files) {
AppLaunchParams params(browser()->profile(), app_id,
extensions::LaunchContainer::kLaunchContainerWindow,
WindowOpenDisposition::NEW_WINDOW,
extensions::AppLaunchSource::kSourceFileHandler);
params.launch_files = files;
content::TestNavigationObserver navigation_observer(
web_app_info_.file_handler->action);
navigation_observer.StartWatchingNewWebContents();
content::WebContents* web_contents =
OpenApplicationWindow(params, web_app_info_.file_handler->action);
navigation_observer.Wait();
return web_contents;
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
WebApplicationInfo web_app_info_;
};
IN_PROC_BROWSER_TEST_P(HostedAppFileHandlingTest, PWAsCanViewLaunchParams) {
ASSERT_TRUE(https_server()->Start());
const std::string app_id = InstallFileHandlingPWA();
content::WebContents* web_contents = LaunchWithFiles(app_id, {});
EXPECT_EQ(0, content::EvalJs(web_contents, "launchParams.files.length"));
}
IN_PROC_BROWSER_TEST_P(HostedAppFileHandlingTest,
PWAsCanReceiveFileLaunchParams) {
ASSERT_TRUE(https_server()->Start());
const std::string app_id = InstallFileHandlingPWA();
base::FilePath test_file_path = NewTestFilePath();
content::WebContents* web_contents =
LaunchWithFiles(app_id, {test_file_path});
EXPECT_EQ(1, content::EvalJs(web_contents, "launchParams.files.length"));
EXPECT_EQ(test_file_path.BaseName().value(),
content::EvalJs(web_contents, "launchParams.files[0].name"));
}
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
class HostedAppBadgingTest : public HostedAppTest { class HostedAppBadgingTest : public HostedAppTest {
public: public:
...@@ -2953,11 +2864,6 @@ INSTANTIATE_TEST_SUITE_P( ...@@ -2953,11 +2864,6 @@ INSTANTIATE_TEST_SUITE_P(
HostedAppSitePerProcessTest, HostedAppSitePerProcessTest,
::testing::Values(AppType::HOSTED_APP)); ::testing::Values(AppType::HOSTED_APP));
INSTANTIATE_TEST_SUITE_P(
/* no prefix */,
HostedAppFileHandlingTest,
::testing::Values(AppType::BOOKMARK_APP));
#if !defined(OS_CHROMEOS) #if !defined(OS_CHROMEOS)
INSTANTIATE_TEST_SUITE_P( INSTANTIATE_TEST_SUITE_P(
/* no prefix */, /* no prefix */,
......
...@@ -206,7 +206,7 @@ void BookmarkAppNavigationBrowserTest::SetUpCommandLine( ...@@ -206,7 +206,7 @@ void BookmarkAppNavigationBrowserTest::SetUpCommandLine(
void BookmarkAppNavigationBrowserTest::SetUpOnMainThread() { void BookmarkAppNavigationBrowserTest::SetUpOnMainThread() {
ExtensionBrowserTest::SetUpOnMainThread(); ExtensionBrowserTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1"); host_resolver()->AddRule("*", "127.0.0.1");
// By default, all SSL cert checks are valid. Can be overriden in tests. // By default, all SSL cert checks are valid. Can be overridden in tests.
cert_verifier_.mock_cert_verifier()->set_default_result(net::OK); cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
} }
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "chrome/common/web_application_info.h" #include "chrome/common/web_application_info.h"
#include "chrome/test/base/ui_test_utils.h" #include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/notification_service.h" #include "content/public/browser/notification_service.h"
#include "net/dns/mock_host_resolver.h"
namespace web_app { namespace web_app {
...@@ -91,4 +92,28 @@ content::WebContents* WebAppControllerBrowserTest::OpenApplication( ...@@ -91,4 +92,28 @@ content::WebContents* WebAppControllerBrowserTest::OpenApplication(
return contents; return contents;
} }
void WebAppControllerBrowserTest::SetUpInProcessBrowserTestFixture() {
extensions::ExtensionBrowserTest::SetUpInProcessBrowserTestFixture();
cert_verifier_.SetUpInProcessBrowserTestFixture();
}
void WebAppControllerBrowserTest::TearDownInProcessBrowserTestFixture() {
extensions::ExtensionBrowserTest::TearDownInProcessBrowserTestFixture();
cert_verifier_.TearDownInProcessBrowserTestFixture();
}
void WebAppControllerBrowserTest::SetUpCommandLine(
base::CommandLine* command_line) {
extensions::ExtensionBrowserTest::SetUpCommandLine(command_line);
cert_verifier_.SetUpCommandLine(command_line);
}
void WebAppControllerBrowserTest::SetUpOnMainThread() {
extensions::ExtensionBrowserTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1");
// By default, all SSL cert checks are valid. Can be overridden in tests.
cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
}
} // namespace web_app } // namespace web_app
...@@ -10,9 +10,14 @@ ...@@ -10,9 +10,14 @@
#include "chrome/browser/extensions/extension_browsertest.h" #include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/web_applications/components/web_app_helpers.h" #include "chrome/browser/web_applications/components/web_app_helpers.h"
#include "chrome/common/web_application_info.h" #include "chrome/common/web_application_info.h"
#include "content/public/test/content_mock_cert_verifier.h"
#include "net/test/embedded_test_server/embedded_test_server.h" #include "net/test/embedded_test_server/embedded_test_server.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace base {
class CommandLine;
}
namespace content { namespace content {
class WebContents; class WebContents;
} }
...@@ -47,10 +52,20 @@ class WebAppControllerBrowserTest ...@@ -47,10 +52,20 @@ class WebAppControllerBrowserTest
net::EmbeddedTestServer* https_server() { return &https_server_; } net::EmbeddedTestServer* https_server() { return &https_server_; }
// ExtensionBrowserTest:
void SetUpInProcessBrowserTestFixture() override;
void TearDownInProcessBrowserTestFixture() override;
void SetUpCommandLine(base::CommandLine* command_line) override;
void SetUpOnMainThread() override;
private: private:
base::test::ScopedFeatureList scoped_feature_list_; base::test::ScopedFeatureList scoped_feature_list_;
net::EmbeddedTestServer https_server_; net::EmbeddedTestServer https_server_;
// Similar to net::MockCertVerifier, but also updates the CertVerifier
// used by the NetworkService.
content::ContentMockCertVerifier cert_verifier_;
DISALLOW_COPY_AND_ASSIGN(WebAppControllerBrowserTest); DISALLOW_COPY_AND_ASSIGN(WebAppControllerBrowserTest);
}; };
......
// 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 "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/thread_restrictions.h"
#include "chrome/browser/apps/app_service/app_launch_params.h"
#include "chrome/browser/apps/launch_service/launch_service.h"
#include "chrome/browser/ui/web_applications/web_app_controller_browsertest.h"
#include "chrome/common/web_application_info.h"
#include "content/public/test/test_navigation_observer.h"
#include "third_party/blink/public/common/features.h"
class WebAppFileHandlingBrowserTest
: public web_app::WebAppControllerBrowserTest {
public:
WebAppFileHandlingBrowserTest() {
scoped_feature_list_.InitWithFeatures(
{blink::features::kNativeFileSystemAPI,
blink::features::kFileHandlingAPI},
{});
}
GURL GetSecureAppURL() {
return https_server()->GetURL("app.com", "/ssl/google.html");
}
GURL GetFileHandlerActionURL() {
return https_server()->GetURL("app.com", "/ssl/blank_page.html");
}
base::FilePath NewTestFilePath() {
// CreateTemporaryFile blocks, temporarily allow blocking.
base::ScopedAllowBlockingForTesting allow_blocking;
base::FilePath test_file_path;
base::CreateTemporaryFile(&test_file_path);
return test_file_path;
}
std::string InstallFileHandlingPWA() {
GURL url = GetSecureAppURL();
auto web_app_info = std::make_unique<WebApplicationInfo>();
web_app_info->app_url = url;
web_app_info->scope = url.GetWithoutFilename();
web_app_info->title = base::ASCIIToUTF16("A Hosted App");
web_app_info->file_handler = blink::Manifest::FileHandler();
web_app_info->file_handler->action = GetFileHandlerActionURL();
{
std::vector<blink::Manifest::FileFilter> filters;
blink::Manifest::FileFilter text = {
base::ASCIIToUTF16("text"),
{base::ASCIIToUTF16(".txt"), base::ASCIIToUTF16("text/*")}};
filters.push_back(text);
web_app_info->file_handler->files = std::move(filters);
}
return WebAppControllerBrowserTest::InstallWebApp(std::move(web_app_info));
}
content::WebContents* LaunchWithFiles(
const std::string& app_id,
const std::vector<base::FilePath>& files) {
AppLaunchParams params(browser()->profile(), app_id,
apps::mojom::LaunchContainer::kLaunchContainerWindow,
WindowOpenDisposition::NEW_WINDOW,
apps::mojom::AppLaunchSource::kSourceFileHandler);
params.launch_files = files;
content::TestNavigationObserver navigation_observer(
GetFileHandlerActionURL());
navigation_observer.StartWatchingNewWebContents();
params.override_url = GetFileHandlerActionURL();
content::WebContents* web_contents =
apps::LaunchService::Get(profile())->OpenApplication(params);
navigation_observer.Wait();
return web_contents;
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_P(WebAppFileHandlingBrowserTest, PWAsCanViewLaunchParams) {
ASSERT_TRUE(https_server()->Start());
const std::string app_id = InstallFileHandlingPWA();
content::WebContents* web_contents = LaunchWithFiles(app_id, {});
EXPECT_EQ(0, content::EvalJs(web_contents, "launchParams.files.length"));
}
IN_PROC_BROWSER_TEST_P(WebAppFileHandlingBrowserTest,
PWAsCanReceiveFileLaunchParams) {
ASSERT_TRUE(https_server()->Start());
const std::string app_id = InstallFileHandlingPWA();
base::FilePath test_file_path = NewTestFilePath();
content::WebContents* web_contents =
LaunchWithFiles(app_id, {test_file_path});
EXPECT_EQ(1, content::EvalJs(web_contents, "launchParams.files.length"));
EXPECT_EQ(test_file_path.BaseName().value(),
content::EvalJs(web_contents, "launchParams.files[0].name"));
}
INSTANTIATE_TEST_SUITE_P(
/* no prefix */,
WebAppFileHandlingBrowserTest,
::testing::Values(
web_app::ControllerType::kHostedAppController,
web_app::ControllerType::kUnifiedControllerWithBookmarkApp));
...@@ -91,8 +91,10 @@ content::WebContents* WebAppLaunchManager::OpenApplication( ...@@ -91,8 +91,10 @@ content::WebContents* WebAppLaunchManager::OpenApplication(
return ShowWebApplicationWindow( return ShowWebApplicationWindow(
params, params.app_id, params, params.app_id,
provider_->registrar().GetAppLaunchURL(params.app_id), browser, params.override_url.is_empty()
WindowOpenDisposition::NEW_FOREGROUND_TAB); ? provider_->registrar().GetAppLaunchURL(params.app_id)
: params.override_url,
browser, WindowOpenDisposition::NEW_FOREGROUND_TAB);
} }
bool WebAppLaunchManager::OpenApplicationWindow( bool WebAppLaunchManager::OpenApplicationWindow(
......
...@@ -1235,6 +1235,7 @@ if (!is_android) { ...@@ -1235,6 +1235,7 @@ if (!is_android) {
"../browser/ui/views/webview_accessibility_browsertest.cc", "../browser/ui/views/webview_accessibility_browsertest.cc",
"../browser/ui/web_applications/bookmark_app_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_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",
"../browser/ui/webui/autofill_and_password_manager_internals/password_manager_internals_ui_browsertest.cc", "../browser/ui/webui/autofill_and_password_manager_internals/password_manager_internals_ui_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