| 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/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "ui/views/controls/tabbed_pane/tabbed_pane.h" | 9 #include "ui/views/controls/tabbed_pane/tabbed_pane.h" |
| 9 #include "ui/views/test/views_test_base.h" | 10 #include "ui/views/test/views_test_base.h" |
| 10 #include "ui/views/widget/widget.h" | 11 #include "ui/views/widget/widget.h" |
| 11 #include "ui/views/widget/widget_delegate.h" | 12 #include "ui/views/widget/widget_delegate.h" |
| 12 | 13 |
| 13 namespace views { | 14 namespace views { |
| 14 | 15 |
| 15 // A view for testing that takes a fixed preferred size upon construction. | 16 // A view for testing that takes a fixed preferred size upon construction. |
| 16 class FixedSizeView : public View { | 17 class FixedSizeView : public View { |
| 17 public: | 18 public: |
| 18 explicit FixedSizeView(const gfx::Size& size) | 19 explicit FixedSizeView(const gfx::Size& size) |
| 19 : size_(size) {} | 20 : size_(size) {} |
| 20 | 21 |
| 21 // Overridden from View: | 22 // Overridden from View: |
| 22 virtual gfx::Size GetPreferredSize() OVERRIDE { | 23 virtual gfx::Size GetPreferredSize() OVERRIDE { |
| 23 return size_; | 24 return size_; |
| 24 } | 25 } |
| 25 | 26 |
| 26 private: | 27 private: |
| 27 const gfx::Size size_; | 28 const gfx::Size size_; |
| 28 | 29 |
| 29 DISALLOW_COPY_AND_ASSIGN(FixedSizeView); | 30 DISALLOW_COPY_AND_ASSIGN(FixedSizeView); |
| 30 }; | 31 }; |
| 31 | 32 |
| 32 class TabbedPaneTest : public ViewsTestBase, | 33 class TabbedPaneTest : public ViewsTestBase { |
| 33 public WidgetDelegate { | |
| 34 public: | 34 public: |
| 35 TabbedPaneTest() {} | 35 TabbedPaneTest() {} |
| 36 | 36 |
| 37 TabbedPane* tabbed_pane_; | 37 void TestSizeAndLayout(TabbedPane* tabbed_pane); |
| 38 |
| 39 void TestAddRemove(TabbedPane* tabbed_pane); |
| 40 |
| 41 TabbedPane* tabbed_pane_; // Owned by the |widget_|'s root View. |
| 42 |
| 43 #if defined(OS_WIN) && !defined(USE_AURA) |
| 44 TabbedPane* tabbed_pane_win_; // Owned by the |widget_|'s root View. |
| 45 #endif |
| 38 | 46 |
| 39 private: | 47 private: |
| 40 virtual void SetUp() OVERRIDE { | 48 virtual void SetUp() OVERRIDE; |
| 41 ViewsTestBase::SetUp(); | |
| 42 tabbed_pane_ = new TabbedPane(); | |
| 43 tabbed_pane_->set_use_native_win_control(true); | |
| 44 window_ = Widget::CreateWindowWithBounds(this, gfx::Rect(0, 0, 100, 100)); | |
| 45 window_->Show(); | |
| 46 } | |
| 47 | 49 |
| 48 virtual void TearDown() OVERRIDE { | 50 scoped_ptr<Widget> widget_; |
| 49 window_->Close(); | |
| 50 ViewsTestBase::TearDown(); | |
| 51 } | |
| 52 | |
| 53 virtual views::View* GetContentsView() OVERRIDE { | |
| 54 return tabbed_pane_; | |
| 55 } | |
| 56 virtual views::Widget* GetWidget() OVERRIDE { | |
| 57 return tabbed_pane_->GetWidget(); | |
| 58 } | |
| 59 virtual const views::Widget* GetWidget() const OVERRIDE { | |
| 60 return tabbed_pane_->GetWidget(); | |
| 61 } | |
| 62 | |
| 63 Widget* window_; | |
| 64 | 51 |
| 65 DISALLOW_COPY_AND_ASSIGN(TabbedPaneTest); | 52 DISALLOW_COPY_AND_ASSIGN(TabbedPaneTest); |
| 66 }; | 53 }; |
| 67 | 54 |
| 68 // Tests that TabbedPane::GetPreferredSize() and TabbedPane::Layout(). | 55 void TabbedPaneTest::SetUp() { |
| 69 TEST_F(TabbedPaneTest, SizeAndLayout) { | 56 ViewsTestBase::SetUp(); |
| 57 widget_.reset(new Widget()); |
| 58 Widget::InitParams params(Widget::InitParams::TYPE_POPUP); |
| 59 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 60 params.bounds = gfx::Rect(0, 0, 100, 100); |
| 61 widget_->Init(params); |
| 62 tabbed_pane_ = new TabbedPane(); |
| 63 // In order to properly initialize the |TabbedPane| it must be added to a |
| 64 // parent view (see the ViewHierarchyChanged method of the |TabbedPane|). |
| 65 widget_->GetRootView()->AddChildView(tabbed_pane_); |
| 66 |
| 67 #if defined(OS_WIN) && !defined(USE_AURA) |
| 68 tabbed_pane_win_ = new TabbedPane(); |
| 69 tabbed_pane_win_->set_use_native_win_control(true); |
| 70 widget_->GetRootView()->AddChildView(tabbed_pane_win_); |
| 71 #endif |
| 72 } |
| 73 |
| 74 void TabbedPaneTest::TestSizeAndLayout(TabbedPane* tabbed_pane) { |
| 70 View* child1 = new FixedSizeView(gfx::Size(20, 10)); | 75 View* child1 = new FixedSizeView(gfx::Size(20, 10)); |
| 71 tabbed_pane_->AddTab(ASCIIToUTF16("tab1"), child1); | 76 tabbed_pane->AddTab(ASCIIToUTF16("tab1"), child1); |
| 72 View* child2 = new FixedSizeView(gfx::Size(5, 5)); | 77 View* child2 = new FixedSizeView(gfx::Size(5, 5)); |
| 73 tabbed_pane_->AddTab(ASCIIToUTF16("tab2"), child2); | 78 tabbed_pane->AddTab(ASCIIToUTF16("tab2"), child2); |
| 74 tabbed_pane_->SelectTabAt(0); | 79 tabbed_pane->SelectTabAt(0); |
| 75 | 80 |
| 76 // Check that the preferred size is larger than the largest child. | 81 // The |tabbed_pane_| implementation of Views has no border by default. |
| 77 gfx::Size pref(tabbed_pane_->GetPreferredSize()); | 82 // Therefore it should be as wide as the widest tab. The native Windows |
| 78 EXPECT_GT(pref.width(), 20); | 83 // tabbed pane has a border that used up extra space. Therefore the preferred |
| 84 // width is larger than the largest child. |
| 85 gfx::Size pref(tabbed_pane->GetPreferredSize()); |
| 86 EXPECT_GE(pref.width(), 20); |
| 79 EXPECT_GT(pref.height(), 10); | 87 EXPECT_GT(pref.height(), 10); |
| 80 | 88 |
| 81 // The bounds of our children should be smaller than the tabbed pane's bounds. | 89 // The bounds of our children should be smaller than the tabbed pane's bounds. |
| 82 tabbed_pane_->SetBounds(0, 0, 100, 200); | 90 tabbed_pane->SetBounds(0, 0, 100, 200); |
| 83 RunPendingMessages(); | 91 RunPendingMessages(); |
| 84 gfx::Rect bounds(child1->bounds()); | 92 gfx::Rect bounds(child1->bounds()); |
| 85 EXPECT_GT(bounds.width(), 0); | 93 EXPECT_GT(bounds.width(), 0); |
| 86 EXPECT_LT(bounds.width(), 100); | 94 // The |tabbed_pane_| has no border. Therefore the children should be as wide |
| 95 // as the |tabbed_pane_|. |
| 96 EXPECT_LE(bounds.width(), 100); |
| 87 EXPECT_GT(bounds.height(), 0); | 97 EXPECT_GT(bounds.height(), 0); |
| 88 EXPECT_LT(bounds.height(), 200); | 98 EXPECT_LT(bounds.height(), 200); |
| 89 | 99 |
| 90 // If we switch to the other tab, it should get assigned the same bounds. | 100 // If we switch to the other tab, it should get assigned the same bounds. |
| 91 tabbed_pane_->SelectTabAt(1); | 101 tabbed_pane->SelectTabAt(1); |
| 92 EXPECT_EQ(bounds, child2->bounds()); | 102 EXPECT_EQ(bounds, child2->bounds()); |
| 93 } | 103 } |
| 94 | 104 |
| 95 TEST_F(TabbedPaneTest, AddRemove) { | 105 void TabbedPaneTest::TestAddRemove(TabbedPane* tabbed_pane) { |
| 96 View* tab0 = new View; | 106 View* tab0 = new View; |
| 97 tabbed_pane_->AddTab(ASCIIToUTF16("tab0"), tab0); | 107 tabbed_pane->AddTab(ASCIIToUTF16("tab0"), tab0); |
| 98 EXPECT_EQ(tab0, tabbed_pane_->GetSelectedTab()); | 108 EXPECT_EQ(tab0, tabbed_pane->GetSelectedTab()); |
| 99 EXPECT_EQ(0, tabbed_pane_->GetSelectedTabIndex()); | 109 EXPECT_EQ(0, tabbed_pane->GetSelectedTabIndex()); |
| 100 | 110 |
| 101 // Add more 3 tabs. | 111 // Add more 3 tabs. |
| 102 tabbed_pane_->AddTab(ASCIIToUTF16("tab1"), new View); | 112 tabbed_pane->AddTab(ASCIIToUTF16("tab1"), new View); |
| 103 tabbed_pane_->AddTab(ASCIIToUTF16("tab2"), new View); | 113 tabbed_pane->AddTab(ASCIIToUTF16("tab2"), new View); |
| 104 tabbed_pane_->AddTab(ASCIIToUTF16("tab3"), new View); | 114 tabbed_pane->AddTab(ASCIIToUTF16("tab3"), new View); |
| 105 EXPECT_EQ(4, tabbed_pane_->GetTabCount()); | 115 EXPECT_EQ(4, tabbed_pane->GetTabCount()); |
| 106 | 116 |
| 107 // Note: AddTab() doesn't select a tab if the tabbed pane isn't empty. | 117 // Note: AddTab() doesn't select a tab if the tabbed pane isn't empty. |
| 108 | 118 |
| 109 // Select the last one. | 119 // Select the last one. |
| 110 tabbed_pane_->SelectTabAt(tabbed_pane_->GetTabCount() - 1); | 120 tabbed_pane->SelectTabAt(tabbed_pane->GetTabCount() - 1); |
| 111 EXPECT_EQ(3, tabbed_pane_->GetSelectedTabIndex()); | 121 EXPECT_EQ(3, tabbed_pane->GetSelectedTabIndex()); |
| 112 | 122 |
| 113 // Remove the last one. | 123 // Remove the last one. |
| 114 delete tabbed_pane_->RemoveTabAtIndex(3); | 124 delete tabbed_pane->RemoveTabAtIndex(3); |
| 115 EXPECT_EQ(3, tabbed_pane_->GetTabCount()); | 125 EXPECT_EQ(3, tabbed_pane->GetTabCount()); |
| 116 | 126 |
| 117 // After removing the last tab, check if the tabbed pane selected the previous | 127 // After removing the last tab, check if the tabbed pane selected the previous |
| 118 // tab. | 128 // tab. |
| 119 EXPECT_EQ(2, tabbed_pane_->GetSelectedTabIndex()); | 129 EXPECT_EQ(2, tabbed_pane->GetSelectedTabIndex()); |
| 120 | 130 |
| 121 tabbed_pane_->AddTabAtIndex(0, ASCIIToUTF16("tab4"), new View, true); | 131 tabbed_pane->AddTabAtIndex(0, ASCIIToUTF16("tab4"), new View, true); |
| 122 | 132 |
| 123 // Assert that even adding a new tab, the tabbed pane doesn't change the | 133 // Assert that even adding a new tab, the tabbed pane doesn't change the |
| 124 // selection, i.e., it doesn't select the new one. | 134 // selection, i.e., it doesn't select the new one. |
| 125 // The last tab should remains selected, instead of the tab added at index 0. | 135 // The last tab should remains selected, instead of the tab added at index 0. |
| 126 EXPECT_EQ(3, tabbed_pane_->GetSelectedTabIndex()); | 136 EXPECT_EQ(3, tabbed_pane->GetSelectedTabIndex()); |
| 127 | 137 |
| 128 // Now change the selected tab. | 138 // Now change the selected tab. |
| 129 tabbed_pane_->SelectTabAt(1); | 139 tabbed_pane->SelectTabAt(1); |
| 130 EXPECT_EQ(1, tabbed_pane_->GetSelectedTabIndex()); | 140 EXPECT_EQ(1, tabbed_pane->GetSelectedTabIndex()); |
| 131 | 141 |
| 132 // Remove the first one. | 142 // Remove the first one. |
| 133 delete tabbed_pane_->RemoveTabAtIndex(0); | 143 delete tabbed_pane->RemoveTabAtIndex(0); |
| 134 EXPECT_EQ(0, tabbed_pane_->GetSelectedTabIndex()); | 144 EXPECT_EQ(0, tabbed_pane->GetSelectedTabIndex()); |
| 145 } |
| 146 |
| 147 // Tests TabbedPane::GetPreferredSize() and TabbedPane::Layout(). |
| 148 TEST_F(TabbedPaneTest, SizeAndLayout) { |
| 149 TestSizeAndLayout(tabbed_pane_); |
| 150 // TODO(markusheintz): Once replacing NativeTabbedPaneWin with |
| 151 // NativeTabbedPaneView is completed (http://crbug.com/138059), then the |
| 152 // TestSizeAndLayout method should be inlined here again and the "ifdef" part |
| 153 // should be deleted. |
| 154 #if defined(OS_WIN) && !defined(USE_AURA) |
| 155 TestSizeAndLayout(tabbed_pane_win_); |
| 156 #endif |
| 157 } |
| 158 |
| 159 TEST_F(TabbedPaneTest, AddRemove) { |
| 160 TestAddRemove(tabbed_pane_); |
| 161 // TODO(markusheintz): Once replacing NativeTabbedPaneWin with |
| 162 // NativeTabbedPaneView is completed (http://crbug.com/138059), then the |
| 163 // TestAddRemove method should be inlined here again and the "ifdef" part |
| 164 // should be deleted. |
| 165 #if defined(OS_WIN) && !defined(USE_AURA) |
| 166 TestAddRemove(tabbed_pane_win_); |
| 167 #endif |
| 135 } | 168 } |
| 136 | 169 |
| 137 } // namespace views | 170 } // namespace views |
| OLD | NEW |