cef/tests/unittests/views/scroll_view_unittest.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

156 lines
5.0 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 "include/cef_pack_strings.h"
#include "include/views/cef_panel.h"
#include "include/views/cef_panel_delegate.h"
#include "include/views/cef_scroll_view.h"
#include "include/wrapper/cef_closure_task.h"
#include "tests/unittests/thread_helper.h"
#include "tests/unittests/views/test_window_delegate.h"
#include "base/bind.h"
#include "testing/gtest/include/gtest/gtest.h"
#define SCROLL_VIEW_TEST_ASYNC(name) \
UI_THREAD_TEST_ASYNC(ViewsScrollViewTest, name)
namespace {
const int kScrollViewID = 1;
const int kContentPanelID = 2;
// Make the Panel larger then the Window so scroll bars appear.
const int kContentPanelSize = TestWindowDelegate::kWSize + 200;
class TestScrollViewDelegate : public CefViewDelegate {
public:
TestScrollViewDelegate() {
}
CefSize GetPreferredSize(CefRefPtr<CefView> view) override {
EXPECT_EQ(kScrollViewID, view->GetID());
got_get_preferred_size_ = true;
return CefSize(kContentPanelSize, kContentPanelSize);
}
bool got_get_preferred_size_ = false;
private:
IMPLEMENT_REFCOUNTING(TestScrollViewDelegate);
DISALLOW_COPY_AND_ASSIGN(TestScrollViewDelegate);
};
class TestPanelDelegate : public CefPanelDelegate {
public:
TestPanelDelegate() {
}
CefSize GetPreferredSize(CefRefPtr<CefView> view) override {
EXPECT_EQ(kContentPanelID, view->GetID());
got_get_preferred_size_ = true;
return CefSize(kContentPanelSize, kContentPanelSize);
}
bool got_get_preferred_size_ = false;
private:
IMPLEMENT_REFCOUNTING(TestPanelDelegate);
DISALLOW_COPY_AND_ASSIGN(TestPanelDelegate);
};
void RunScrollViewLayout(bool with_delegate,
CefRefPtr<CefWindow> window) {
CefRefPtr<TestScrollViewDelegate> scroll_view_delegate;
CefRefPtr<TestPanelDelegate> panel_delegate;
if (with_delegate) {
scroll_view_delegate = new TestScrollViewDelegate();
panel_delegate = new TestPanelDelegate();
}
CefRefPtr<CefScrollView> scroll_view =
CefScrollView::CreateScrollView(scroll_view_delegate);
EXPECT_TRUE(scroll_view.get());
EXPECT_TRUE(scroll_view->AsScrollView().get());
// Verify default state.
EXPECT_FALSE(scroll_view->GetContentView().get());
EXPECT_EQ(CefRect(0, 0, 0, 0), scroll_view->GetVisibleContentRect());
EXPECT_FALSE(scroll_view->HasHorizontalScrollbar());
EXPECT_FALSE(scroll_view->HasVerticalScrollbar());
scroll_view->SetID(kScrollViewID);
scroll_view->SetBackgroundColor(CefColorSetARGB(255, 0, 255, 0));
CefRefPtr<CefPanel> content_panel = CefPanel::CreatePanel(panel_delegate);
content_panel->SetID(kContentPanelID);
content_panel->SetBackgroundColor(CefColorSetARGB(255, 255, 0, 0));
if (!with_delegate) {
// Explicitly set the content panel size. Otherwise, it will come from the
// delegate.
content_panel->SetSize(CefSize(kContentPanelSize, kContentPanelSize));
}
scroll_view->SetContentView(content_panel);
EXPECT_TRUE(content_panel->IsSame(scroll_view->GetContentView()));
window->AddChildView(scroll_view);
// Force layout.
window->Layout();
EXPECT_TRUE(scroll_view->HasHorizontalScrollbar());
EXPECT_TRUE(scroll_view->HasVerticalScrollbar());
if (with_delegate) {
EXPECT_TRUE(scroll_view_delegate->got_get_preferred_size_);
EXPECT_TRUE(panel_delegate->got_get_preferred_size_);
}
window->Show();
// With default FillLayout the ScrollView should be the size of the Window's
// client area.
const CefRect& client_bounds = window->GetClientAreaBoundsInScreen();
const CefRect& scroll_view_bounds = scroll_view->GetBoundsInScreen();
EXPECT_EQ(client_bounds, scroll_view_bounds);
// Content panel size should be unchanged.
const CefSize& content_panel_size = content_panel->GetSize();
EXPECT_EQ(CefSize(kContentPanelSize, kContentPanelSize), content_panel_size);
const int sb_height = scroll_view->GetHorizontalScrollbarHeight();
EXPECT_GT(sb_height, 0);
const int sb_width = scroll_view->GetVerticalScrollbarWidth();
EXPECT_GT(sb_width, 0);
// Verify visible content panel region.
const CefRect& visible_rect = scroll_view->GetVisibleContentRect();
EXPECT_EQ(CefRect(0, 0, scroll_view_bounds.width - sb_width,
scroll_view_bounds.height - sb_height), visible_rect);
}
void ScrollViewLayout(base::WaitableEvent* event,
bool with_delegate) {
TestWindowDelegate::RunTest(event,
base::Bind(RunScrollViewLayout, with_delegate), false);
}
void ScrollViewLayoutWithDelegateImpl(base::WaitableEvent* event) {
ScrollViewLayout(event, true);
}
void ScrollViewLayoutNoDelegateImpl(base::WaitableEvent* event) {
ScrollViewLayout(event, false);
}
} // namespace
// Test ScrollView layout. This is primarily to exercise exposed CEF APIs and is
// not intended to comprehensively test ScrollView-related behavior (which we
// presume that Chromium is testing).
SCROLL_VIEW_TEST_ASYNC(ScrollViewLayoutWithDelegate);
SCROLL_VIEW_TEST_ASYNC(ScrollViewLayoutNoDelegate);