| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/string16.h" | 8 #include "base/string16.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 | 123 |
| 124 virtual void SetUp() { | 124 virtual void SetUp() { |
| 125 window_ = Window::CreateChromeWindow(NULL, bounds(), this); | 125 window_ = Window::CreateChromeWindow(NULL, bounds(), this); |
| 126 InitContentView(); | 126 InitContentView(); |
| 127 window_->Show(); | 127 window_->Show(); |
| 128 } | 128 } |
| 129 | 129 |
| 130 virtual void TearDown() { | 130 virtual void TearDown() { |
| 131 if (focus_change_listener_) | 131 if (focus_change_listener_) |
| 132 GetFocusManager()->RemoveFocusChangeListener(focus_change_listener_); | 132 GetFocusManager()->RemoveFocusChangeListener(focus_change_listener_); |
| 133 window_->CloseWindow(); | 133 window_->Close(); |
| 134 | 134 |
| 135 // Flush the message loop to make Purify happy. | 135 // Flush the message loop to make Purify happy. |
| 136 message_loop()->RunAllPending(); | 136 message_loop()->RunAllPending(); |
| 137 } | 137 } |
| 138 | 138 |
| 139 FocusManager* GetFocusManager() { | 139 FocusManager* GetFocusManager() { |
| 140 #if defined(OS_WIN) | 140 return window_->GetFocusManager(); |
| 141 return static_cast<WindowWin*>(window_)->GetFocusManager(); | |
| 142 #elif defined(OS_LINUX) | |
| 143 return static_cast<WindowGtk*>(window_)->GetFocusManager(); | |
| 144 #else | |
| 145 NOTIMPLEMENTED(); | |
| 146 #endif | |
| 147 } | 141 } |
| 148 | 142 |
| 149 void FocusNativeView(gfx::NativeView native_view) { | 143 void FocusNativeView(gfx::NativeView native_view) { |
| 150 #if defined(OS_WIN) | 144 #if defined(OS_WIN) |
| 151 ::SendMessage(native_view, WM_SETFOCUS, NULL, NULL); | 145 ::SendMessage(native_view, WM_SETFOCUS, NULL, NULL); |
| 152 #else | 146 #else |
| 153 gint return_val; | 147 gint return_val; |
| 154 GdkEventFocus event; | 148 GdkEventFocus event; |
| 155 event.type = GDK_FOCUS_CHANGE; | 149 event.type = GDK_FOCUS_CHANGE; |
| 156 event.window = | 150 event.window = |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 | 277 |
| 284 virtual FocusTraversable* GetFocusTraversable() { | 278 virtual FocusTraversable* GetFocusTraversable() { |
| 285 return widget_->GetRootView(); | 279 return widget_->GetRootView(); |
| 286 } | 280 } |
| 287 | 281 |
| 288 virtual void ViewHierarchyChanged(bool is_add, View *parent, View *child) { | 282 virtual void ViewHierarchyChanged(bool is_add, View *parent, View *child) { |
| 289 NativeViewHost::ViewHierarchyChanged(is_add, parent, child); | 283 NativeViewHost::ViewHierarchyChanged(is_add, parent, child); |
| 290 | 284 |
| 291 if (child == this && is_add) { | 285 if (child == this && is_add) { |
| 292 if (!widget_) { | 286 if (!widget_) { |
| 293 widget_ = Widget::CreateWidget(); | 287 widget_ = new Widget; |
| 294 Widget::InitParams params(Widget::InitParams::TYPE_CONTROL); | 288 Widget::InitParams params(Widget::InitParams::TYPE_CONTROL); |
| 295 #if defined(OS_WIN) | 289 #if defined(OS_WIN) |
| 296 params.parent = parent->GetRootView()->GetWidget()->GetNativeView(); | 290 params.parent = parent->GetRootView()->GetWidget()->GetNativeView(); |
| 297 #elif defined(TOOLKIT_USES_GTK) | 291 #elif defined(TOOLKIT_USES_GTK) |
| 298 params.parent = native_view(); | 292 params.parent = native_view(); |
| 299 #endif | 293 #endif |
| 300 widget_->Init(params); | 294 widget_->Init(params); |
| 301 widget_->SetFocusTraversableParentView(this); | 295 widget_->SetFocusTraversableParentView(this); |
| 302 widget_->SetContentsView(child_); | 296 widget_->SetContentsView(child_); |
| 303 } | 297 } |
| (...skipping 1291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1595 ATOM atom = RegisterClassEx(&class_ex); | 1589 ATOM atom = RegisterClassEx(&class_ex); |
| 1596 ASSERT_TRUE(atom); | 1590 ASSERT_TRUE(atom); |
| 1597 | 1591 |
| 1598 // Create a native dialog window. | 1592 // Create a native dialog window. |
| 1599 HWND hwnd = CreateWindowEx(0, class_ex.lpszClassName, NULL, | 1593 HWND hwnd = CreateWindowEx(0, class_ex.lpszClassName, NULL, |
| 1600 WS_OVERLAPPEDWINDOW, 0, 0, 200, 200, | 1594 WS_OVERLAPPEDWINDOW, 0, 0, 200, 200, |
| 1601 NULL, NULL, NULL, NULL); | 1595 NULL, NULL, NULL, NULL); |
| 1602 ASSERT_TRUE(hwnd); | 1596 ASSERT_TRUE(hwnd); |
| 1603 | 1597 |
| 1604 // Create a view window parented to native dialog. | 1598 // Create a view window parented to native dialog. |
| 1605 scoped_ptr<Widget> widget1(Widget::CreateWidget()); | 1599 scoped_ptr<Widget> widget1(new Widget); |
| 1606 Widget::InitParams params(Widget::InitParams::TYPE_CONTROL); | 1600 Widget::InitParams params(Widget::InitParams::TYPE_CONTROL); |
| 1607 params.delete_on_destroy = false; | 1601 params.delete_on_destroy = false; |
| 1608 params.parent = hwnd; | 1602 params.parent = hwnd; |
| 1609 params.bounds = gfx::Rect(0, 0, 100, 100); | 1603 params.bounds = gfx::Rect(0, 0, 100, 100); |
| 1610 widget1->Init(params); | 1604 widget1->Init(params); |
| 1611 | 1605 |
| 1612 // Get the focus manager directly from the first window. Should exist | 1606 // Get the focus manager directly from the first window. Should exist |
| 1613 // because the first window is the root widget. | 1607 // because the first window is the root widget. |
| 1614 views::FocusManager* focus_manager_member1 = widget1->GetFocusManager(); | 1608 views::FocusManager* focus_manager_member1 = widget1->GetFocusManager(); |
| 1615 EXPECT_TRUE(focus_manager_member1); | 1609 EXPECT_TRUE(focus_manager_member1); |
| 1616 | 1610 |
| 1617 // Create another view window parented to the first view window. | 1611 // Create another view window parented to the first view window. |
| 1618 scoped_ptr<Widget> widget2(Widget::CreateWidget()); | 1612 scoped_ptr<Widget> widget2(new Widget); |
| 1619 params.parent = widget1->GetNativeView(); | 1613 params.parent = widget1->GetNativeView(); |
| 1620 widget2->Init(params); | 1614 widget2->Init(params); |
| 1621 | 1615 |
| 1622 // Get the focus manager directly from the second window. Should return the | 1616 // Get the focus manager directly from the second window. Should return the |
| 1623 // first window's focus manager. | 1617 // first window's focus manager. |
| 1624 views::FocusManager* focus_manager_member2 = widget2->GetFocusManager(); | 1618 views::FocusManager* focus_manager_member2 = widget2->GetFocusManager(); |
| 1625 EXPECT_EQ(focus_manager_member2, focus_manager_member1); | 1619 EXPECT_EQ(focus_manager_member2, focus_manager_member1); |
| 1626 | 1620 |
| 1627 // Get the focus manager indirectly using the first window handle. Should | 1621 // Get the focus manager indirectly using the first window handle. Should |
| 1628 // return the first window's focus manager. | 1622 // return the first window's focus manager. |
| 1629 views::FocusManager* focus_manager_indirect = | 1623 views::FocusManager* focus_manager_indirect = |
| 1630 views::FocusManager::GetFocusManagerForNativeView( | 1624 views::FocusManager::GetFocusManagerForNativeView( |
| 1631 widget1->GetNativeView()); | 1625 widget1->GetNativeView()); |
| 1632 EXPECT_EQ(focus_manager_indirect, focus_manager_member1); | 1626 EXPECT_EQ(focus_manager_indirect, focus_manager_member1); |
| 1633 | 1627 |
| 1634 // Get the focus manager indirectly using the second window handle. Should | 1628 // Get the focus manager indirectly using the second window handle. Should |
| 1635 // return the first window's focus manager. | 1629 // return the first window's focus manager. |
| 1636 focus_manager_indirect = | 1630 focus_manager_indirect = |
| 1637 views::FocusManager::GetFocusManagerForNativeView( | 1631 views::FocusManager::GetFocusManagerForNativeView( |
| 1638 widget2->GetNativeView()); | 1632 widget2->GetNativeView()); |
| 1639 EXPECT_EQ(focus_manager_indirect, focus_manager_member1); | 1633 EXPECT_EQ(focus_manager_indirect, focus_manager_member1); |
| 1640 | 1634 |
| 1641 DestroyWindow(hwnd); | 1635 DestroyWindow(hwnd); |
| 1642 } | 1636 } |
| 1643 #endif | 1637 #endif |
| 1644 | 1638 |
| 1645 #if defined(OS_CHROMEOS) | |
| 1646 class FocusManagerDtorTest : public FocusManagerTest { | 1639 class FocusManagerDtorTest : public FocusManagerTest { |
| 1647 protected: | 1640 protected: |
| 1648 typedef std::vector<std::string> DtorTrackVector; | 1641 typedef std::vector<std::string> DtorTrackVector; |
| 1649 | 1642 |
| 1650 class FocusManagerDtorTracked : public FocusManager { | 1643 class FocusManagerDtorTracked : public FocusManager { |
| 1651 public: | 1644 public: |
| 1652 FocusManagerDtorTracked(Widget* widget, DtorTrackVector* dtor_tracker) | 1645 FocusManagerDtorTracked(Widget* widget, DtorTrackVector* dtor_tracker) |
| 1653 : FocusManager(widget), | 1646 : FocusManager(widget), |
| 1654 dtor_tracker_(dtor_tracker) { | 1647 dtor_tracker_(dtor_tracker) { |
| 1655 } | 1648 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 1668 : NativeButton(NULL, text), | 1661 : NativeButton(NULL, text), |
| 1669 dtor_tracker_(dtor_tracker) { | 1662 dtor_tracker_(dtor_tracker) { |
| 1670 }; | 1663 }; |
| 1671 virtual ~NativeButtonDtorTracked() { | 1664 virtual ~NativeButtonDtorTracked() { |
| 1672 dtor_tracker_->push_back("NativeButtonDtorTracked"); | 1665 dtor_tracker_->push_back("NativeButtonDtorTracked"); |
| 1673 } | 1666 } |
| 1674 | 1667 |
| 1675 DtorTrackVector* dtor_tracker_; | 1668 DtorTrackVector* dtor_tracker_; |
| 1676 }; | 1669 }; |
| 1677 | 1670 |
| 1678 class WindowGtkDtorTracked : public WindowGtk { | 1671 class WindowDtorTracked : public Window { |
| 1679 public: | 1672 public: |
| 1680 WindowGtkDtorTracked(WindowDelegate* window_delegate, | 1673 WindowDtorTracked(WindowDelegate* window_delegate, |
| 1681 DtorTrackVector* dtor_tracker) | 1674 DtorTrackVector* dtor_tracker) |
| 1682 : dtor_tracker_(dtor_tracker) { | 1675 : dtor_tracker_(dtor_tracker) { |
| 1683 tracked_focus_manager_ = new FocusManagerDtorTracked(this, | 1676 tracked_focus_manager_ = new FocusManagerDtorTracked(this, dtor_tracker_); |
| 1684 dtor_tracker_); | |
| 1685 Window::InitParams params(window_delegate); | 1677 Window::InitParams params(window_delegate); |
| 1686 params.widget_init_params.bounds = gfx::Rect(0, 0, 100, 100); | 1678 params.widget_init_params.bounds = gfx::Rect(0, 0, 100, 100); |
| 1687 GetWindow()->InitWindow(params); | 1679 InitWindow(params); |
| 1688 ReplaceFocusManager(tracked_focus_manager_); | 1680 ReplaceFocusManager(tracked_focus_manager_); |
| 1689 } | 1681 } |
| 1690 | 1682 |
| 1691 virtual ~WindowGtkDtorTracked() { | 1683 virtual ~WindowDtorTracked() { |
| 1692 dtor_tracker_->push_back("WindowGtkDtorTracked"); | 1684 dtor_tracker_->push_back("WindowGtkDtorTracked"); |
| 1693 } | 1685 } |
| 1694 | 1686 |
| 1695 FocusManagerDtorTracked* tracked_focus_manager_; | 1687 FocusManagerDtorTracked* tracked_focus_manager_; |
| 1696 DtorTrackVector* dtor_tracker_; | 1688 DtorTrackVector* dtor_tracker_; |
| 1697 }; | 1689 }; |
| 1698 | 1690 |
| 1699 public: | 1691 public: |
| 1700 virtual void SetUp() { | 1692 virtual void SetUp() { |
| 1701 // Create WindowGtkDtorTracked that uses FocusManagerDtorTracked. | 1693 // Create WindowGtkDtorTracked that uses FocusManagerDtorTracked. |
| 1702 window_ = new WindowGtkDtorTracked(this, &dtor_tracker_); | 1694 window_ = new WindowDtorTracked(this, &dtor_tracker_); |
| 1703 ASSERT_TRUE(GetFocusManager() == | 1695 ASSERT_TRUE(GetFocusManager() == static_cast<WindowDtorTracked*>( |
| 1704 static_cast<WindowGtkDtorTracked*>(window_)->tracked_focus_manager_); | 1696 window_)->tracked_focus_manager_); |
| 1705 | 1697 window_->Show(); |
| 1706 window_->Show(); | |
| 1707 } | 1698 } |
| 1708 | 1699 |
| 1709 virtual void TearDown() { | 1700 virtual void TearDown() { |
| 1710 if (window_) { | 1701 if (window_) { |
| 1711 window_->CloseWindow(); | 1702 window_->Close(); |
| 1712 message_loop()->RunAllPending(); | 1703 message_loop()->RunAllPending(); |
| 1713 } | 1704 } |
| 1714 } | 1705 } |
| 1715 | 1706 |
| 1716 DtorTrackVector dtor_tracker_; | 1707 DtorTrackVector dtor_tracker_; |
| 1717 }; | 1708 }; |
| 1718 | 1709 |
| 1719 TEST_F(FocusManagerDtorTest, FocusManagerDestructedLast) { | 1710 TEST_F(FocusManagerDtorTest, FocusManagerDestructedLast) { |
| 1720 // Setup views hierarchy. | 1711 // Setup views hierarchy. |
| 1721 TabbedPane* tabbed_pane = new TabbedPane(); | 1712 TabbedPane* tabbed_pane = new TabbedPane(); |
| 1722 content_view_->AddChildView(tabbed_pane); | 1713 content_view_->AddChildView(tabbed_pane); |
| 1723 | 1714 |
| 1724 NativeButtonDtorTracked* button = new NativeButtonDtorTracked(L"button", | 1715 NativeButtonDtorTracked* button = new NativeButtonDtorTracked(L"button", |
| 1725 &dtor_tracker_); | 1716 &dtor_tracker_); |
| 1726 tabbed_pane->AddTab(L"Awesome tab", button); | 1717 tabbed_pane->AddTab(L"Awesome tab", button); |
| 1727 | 1718 |
| 1728 // Close the window. | 1719 // Close the window. |
| 1729 window_->CloseWindow(); | 1720 window_->Close(); |
| 1730 message_loop()->RunAllPending(); | 1721 message_loop()->RunAllPending(); |
| 1731 | 1722 |
| 1732 // Test window, button and focus manager should all be destructed. | 1723 // Test window, button and focus manager should all be destructed. |
| 1733 ASSERT_EQ(3, static_cast<int>(dtor_tracker_.size())); | 1724 ASSERT_EQ(3, static_cast<int>(dtor_tracker_.size())); |
| 1734 | 1725 |
| 1735 // Focus manager should be the last one to destruct. | 1726 // Focus manager should be the last one to destruct. |
| 1736 ASSERT_STREQ("FocusManagerDtorTracked", dtor_tracker_[2].c_str()); | 1727 ASSERT_STREQ("FocusManagerDtorTracked", dtor_tracker_[2].c_str()); |
| 1737 | 1728 |
| 1738 // Clear window_ so that we don't try to close it again. | 1729 // Clear window_ so that we don't try to close it again. |
| 1739 window_ = NULL; | 1730 window_ = NULL; |
| 1740 } | 1731 } |
| 1741 | 1732 |
| 1742 #endif // defined(OS_CHROMEOS) | |
| 1743 | 1733 |
| 1744 } // namespace views | 1734 } // namespace views |
| OLD | NEW |