Commit f6f6a0bb authored by tapted@chromium.org's avatar tapted@chromium.org

MacViews: Encapsulate some Aura specifics in widget_unittest.cc

This adds a few helper functions to WidgetTest for getting information
from a native widget and makes widget_test.h responsible for defining
typedefs for PlatformNativeWidget and (when not ChromeOS)
PlatformDesktopNativeWidget.

4 aura-specific tests (that are #ifdefd out on ChromeOS) that use
DesktopAuraTopLevelWindowTest are moved from widget/widget_unittest.cc
to widget/desktop_aura/desktop_native_widget_aura_unittest.cc

widget_unittests.cc doesn't yet compile on Mac with this alone due to
the event generator (addressed in CL/322893005).

BUG=378134
TEST=views_unittests

Review URL: https://codereview.chromium.org/334573008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@278400 0039d316-1c4b-4281-b951-d872f2087c98
parent 14b629f0
...@@ -14,8 +14,9 @@ namespace test { ...@@ -14,8 +14,9 @@ namespace test {
// mock it. // mock it.
NativeWidgetCapture::NativeWidgetCapture( NativeWidgetCapture::NativeWidgetCapture(
internal::NativeWidgetDelegate* delegate) internal::NativeWidgetDelegate* delegate)
: NativeWidgetAura(delegate), : PlatformNativeWidget(delegate),
mouse_capture_(false) {} mouse_capture_(false) {}
NativeWidgetCapture::~NativeWidgetCapture() {} NativeWidgetCapture::~NativeWidgetCapture() {}
void NativeWidgetCapture::SetCapture() { void NativeWidgetCapture::SetCapture() {
......
...@@ -7,13 +7,35 @@ ...@@ -7,13 +7,35 @@
#include "ui/gfx/native_widget_types.h" #include "ui/gfx/native_widget_types.h"
#include "ui/views/test/views_test_base.h" #include "ui/views/test/views_test_base.h"
#if defined(USE_AURA)
#include "ui/views/widget/native_widget_aura.h" #include "ui/views/widget/native_widget_aura.h"
#if !defined(OS_CHROMEOS)
#include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
#endif
#elif defined(OS_MACOSX)
#include "ui/views/widget/native_widget_mac.h"
#endif
namespace ui {
class EventProcessor;
}
namespace views { namespace views {
class NativeWidget; class NativeWidget;
class Widget; class Widget;
#if defined(USE_AURA)
typedef NativeWidgetAura PlatformNativeWidget;
#if !defined(OS_CHROMEOS)
typedef DesktopNativeWidgetAura PlatformDesktopNativeWidget;
#endif
#elif defined(OS_MACOSX)
typedef NativeWidgetMac PlatformNativeWidget;
typedef NativeWidgetMac PlatformDesktopNativeWidget;
#endif
namespace internal { namespace internal {
class RootView; class RootView;
...@@ -24,7 +46,7 @@ namespace test { ...@@ -24,7 +46,7 @@ namespace test {
// A widget that assumes mouse capture always works. It won't on Aura in // A widget that assumes mouse capture always works. It won't on Aura in
// testing, so we mock it. // testing, so we mock it.
class NativeWidgetCapture : public NativeWidgetAura { class NativeWidgetCapture : public PlatformNativeWidget {
public: public:
explicit NativeWidgetCapture(internal::NativeWidgetDelegate* delegate); explicit NativeWidgetCapture(internal::NativeWidgetDelegate* delegate);
virtual ~NativeWidgetCapture(); virtual ~NativeWidgetCapture();
...@@ -65,6 +87,17 @@ class WidgetTest : public ViewsTestBase { ...@@ -65,6 +87,17 @@ class WidgetTest : public ViewsTestBase {
View* GetGestureHandler(internal::RootView* root_view); View* GetGestureHandler(internal::RootView* root_view);
// Simulate a OS-level destruction of the native widget held by |widget|.
static void SimulateNativeDestroy(Widget* widget);
// Return true if |window| is visible according to the native platform.
static bool IsNativeWindowVisible(gfx::NativeWindow window);
// Return the event processor for |widget|. On aura platforms, this is an
// aura::WindowEventDispatcher. Otherwise, it is a bridge to the OS event
// processor.
static ui::EventProcessor* GetEventProcessor(Widget* widget);
private: private:
DISALLOW_COPY_AND_ASSIGN(WidgetTest); DISALLOW_COPY_AND_ASSIGN(WidgetTest);
}; };
......
// Copyright 2014 The Chromium 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 "ui/views/test/widget_test.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/views/widget/widget.h"
namespace views {
namespace test {
// static
void WidgetTest::SimulateNativeDestroy(Widget* widget) {
delete widget->GetNativeView();
}
// static
bool WidgetTest::IsNativeWindowVisible(gfx::NativeWindow window) {
return window->IsVisible();
}
// static
ui::EventProcessor* WidgetTest::GetEventProcessor(Widget* widget) {
return widget->GetNativeWindow()->GetHost()->event_processor();
}
} // namespace test
} // namespace views
// Copyright 2014 The Chromium 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 "ui/views/test/widget_test.h"
#include <Cocoa/Cocoa.h>
#include "ui/views/widget/root_view.h"
namespace views {
namespace test {
// static
void WidgetTest::SimulateNativeDestroy(Widget* widget) {
DCHECK([widget->GetNativeWindow() isReleasedWhenClosed]);
[widget->GetNativeWindow() close];
}
// static
bool WidgetTest::IsNativeWindowVisible(gfx::NativeWindow window) {
return [window isVisible];
}
// static
ui::EventProcessor* WidgetTest::GetEventProcessor(Widget* widget) {
return static_cast<internal::RootView*>(widget->GetRootView());
}
} // namespace test
} // namespace views
...@@ -627,6 +627,8 @@ ...@@ -627,6 +627,8 @@
'test/views_test_helper_aura.h', 'test/views_test_helper_aura.h',
'test/widget_test.cc', 'test/widget_test.cc',
'test/widget_test.h', 'test/widget_test.h',
'test/widget_test_aura.cc',
'test/widget_test_mac.mm',
'test/x11_property_change_waiter.cc', 'test/x11_property_change_waiter.cc',
'test/x11_property_change_waiter.h', 'test/x11_property_change_waiter.h',
], ],
......
...@@ -5,15 +5,20 @@ ...@@ -5,15 +5,20 @@
#include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h" #include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
#include "base/bind.h" #include "base/bind.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/client/cursor_client.h" #include "ui/aura/client/cursor_client.h"
#include "ui/aura/client/window_tree_client.h"
#include "ui/aura/test/event_generator.h"
#include "ui/aura/test/test_window_delegate.h" #include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/window.h" #include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h" #include "ui/aura/window_tree_host.h"
#include "ui/views/test/views_test_base.h" #include "ui/views/test/views_test_base.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/widget/widget.h" #include "ui/views/widget/widget.h"
#include "ui/wm/public/dispatcher_client.h" #include "ui/wm/public/dispatcher_client.h"
namespace views { namespace views {
namespace test {
typedef ViewsTestBase DesktopNativeWidgetAuraTest; typedef ViewsTestBase DesktopNativeWidgetAuraTest;
...@@ -229,4 +234,166 @@ TEST_F(DesktopNativeWidgetAuraTest, WidgetCanBeDestroyedFromNestedLoop) { ...@@ -229,4 +234,166 @@ TEST_F(DesktopNativeWidgetAuraTest, WidgetCanBeDestroyedFromNestedLoop) {
run_loop.Run(); run_loop.Run();
} }
// This class provides functionality to create fullscreen and top level popup
// windows. It additionally tests whether the destruction of these windows
// occurs correctly in desktop AURA without crashing.
// It provides facilities to test the following cases:-
// 1. Child window destroyed which should lead to the destruction of the
// parent.
// 2. Parent window destroyed which should lead to the child being destroyed.
class DesktopAuraTopLevelWindowTest
: public views::TestViewsDelegate,
public aura::WindowObserver {
public:
DesktopAuraTopLevelWindowTest()
: top_level_widget_(NULL),
owned_window_(NULL),
owner_destroyed_(false),
owned_window_destroyed_(false) {}
virtual ~DesktopAuraTopLevelWindowTest() {
EXPECT_TRUE(owner_destroyed_);
EXPECT_TRUE(owned_window_destroyed_);
top_level_widget_ = NULL;
owned_window_ = NULL;
}
// views::TestViewsDelegate overrides.
virtual void OnBeforeWidgetInit(
Widget::InitParams* params,
internal::NativeWidgetDelegate* delegate) OVERRIDE {
if (!params->native_widget)
params->native_widget = new views::DesktopNativeWidgetAura(delegate);
}
void CreateTopLevelWindow(const gfx::Rect& bounds, bool fullscreen) {
Widget::InitParams init_params;
init_params.type = Widget::InitParams::TYPE_WINDOW;
init_params.bounds = bounds;
init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
init_params.layer_type = aura::WINDOW_LAYER_NOT_DRAWN;
init_params.accept_events = fullscreen;
widget_.Init(init_params);
owned_window_ = new aura::Window(&child_window_delegate_);
owned_window_->SetType(ui::wm::WINDOW_TYPE_NORMAL);
owned_window_->SetName("TestTopLevelWindow");
if (fullscreen) {
owned_window_->SetProperty(aura::client::kShowStateKey,
ui::SHOW_STATE_FULLSCREEN);
} else {
owned_window_->SetType(ui::wm::WINDOW_TYPE_MENU);
}
owned_window_->Init(aura::WINDOW_LAYER_TEXTURED);
aura::client::ParentWindowWithContext(
owned_window_,
widget_.GetNativeView()->GetRootWindow(),
gfx::Rect(0, 0, 1900, 1600));
owned_window_->Show();
owned_window_->AddObserver(this);
ASSERT_TRUE(owned_window_->parent() != NULL);
owned_window_->parent()->AddObserver(this);
top_level_widget_ =
views::Widget::GetWidgetForNativeView(owned_window_->parent());
ASSERT_TRUE(top_level_widget_ != NULL);
}
void DestroyOwnedWindow() {
ASSERT_TRUE(owned_window_ != NULL);
delete owned_window_;
}
void DestroyOwnerWindow() {
ASSERT_TRUE(top_level_widget_ != NULL);
top_level_widget_->CloseNow();
}
virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {
window->RemoveObserver(this);
if (window == owned_window_) {
owned_window_destroyed_ = true;
} else if (window == top_level_widget_->GetNativeView()) {
owner_destroyed_ = true;
} else {
ADD_FAILURE() << "Unexpected window destroyed callback: " << window;
}
}
aura::Window* owned_window() {
return owned_window_;
}
views::Widget* top_level_widget() {
return top_level_widget_;
}
private:
views::Widget widget_;
views::Widget* top_level_widget_;
aura::Window* owned_window_;
bool owner_destroyed_;
bool owned_window_destroyed_;
aura::test::TestWindowDelegate child_window_delegate_;
DISALLOW_COPY_AND_ASSIGN(DesktopAuraTopLevelWindowTest);
};
typedef WidgetTest DesktopAuraWidgetTest;
TEST_F(DesktopAuraWidgetTest, FullscreenWindowDestroyedBeforeOwnerTest) {
ViewsDelegate::views_delegate = NULL;
DesktopAuraTopLevelWindowTest fullscreen_window;
ASSERT_NO_FATAL_FAILURE(fullscreen_window.CreateTopLevelWindow(
gfx::Rect(0, 0, 200, 200), true));
RunPendingMessages();
ASSERT_NO_FATAL_FAILURE(fullscreen_window.DestroyOwnedWindow());
RunPendingMessages();
}
TEST_F(DesktopAuraWidgetTest, FullscreenWindowOwnerDestroyed) {
ViewsDelegate::views_delegate = NULL;
DesktopAuraTopLevelWindowTest fullscreen_window;
ASSERT_NO_FATAL_FAILURE(fullscreen_window.CreateTopLevelWindow(
gfx::Rect(0, 0, 200, 200), true));
RunPendingMessages();
ASSERT_NO_FATAL_FAILURE(fullscreen_window.DestroyOwnerWindow());
RunPendingMessages();
}
TEST_F(DesktopAuraWidgetTest, TopLevelOwnedPopupTest) {
ViewsDelegate::views_delegate = NULL;
DesktopAuraTopLevelWindowTest popup_window;
ASSERT_NO_FATAL_FAILURE(popup_window.CreateTopLevelWindow(
gfx::Rect(0, 0, 200, 200), false));
RunPendingMessages();
ASSERT_NO_FATAL_FAILURE(popup_window.DestroyOwnedWindow());
RunPendingMessages();
}
// This test validates that when a top level owned popup Aura window is
// resized, the widget is resized as well.
TEST_F(DesktopAuraWidgetTest, TopLevelOwnedPopupResizeTest) {
ViewsDelegate::views_delegate = NULL;
DesktopAuraTopLevelWindowTest popup_window;
ASSERT_NO_FATAL_FAILURE(popup_window.CreateTopLevelWindow(
gfx::Rect(0, 0, 200, 200), false));
gfx::Rect new_size(0, 0, 400, 400);
popup_window.owned_window()->SetBounds(new_size);
EXPECT_EQ(popup_window.top_level_widget()->GetNativeView()->bounds().size(),
new_size.size());
RunPendingMessages();
ASSERT_NO_FATAL_FAILURE(popup_window.DestroyOwnedWindow());
RunPendingMessages();
}
} // namespace test
} // namespace views } // namespace views
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment