2012-10-16 21:28:07 +02:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2021-06-17 22:08:01 +02:00
|
|
|
#include "include/base/cef_callback.h"
|
2014-07-15 20:10:40 +02:00
|
|
|
#include "include/wrapper/cef_closure_task.h"
|
2016-11-18 18:31:21 +01:00
|
|
|
#include "tests/ceftests/test_handler.h"
|
|
|
|
#include "tests/ceftests/test_util.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/gtest/include/gtest/gtest.h"
|
2012-10-16 21:28:07 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char* kTestUrl = "http://tests/DialogTestHandler";
|
|
|
|
|
|
|
|
class DialogTestHandler : public TestHandler {
|
|
|
|
public:
|
|
|
|
struct TestConfig {
|
|
|
|
explicit TestConfig(FileDialogMode dialog_mode)
|
2017-05-17 11:29:28 +02:00
|
|
|
: mode(dialog_mode),
|
|
|
|
title("Test Title"),
|
|
|
|
default_file_name("Test File Name"),
|
|
|
|
callback_async(false),
|
|
|
|
callback_cancel(false) {
|
2012-10-16 21:28:07 +02:00
|
|
|
accept_types.push_back("text/*");
|
|
|
|
accept_types.push_back(".js");
|
|
|
|
accept_types.push_back(".css");
|
|
|
|
}
|
|
|
|
|
|
|
|
FileDialogMode mode;
|
|
|
|
CefString title;
|
|
|
|
CefString default_file_name;
|
|
|
|
std::vector<CefString> accept_types;
|
|
|
|
|
|
|
|
bool callback_async; // True if the callback should execute asynchronously.
|
|
|
|
bool callback_cancel; // True if the callback should cancel.
|
|
|
|
std::vector<CefString> callback_paths; // Resulting paths if not cancelled.
|
|
|
|
};
|
|
|
|
|
|
|
|
class Callback : public CefRunFileDialogCallback {
|
|
|
|
public:
|
2017-05-17 11:29:28 +02:00
|
|
|
explicit Callback(DialogTestHandler* handler) : handler_(handler) {}
|
2012-10-16 21:28:07 +02:00
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnFileDialogDismissed(
|
|
|
|
const std::vector<CefString>& file_paths) override {
|
2012-10-16 21:28:07 +02:00
|
|
|
handler_->got_onfiledialogdismissed_.yes();
|
|
|
|
|
2015-01-20 19:24:54 +01:00
|
|
|
if (handler_->config_.callback_cancel) {
|
2012-10-16 21:28:07 +02:00
|
|
|
EXPECT_TRUE(file_paths.empty());
|
2015-01-20 19:24:54 +01:00
|
|
|
} else {
|
2012-10-16 21:28:07 +02:00
|
|
|
TestStringVectorEqual(handler_->config_.callback_paths, file_paths);
|
2015-01-20 19:24:54 +01:00
|
|
|
}
|
2012-10-16 21:28:07 +02:00
|
|
|
|
|
|
|
handler_->DestroyTest();
|
2020-01-15 14:34:01 +01:00
|
|
|
handler_ = nullptr;
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
DialogTestHandler* handler_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(Callback);
|
|
|
|
};
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
explicit DialogTestHandler(const TestConfig& config) : config_(config) {}
|
2012-10-16 21:28:07 +02:00
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void RunTest() override {
|
2012-10-16 21:28:07 +02:00
|
|
|
AddResource(kTestUrl, "<html><body>TEST</body></html>", "text/html");
|
|
|
|
|
|
|
|
// Create the browser
|
|
|
|
CreateBrowser(kTestUrl);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
// Time out the test after a reasonable period of time.
|
|
|
|
SetTestTimeout();
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnLoadEnd(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
int httpStatusCode) override {
|
2022-04-15 21:55:23 +02:00
|
|
|
browser->GetHost()->RunFileDialog(config_.mode, config_.title,
|
|
|
|
config_.default_file_name,
|
|
|
|
config_.accept_types, new Callback(this));
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExecuteCallback(CefRefPtr<CefFileDialogCallback> callback) {
|
|
|
|
if (config_.callback_cancel)
|
|
|
|
callback->Cancel();
|
|
|
|
else
|
2022-04-15 21:55:23 +02:00
|
|
|
callback->Continue(config_.callback_paths);
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CefDialogHandler
|
2017-05-17 11:29:28 +02:00
|
|
|
bool OnFileDialog(CefRefPtr<CefBrowser> browser,
|
|
|
|
FileDialogMode mode,
|
|
|
|
const CefString& title,
|
|
|
|
const CefString& default_file_name,
|
|
|
|
const std::vector<CefString>& accept_types,
|
|
|
|
CefRefPtr<CefFileDialogCallback> callback) override {
|
2012-10-16 21:28:07 +02:00
|
|
|
got_onfiledialog_.yes();
|
|
|
|
|
|
|
|
std::string url = browser->GetMainFrame()->GetURL();
|
|
|
|
EXPECT_STREQ(kTestUrl, url.c_str());
|
|
|
|
|
|
|
|
EXPECT_EQ(config_.mode, mode);
|
|
|
|
EXPECT_STREQ(config_.title.ToString().c_str(), title.ToString().c_str());
|
|
|
|
EXPECT_STREQ(config_.default_file_name.ToString().c_str(),
|
|
|
|
default_file_name.ToString().c_str());
|
|
|
|
TestStringVectorEqual(config_.accept_types, accept_types);
|
|
|
|
|
|
|
|
if (config_.callback_async) {
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI, base::BindOnce(&DialogTestHandler::ExecuteCallback,
|
|
|
|
this, callback));
|
2012-10-16 21:28:07 +02:00
|
|
|
} else {
|
|
|
|
ExecuteCallback(callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void DestroyTest() override {
|
2012-10-16 21:28:07 +02:00
|
|
|
EXPECT_TRUE(got_onfiledialog_);
|
|
|
|
EXPECT_TRUE(got_onfiledialogdismissed_);
|
|
|
|
|
|
|
|
TestHandler::DestroyTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
TestConfig config_;
|
|
|
|
|
|
|
|
TrackCallback got_onfiledialog_;
|
|
|
|
TrackCallback got_onfiledialogdismissed_;
|
2015-09-09 16:05:39 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(DialogTestHandler);
|
2012-10-16 21:28:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Test with all parameters empty.
|
|
|
|
TEST(DialogTest, FileEmptyParams) {
|
|
|
|
DialogTestHandler::TestConfig config(FILE_DIALOG_OPEN);
|
|
|
|
config.title.clear();
|
|
|
|
config.default_file_name.clear();
|
|
|
|
config.accept_types.clear();
|
|
|
|
config.callback_async = false;
|
|
|
|
config.callback_cancel = false;
|
|
|
|
|
|
|
|
CefRefPtr<DialogTestHandler> handler = new DialogTestHandler(config);
|
|
|
|
handler->ExecuteTest();
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DialogTest, FileOpen) {
|
|
|
|
DialogTestHandler::TestConfig config(FILE_DIALOG_OPEN);
|
|
|
|
config.callback_async = false;
|
|
|
|
config.callback_cancel = false;
|
|
|
|
config.callback_paths.push_back("/path/to/file1.txt");
|
|
|
|
|
|
|
|
CefRefPtr<DialogTestHandler> handler = new DialogTestHandler(config);
|
|
|
|
handler->ExecuteTest();
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DialogTest, FileOpenCancel) {
|
|
|
|
DialogTestHandler::TestConfig config(FILE_DIALOG_OPEN);
|
|
|
|
config.callback_async = false;
|
|
|
|
config.callback_cancel = true;
|
|
|
|
|
|
|
|
CefRefPtr<DialogTestHandler> handler = new DialogTestHandler(config);
|
|
|
|
handler->ExecuteTest();
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DialogTest, FileOpenAsync) {
|
|
|
|
DialogTestHandler::TestConfig config(FILE_DIALOG_OPEN);
|
|
|
|
config.callback_async = true;
|
|
|
|
config.callback_cancel = false;
|
|
|
|
config.callback_paths.push_back("/path/to/file1.txt");
|
|
|
|
|
|
|
|
CefRefPtr<DialogTestHandler> handler = new DialogTestHandler(config);
|
|
|
|
handler->ExecuteTest();
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DialogTest, FileOpenAsyncCancel) {
|
|
|
|
DialogTestHandler::TestConfig config(FILE_DIALOG_OPEN);
|
|
|
|
config.callback_async = false;
|
|
|
|
config.callback_cancel = true;
|
|
|
|
|
|
|
|
CefRefPtr<DialogTestHandler> handler = new DialogTestHandler(config);
|
|
|
|
handler->ExecuteTest();
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DialogTest, FileOpenMultiple) {
|
|
|
|
DialogTestHandler::TestConfig config(FILE_DIALOG_OPEN_MULTIPLE);
|
|
|
|
config.callback_async = false;
|
|
|
|
config.callback_cancel = false;
|
|
|
|
config.callback_paths.push_back("/path/to/file1.txt");
|
|
|
|
config.callback_paths.push_back("/path/to/file2.txt");
|
|
|
|
|
|
|
|
CefRefPtr<DialogTestHandler> handler = new DialogTestHandler(config);
|
|
|
|
handler->ExecuteTest();
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DialogTest, FileOpenMultipleCancel) {
|
|
|
|
DialogTestHandler::TestConfig config(FILE_DIALOG_OPEN_MULTIPLE);
|
|
|
|
config.callback_async = false;
|
|
|
|
config.callback_cancel = true;
|
|
|
|
|
|
|
|
CefRefPtr<DialogTestHandler> handler = new DialogTestHandler(config);
|
|
|
|
handler->ExecuteTest();
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DialogTest, FileOpenMultipleAsync) {
|
|
|
|
DialogTestHandler::TestConfig config(FILE_DIALOG_OPEN_MULTIPLE);
|
|
|
|
config.callback_async = true;
|
|
|
|
config.callback_cancel = false;
|
|
|
|
config.callback_paths.push_back("/path/to/file1.txt");
|
|
|
|
config.callback_paths.push_back("/path/to/file2.txt");
|
|
|
|
|
|
|
|
CefRefPtr<DialogTestHandler> handler = new DialogTestHandler(config);
|
|
|
|
handler->ExecuteTest();
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DialogTest, FileOpenMultipleAsyncCancel) {
|
|
|
|
DialogTestHandler::TestConfig config(FILE_DIALOG_OPEN_MULTIPLE);
|
|
|
|
config.callback_async = false;
|
|
|
|
config.callback_cancel = true;
|
|
|
|
|
|
|
|
CefRefPtr<DialogTestHandler> handler = new DialogTestHandler(config);
|
|
|
|
handler->ExecuteTest();
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
2015-01-20 19:24:54 +01:00
|
|
|
TEST(DialogTest, FileOpenFolder) {
|
|
|
|
DialogTestHandler::TestConfig config(FILE_DIALOG_OPEN_FOLDER);
|
|
|
|
config.callback_async = false;
|
|
|
|
config.callback_cancel = false;
|
|
|
|
config.callback_paths.push_back("/path/to/folder");
|
|
|
|
|
|
|
|
CefRefPtr<DialogTestHandler> handler = new DialogTestHandler(config);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
ReleaseAndWaitForDestructor(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DialogTest, FileOpenFolderCancel) {
|
|
|
|
DialogTestHandler::TestConfig config(FILE_DIALOG_OPEN_FOLDER);
|
|
|
|
config.callback_async = false;
|
|
|
|
config.callback_cancel = true;
|
|
|
|
|
|
|
|
CefRefPtr<DialogTestHandler> handler = new DialogTestHandler(config);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
ReleaseAndWaitForDestructor(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DialogTest, FileOpenFolderAsync) {
|
|
|
|
DialogTestHandler::TestConfig config(FILE_DIALOG_OPEN_FOLDER);
|
|
|
|
config.callback_async = true;
|
|
|
|
config.callback_cancel = false;
|
|
|
|
config.callback_paths.push_back("/path/to/folder");
|
|
|
|
|
|
|
|
CefRefPtr<DialogTestHandler> handler = new DialogTestHandler(config);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
ReleaseAndWaitForDestructor(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DialogTest, FileOpenFolderAsyncCancel) {
|
|
|
|
DialogTestHandler::TestConfig config(FILE_DIALOG_OPEN_FOLDER);
|
|
|
|
config.callback_async = false;
|
|
|
|
config.callback_cancel = true;
|
|
|
|
|
|
|
|
CefRefPtr<DialogTestHandler> handler = new DialogTestHandler(config);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
ReleaseAndWaitForDestructor(handler);
|
|
|
|
}
|
|
|
|
|
2012-10-16 21:28:07 +02:00
|
|
|
TEST(DialogTest, FileSave) {
|
|
|
|
DialogTestHandler::TestConfig config(FILE_DIALOG_SAVE);
|
|
|
|
config.callback_async = false;
|
|
|
|
config.callback_cancel = false;
|
|
|
|
config.callback_paths.push_back("/path/to/file1.txt");
|
|
|
|
|
|
|
|
CefRefPtr<DialogTestHandler> handler = new DialogTestHandler(config);
|
|
|
|
handler->ExecuteTest();
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DialogTest, FileSaveCancel) {
|
|
|
|
DialogTestHandler::TestConfig config(FILE_DIALOG_SAVE);
|
|
|
|
config.callback_async = false;
|
|
|
|
config.callback_cancel = true;
|
|
|
|
|
|
|
|
CefRefPtr<DialogTestHandler> handler = new DialogTestHandler(config);
|
|
|
|
handler->ExecuteTest();
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DialogTest, FileSaveAsync) {
|
|
|
|
DialogTestHandler::TestConfig config(FILE_DIALOG_SAVE);
|
|
|
|
config.callback_async = true;
|
|
|
|
config.callback_cancel = false;
|
|
|
|
config.callback_paths.push_back("/path/to/file1.txt");
|
|
|
|
|
|
|
|
CefRefPtr<DialogTestHandler> handler = new DialogTestHandler(config);
|
|
|
|
handler->ExecuteTest();
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DialogTest, FileSaveAsyncCancel) {
|
|
|
|
DialogTestHandler::TestConfig config(FILE_DIALOG_SAVE);
|
|
|
|
config.callback_async = false;
|
|
|
|
config.callback_cancel = true;
|
|
|
|
|
|
|
|
CefRefPtr<DialogTestHandler> handler = new DialogTestHandler(config);
|
|
|
|
handler->ExecuteTest();
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-10-16 21:28:07 +02:00
|
|
|
}
|