2016-01-19 21:09:01 +01:00
|
|
|
// Copyright (c) 2016 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.
|
|
|
|
|
2016-11-18 18:31:21 +01:00
|
|
|
#include "tests/ceftests/views/test_window_delegate.h"
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
#include "include/cef_command_line.h"
|
|
|
|
#include "include/views/cef_window.h"
|
|
|
|
#include "include/views/cef_window_delegate.h"
|
|
|
|
#include "include/wrapper/cef_closure_task.h"
|
2023-01-13 19:49:33 +01:00
|
|
|
#include "tests/ceftests/test_util.h"
|
2016-11-18 18:31:21 +01:00
|
|
|
#include "tests/ceftests/thread_helper.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/gtest/include/gtest/gtest.h"
|
2016-01-19 21:09:01 +01:00
|
|
|
|
2019-10-04 16:23:58 +02:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include <windows.h>
|
2024-04-30 17:45:07 +02:00
|
|
|
|
2022-07-27 19:57:53 +02:00
|
|
|
#include "tests/shared/browser/geometry_util.h"
|
|
|
|
#include "tests/shared/browser/util_win.h"
|
2019-10-04 16:23:58 +02:00
|
|
|
#endif
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Test timeout in MS.
|
|
|
|
const int kTestTimeout = 5000;
|
|
|
|
|
2023-09-25 21:40:17 +02:00
|
|
|
#if defined(OS_MAC)
|
|
|
|
// Match the value in view_util_mac.mm.
|
|
|
|
constexpr float kDefaultTitleBarHeight = 30;
|
|
|
|
#endif
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// static
|
|
|
|
const int TestWindowDelegate::kWSize = 400;
|
|
|
|
|
|
|
|
// static
|
2016-11-15 22:18:41 +01:00
|
|
|
void TestWindowDelegate::RunTest(CefRefPtr<CefWaitableEvent> event,
|
2024-04-10 23:34:11 +02:00
|
|
|
std::unique_ptr<Config> config,
|
|
|
|
TestWindowDelegateFactory factory) {
|
2022-07-27 19:57:53 +02:00
|
|
|
CefSize window_size{config->window_size, config->window_size};
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
if (!config->frameless) {
|
2023-09-25 21:40:17 +02:00
|
|
|
#if defined(OS_WIN)
|
2022-07-27 19:57:53 +02:00
|
|
|
// Expand the client area size to full window size based on the default
|
|
|
|
// frame window style. AdjustWindowRect expects pixel coordinates, so
|
|
|
|
// perform the necessary conversions.
|
|
|
|
auto scale_factor = client::GetDeviceScaleFactor();
|
|
|
|
auto scaled_size =
|
|
|
|
client::LogicalToDevice(config->window_size, scale_factor);
|
|
|
|
|
|
|
|
// Convert from DIP to pixel coords.
|
|
|
|
RECT rect = {0, 0, scaled_size, scaled_size};
|
|
|
|
|
2019-10-04 16:23:58 +02:00
|
|
|
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
|
|
|
|
false /* has_menu */);
|
2022-07-27 19:57:53 +02:00
|
|
|
|
|
|
|
// Convert from pixel to DIP coords.
|
|
|
|
auto scaled_rect = client::DeviceToLogical(
|
|
|
|
{rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top},
|
|
|
|
scale_factor);
|
|
|
|
window_size = {scaled_rect.width, scaled_rect.height};
|
2023-09-25 21:40:17 +02:00
|
|
|
#elif defined(OS_MAC)
|
|
|
|
// Expand client area size to include the default titlebar height.
|
|
|
|
window_size.height += kDefaultTitleBarHeight;
|
|
|
|
#endif
|
2019-10-04 16:23:58 +02:00
|
|
|
}
|
|
|
|
|
2024-04-10 23:34:11 +02:00
|
|
|
TestWindowDelegate* delegate;
|
|
|
|
if (!factory.is_null()) {
|
|
|
|
delegate = std::move(factory).Run(event, std::move(config), window_size);
|
|
|
|
CHECK(delegate);
|
|
|
|
} else {
|
|
|
|
delegate = new TestWindowDelegate(event, std::move(config), window_size);
|
|
|
|
}
|
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
auto window = CefWindow::CreateTopLevelWindow(delegate);
|
|
|
|
EXPECT_EQ(delegate->GetWindowRuntimeStyle(), window->GetRuntimeStyle());
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestWindowDelegate::OnWindowCreated(CefRefPtr<CefWindow> window) {
|
|
|
|
EXPECT_FALSE(window_);
|
|
|
|
window_ = window;
|
|
|
|
|
|
|
|
EXPECT_TRUE(window->IsValid());
|
|
|
|
EXPECT_FALSE(window->IsClosed());
|
|
|
|
|
|
|
|
EXPECT_FALSE(window->IsActive());
|
|
|
|
EXPECT_FALSE(window->IsAlwaysOnTop());
|
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
const std::string& title = ComputeViewsWindowTitle(window, nullptr);
|
2016-01-19 21:09:01 +01:00
|
|
|
window->SetTitle(title);
|
2024-04-17 18:01:26 +02:00
|
|
|
EXPECT_STREQ(title.c_str(), window->GetTitle().ToString().c_str());
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
EXPECT_FALSE(window->GetWindowIcon().get());
|
|
|
|
EXPECT_FALSE(window->GetWindowAppIcon().get());
|
|
|
|
|
2020-10-08 21:54:42 +02:00
|
|
|
auto display = window->GetDisplay();
|
|
|
|
EXPECT_TRUE(display.get());
|
2016-01-19 21:09:01 +01:00
|
|
|
|
2020-09-02 20:25:25 +02:00
|
|
|
// Size will come from GetGetInitialBounds() or GetPreferredSize() on
|
|
|
|
// initial Window creation.
|
|
|
|
EXPECT_TRUE(got_get_initial_bounds_);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (config_->window_origin.IsEmpty()) {
|
2020-09-02 20:25:25 +02:00
|
|
|
EXPECT_TRUE(got_get_preferred_size_);
|
2023-01-02 23:59:03 +01:00
|
|
|
} else {
|
2020-09-02 20:25:25 +02:00
|
|
|
EXPECT_FALSE(got_get_preferred_size_);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-09-02 20:25:25 +02:00
|
|
|
|
2023-09-25 21:40:17 +02:00
|
|
|
// Expectations for the default |initial_show_state| value.
|
|
|
|
if (config_->initial_show_state == CEF_SHOW_STATE_NORMAL) {
|
|
|
|
EXPECT_FALSE(window->IsVisible());
|
|
|
|
EXPECT_FALSE(window->IsDrawn());
|
|
|
|
|
|
|
|
EXPECT_FALSE(window->IsMaximized());
|
|
|
|
EXPECT_FALSE(window->IsMinimized());
|
|
|
|
EXPECT_FALSE(window->IsFullscreen());
|
|
|
|
|
|
|
|
CefRect client_bounds = window->GetBounds();
|
|
|
|
if (!config_->window_origin.IsEmpty()) {
|
|
|
|
EXPECT_EQ(config_->window_origin.x, client_bounds.x);
|
|
|
|
EXPECT_EQ(config_->window_origin.y, client_bounds.y);
|
|
|
|
} else {
|
|
|
|
// Default origin is the upper-left corner of the display's work area.
|
|
|
|
auto work_area = display->GetWorkArea();
|
|
|
|
EXPECT_NEAR(work_area.x, client_bounds.x, 1);
|
|
|
|
EXPECT_NEAR(work_area.y, client_bounds.y, 1);
|
|
|
|
}
|
2020-09-02 20:25:25 +02:00
|
|
|
|
2023-09-25 21:40:17 +02:00
|
|
|
if (config_->frameless) {
|
|
|
|
EXPECT_NEAR(config_->window_size, client_bounds.width, 2);
|
|
|
|
EXPECT_NEAR(config_->window_size, client_bounds.height, 2);
|
|
|
|
} else {
|
|
|
|
// Client area bounds calculation might have off-by-one errors on Windows
|
|
|
|
// due to non-client frame size being calculated internally in pixels and
|
|
|
|
// then converted to DIPs. See https://crbug.com/602692.
|
|
|
|
EXPECT_NEAR(client_bounds.width, window_size_.width, 2);
|
|
|
|
EXPECT_NEAR(client_bounds.height, window_size_.height, 2);
|
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
2017-02-17 00:19:43 +01:00
|
|
|
// Run the callback.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!config_->on_window_created.is_null()) {
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(config_->on_window_created).Run(window);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
if (config_->close_window) {
|
2016-01-19 21:09:01 +01:00
|
|
|
// Close the window asynchronously.
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI,
|
|
|
|
base::BindOnce(&TestWindowDelegate::OnCloseWindow, this));
|
2023-01-13 19:49:33 +01:00
|
|
|
} else {
|
|
|
|
const auto timeout = GetConfiguredTestTimeout(kTestTimeout);
|
|
|
|
if (timeout) {
|
|
|
|
// Timeout the test after a reasonable delay. Use a WeakPtr so that the
|
|
|
|
// delayed task doesn't keep this object alive.
|
|
|
|
CefPostDelayedTask(TID_UI,
|
|
|
|
base::BindOnce(&TestWindowDelegate::OnTimeoutWindow,
|
|
|
|
weak_ptr_factory_.GetWeakPtr()),
|
|
|
|
*timeout);
|
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestWindowDelegate::OnWindowDestroyed(CefRefPtr<CefWindow> window) {
|
|
|
|
EXPECT_TRUE(window->IsSame(window_));
|
|
|
|
|
|
|
|
EXPECT_TRUE(window->IsValid());
|
|
|
|
EXPECT_TRUE(window->IsClosed());
|
|
|
|
EXPECT_FALSE(window->IsVisible());
|
|
|
|
EXPECT_FALSE(window->IsDrawn());
|
|
|
|
|
2017-02-17 00:19:43 +01:00
|
|
|
// Run the callback.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!config_->on_window_destroyed.is_null()) {
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(config_->on_window_destroyed).Run(window);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-02-17 00:19:43 +01:00
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
window_ = nullptr;
|
|
|
|
|
|
|
|
// Don't execute the timeout callback.
|
|
|
|
weak_ptr_factory_.InvalidateWeakPtrs();
|
|
|
|
}
|
|
|
|
|
2023-09-25 21:40:17 +02:00
|
|
|
void TestWindowDelegate::OnWindowFullscreenTransition(
|
|
|
|
CefRefPtr<CefWindow> window,
|
|
|
|
bool is_completed) {
|
|
|
|
EXPECT_TRUE(window->IsSame(window_));
|
|
|
|
|
|
|
|
EXPECT_TRUE(window->IsValid());
|
|
|
|
EXPECT_FALSE(window->IsClosed());
|
|
|
|
EXPECT_TRUE(window->IsVisible());
|
|
|
|
EXPECT_TRUE(window->IsDrawn());
|
|
|
|
|
|
|
|
fullscreen_transition_callback_count_++;
|
|
|
|
|
|
|
|
#if defined(OS_MAC)
|
2024-09-26 19:52:28 +02:00
|
|
|
// Only one callback when window is initially shown fullscreen on MacOS.
|
|
|
|
if (config_->initial_show_state == CEF_SHOW_STATE_FULLSCREEN &&
|
|
|
|
fullscreen_transition_complete_count_ == 0) {
|
|
|
|
EXPECT_TRUE(is_completed);
|
|
|
|
EXPECT_EQ(1U, fullscreen_transition_callback_count_);
|
|
|
|
} else {
|
|
|
|
// Two callbacks otherwise.
|
|
|
|
EXPECT_EQ(is_completed ? 2U : 1U, fullscreen_transition_callback_count_);
|
|
|
|
}
|
2023-09-25 21:40:17 +02:00
|
|
|
#else
|
|
|
|
// Single callback on other platforms.
|
|
|
|
EXPECT_TRUE(is_completed);
|
|
|
|
EXPECT_EQ(1U, fullscreen_transition_callback_count_);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (is_completed) {
|
|
|
|
fullscreen_transition_complete_count_++;
|
|
|
|
|
|
|
|
// Reset intermediate state.
|
|
|
|
fullscreen_transition_callback_count_ = 0;
|
|
|
|
|
|
|
|
// Run the callback.
|
|
|
|
if (!config_->on_window_fullscreen_transition_complete.is_null()) {
|
|
|
|
config_->on_window_fullscreen_transition_complete.Run(
|
|
|
|
window, fullscreen_transition_complete_count_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
bool TestWindowDelegate::IsFrameless(CefRefPtr<CefWindow> window) {
|
2021-06-19 21:54:45 +02:00
|
|
|
return config_->frameless;
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
2020-09-02 20:25:25 +02:00
|
|
|
CefRect TestWindowDelegate::GetInitialBounds(CefRefPtr<CefWindow> window) {
|
|
|
|
got_get_initial_bounds_ = true;
|
2021-06-19 21:54:45 +02:00
|
|
|
if (!config_->window_origin.IsEmpty()) {
|
|
|
|
return CefRect(config_->window_origin.x, config_->window_origin.y,
|
2020-09-02 20:25:25 +02:00
|
|
|
window_size_.width, window_size_.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call GetPreferredSize().
|
|
|
|
return CefRect();
|
|
|
|
}
|
|
|
|
|
2023-09-25 21:40:17 +02:00
|
|
|
cef_show_state_t TestWindowDelegate::GetInitialShowState(
|
|
|
|
CefRefPtr<CefWindow> window) {
|
|
|
|
return config_->initial_show_state;
|
|
|
|
}
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
CefSize TestWindowDelegate::GetPreferredSize(CefRefPtr<CefView> view) {
|
|
|
|
got_get_preferred_size_ = true;
|
2019-10-04 16:23:58 +02:00
|
|
|
return window_size_;
|
2017-02-17 00:19:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TestWindowDelegate::OnAccelerator(CefRefPtr<CefWindow> window,
|
|
|
|
int command_id) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!config_->on_accelerator.is_null()) {
|
2021-06-19 21:54:45 +02:00
|
|
|
return config_->on_accelerator.Run(window_, command_id);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-02-17 00:19:43 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TestWindowDelegate::OnKeyEvent(CefRefPtr<CefWindow> window,
|
|
|
|
const CefKeyEvent& event) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!config_->on_key_event.is_null()) {
|
2021-06-19 21:54:45 +02:00
|
|
|
return config_->on_key_event.Run(window_, event);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-02-17 00:19:43 +01:00
|
|
|
return false;
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
2024-04-17 18:01:26 +02:00
|
|
|
cef_runtime_style_t TestWindowDelegate::GetWindowRuntimeStyle() {
|
|
|
|
return UseAlloyStyleWindowGlobal() ? CEF_RUNTIME_STYLE_ALLOY
|
|
|
|
: CEF_RUNTIME_STYLE_CHROME;
|
|
|
|
}
|
|
|
|
|
2016-11-15 22:18:41 +01:00
|
|
|
TestWindowDelegate::TestWindowDelegate(CefRefPtr<CefWaitableEvent> event,
|
2021-06-19 21:54:45 +02:00
|
|
|
std::unique_ptr<Config> config,
|
2019-10-04 16:23:58 +02:00
|
|
|
const CefSize& window_size)
|
|
|
|
: event_(event),
|
2021-06-19 21:54:45 +02:00
|
|
|
config_(std::move(config)),
|
2019-10-04 16:23:58 +02:00
|
|
|
window_size_(window_size),
|
|
|
|
weak_ptr_factory_(this) {}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
TestWindowDelegate::~TestWindowDelegate() {
|
|
|
|
// Complete the test (signal the event) asynchronously so objects on the call
|
|
|
|
// stack have a chance to unwind.
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI, base::BindOnce(SignalEvent, event_));
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestWindowDelegate::OnCloseWindow() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!window_) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
EXPECT_TRUE(window_->IsValid());
|
|
|
|
EXPECT_FALSE(window_->IsClosed());
|
|
|
|
|
|
|
|
// Close() may clear |window_| so keep a reference.
|
|
|
|
CefRefPtr<CefWindow> window = window_;
|
|
|
|
window->Close();
|
|
|
|
|
|
|
|
EXPECT_TRUE(window->IsValid());
|
|
|
|
EXPECT_TRUE(window->IsClosed());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestWindowDelegate::OnTimeoutWindow() {
|
|
|
|
EXPECT_TRUE(false) << "Test timed out after " << kTestTimeout << "ms";
|
|
|
|
OnCloseWindow();
|
|
|
|
}
|