cef/tests/unittests/views/test_window_delegate.cc
Marshall Greenblatt 06e73fff15 Implement Views framework on Windows and Linux (issue #1749).
- Add Views header files in a new include/views directory.
- Add initial top-level window (CefWindow), control (CefBrowserView,
  CefLabelButton, CefMenuButton, CefPanel, CefScrollView,
  CefTextfield) and layout (CefBoxLayout, CefFlowLayout) support.
  See libcef/browser/views/view_impl.h comments for implementation
  details.
- Add Views example usage in cefclient and cefsimple and Views unit
  tests in cef_unittests. Pass the `--use-views` command-line flag to
  cefclient, cefsimple and cef_unittests to run using the Views
  framework instead of platform APIs. For cefclient and cefsimple
  this will create the browser window and all related functionality
  using the Views framework. For cef_unittests this will run all
  tests (except OSR tests) in a Views-based browser window. Views-
  specific unit tests (`--gtest_filter=Views*`) will be run even if
  the the `--use-views` flag is not specified.
- Pass the `--hide-frame` command-line flag to cefclient to demo a
  frameless Views-based browser window.
- Pass the `--hide-controls` command-line flag to cefclient to demo a
  browser window without top controls. This also works in non-Views
  mode.
- Pass the `--enable-high-dpi-support` command-line flag to
  cef_unittests on Windows to test high-DPI support on a display
  that supports it.
- Add CefImage for reading/writing image file formats.
- Add CefBrowser::DownloadImage() for downloading image URLs as a
  CefImage representation. This is primarily for loading favicons.
- Add CefMenuModel::CreateMenuModel() and CefMenuModelDelegate for
  creating custom menus. This is primarily for use with
  CefMenuButton.
- Add CefBrowser::TryCloseBrowser() helper for closing a browser.
  Also improve related documentation in cef_life_span_handler.h.
- Rename cef_page_range_t to cef_range_t. It is now also used by
  CefTextfield.
- Remove CefLifeSpanHandler::RunModal() which is never called.
- Add draggable regions example to cefclient.
2016-04-26 11:58:13 -04:00

154 lines
4.8 KiB
C++

// 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.
#include "tests/unittests/views/test_window_delegate.h"
#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"
#include "tests/unittests/thread_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
// Test timeout in MS.
const int kTestTimeout = 5000;
} // namespace
// static
const int TestWindowDelegate::kWSize = 400;
// static
void TestWindowDelegate::RunTest(base::WaitableEvent* event,
const WindowTest& window_test,
bool frameless,
bool close_window,
int window_size) {
CefWindow::CreateTopLevelWindow(
new TestWindowDelegate(event, window_test, frameless, close_window,
window_size));
}
void TestWindowDelegate::OnWindowCreated(CefRefPtr<CefWindow> window) {
EXPECT_FALSE(window_);
window_ = window;
EXPECT_TRUE(window->IsValid());
EXPECT_FALSE(window->IsClosed());
EXPECT_FALSE(window->IsVisible());
EXPECT_FALSE(window->IsDrawn());
EXPECT_FALSE(window->IsActive());
EXPECT_FALSE(window->IsAlwaysOnTop());
EXPECT_FALSE(window->IsMaximized());
EXPECT_FALSE(window->IsMinimized());
EXPECT_FALSE(window->IsFullscreen());
const char* title = "ViewsTest";
window->SetTitle(title);
EXPECT_STREQ(title, window->GetTitle().ToString().c_str());
EXPECT_FALSE(window->GetWindowIcon().get());
EXPECT_FALSE(window->GetWindowAppIcon().get());
EXPECT_TRUE(window->GetDisplay().get());
// Size will come from GetPreferredSize() on initial Window creation.
EXPECT_TRUE(got_get_preferred_size_);
CefRect client_bounds = window->GetClientAreaBoundsInScreen();
if (frameless_) {
EXPECT_EQ(window_size_, client_bounds.width);
EXPECT_EQ(window_size_, client_bounds.height);
} 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 http://crbug.com/602692.
EXPECT_TRUE(abs(client_bounds.width - window_size_) <= 1);
EXPECT_TRUE(abs(client_bounds.height - window_size_) <= 1);
}
// Run the requested test.
if (!window_test_.is_null())
window_test_.Run(window);
if (close_window_) {
// Close the window asynchronously.
CefPostTask(TID_UI,
base::Bind(&TestWindowDelegate::OnCloseWindow, this));
} else if (!CefCommandLine::GetGlobalCommandLine()->HasSwitch(
"disable-test-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::Bind(&TestWindowDelegate::OnTimeoutWindow,
weak_ptr_factory_.GetWeakPtr()), kTestTimeout);
}
}
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());
window_ = nullptr;
// Don't execute the timeout callback.
weak_ptr_factory_.InvalidateWeakPtrs();
}
bool TestWindowDelegate::IsFrameless(CefRefPtr<CefWindow> window) {
return frameless_;
}
CefSize TestWindowDelegate::GetPreferredSize(CefRefPtr<CefView> view) {
got_get_preferred_size_ = true;
return CefSize(window_size_, window_size_);
}
TestWindowDelegate::TestWindowDelegate(base::WaitableEvent* event,
const WindowTest& window_test,
bool frameless,
bool close_window,
int window_size)
: event_(event),
window_test_(window_test),
frameless_(frameless),
close_window_(close_window),
window_size_(window_size),
weak_ptr_factory_(this) {
}
TestWindowDelegate::~TestWindowDelegate() {
// Complete the test (signal the event) asynchronously so objects on the call
// stack have a chance to unwind.
CefPostTask(TID_UI, base::Bind(SignalEvent, event_));
}
void TestWindowDelegate::OnCloseWindow() {
if (!window_)
return;
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();
}