| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/message_loop.h" | |
| 6 #include "base/utf_string_conversions.h" | |
| 7 #include "testing/gtest/include/gtest/gtest.h" | |
| 8 #include "ui/views/test/views_test_base.h" | |
| 9 #include "ui/views/widget/widget.h" | |
| 10 #include "ui/views/widget/widget_delegate.h" | |
| 11 #include "views/controls/tabbed_pane/tabbed_pane.h" | |
| 12 | |
| 13 namespace views { | |
| 14 | |
| 15 // A view for testing that takes a fixed preferred size upon construction. | |
| 16 class FixedSizeView : public View { | |
| 17 public: | |
| 18 FixedSizeView(const gfx::Size& size) | |
| 19 : size_(size) {} | |
| 20 | |
| 21 virtual gfx::Size GetPreferredSize() { | |
| 22 return size_; | |
| 23 } | |
| 24 | |
| 25 private: | |
| 26 const gfx::Size size_; | |
| 27 | |
| 28 DISALLOW_COPY_AND_ASSIGN(FixedSizeView); | |
| 29 }; | |
| 30 | |
| 31 class TabbedPaneTest : public ViewsTestBase, | |
| 32 public WidgetDelegate { | |
| 33 public: | |
| 34 TabbedPaneTest() {} | |
| 35 | |
| 36 TabbedPane* tabbed_pane_; | |
| 37 | |
| 38 private: | |
| 39 virtual void SetUp() OVERRIDE { | |
| 40 ViewsTestBase::SetUp(); | |
| 41 tabbed_pane_ = new TabbedPane(); | |
| 42 window_ = Widget::CreateWindowWithBounds(this, gfx::Rect(0, 0, 100, 100)); | |
| 43 window_->Show(); | |
| 44 } | |
| 45 | |
| 46 virtual void TearDown() OVERRIDE { | |
| 47 window_->Close(); | |
| 48 ViewsTestBase::TearDown(); | |
| 49 } | |
| 50 | |
| 51 virtual views::View* GetContentsView() OVERRIDE { | |
| 52 return tabbed_pane_; | |
| 53 } | |
| 54 virtual views::Widget* GetWidget() OVERRIDE { | |
| 55 return tabbed_pane_->GetWidget(); | |
| 56 } | |
| 57 virtual const views::Widget* GetWidget() const OVERRIDE { | |
| 58 return tabbed_pane_->GetWidget(); | |
| 59 } | |
| 60 | |
| 61 Widget* window_; | |
| 62 | |
| 63 DISALLOW_COPY_AND_ASSIGN(TabbedPaneTest); | |
| 64 }; | |
| 65 | |
| 66 // Tests that TabbedPane::GetPreferredSize() and TabbedPane::Layout(). | |
| 67 TEST_F(TabbedPaneTest, SizeAndLayout) { | |
| 68 View* child1 = new FixedSizeView(gfx::Size(20, 10)); | |
| 69 tabbed_pane_->AddTab(ASCIIToUTF16("tab1"), child1); | |
| 70 View* child2 = new FixedSizeView(gfx::Size(5, 5)); | |
| 71 tabbed_pane_->AddTab(ASCIIToUTF16("tab2"), child2); | |
| 72 tabbed_pane_->SelectTabAt(0); | |
| 73 | |
| 74 // Check that the preferred size is larger than the largest child. | |
| 75 gfx::Size pref(tabbed_pane_->GetPreferredSize()); | |
| 76 EXPECT_GT(pref.width(), 20); | |
| 77 EXPECT_GT(pref.height(), 10); | |
| 78 | |
| 79 // The bounds of our children should be smaller than the tabbed pane's bounds. | |
| 80 tabbed_pane_->SetBounds(0, 0, 100, 200); | |
| 81 RunPendingMessages(); | |
| 82 gfx::Rect bounds(child1->bounds()); | |
| 83 EXPECT_GT(bounds.width(), 0); | |
| 84 EXPECT_LT(bounds.width(), 100); | |
| 85 EXPECT_GT(bounds.height(), 0); | |
| 86 EXPECT_LT(bounds.height(), 200); | |
| 87 | |
| 88 // If we switch to the other tab, it should get assigned the same bounds. | |
| 89 tabbed_pane_->SelectTabAt(1); | |
| 90 EXPECT_EQ(bounds, child2->bounds()); | |
| 91 } | |
| 92 | |
| 93 TEST_F(TabbedPaneTest, AddRemove) { | |
| 94 View* tab0 = new View; | |
| 95 tabbed_pane_->AddTab(ASCIIToUTF16("tab0"), tab0); | |
| 96 EXPECT_EQ(tab0, tabbed_pane_->GetSelectedTab()); | |
| 97 EXPECT_EQ(0, tabbed_pane_->GetSelectedTabIndex()); | |
| 98 | |
| 99 // Add more 3 tabs. | |
| 100 tabbed_pane_->AddTab(ASCIIToUTF16("tab1"), new View); | |
| 101 tabbed_pane_->AddTab(ASCIIToUTF16("tab2"), new View); | |
| 102 tabbed_pane_->AddTab(ASCIIToUTF16("tab3"), new View); | |
| 103 EXPECT_EQ(4, tabbed_pane_->GetTabCount()); | |
| 104 | |
| 105 // Note: AddTab() doesn't select a tab if the tabbed pane isn't empty. | |
| 106 | |
| 107 // Select the last one. | |
| 108 tabbed_pane_->SelectTabAt(tabbed_pane_->GetTabCount() - 1); | |
| 109 EXPECT_EQ(3, tabbed_pane_->GetSelectedTabIndex()); | |
| 110 | |
| 111 // Remove the last one. | |
| 112 delete tabbed_pane_->RemoveTabAtIndex(3); | |
| 113 EXPECT_EQ(3, tabbed_pane_->GetTabCount()); | |
| 114 | |
| 115 // After removing the last tab, check if the tabbed pane selected the previous | |
| 116 // tab. | |
| 117 EXPECT_EQ(2, tabbed_pane_->GetSelectedTabIndex()); | |
| 118 | |
| 119 tabbed_pane_->AddTabAtIndex(0, ASCIIToUTF16("tab4"), new View, true); | |
| 120 | |
| 121 // Assert that even adding a new tab, the tabbed pane doesn't change the | |
| 122 // selection, i.e., it doesn't select the new one. | |
| 123 // The last tab should remains selected, instead of the tab added at index 0. | |
| 124 EXPECT_EQ(3, tabbed_pane_->GetSelectedTabIndex()); | |
| 125 | |
| 126 // Now change the selected tab. | |
| 127 tabbed_pane_->SelectTabAt(1); | |
| 128 EXPECT_EQ(1, tabbed_pane_->GetSelectedTabIndex()); | |
| 129 | |
| 130 // Remove the first one. | |
| 131 delete tabbed_pane_->RemoveTabAtIndex(0); | |
| 132 EXPECT_EQ(0, tabbed_pane_->GetSelectedTabIndex()); | |
| 133 } | |
| 134 | |
| 135 } // namespace views | |
| OLD | NEW |