2013-10-18 18:33:56 +02:00
|
|
|
// Copyright (c) 2013 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"
|
2017-05-31 17:33:30 +02:00
|
|
|
#include "include/test/cef_test_helpers.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/routing_test_handler.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/gtest/include/gtest/gtest.h"
|
2013-10-18 18:33:56 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2023-04-29 00:17:03 +02:00
|
|
|
const char kLifeSpanUrl[] = "https://tests-life-span/test.html";
|
2013-10-18 18:33:56 +02:00
|
|
|
const char kUnloadDialogText[] = "Are you sure?";
|
|
|
|
const char kUnloadMsg[] = "LifeSpanTestHandler.Unload";
|
|
|
|
|
|
|
|
// Browser side.
|
2014-01-28 00:31:03 +01:00
|
|
|
class LifeSpanTestHandler : public RoutingTestHandler {
|
2013-10-18 18:33:56 +02:00
|
|
|
public:
|
|
|
|
struct Settings {
|
2024-01-20 18:00:09 +01:00
|
|
|
Settings() = default;
|
|
|
|
|
|
|
|
bool force_close = false;
|
|
|
|
bool add_onunload_handler = false;
|
|
|
|
bool allow_do_close = true;
|
|
|
|
bool accept_before_unload_dialog = true;
|
2013-10-18 18:33:56 +02:00
|
|
|
};
|
|
|
|
|
2024-01-20 18:00:09 +01:00
|
|
|
explicit LifeSpanTestHandler(const Settings& settings) : settings_(settings) {
|
2015-01-10 00:40:26 +01:00
|
|
|
// By default no LifeSpan tests call DestroyTest().
|
|
|
|
SetDestroyTestExpected(false);
|
|
|
|
}
|
2013-10-18 18:33:56 +02:00
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void RunTest() override {
|
2013-10-18 18:33:56 +02:00
|
|
|
// Add the resources that we will navigate to/from.
|
|
|
|
std::string page = "<html><script>";
|
2014-01-28 00:31:03 +01:00
|
|
|
|
|
|
|
page += "window.onunload = function() { window.testQuery({request:'" +
|
2017-05-17 11:29:28 +02:00
|
|
|
std::string(kUnloadMsg) + "'}); };";
|
2013-10-18 18:33:56 +02:00
|
|
|
|
|
|
|
if (settings_.add_onunload_handler) {
|
|
|
|
page += "window.onbeforeunload = function() { return '" +
|
|
|
|
std::string(kUnloadDialogText) + "'; };";
|
|
|
|
}
|
|
|
|
|
|
|
|
page += "</script><body>Page</body></html>";
|
|
|
|
AddResource(kLifeSpanUrl, page, "text/html");
|
|
|
|
|
|
|
|
// Create the browser.
|
|
|
|
CreateBrowser(kLifeSpanUrl);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
// Intentionally don't call SetTestTimeout() for these tests.
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnAfterCreated(CefRefPtr<CefBrowser> browser) override {
|
2013-10-18 18:33:56 +02:00
|
|
|
got_after_created_.yes();
|
2014-01-28 00:31:03 +01:00
|
|
|
RoutingTestHandler::OnAfterCreated(browser);
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool DoClose(CefRefPtr<CefBrowser> browser) override {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (executing_delay_close_) {
|
2013-10-18 18:33:56 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2013-10-18 18:33:56 +02:00
|
|
|
|
|
|
|
EXPECT_TRUE(browser->IsSame(GetBrowser()));
|
|
|
|
|
|
|
|
got_do_close_.yes();
|
|
|
|
|
|
|
|
if (!settings_.allow_do_close) {
|
|
|
|
// The close will be canceled.
|
|
|
|
ScheduleDelayClose();
|
|
|
|
}
|
|
|
|
|
|
|
|
return !settings_.allow_do_close;
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnBeforeClose(CefRefPtr<CefBrowser> browser) override {
|
2013-10-18 18:33:56 +02:00
|
|
|
if (!executing_delay_close_) {
|
|
|
|
got_before_close_.yes();
|
|
|
|
EXPECT_TRUE(browser->IsSame(GetBrowser()));
|
|
|
|
}
|
|
|
|
|
2014-01-28 00:31:03 +01:00
|
|
|
RoutingTestHandler::OnBeforeClose(browser);
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
bool OnBeforeUnloadDialog(CefRefPtr<CefBrowser> browser,
|
|
|
|
const CefString& message_text,
|
|
|
|
bool is_reload,
|
|
|
|
CefRefPtr<CefJSDialogCallback> callback) override {
|
2013-10-18 18:33:56 +02:00
|
|
|
if (executing_delay_close_) {
|
|
|
|
callback->Continue(true, CefString());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_TRUE(browser->IsSame(GetBrowser()));
|
2016-03-16 03:55:59 +01:00
|
|
|
|
|
|
|
// The message is no longer configurable via JavaScript.
|
2023-04-29 00:17:03 +02:00
|
|
|
// See https://crbug.com/587940.
|
2016-03-16 03:55:59 +01:00
|
|
|
EXPECT_STREQ("Is it OK to leave/reload this page?",
|
|
|
|
message_text.ToString().c_str());
|
|
|
|
|
2013-10-18 18:33:56 +02:00
|
|
|
EXPECT_FALSE(is_reload);
|
|
|
|
EXPECT_TRUE(callback.get());
|
|
|
|
|
|
|
|
if (!settings_.accept_before_unload_dialog) {
|
|
|
|
// The close will be canceled.
|
|
|
|
ScheduleDelayClose();
|
|
|
|
}
|
|
|
|
|
|
|
|
got_before_unload_dialog_.yes();
|
|
|
|
callback->Continue(settings_.accept_before_unload_dialog, CefString());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnLoadEnd(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
int httpStatusCode) override {
|
2013-10-18 18:33:56 +02:00
|
|
|
got_load_end_.yes();
|
|
|
|
EXPECT_TRUE(browser->IsSame(GetBrowser()));
|
|
|
|
|
2017-05-31 17:33:30 +02:00
|
|
|
if (settings_.add_onunload_handler) {
|
|
|
|
// Send the page a user gesture to enable firing of the onbefureunload
|
|
|
|
// handler. See https://crbug.com/707007.
|
|
|
|
CefExecuteJavaScriptWithUserGestureForTests(frame, CefString());
|
|
|
|
}
|
|
|
|
|
2013-10-18 18:33:56 +02:00
|
|
|
// Attempt to close the browser.
|
2016-01-19 21:09:01 +01:00
|
|
|
CloseBrowser(browser, settings_.force_close);
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool OnQuery(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
2023-06-01 16:06:15 +02:00
|
|
|
int64_t query_id,
|
2014-11-12 20:25:15 +01:00
|
|
|
const CefString& request,
|
|
|
|
bool persistent,
|
|
|
|
CefRefPtr<Callback> callback) override {
|
2014-01-28 00:31:03 +01:00
|
|
|
if (request.ToString() == kUnloadMsg) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!executing_delay_close_) {
|
2013-10-18 18:33:56 +02:00
|
|
|
got_unload_message_.yes();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
2014-01-28 00:31:03 +01:00
|
|
|
callback->Success("");
|
|
|
|
return true;
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TrackCallback got_after_created_;
|
|
|
|
TrackCallback got_do_close_;
|
|
|
|
TrackCallback got_before_close_;
|
|
|
|
TrackCallback got_before_unload_dialog_;
|
|
|
|
TrackCallback got_unload_message_;
|
|
|
|
TrackCallback got_load_end_;
|
|
|
|
TrackCallback got_delay_close_;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Wait a bit to make sure no additional events are received and then close
|
|
|
|
// the window.
|
|
|
|
void ScheduleDelayClose() {
|
2015-01-10 00:40:26 +01:00
|
|
|
// This test will call DestroyTest().
|
|
|
|
SetDestroyTestExpected(true);
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostDelayedTask(
|
|
|
|
TID_UI, base::BindOnce(&LifeSpanTestHandler::DelayClose, this), 100);
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DelayClose() {
|
|
|
|
got_delay_close_.yes();
|
|
|
|
executing_delay_close_ = true;
|
|
|
|
DestroyTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
Settings settings_;
|
|
|
|
|
|
|
|
// Forces the window to close (bypasses test conditions).
|
2024-01-20 18:00:09 +01:00
|
|
|
bool executing_delay_close_ = false;
|
2015-09-09 16:05:39 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(LifeSpanTestHandler);
|
2013-10-18 18:33:56 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(LifeSpanTest, DoCloseAllow) {
|
|
|
|
LifeSpanTestHandler::Settings settings;
|
|
|
|
settings.allow_do_close = true;
|
|
|
|
CefRefPtr<LifeSpanTestHandler> handler = new LifeSpanTestHandler(settings);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
|
|
|
|
EXPECT_TRUE(handler->got_after_created_);
|
|
|
|
EXPECT_TRUE(handler->got_do_close_);
|
|
|
|
EXPECT_TRUE(handler->got_before_close_);
|
|
|
|
EXPECT_FALSE(handler->got_before_unload_dialog_);
|
|
|
|
EXPECT_TRUE(handler->got_unload_message_);
|
|
|
|
EXPECT_TRUE(handler->got_load_end_);
|
|
|
|
EXPECT_FALSE(handler->got_delay_close_);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LifeSpanTest, DoCloseAllowForce) {
|
|
|
|
LifeSpanTestHandler::Settings settings;
|
|
|
|
settings.allow_do_close = true;
|
|
|
|
settings.force_close = true;
|
|
|
|
CefRefPtr<LifeSpanTestHandler> handler = new LifeSpanTestHandler(settings);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
|
|
|
|
EXPECT_TRUE(handler->got_after_created_);
|
|
|
|
EXPECT_TRUE(handler->got_do_close_);
|
|
|
|
EXPECT_TRUE(handler->got_before_close_);
|
|
|
|
EXPECT_FALSE(handler->got_before_unload_dialog_);
|
|
|
|
EXPECT_TRUE(handler->got_unload_message_);
|
|
|
|
EXPECT_TRUE(handler->got_load_end_);
|
|
|
|
EXPECT_FALSE(handler->got_delay_close_);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LifeSpanTest, DoCloseDisallow) {
|
|
|
|
LifeSpanTestHandler::Settings settings;
|
|
|
|
settings.allow_do_close = false;
|
|
|
|
CefRefPtr<LifeSpanTestHandler> handler = new LifeSpanTestHandler(settings);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
|
|
|
|
EXPECT_TRUE(handler->got_after_created_);
|
|
|
|
EXPECT_TRUE(handler->got_do_close_);
|
|
|
|
EXPECT_FALSE(handler->got_before_close_);
|
|
|
|
EXPECT_FALSE(handler->got_before_unload_dialog_);
|
|
|
|
EXPECT_TRUE(handler->got_unload_message_);
|
|
|
|
EXPECT_TRUE(handler->got_load_end_);
|
|
|
|
EXPECT_TRUE(handler->got_delay_close_);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LifeSpanTest, DoCloseDisallowForce) {
|
|
|
|
LifeSpanTestHandler::Settings settings;
|
|
|
|
settings.allow_do_close = false;
|
|
|
|
settings.force_close = true;
|
|
|
|
CefRefPtr<LifeSpanTestHandler> handler = new LifeSpanTestHandler(settings);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
|
|
|
|
EXPECT_TRUE(handler->got_after_created_);
|
|
|
|
EXPECT_TRUE(handler->got_do_close_);
|
|
|
|
EXPECT_FALSE(handler->got_before_close_);
|
|
|
|
EXPECT_FALSE(handler->got_before_unload_dialog_);
|
|
|
|
EXPECT_TRUE(handler->got_unload_message_);
|
|
|
|
EXPECT_TRUE(handler->got_load_end_);
|
|
|
|
EXPECT_TRUE(handler->got_delay_close_);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LifeSpanTest, DoCloseDisallowWithOnUnloadAllow) {
|
|
|
|
LifeSpanTestHandler::Settings settings;
|
|
|
|
settings.allow_do_close = false;
|
|
|
|
settings.add_onunload_handler = true;
|
|
|
|
settings.accept_before_unload_dialog = true;
|
|
|
|
CefRefPtr<LifeSpanTestHandler> handler = new LifeSpanTestHandler(settings);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
|
|
|
|
EXPECT_TRUE(handler->got_after_created_);
|
|
|
|
EXPECT_TRUE(handler->got_do_close_);
|
|
|
|
EXPECT_FALSE(handler->got_before_close_);
|
|
|
|
EXPECT_TRUE(handler->got_before_unload_dialog_);
|
|
|
|
EXPECT_TRUE(handler->got_unload_message_);
|
|
|
|
EXPECT_TRUE(handler->got_load_end_);
|
|
|
|
EXPECT_TRUE(handler->got_delay_close_);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LifeSpanTest, DoCloseAllowWithOnUnloadForce) {
|
|
|
|
LifeSpanTestHandler::Settings settings;
|
|
|
|
settings.allow_do_close = true;
|
|
|
|
settings.add_onunload_handler = true;
|
|
|
|
settings.force_close = true;
|
|
|
|
CefRefPtr<LifeSpanTestHandler> handler = new LifeSpanTestHandler(settings);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
|
|
|
|
EXPECT_TRUE(handler->got_after_created_);
|
|
|
|
EXPECT_TRUE(handler->got_do_close_);
|
|
|
|
EXPECT_TRUE(handler->got_before_close_);
|
2024-06-18 18:24:55 +02:00
|
|
|
EXPECT_TRUE(handler->got_before_unload_dialog_);
|
2013-10-18 18:33:56 +02:00
|
|
|
EXPECT_TRUE(handler->got_unload_message_);
|
|
|
|
EXPECT_TRUE(handler->got_load_end_);
|
|
|
|
EXPECT_FALSE(handler->got_delay_close_);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LifeSpanTest, DoCloseDisallowWithOnUnloadForce) {
|
|
|
|
LifeSpanTestHandler::Settings settings;
|
|
|
|
settings.allow_do_close = false;
|
|
|
|
settings.add_onunload_handler = true;
|
|
|
|
settings.force_close = true;
|
|
|
|
CefRefPtr<LifeSpanTestHandler> handler = new LifeSpanTestHandler(settings);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
|
|
|
|
EXPECT_TRUE(handler->got_after_created_);
|
|
|
|
EXPECT_TRUE(handler->got_do_close_);
|
|
|
|
EXPECT_FALSE(handler->got_before_close_);
|
2024-06-18 18:24:55 +02:00
|
|
|
EXPECT_TRUE(handler->got_before_unload_dialog_);
|
2013-10-18 18:33:56 +02:00
|
|
|
EXPECT_TRUE(handler->got_unload_message_);
|
|
|
|
EXPECT_TRUE(handler->got_load_end_);
|
|
|
|
EXPECT_TRUE(handler->got_delay_close_);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LifeSpanTest, OnUnloadAllow) {
|
|
|
|
LifeSpanTestHandler::Settings settings;
|
|
|
|
settings.add_onunload_handler = true;
|
|
|
|
settings.accept_before_unload_dialog = true;
|
|
|
|
CefRefPtr<LifeSpanTestHandler> handler = new LifeSpanTestHandler(settings);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
|
|
|
|
EXPECT_TRUE(handler->got_after_created_);
|
|
|
|
EXPECT_TRUE(handler->got_do_close_);
|
|
|
|
EXPECT_TRUE(handler->got_before_close_);
|
|
|
|
EXPECT_TRUE(handler->got_before_unload_dialog_);
|
|
|
|
EXPECT_TRUE(handler->got_unload_message_);
|
|
|
|
EXPECT_TRUE(handler->got_load_end_);
|
|
|
|
EXPECT_FALSE(handler->got_delay_close_);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(LifeSpanTest, OnUnloadDisallow) {
|
|
|
|
LifeSpanTestHandler::Settings settings;
|
|
|
|
settings.add_onunload_handler = true;
|
|
|
|
settings.accept_before_unload_dialog = false;
|
|
|
|
CefRefPtr<LifeSpanTestHandler> handler = new LifeSpanTestHandler(settings);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
|
|
|
|
EXPECT_TRUE(handler->got_after_created_);
|
|
|
|
EXPECT_FALSE(handler->got_do_close_);
|
|
|
|
EXPECT_FALSE(handler->got_before_close_);
|
|
|
|
EXPECT_TRUE(handler->got_before_unload_dialog_);
|
|
|
|
EXPECT_FALSE(handler->got_unload_message_);
|
|
|
|
EXPECT_TRUE(handler->got_load_end_);
|
|
|
|
EXPECT_TRUE(handler->got_delay_close_);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2013-10-18 18:33:56 +02:00
|
|
|
}
|