| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "ui/gfx/native_widget_types.h" | 10 #include "ui/gfx/native_widget_types.h" |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE { | 130 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE { |
| 131 if (event->type() == ui::ET_GESTURE_BEGIN) { | 131 if (event->type() == ui::ET_GESTURE_BEGIN) { |
| 132 GetWidget()->SetCapture(this); | 132 GetWidget()->SetCapture(this); |
| 133 event->StopPropagation(); | 133 event->StopPropagation(); |
| 134 } | 134 } |
| 135 } | 135 } |
| 136 | 136 |
| 137 DISALLOW_COPY_AND_ASSIGN(GestureCaptureView); | 137 DISALLOW_COPY_AND_ASSIGN(GestureCaptureView); |
| 138 }; | 138 }; |
| 139 | 139 |
| 140 typedef ViewsTestBase WidgetTest; | 140 class WidgetTest : public ViewsTestBase { |
| 141 public: |
| 142 WidgetTest() {} |
| 143 virtual ~WidgetTest() {} |
| 141 | 144 |
| 142 NativeWidget* CreatePlatformNativeWidget( | 145 NativeWidget* CreatePlatformNativeWidget( |
| 143 internal::NativeWidgetDelegate* delegate) { | 146 internal::NativeWidgetDelegate* delegate) { |
| 144 return new NativeWidgetPlatformForTest(delegate); | 147 return new NativeWidgetPlatformForTest(delegate); |
| 145 } | 148 } |
| 146 | 149 |
| 147 Widget* CreateTopLevelPlatformWidget() { | 150 Widget* CreateTopLevelPlatformWidget() { |
| 148 Widget* toplevel = new Widget; | 151 Widget* toplevel = new Widget; |
| 149 Widget::InitParams toplevel_params(Widget::InitParams::TYPE_WINDOW); | 152 Widget::InitParams toplevel_params = |
| 150 toplevel_params.native_widget = CreatePlatformNativeWidget(toplevel); | 153 CreateParams(Widget::InitParams::TYPE_WINDOW); |
| 151 toplevel->Init(toplevel_params); | 154 toplevel_params.native_widget = CreatePlatformNativeWidget(toplevel); |
| 152 return toplevel; | 155 toplevel->Init(toplevel_params); |
| 153 } | 156 return toplevel; |
| 157 } |
| 154 | 158 |
| 155 Widget* CreateChildPlatformWidget(gfx::NativeView parent_native_view) { | 159 Widget* CreateChildPlatformWidget(gfx::NativeView parent_native_view) { |
| 156 Widget* child = new Widget; | 160 Widget* child = new Widget; |
| 157 Widget::InitParams child_params(Widget::InitParams::TYPE_CONTROL); | 161 Widget::InitParams child_params = |
| 158 child_params.native_widget = CreatePlatformNativeWidget(child); | 162 CreateParams(Widget::InitParams::TYPE_CONTROL); |
| 159 child_params.parent = parent_native_view; | 163 child_params.native_widget = CreatePlatformNativeWidget(child); |
| 160 child->Init(child_params); | 164 child_params.parent = parent_native_view; |
| 161 child->SetContentsView(new View); | 165 child->Init(child_params); |
| 162 return child; | 166 child->SetContentsView(new View); |
| 163 } | 167 return child; |
| 168 } |
| 164 | 169 |
| 165 #if defined(OS_WIN) && !defined(USE_AURA) | 170 #if defined(OS_WIN) && !defined(USE_AURA) |
| 166 // On Windows, it is possible for us to have a child window that is TYPE_POPUP. | 171 // On Windows, it is possible for us to have a child window that is |
| 167 Widget* CreateChildPopupPlatformWidget(gfx::NativeView parent_native_view) { | 172 // TYPE_POPUP. |
| 168 Widget* child = new Widget; | 173 Widget* CreateChildPopupPlatformWidget(gfx::NativeView parent_native_view) { |
| 169 Widget::InitParams child_params(Widget::InitParams::TYPE_POPUP); | 174 Widget* child = new Widget; |
| 170 child_params.child = true; | 175 Widget::InitParams child_params = |
| 171 child_params.native_widget = CreatePlatformNativeWidget(child); | 176 CreateParams(Widget::InitParams::TYPE_POPUP); |
| 172 child_params.parent = parent_native_view; | 177 child_params.child = true; |
| 173 child->Init(child_params); | 178 child_params.native_widget = CreatePlatformNativeWidget(child); |
| 174 child->SetContentsView(new View); | 179 child_params.parent = parent_native_view; |
| 175 return child; | 180 child->Init(child_params); |
| 176 } | 181 child->SetContentsView(new View); |
| 182 return child; |
| 183 } |
| 177 #endif | 184 #endif |
| 178 | 185 |
| 179 Widget* CreateTopLevelNativeWidget() { | 186 Widget* CreateTopLevelNativeWidget() { |
| 180 Widget* toplevel = new Widget; | 187 Widget* toplevel = new Widget; |
| 181 Widget::InitParams params(Widget::InitParams::TYPE_WINDOW); | 188 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW); |
| 182 toplevel->Init(params); | 189 toplevel->Init(params); |
| 183 toplevel->SetContentsView(new View); | 190 toplevel->SetContentsView(new View); |
| 184 return toplevel; | 191 return toplevel; |
| 185 } | 192 } |
| 186 | 193 |
| 187 Widget* CreateChildNativeWidgetWithParent(Widget* parent) { | 194 Widget* CreateChildNativeWidgetWithParent(Widget* parent) { |
| 188 Widget* child = new Widget; | 195 Widget* child = new Widget; |
| 189 Widget::InitParams params(Widget::InitParams::TYPE_CONTROL); | 196 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_CONTROL); |
| 190 params.parent_widget = parent; | 197 params.parent_widget = parent; |
| 191 child->Init(params); | 198 child->Init(params); |
| 192 child->SetContentsView(new View); | 199 child->SetContentsView(new View); |
| 193 return child; | 200 return child; |
| 194 } | 201 } |
| 195 | 202 |
| 196 Widget* CreateChildNativeWidget() { | 203 Widget* CreateChildNativeWidget() { |
| 197 return CreateChildNativeWidgetWithParent(NULL); | 204 return CreateChildNativeWidgetWithParent(NULL); |
| 198 } | 205 } |
| 206 }; |
| 199 | 207 |
| 200 bool WidgetHasMouseCapture(const Widget* widget) { | 208 bool WidgetHasMouseCapture(const Widget* widget) { |
| 201 return static_cast<const internal::NativeWidgetPrivate*>(widget-> | 209 return static_cast<const internal::NativeWidgetPrivate*>(widget-> |
| 202 native_widget())->HasCapture(); | 210 native_widget())->HasCapture(); |
| 203 } | 211 } |
| 204 | 212 |
| 205 ui::WindowShowState GetWidgetShowState(const Widget* widget) { | 213 ui::WindowShowState GetWidgetShowState(const Widget* widget) { |
| 206 // Use IsMaximized/IsMinimized/IsFullScreen instead of GetWindowPlacement | 214 // Use IsMaximized/IsMinimized/IsFullScreen instead of GetWindowPlacement |
| 207 // because the former is implemented on all platforms but the latter is not. | 215 // because the former is implemented on all platforms but the latter is not. |
| 208 return widget->IsFullscreen() ? ui::SHOW_STATE_FULLSCREEN : | 216 return widget->IsFullscreen() ? ui::SHOW_STATE_FULLSCREEN : |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 517 | 525 |
| 518 DISALLOW_COPY_AND_ASSIGN(OwnershipTestWidget); | 526 DISALLOW_COPY_AND_ASSIGN(OwnershipTestWidget); |
| 519 }; | 527 }; |
| 520 | 528 |
| 521 // Widget owns its NativeWidget, part 1: NativeWidget is a platform-native | 529 // Widget owns its NativeWidget, part 1: NativeWidget is a platform-native |
| 522 // widget. | 530 // widget. |
| 523 TEST_F(WidgetOwnershipTest, Ownership_WidgetOwnsPlatformNativeWidget) { | 531 TEST_F(WidgetOwnershipTest, Ownership_WidgetOwnsPlatformNativeWidget) { |
| 524 OwnershipTestState state; | 532 OwnershipTestState state; |
| 525 | 533 |
| 526 scoped_ptr<Widget> widget(new OwnershipTestWidget(&state)); | 534 scoped_ptr<Widget> widget(new OwnershipTestWidget(&state)); |
| 527 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | 535 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 528 params.native_widget = | 536 params.native_widget = |
| 529 new OwnershipTestNativeWidgetPlatform(widget.get(), &state); | 537 new OwnershipTestNativeWidgetPlatform(widget.get(), &state); |
| 530 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
| 531 widget->Init(params); | 538 widget->Init(params); |
| 532 | 539 |
| 533 // Now delete the Widget, which should delete the NativeWidget. | 540 // Now delete the Widget, which should delete the NativeWidget. |
| 534 widget.reset(); | 541 widget.reset(); |
| 535 | 542 |
| 536 EXPECT_TRUE(state.widget_deleted); | 543 EXPECT_TRUE(state.widget_deleted); |
| 537 EXPECT_TRUE(state.native_widget_deleted); | 544 EXPECT_TRUE(state.native_widget_deleted); |
| 538 | 545 |
| 539 // TODO(beng): write test for this ownership scenario and the NativeWidget | 546 // TODO(beng): write test for this ownership scenario and the NativeWidget |
| 540 // being deleted out from under the Widget. | 547 // being deleted out from under the Widget. |
| 541 } | 548 } |
| 542 | 549 |
| 543 // Widget owns its NativeWidget, part 2: NativeWidget is a NativeWidget. | 550 // Widget owns its NativeWidget, part 2: NativeWidget is a NativeWidget. |
| 544 TEST_F(WidgetOwnershipTest, Ownership_WidgetOwnsViewsNativeWidget) { | 551 TEST_F(WidgetOwnershipTest, Ownership_WidgetOwnsViewsNativeWidget) { |
| 545 OwnershipTestState state; | 552 OwnershipTestState state; |
| 546 | 553 |
| 547 scoped_ptr<Widget> widget(new OwnershipTestWidget(&state)); | 554 scoped_ptr<Widget> widget(new OwnershipTestWidget(&state)); |
| 548 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | 555 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 549 params.native_widget = | 556 params.native_widget = |
| 550 new OwnershipTestNativeWidgetPlatform(widget.get(), &state); | 557 new OwnershipTestNativeWidgetPlatform(widget.get(), &state); |
| 551 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
| 552 widget->Init(params); | 558 widget->Init(params); |
| 553 | 559 |
| 554 // Now delete the Widget, which should delete the NativeWidget. | 560 // Now delete the Widget, which should delete the NativeWidget. |
| 555 widget.reset(); | 561 widget.reset(); |
| 556 | 562 |
| 557 EXPECT_TRUE(state.widget_deleted); | 563 EXPECT_TRUE(state.widget_deleted); |
| 558 EXPECT_TRUE(state.native_widget_deleted); | 564 EXPECT_TRUE(state.native_widget_deleted); |
| 559 | 565 |
| 560 // TODO(beng): write test for this ownership scenario and the NativeWidget | 566 // TODO(beng): write test for this ownership scenario and the NativeWidget |
| 561 // being deleted out from under the Widget. | 567 // being deleted out from under the Widget. |
| 562 } | 568 } |
| 563 | 569 |
| 564 // Widget owns its NativeWidget, part 3: NativeWidget is a NativeWidget, | 570 // Widget owns its NativeWidget, part 3: NativeWidget is a NativeWidget, |
| 565 // destroy the parent view. | 571 // destroy the parent view. |
| 566 TEST_F(WidgetOwnershipTest, | 572 TEST_F(WidgetOwnershipTest, |
| 567 Ownership_WidgetOwnsViewsNativeWidget_DestroyParentView) { | 573 Ownership_WidgetOwnsViewsNativeWidget_DestroyParentView) { |
| 568 OwnershipTestState state; | 574 OwnershipTestState state; |
| 569 | 575 |
| 570 Widget* toplevel = CreateTopLevelPlatformWidget(); | 576 Widget* toplevel = CreateTopLevelPlatformWidget(); |
| 571 | 577 |
| 572 scoped_ptr<Widget> widget(new OwnershipTestWidget(&state)); | 578 scoped_ptr<Widget> widget(new OwnershipTestWidget(&state)); |
| 573 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); | 579 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 574 params.native_widget = | 580 params.native_widget = |
| 575 new OwnershipTestNativeWidgetPlatform(widget.get(), &state); | 581 new OwnershipTestNativeWidgetPlatform(widget.get(), &state); |
| 576 params.parent_widget = toplevel; | 582 params.parent_widget = toplevel; |
| 577 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | |
| 578 widget->Init(params); | 583 widget->Init(params); |
| 579 | 584 |
| 580 // Now close the toplevel, which deletes the view hierarchy. | 585 // Now close the toplevel, which deletes the view hierarchy. |
| 581 toplevel->CloseNow(); | 586 toplevel->CloseNow(); |
| 582 | 587 |
| 583 RunPendingMessages(); | 588 RunPendingMessages(); |
| 584 | 589 |
| 585 // This shouldn't delete the widget because it shouldn't be deleted | 590 // This shouldn't delete the widget because it shouldn't be deleted |
| 586 // from the native side. | 591 // from the native side. |
| 587 EXPECT_FALSE(state.widget_deleted); | 592 EXPECT_FALSE(state.widget_deleted); |
| (...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1022 EXPECT_TRUE(toplevel->OnKeyEvent(backspace_p)); | 1027 EXPECT_TRUE(toplevel->OnKeyEvent(backspace_p)); |
| 1023 ui::KeyEvent backspace_r(ui::ET_KEY_RELEASED, ui::VKEY_DELETE, 0, false); | 1028 ui::KeyEvent backspace_r(ui::ET_KEY_RELEASED, ui::VKEY_DELETE, 0, false); |
| 1024 EXPECT_FALSE(toplevel->OnKeyEvent(backspace_r)); | 1029 EXPECT_FALSE(toplevel->OnKeyEvent(backspace_r)); |
| 1025 | 1030 |
| 1026 toplevel->Close(); | 1031 toplevel->Close(); |
| 1027 } | 1032 } |
| 1028 #endif // defined(USE_AURA) | 1033 #endif // defined(USE_AURA) |
| 1029 | 1034 |
| 1030 } // namespace | 1035 } // namespace |
| 1031 } // namespace views | 1036 } // namespace views |
| OLD | NEW |