| 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 <utility> | 5 #include <utility> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "ui/base/accelerators/accelerator.h" | 9 #include "ui/base/accelerators/accelerator.h" |
| 10 #include "ui/base/keycodes/keyboard_codes.h" | 10 #include "ui/base/keycodes/keyboard_codes.h" |
| 11 #include "ui/views/controls/button/text_button.h" | 11 #include "ui/views/controls/button/text_button.h" |
| 12 #include "ui/views/controls/textfield/textfield.h" | 12 #include "ui/views/controls/textfield/textfield.h" |
| 13 #include "ui/views/focus/accelerator_handler.h" | 13 #include "ui/views/focus/accelerator_handler.h" |
| 14 #include "ui/views/focus/focus_manager_factory.h" | 14 #include "ui/views/focus/focus_manager_factory.h" |
| 15 #include "ui/views/focus/focus_manager_test.h" | 15 #include "ui/views/focus/focus_manager_test.h" |
| 16 #include "ui/views/focus/widget_focus_manager.h" | 16 #include "ui/views/focus/widget_focus_manager.h" |
| 17 #include "ui/views/widget/widget.h" | 17 #include "ui/views/widget/widget.h" |
| 18 | 18 |
| 19 #if defined(USE_AURA) | 19 #if defined(USE_AURA) |
| 20 #include "ui/aura/client/focus_client.h" | 20 #include "ui/aura/client/focus_client.h" |
| 21 #include "ui/aura/window.h" | 21 #include "ui/aura/window.h" |
| 22 #else | |
| 23 #include "ui/views/controls/tabbed_pane/native_tabbed_pane_wrapper.h" | |
| 24 #include "ui/views/controls/tabbed_pane/tabbed_pane.h" | |
| 25 #endif | 22 #endif |
| 26 | 23 |
| 27 namespace views { | 24 namespace views { |
| 28 | 25 |
| 29 void FocusNativeView(gfx::NativeView view) { | 26 void FocusNativeView(gfx::NativeView view) { |
| 30 #if defined(USE_AURA) | 27 #if defined(USE_AURA) |
| 31 aura::client::GetFocusClient(view)->FocusWindow(view); | 28 aura::client::GetFocusClient(view)->FocusWindow(view); |
| 32 #elif defined(OS_WIN) | 29 #elif defined(OS_WIN) |
| 33 SetFocus(view); | 30 SetFocus(view); |
| 34 #else | 31 #else |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 | 165 |
| 169 #if !defined(USE_AURA) | 166 #if !defined(USE_AURA) |
| 170 class TestTextfield : public Textfield { | 167 class TestTextfield : public Textfield { |
| 171 public: | 168 public: |
| 172 TestTextfield() {} | 169 TestTextfield() {} |
| 173 virtual gfx::NativeView TestGetNativeControlView() { | 170 virtual gfx::NativeView TestGetNativeControlView() { |
| 174 return native_wrapper_->GetTestingHandle(); | 171 return native_wrapper_->GetTestingHandle(); |
| 175 } | 172 } |
| 176 }; | 173 }; |
| 177 | 174 |
| 178 class TestTabbedPane : public TabbedPane { | |
| 179 public: | |
| 180 TestTabbedPane() {} | |
| 181 virtual gfx::NativeView TestGetNativeControlView() { | |
| 182 return native_tabbed_pane_->GetTestingHandle(); | |
| 183 } | |
| 184 }; | |
| 185 | |
| 186 // Tests that NativeControls do set the focused View appropriately on the | 175 // Tests that NativeControls do set the focused View appropriately on the |
| 187 // FocusManager. | 176 // FocusManager. |
| 188 TEST_F(FocusManagerTest, DISABLED_FocusNativeControls) { | 177 TEST_F(FocusManagerTest, DISABLED_FocusNativeControls) { |
| 189 TestTextfield* textfield = new TestTextfield(); | 178 TestTextfield* textfield = new TestTextfield(); |
| 190 TestTabbedPane* tabbed_pane = new TestTabbedPane(); | |
| 191 tabbed_pane->set_use_native_win_control(true); | |
| 192 TestTextfield* textfield2 = new TestTextfield(); | |
| 193 | |
| 194 GetContentsView()->AddChildView(textfield); | 179 GetContentsView()->AddChildView(textfield); |
| 195 GetContentsView()->AddChildView(tabbed_pane); | |
| 196 | |
| 197 tabbed_pane->AddTab(ASCIIToUTF16("Awesome textfield"), textfield2); | |
| 198 | |
| 199 // Simulate the native view getting the native focus (such as by user click). | 180 // Simulate the native view getting the native focus (such as by user click). |
| 200 FocusNativeView(textfield->TestGetNativeControlView()); | 181 FocusNativeView(textfield->TestGetNativeControlView()); |
| 201 EXPECT_EQ(textfield, GetFocusManager()->GetFocusedView()); | 182 EXPECT_EQ(textfield, GetFocusManager()->GetFocusedView()); |
| 202 | |
| 203 FocusNativeView(tabbed_pane->TestGetNativeControlView()); | |
| 204 EXPECT_EQ(tabbed_pane, GetFocusManager()->GetFocusedView()); | |
| 205 | |
| 206 FocusNativeView(textfield2->TestGetNativeControlView()); | |
| 207 EXPECT_EQ(textfield2, GetFocusManager()->GetFocusedView()); | |
| 208 } | 183 } |
| 209 #endif | 184 #endif |
| 210 | 185 |
| 211 // There is no tabbed pane in Aura. | |
| 212 #if !defined(USE_AURA) | |
| 213 TEST_F(FocusManagerTest, ContainsView) { | |
| 214 View* view = new View(); | |
| 215 scoped_ptr<View> detached_view(new View()); | |
| 216 TabbedPane* tabbed_pane = new TabbedPane(); | |
| 217 tabbed_pane->set_use_native_win_control(true); | |
| 218 TabbedPane* nested_tabbed_pane = new TabbedPane(); | |
| 219 nested_tabbed_pane->set_use_native_win_control(true); | |
| 220 NativeTextButton* tab_button = new NativeTextButton( | |
| 221 NULL, ASCIIToUTF16("tab button")); | |
| 222 | |
| 223 GetContentsView()->AddChildView(view); | |
| 224 GetContentsView()->AddChildView(tabbed_pane); | |
| 225 // Adding a View inside a TabbedPane to test the case of nested root view. | |
| 226 | |
| 227 tabbed_pane->AddTab(ASCIIToUTF16("Awesome tab"), nested_tabbed_pane); | |
| 228 nested_tabbed_pane->AddTab(ASCIIToUTF16("Awesomer tab"), tab_button); | |
| 229 | |
| 230 EXPECT_TRUE(GetFocusManager()->ContainsView(view)); | |
| 231 EXPECT_TRUE(GetFocusManager()->ContainsView(tabbed_pane)); | |
| 232 EXPECT_TRUE(GetFocusManager()->ContainsView(nested_tabbed_pane)); | |
| 233 EXPECT_TRUE(GetFocusManager()->ContainsView(tab_button)); | |
| 234 EXPECT_FALSE(GetFocusManager()->ContainsView(detached_view.get())); | |
| 235 } | |
| 236 #endif | |
| 237 | |
| 238 // Counts accelerator calls. | 186 // Counts accelerator calls. |
| 239 class TestAcceleratorTarget : public ui::AcceleratorTarget { | 187 class TestAcceleratorTarget : public ui::AcceleratorTarget { |
| 240 public: | 188 public: |
| 241 explicit TestAcceleratorTarget(bool process_accelerator) | 189 explicit TestAcceleratorTarget(bool process_accelerator) |
| 242 : accelerator_count_(0), | 190 : accelerator_count_(0), |
| 243 process_accelerator_(process_accelerator), | 191 process_accelerator_(process_accelerator), |
| 244 can_handle_accelerators_(true) {} | 192 can_handle_accelerators_(true) {} |
| 245 | 193 |
| 246 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE { | 194 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE { |
| 247 ++accelerator_count_; | 195 ++accelerator_count_; |
| (...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 632 ViewsTestBase::TearDown(); | 580 ViewsTestBase::TearDown(); |
| 633 } | 581 } |
| 634 | 582 |
| 635 FocusManager* tracked_focus_manager_; | 583 FocusManager* tracked_focus_manager_; |
| 636 DtorTrackVector dtor_tracker_; | 584 DtorTrackVector dtor_tracker_; |
| 637 }; | 585 }; |
| 638 | 586 |
| 639 #if !defined(USE_AURA) | 587 #if !defined(USE_AURA) |
| 640 TEST_F(FocusManagerDtorTest, FocusManagerDestructedLast) { | 588 TEST_F(FocusManagerDtorTest, FocusManagerDestructedLast) { |
| 641 // Setup views hierarchy. | 589 // Setup views hierarchy. |
| 642 TabbedPane* tabbed_pane = new TabbedPane(); | 590 GetContentsView()->AddChildView(new TestTextfield()); |
| 643 tabbed_pane->set_use_native_win_control(true); | 591 GetContentsView()->AddChildView(new NativeButtonDtorTracked( |
| 644 GetContentsView()->AddChildView(tabbed_pane); | 592 ASCIIToUTF16("button"), &dtor_tracker_)); |
| 645 | |
| 646 NativeButtonDtorTracked* button = new NativeButtonDtorTracked( | |
| 647 ASCIIToUTF16("button"), &dtor_tracker_); | |
| 648 tabbed_pane->AddTab(ASCIIToUTF16("Awesome tab"), button); | |
| 649 | 593 |
| 650 // Close the window. | 594 // Close the window. |
| 651 GetWidget()->Close(); | 595 GetWidget()->Close(); |
| 652 RunPendingMessages(); | 596 RunPendingMessages(); |
| 653 | 597 |
| 654 // Test window, button and focus manager should all be destructed. | 598 // Test window, button and focus manager should all be destructed. |
| 655 ASSERT_EQ(3, static_cast<int>(dtor_tracker_.size())); | 599 ASSERT_EQ(3, static_cast<int>(dtor_tracker_.size())); |
| 656 | 600 |
| 657 // Focus manager should be the last one to destruct. | 601 // Focus manager should be the last one to destruct. |
| 658 ASSERT_STREQ("FocusManagerDtorTracked", dtor_tracker_[2].c_str()); | 602 ASSERT_STREQ("FocusManagerDtorTracked", dtor_tracker_[2].c_str()); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 696 views::View* v3 = new View; | 640 views::View* v3 = new View; |
| 697 v3->set_focusable(true); | 641 v3->set_focusable(true); |
| 698 GetContentsView()->AddChildView(v3); | 642 GetContentsView()->AddChildView(v3); |
| 699 | 643 |
| 700 v3->RequestFocus(); | 644 v3->RequestFocus(); |
| 701 GetWidget()->GetFocusManager()->AdvanceFocus(true); | 645 GetWidget()->GetFocusManager()->AdvanceFocus(true); |
| 702 EXPECT_TRUE(v1->HasFocus()); | 646 EXPECT_TRUE(v1->HasFocus()); |
| 703 } | 647 } |
| 704 | 648 |
| 705 } // namespace views | 649 } // namespace views |
| OLD | NEW |