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