cef/tests/cefclient/browser/window_test.cc
Marshall Greenblatt dca0435d2f chrome: Add support for Alloy style browsers and windows (see #3681)
Split the Alloy runtime into bootstrap and style components. Support
creation of Alloy style browsers and windows with the Chrome runtime.
Chrome runtime (`--enable-chrome-runtime`) + Alloy style
(`--use-alloy-style`) supports Views (`--use-views`), native parent
(`--use-native`) and windowless rendering
(`--off-screen-rendering-enabled`).

Print preview is supported in all cases except with windowless rendering
on all platforms and native parent on MacOS. It is disabled by default
with Alloy style for legacy compatibility. Where supported it can be
enabled or disabled globally using `--[enable|disable]-print-preview` or
configured on a per-RequestContext basis using the
`printing.print_preview_disabled` preference. It also behaves as
expected when triggered via the PDF viewer print button.

Chrome runtime + Alloy style behavior differs from Alloy runtime in the
following significant ways:

- Supports Chrome error pages by default.
- DevTools popups are Chrome style only (cannot be windowless).
- The Alloy extension API will not supported.

Chrome runtime + Alloy style passes all expected Alloy ceftests except
the following:

- `DisplayTest.AutoResize` (Alloy extension API not supported)
- `DownloadTest.*` (Download API not yet supported)
- `ExtensionTest.*` (Alloy extension API not supported)

This change also adds Chrome runtime support for
CefContextMenuHandler::RunContextMenu (see #3293).

This change also explicitly blocks (and doesn't retry) FrameAttached
requests from PDF viewer and print preview excluded frames (see #3664).

Known issues specific to Chrome runtime + Alloy style:
- DevTools popup with windowless rendering doesn't load successfully.
  Use windowed rendering or remote debugging as a workaround.
- Chrome style Window with Alloy style BrowserView (`--use-alloy-style
  --use-chrome-style-window`) does not show Chrome theme changes.

To test:
- Run `ceftests --enable-chrome-runtime --use-alloy-style
       [--use-chrome-style-window] [--use-views|--use-native]
       --gtest_filter=...`
- Run `cefclient --enable-chrome-runtime --use-alloy-style
       [--use-chrome-style-window]
       [--use-views|--use-native|--off-screen-rendering-enabled]`
- Run `cefsimple --enable-chrome-runtime --use-alloy-style [--use-views]`
2024-04-22 14:57:37 -04:00

138 lines
4.2 KiB
C++

// 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.
#include "tests/cefclient/browser/window_test.h"
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include "include/base/cef_callback.h"
#include "include/views/cef_browser_view.h"
#include "include/wrapper/cef_stream_resource_handler.h"
#include "tests/cefclient/browser/main_context.h"
#include "tests/cefclient/browser/test_runner.h"
#include "tests/cefclient/browser/window_test_runner.h"
#include "tests/cefclient/browser/window_test_runner_views.h"
#if defined(OS_WIN)
#include "tests/cefclient/browser/window_test_runner_win.h"
#elif defined(OS_LINUX)
#include "tests/cefclient/browser/window_test_runner_gtk.h"
#elif defined(OS_MAC)
#include "tests/cefclient/browser/window_test_runner_mac.h"
#endif
namespace client::window_test {
namespace {
const char kTestUrlPath[] = "/window";
const char kMessagePositionName[] = "WindowTest.Position";
const char kMessageMinimizeName[] = "WindowTest.Minimize";
const char kMessageMaximizeName[] = "WindowTest.Maximize";
const char kMessageRestoreName[] = "WindowTest.Restore";
const char kMessageFullscreenName[] = "WindowTest.Fullscreen";
const char kMessageTitlebarHeightName[] = "WindowTest.TitlebarHeight";
// Create the appropriate platform test runner object.
std::unique_ptr<WindowTestRunner> CreateWindowTestRunner(
CefRefPtr<CefBrowser> browser) {
if (CefBrowserView::GetForBrowser(browser)) {
// Browser is Views-hosted.
return std::make_unique<WindowTestRunnerViews>();
}
#if defined(OS_WIN)
return std::make_unique<WindowTestRunnerWin>();
#elif defined(OS_LINUX)
return std::make_unique<WindowTestRunnerGtk>();
#elif defined(OS_MAC)
return std::make_unique<WindowTestRunnerMac>();
#else
#error "No implementation available for your platform."
#endif
}
// Parse the comma-delimited list of integer values.
std::vector<int> ParsePosition(const std::string& message_name) {
std::vector<int> vec;
const std::string& vals = message_name.substr(sizeof(kMessagePositionName));
std::stringstream ss(vals);
int i;
while (ss >> i) {
vec.push_back(i);
if (ss.peek() == ',') {
ss.ignore();
}
}
return vec;
}
std::optional<float> ParseHeight(const std::string& message) {
if (message.size() > sizeof(kMessageTitlebarHeightName)) {
const std::string& val = message.substr(sizeof(kMessageTitlebarHeightName));
return std::stof(val);
} else {
return std::nullopt;
}
}
// Handle messages in the browser process.
class Handler : public CefMessageRouterBrowserSide::Handler {
public:
Handler() = default;
// Called due to cefBroadcast execution in window.html.
bool OnQuery(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
int64_t query_id,
const CefString& request,
bool persistent,
CefRefPtr<Callback> callback) override {
// Only handle messages from the test URL.
const std::string& url = frame->GetURL();
if (!test_runner::IsTestURL(url, kTestUrlPath)) {
return false;
}
auto runner = CreateWindowTestRunner(browser);
const std::string& message_name = request;
if (message_name.find(kMessagePositionName) == 0) {
const auto vec = ParsePosition(message_name);
if (vec.size() == 4) {
runner->SetPos(browser, vec[0], vec[1], vec[2], vec[3]);
}
} else if (message_name == kMessageMinimizeName) {
runner->Minimize(browser);
} else if (message_name == kMessageMaximizeName) {
runner->Maximize(browser);
} else if (message_name == kMessageRestoreName) {
runner->Restore(browser);
} else if (message_name == kMessageFullscreenName) {
runner->Fullscreen(browser);
} else if (message_name.find(kMessageTitlebarHeightName) == 0) {
const auto height = ParseHeight(message_name);
runner->SetTitleBarHeight(browser, height);
} else {
NOTREACHED();
}
callback->Success("");
return true;
}
};
} // namespace
void CreateMessageHandlers(test_runner::MessageHandlerSet& handlers) {
handlers.insert(new Handler());
}
} // namespace client::window_test