Commit 7cf26221 authored by Marijn Kruisselbrink's avatar Marijn Kruisselbrink Committed by Commit Bot

[Native File System] Start adding some end-to-end browser tests.

In particular some tests that test saving to files, as well as making
sure that the usage indicator is shown when the site has write access.

Bug: 993597
Change-Id: I2ee793921b6ea612731e278e8fbd5f4c5cfd093a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1761249
Commit-Queue: Marijn Kruisselbrink <mek@chromium.org>
Reviewed-by: default avatarVictor Costan <pwnall@chromium.org>
Cr-Commit-Position: refs/heads/master@{#690138}
parent 78cc5139
...@@ -88,6 +88,11 @@ void NativeFileSystemPermissionRequestManager::DequeueAndShowRequest() { ...@@ -88,6 +88,11 @@ void NativeFileSystemPermissionRequestManager::DequeueAndShowRequest() {
current_request_ = std::move(queued_requests_.front()); current_request_ = std::move(queued_requests_.front());
queued_requests_.pop_front(); queued_requests_.pop_front();
if (auto_response_for_test_) {
OnPermissionDialogResult(*auto_response_for_test_);
return;
}
ShowNativeFileSystemPermissionDialog( ShowNativeFileSystemPermissionDialog(
current_request_->data.origin, current_request_->data.path, current_request_->data.origin, current_request_->data.path,
current_request_->data.is_directory, current_request_->data.is_directory,
......
...@@ -47,6 +47,15 @@ class NativeFileSystemPermissionRequestManager ...@@ -47,6 +47,15 @@ class NativeFileSystemPermissionRequestManager
void AddRequest(RequestData request, void AddRequest(RequestData request,
base::OnceCallback<void(PermissionAction result)> callback); base::OnceCallback<void(PermissionAction result)> callback);
// Do NOT use this method in production code. Use this method in browser
// tests that need to accept or deny permissions when requested in
// JavaScript. Your test needs to call this before permission is requested,
// and then the bubble will proceed as desired as soon as it would have been
// shown.
void set_auto_response_for_test(base::Optional<PermissionAction> response) {
auto_response_for_test_ = response;
}
private: private:
friend class content::WebContentsUserData< friend class content::WebContentsUserData<
NativeFileSystemPermissionRequestManager>; NativeFileSystemPermissionRequestManager>;
...@@ -73,6 +82,8 @@ class NativeFileSystemPermissionRequestManager ...@@ -73,6 +82,8 @@ class NativeFileSystemPermissionRequestManager
// We only show new prompts when this is true. // We only show new prompts when this is true.
bool main_frame_has_fully_loaded_ = false; bool main_frame_has_fully_loaded_ = false;
base::Optional<PermissionAction> auto_response_for_test_;
base::WeakPtrFactory<NativeFileSystemPermissionRequestManager> weak_factory_{ base::WeakPtrFactory<NativeFileSystemPermissionRequestManager> weak_factory_{
this}; this};
WEB_CONTENTS_USER_DATA_KEY_DECL(); WEB_CONTENTS_USER_DATA_KEY_DECL();
......
// 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_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/native_file_system/native_file_system_permission_request_manager.h"
#include "chrome/browser/permissions/permission_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/toolbar_button_provider.h"
#include "chrome/browser/ui/views/page_action/omnibox_page_action_icon_container_view.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "third_party/blink/public/common/features.h"
#include "ui/shell_dialogs/select_file_dialog.h"
#include "ui/shell_dialogs/select_file_dialog_factory.h"
#include "ui/shell_dialogs/select_file_policy.h"
namespace {
// Fake ui::SelectFileDialog that selects one or more pre-determined files.
class FakeSelectFileDialog : public ui::SelectFileDialog {
public:
FakeSelectFileDialog(std::vector<base::FilePath> result,
Listener* listener,
std::unique_ptr<ui::SelectFilePolicy> policy)
: ui::SelectFileDialog(listener, std::move(policy)),
result_(std::move(result)) {}
protected:
void SelectFileImpl(Type type,
const base::string16& title,
const base::FilePath& default_path,
const FileTypeInfo* file_types,
int file_type_index,
const base::FilePath::StringType& default_extension,
gfx::NativeWindow owning_window,
void* params) override {
if (result_.size() == 1)
listener_->FileSelected(result_[0], 0, params);
else
listener_->MultiFilesSelected(result_, params);
}
bool IsRunning(gfx::NativeWindow owning_window) const override {
return false;
}
void ListenerDestroyed() override {}
bool HasMultipleFileTypeChoicesImpl() override { return false; }
private:
~FakeSelectFileDialog() override = default;
std::vector<base::FilePath> result_;
};
class FakeSelectFileDialogFactory : public ui::SelectFileDialogFactory {
public:
explicit FakeSelectFileDialogFactory(std::vector<base::FilePath> result)
: result_(std::move(result)) {}
~FakeSelectFileDialogFactory() override = default;
ui::SelectFileDialog* Create(
ui::SelectFileDialog::Listener* listener,
std::unique_ptr<ui::SelectFilePolicy> policy) override {
return new FakeSelectFileDialog(result_, listener, std::move(policy));
}
private:
std::vector<base::FilePath> result_;
};
} // namespace
// End-to-end tests for the native file system API. Among other things, these
// test the integration between usage of the Native File System API and the
// various bits of UI and permissions checks implemented in the chrome layer.
class NativeFileSystemBrowserTest : public InProcessBrowserTest {
public:
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
scoped_feature_list_.InitAndEnableFeature(
blink::features::kNativeFileSystemAPI);
ASSERT_TRUE(embedded_test_server()->Start());
InProcessBrowserTest::SetUp();
}
void TearDown() override {
InProcessBrowserTest::TearDown();
ASSERT_TRUE(temp_dir_.Delete());
ui::SelectFileDialog::SetFactory(nullptr);
}
base::FilePath CreateTestFile(const std::string& contents) {
base::ScopedAllowBlockingForTesting allow_blocking;
base::FilePath result;
EXPECT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &result));
EXPECT_EQ(int{contents.size()},
base::WriteFile(result, contents.data(), contents.size()));
return result;
}
bool IsUsageIndicatorVisible() {
auto* browser_view = BrowserView::GetBrowserViewForBrowser(browser());
auto* icon_view = browser_view->toolbar_button_provider()
->GetOmniboxPageActionIconContainerView()
->GetPageActionIconView(
PageActionIconType::kNativeFileSystemAccess);
return icon_view && icon_view->GetVisible();
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
base::ScopedTempDir temp_dir_;
};
#if defined(OS_CHROMEOS)
// TODO(https://crbug.com/993597): Writing is currently broken on ChromeOS.
#define MAYBE_SaveFile DISABLED_SaveFile
#else
#define MAYBE_SaveFile SaveFile
#endif
IN_PROC_BROWSER_TEST_F(NativeFileSystemBrowserTest, MAYBE_SaveFile) {
const base::FilePath test_file = CreateTestFile("");
const std::string file_contents = "file contents to write";
ui::SelectFileDialog::SetFactory(
new FakeSelectFileDialogFactory({test_file}));
ui_test_utils::NavigateToURL(browser(),
embedded_test_server()->GetURL("/title1.html"));
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_FALSE(IsUsageIndicatorVisible());
EXPECT_EQ(test_file.BaseName().AsUTF8Unsafe(),
content::EvalJs(web_contents,
"(async () => {"
" let e = await self.chooseFileSystemEntries("
" {type: 'saveFile'});"
" self.entry = e;"
" return e.name; })()"));
EXPECT_TRUE(IsUsageIndicatorVisible())
<< "A save file dialog implicitly grants write access, so usage "
"indicator should be visible.";
EXPECT_EQ(
int{file_contents.size()},
content::EvalJs(
web_contents,
content::JsReplace("(async () => {"
" const w = await self.entry.createWriter();"
" await w.write(0, new Blob([$1]));"
" await w.close();"
" return (await self.entry.getFile()).size; })()",
file_contents)));
{
base::ScopedAllowBlockingForTesting allow_blocking;
std::string read_contents;
EXPECT_TRUE(base::ReadFileToString(test_file, &read_contents));
EXPECT_EQ(file_contents, read_contents);
}
}
#if defined(OS_CHROMEOS)
// TODO(https://crbug.com/993597): Writing is currently broken on ChromeOS.
#define MAYBE_OpenFile DISABLED_OpenFile
#else
#define MAYBE_OpenFile OpenFile
#endif
IN_PROC_BROWSER_TEST_F(NativeFileSystemBrowserTest, MAYBE_OpenFile) {
const base::FilePath test_file = CreateTestFile("");
const std::string file_contents = "file contents to write";
ui::SelectFileDialog::SetFactory(
new FakeSelectFileDialogFactory({test_file}));
ui_test_utils::NavigateToURL(browser(),
embedded_test_server()->GetURL("/title1.html"));
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
NativeFileSystemPermissionRequestManager::FromWebContents(web_contents)
->set_auto_response_for_test(PermissionAction::GRANTED);
EXPECT_FALSE(IsUsageIndicatorVisible());
EXPECT_EQ(test_file.BaseName().AsUTF8Unsafe(),
content::EvalJs(web_contents,
"(async () => {"
" let e = await self.chooseFileSystemEntries("
" {type: 'openFile'});"
" self.entry = e;"
" return e.name; })()"));
// Should be writable yet, so no usage indicator.
EXPECT_FALSE(IsUsageIndicatorVisible());
EXPECT_EQ(
int{file_contents.size()},
content::EvalJs(
web_contents,
content::JsReplace("(async () => {"
" const w = await self.entry.createWriter();"
" await w.write(0, new Blob([$1]));"
" await w.close();"
" return (await self.entry.getFile()).size; })()",
file_contents)));
// Should have prompted for and received write access, so usage indicator
// should now be visible.
EXPECT_TRUE(IsUsageIndicatorVisible());
{
base::ScopedAllowBlockingForTesting allow_blocking;
std::string read_contents;
EXPECT_TRUE(base::ReadFileToString(test_file, &read_contents));
EXPECT_EQ(file_contents, read_contents);
}
}
// TODO(mek): Add more end-to-end test including other bits of UI.
...@@ -1836,6 +1836,7 @@ if (!is_android) { ...@@ -1836,6 +1836,7 @@ if (!is_android) {
"../browser/ui/views/media_router/media_router_dialog_controller_views_browsertest.cc", "../browser/ui/views/media_router/media_router_dialog_controller_views_browsertest.cc",
"../browser/ui/views/media_router/media_router_ui_browsertest.cc", "../browser/ui/views/media_router/media_router_ui_browsertest.cc",
"../browser/ui/views/media_router/presentation_receiver_window_view_browsertest.cc", "../browser/ui/views/media_router/presentation_receiver_window_view_browsertest.cc",
"../browser/ui/views/native_file_system/native_file_system_browsertest.cc",
"../browser/ui/views/native_file_system/native_file_system_directory_access_confirmation_view_browsertest.cc", "../browser/ui/views/native_file_system/native_file_system_directory_access_confirmation_view_browsertest.cc",
"../browser/ui/views/native_file_system/native_file_system_permission_view_browsertest.cc", "../browser/ui/views/native_file_system/native_file_system_permission_view_browsertest.cc",
"../browser/ui/views/native_file_system/native_file_system_restricted_directory_dialog_view_browsertest.cc", "../browser/ui/views/native_file_system/native_file_system_restricted_directory_dialog_view_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