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" |
11 | 13 |
12 namespace views { | 14 namespace views { |
13 | 15 |
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 typedef ViewsTestBase TabbedPaneTest; | 33 class TabbedPaneTest : public ViewsTestBase { |
| 34 public: |
| 35 TabbedPaneTest() {} |
34 | 36 |
35 // Tests TabbedPane::GetPreferredSize() and TabbedPane::Layout(). | 37 void TestSizeAndLayout(TabbedPane* tabbed_pane); |
36 TEST_F(TabbedPaneTest, SizeAndLayout) { | 38 |
37 scoped_ptr<TabbedPane> tabbed_pane(new TabbedPane()); | 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 |
| 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) { |
38 View* child1 = new FixedSizeView(gfx::Size(20, 10)); | 75 View* child1 = new FixedSizeView(gfx::Size(20, 10)); |
39 tabbed_pane->AddTab(ASCIIToUTF16("tab1"), child1); | 76 tabbed_pane->AddTab(ASCIIToUTF16("tab1"), child1); |
40 View* child2 = new FixedSizeView(gfx::Size(5, 5)); | 77 View* child2 = new FixedSizeView(gfx::Size(5, 5)); |
41 tabbed_pane->AddTab(ASCIIToUTF16("tab2"), child2); | 78 tabbed_pane->AddTab(ASCIIToUTF16("tab2"), child2); |
42 tabbed_pane->SelectTabAt(0); | 79 tabbed_pane->SelectTabAt(0); |
43 | 80 |
44 // The |tabbed_pane| implementation of Views has no border by default. | 81 // The |tabbed_pane_| implementation of Views has no border by default. |
45 // Therefore it should be as wide as the widest tab. The native Windows | 82 // Therefore it should be as wide as the widest tab. The native Windows |
46 // tabbed pane has a border that used up extra space. Therefore the preferred | 83 // tabbed pane has a border that used up extra space. Therefore the preferred |
47 // width is larger than the largest child. | 84 // width is larger than the largest child. |
48 gfx::Size pref(tabbed_pane->GetPreferredSize()); | 85 gfx::Size pref(tabbed_pane->GetPreferredSize()); |
49 EXPECT_GE(pref.width(), 20); | 86 EXPECT_GE(pref.width(), 20); |
50 EXPECT_GT(pref.height(), 10); | 87 EXPECT_GT(pref.height(), 10); |
51 | 88 |
52 // 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. |
53 tabbed_pane->SetBounds(0, 0, 100, 200); | 90 tabbed_pane->SetBounds(0, 0, 100, 200); |
54 RunPendingMessages(); | 91 RunPendingMessages(); |
55 gfx::Rect bounds(child1->bounds()); | 92 gfx::Rect bounds(child1->bounds()); |
56 EXPECT_GT(bounds.width(), 0); | 93 EXPECT_GT(bounds.width(), 0); |
57 // The |tabbed_pane| has no border. Therefore the children should be as wide | 94 // The |tabbed_pane_| has no border. Therefore the children should be as wide |
58 // as the |tabbed_pane|. | 95 // as the |tabbed_pane_|. |
59 EXPECT_LE(bounds.width(), 100); | 96 EXPECT_LE(bounds.width(), 100); |
60 EXPECT_GT(bounds.height(), 0); | 97 EXPECT_GT(bounds.height(), 0); |
61 EXPECT_LT(bounds.height(), 200); | 98 EXPECT_LT(bounds.height(), 200); |
62 | 99 |
63 // 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. |
64 tabbed_pane->SelectTabAt(1); | 101 tabbed_pane->SelectTabAt(1); |
65 EXPECT_EQ(bounds, child2->bounds()); | 102 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()); |
66 } | 109 } |
67 | 110 |
68 TEST_F(TabbedPaneTest, AddAndSelect) { | 111 void TabbedPaneTest::TestAddRemove(TabbedPane* tabbed_pane) { |
69 scoped_ptr<TabbedPane> tabbed_pane(new TabbedPane()); | 112 View* tab0 = new View; |
70 // Add several tabs; only the first should be a selected automatically. | 113 tabbed_pane->AddTab(ASCIIToUTF16("tab0"), tab0); |
71 for (int i = 0; i < 3; ++i) { | 114 EXPECT_EQ(tab0, tabbed_pane->GetSelectedTab()); |
72 View* tab = new View(); | 115 EXPECT_EQ(0, tabbed_pane->GetSelectedTabIndex()); |
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 } | |
77 | 116 |
78 // Select each tab. | 117 // Add more 3 tabs. |
79 for (int i = 0; i < tabbed_pane->GetTabCount(); ++i) { | 118 tabbed_pane->AddTab(ASCIIToUTF16("tab1"), new View); |
80 tabbed_pane->SelectTabAt(i); | 119 tabbed_pane->AddTab(ASCIIToUTF16("tab2"), new View); |
81 EXPECT_EQ(i, tabbed_pane->selected_tab_index()); | 120 tabbed_pane->AddTab(ASCIIToUTF16("tab3"), new View); |
82 } | 121 EXPECT_EQ(4, tabbed_pane->GetTabCount()); |
83 | 122 |
84 // Add a tab at index 0, it should not be selected automatically. | 123 // Note: AddTab() doesn't select a tab if the tabbed pane isn't empty. |
85 View* tab0 = new View(); | 124 |
86 tabbed_pane->AddTabAtIndex(0, ASCIIToUTF16("tab0"), tab0); | 125 // Select the last one. |
87 EXPECT_NE(tab0, tabbed_pane->GetSelectedTab()); | 126 tabbed_pane->SelectTabAt(tabbed_pane->GetTabCount() - 1); |
88 EXPECT_NE(0, tabbed_pane->selected_tab_index()); | 127 EXPECT_EQ(3, tabbed_pane->GetSelectedTabIndex()); |
| 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()); |
89 } | 158 } |
90 | 159 |
91 } // namespace | 160 // Tests TabbedPane::GetPreferredSize() and TabbedPane::Layout(). |
| 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 } |
92 | 182 |
93 } // namespace views | 183 } // namespace views |
OLD | NEW |