| 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" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 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 class TabbedPaneTest : public ViewsTestBase { |
| 34 public: | 34 public: |
| 35 TabbedPaneTest() {} | 35 TabbedPaneTest() {} |
| 36 | 36 |
| 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. | 37 TabbedPane* tabbed_pane_; // Owned by the |widget_|'s root View. |
| 42 | 38 |
| 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: | 39 private: |
| 48 virtual void SetUp() OVERRIDE; | 40 virtual void SetUp() OVERRIDE; |
| 49 | 41 |
| 50 scoped_ptr<Widget> widget_; | 42 scoped_ptr<Widget> widget_; |
| 51 | 43 |
| 52 DISALLOW_COPY_AND_ASSIGN(TabbedPaneTest); | 44 DISALLOW_COPY_AND_ASSIGN(TabbedPaneTest); |
| 53 }; | 45 }; |
| 54 | 46 |
| 55 void TabbedPaneTest::SetUp() { | 47 void TabbedPaneTest::SetUp() { |
| 56 ViewsTestBase::SetUp(); | 48 ViewsTestBase::SetUp(); |
| 57 widget_.reset(new Widget()); | 49 widget_.reset(new Widget()); |
| 58 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); | 50 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP); |
| 59 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 51 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 60 params.bounds = gfx::Rect(0, 0, 100, 100); | 52 params.bounds = gfx::Rect(0, 0, 100, 100); |
| 61 widget_->Init(params); | 53 widget_->Init(params); |
| 62 tabbed_pane_ = new TabbedPane(); | 54 tabbed_pane_ = new TabbedPane(); |
| 63 // In order to properly initialize the |TabbedPane| it must be added to a | 55 // In order to properly initialize the |TabbedPane| it must be added to a |
| 64 // parent view (see the ViewHierarchyChanged method of the |TabbedPane|). | 56 // parent view (see the ViewHierarchyChanged method of the |TabbedPane|). |
| 65 widget_->GetRootView()->AddChildView(tabbed_pane_); | 57 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 } | 58 } |
| 73 | 59 |
| 74 void TabbedPaneTest::TestSizeAndLayout(TabbedPane* tabbed_pane) { | 60 // Tests TabbedPane::GetPreferredSize() and TabbedPane::Layout(). |
| 61 TEST_F(TabbedPaneTest, SizeAndLayout) { |
| 75 View* child1 = new FixedSizeView(gfx::Size(20, 10)); | 62 View* child1 = new FixedSizeView(gfx::Size(20, 10)); |
| 76 tabbed_pane->AddTab(ASCIIToUTF16("tab1"), child1); | 63 tabbed_pane_->AddTab(ASCIIToUTF16("tab1"), child1); |
| 77 View* child2 = new FixedSizeView(gfx::Size(5, 5)); | 64 View* child2 = new FixedSizeView(gfx::Size(5, 5)); |
| 78 tabbed_pane->AddTab(ASCIIToUTF16("tab2"), child2); | 65 tabbed_pane_->AddTab(ASCIIToUTF16("tab2"), child2); |
| 79 tabbed_pane->SelectTabAt(0); | 66 tabbed_pane_->SelectTabAt(0); |
| 80 | 67 |
| 81 // The |tabbed_pane_| implementation of Views has no border by default. | 68 // 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 | 69 // 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 | 70 // tabbed pane has a border that used up extra space. Therefore the preferred |
| 84 // width is larger than the largest child. | 71 // width is larger than the largest child. |
| 85 gfx::Size pref(tabbed_pane->GetPreferredSize()); | 72 gfx::Size pref(tabbed_pane_->GetPreferredSize()); |
| 86 EXPECT_GE(pref.width(), 20); | 73 EXPECT_GE(pref.width(), 20); |
| 87 EXPECT_GT(pref.height(), 10); | 74 EXPECT_GT(pref.height(), 10); |
| 88 | 75 |
| 89 // The bounds of our children should be smaller than the tabbed pane's bounds. | 76 // The bounds of our children should be smaller than the tabbed pane's bounds. |
| 90 tabbed_pane->SetBounds(0, 0, 100, 200); | 77 tabbed_pane_->SetBounds(0, 0, 100, 200); |
| 91 RunPendingMessages(); | 78 RunPendingMessages(); |
| 92 gfx::Rect bounds(child1->bounds()); | 79 gfx::Rect bounds(child1->bounds()); |
| 93 EXPECT_GT(bounds.width(), 0); | 80 EXPECT_GT(bounds.width(), 0); |
| 94 // The |tabbed_pane_| has no border. Therefore the children should be as wide | 81 // The |tabbed_pane_| has no border. Therefore the children should be as wide |
| 95 // as the |tabbed_pane_|. | 82 // as the |tabbed_pane_|. |
| 96 EXPECT_LE(bounds.width(), 100); | 83 EXPECT_LE(bounds.width(), 100); |
| 97 EXPECT_GT(bounds.height(), 0); | 84 EXPECT_GT(bounds.height(), 0); |
| 98 EXPECT_LT(bounds.height(), 200); | 85 EXPECT_LT(bounds.height(), 200); |
| 99 | 86 |
| 100 // If we switch to the other tab, it should get assigned the same bounds. | 87 // If we switch to the other tab, it should get assigned the same bounds. |
| 101 tabbed_pane->SelectTabAt(1); | 88 tabbed_pane_->SelectTabAt(1); |
| 102 EXPECT_EQ(bounds, child2->bounds()); | 89 EXPECT_EQ(bounds, child2->bounds()); |
| 103 | 90 |
| 104 // Clean up. | 91 // Clean up. |
| 105 delete tabbed_pane->RemoveTabAtIndex(0); | 92 delete tabbed_pane_->RemoveTabAtIndex(0); |
| 106 EXPECT_EQ(1, tabbed_pane->GetTabCount()); | 93 EXPECT_EQ(1, tabbed_pane_->GetTabCount()); |
| 107 delete tabbed_pane->RemoveTabAtIndex(0); | 94 delete tabbed_pane_->RemoveTabAtIndex(0); |
| 108 EXPECT_EQ(0, tabbed_pane->GetTabCount()); | 95 EXPECT_EQ(0, tabbed_pane_->GetTabCount()); |
| 109 } | 96 } |
| 110 | 97 |
| 111 void TabbedPaneTest::TestAddRemove(TabbedPane* tabbed_pane) { | 98 TEST_F(TabbedPaneTest, AddRemove) { |
| 112 View* tab0 = new View; | 99 View* tab0 = new View; |
| 113 tabbed_pane->AddTab(ASCIIToUTF16("tab0"), tab0); | 100 tabbed_pane_->AddTab(ASCIIToUTF16("tab0"), tab0); |
| 114 EXPECT_EQ(tab0, tabbed_pane->GetSelectedTab()); | 101 EXPECT_EQ(tab0, tabbed_pane_->GetSelectedTab()); |
| 115 EXPECT_EQ(0, tabbed_pane->GetSelectedTabIndex()); | 102 EXPECT_EQ(0, tabbed_pane_->GetSelectedTabIndex()); |
| 116 | 103 |
| 117 // Add more 3 tabs. | 104 // Add more 3 tabs. |
| 118 tabbed_pane->AddTab(ASCIIToUTF16("tab1"), new View); | 105 tabbed_pane_->AddTab(ASCIIToUTF16("tab1"), new View); |
| 119 tabbed_pane->AddTab(ASCIIToUTF16("tab2"), new View); | 106 tabbed_pane_->AddTab(ASCIIToUTF16("tab2"), new View); |
| 120 tabbed_pane->AddTab(ASCIIToUTF16("tab3"), new View); | 107 tabbed_pane_->AddTab(ASCIIToUTF16("tab3"), new View); |
| 121 EXPECT_EQ(4, tabbed_pane->GetTabCount()); | 108 EXPECT_EQ(4, tabbed_pane_->GetTabCount()); |
| 122 | 109 |
| 123 // Note: AddTab() doesn't select a tab if the tabbed pane isn't empty. | 110 // Note: AddTab() doesn't select a tab if the tabbed pane isn't empty. |
| 124 | 111 |
| 125 // Select the last one. | 112 // Select the last one. |
| 126 tabbed_pane->SelectTabAt(tabbed_pane->GetTabCount() - 1); | 113 tabbed_pane_->SelectTabAt(tabbed_pane_->GetTabCount() - 1); |
| 127 EXPECT_EQ(3, tabbed_pane->GetSelectedTabIndex()); | 114 EXPECT_EQ(3, tabbed_pane_->GetSelectedTabIndex()); |
| 128 | 115 |
| 129 // Remove the last one. | 116 // Remove the last one. |
| 130 delete tabbed_pane->RemoveTabAtIndex(3); | 117 delete tabbed_pane_->RemoveTabAtIndex(3); |
| 131 EXPECT_EQ(3, tabbed_pane->GetTabCount()); | 118 EXPECT_EQ(3, tabbed_pane_->GetTabCount()); |
| 132 | 119 |
| 133 // After removing the last tab, check if the tabbed pane selected the previous | 120 // After removing the last tab, check if the tabbed pane selected the previous |
| 134 // tab. | 121 // tab. |
| 135 EXPECT_EQ(2, tabbed_pane->GetSelectedTabIndex()); | 122 EXPECT_EQ(2, tabbed_pane_->GetSelectedTabIndex()); |
| 136 | 123 |
| 137 tabbed_pane->AddTabAtIndex(0, ASCIIToUTF16("tab4"), new View, true); | 124 tabbed_pane_->AddTabAtIndex(0, ASCIIToUTF16("tab4"), new View, true); |
| 138 | 125 |
| 139 // Assert that even adding a new tab, the tabbed pane doesn't change the | 126 // 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. | 127 // 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. | 128 // The last tab should remains selected, instead of the tab added at index 0. |
| 142 EXPECT_EQ(3, tabbed_pane->GetSelectedTabIndex()); | 129 EXPECT_EQ(3, tabbed_pane_->GetSelectedTabIndex()); |
| 143 | 130 |
| 144 // Now change the selected tab. | 131 // Now change the selected tab. |
| 145 tabbed_pane->SelectTabAt(1); | 132 tabbed_pane_->SelectTabAt(1); |
| 146 EXPECT_EQ(1, tabbed_pane->GetSelectedTabIndex()); | 133 EXPECT_EQ(1, tabbed_pane_->GetSelectedTabIndex()); |
| 147 | 134 |
| 148 // Remove the first one. | 135 // Remove the first one. |
| 149 delete tabbed_pane->RemoveTabAtIndex(0); | 136 delete tabbed_pane_->RemoveTabAtIndex(0); |
| 150 EXPECT_EQ(0, tabbed_pane->GetSelectedTabIndex()); | 137 EXPECT_EQ(0, tabbed_pane_->GetSelectedTabIndex()); |
| 151 | 138 |
| 152 // Clean up the other panes. | 139 // Clean up the other panes. |
| 153 EXPECT_EQ(3, tabbed_pane->GetTabCount()); | 140 EXPECT_EQ(3, tabbed_pane_->GetTabCount()); |
| 154 delete tabbed_pane->RemoveTabAtIndex(0); | 141 delete tabbed_pane_->RemoveTabAtIndex(0); |
| 155 delete tabbed_pane->RemoveTabAtIndex(0); | 142 delete tabbed_pane_->RemoveTabAtIndex(0); |
| 156 delete tabbed_pane->RemoveTabAtIndex(0); | 143 delete tabbed_pane_->RemoveTabAtIndex(0); |
| 157 EXPECT_EQ(0, tabbed_pane->GetTabCount()); | 144 EXPECT_EQ(0, tabbed_pane_->GetTabCount()); |
| 158 } | |
| 159 | |
| 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 } | 145 } |
| 182 | 146 |
| 183 } // namespace views | 147 } // namespace views |
| OLD | NEW |