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 3 // found in the LICENSE file. 4 5 #include "base/memory/scoped_ptr.h" 6 #include "base/message_loop/message_loop.h" 7 #include "base/strings/utf_string_conversions.h" 8 #include "testing/gtest/include/gtest/gtest.h" 9 #include "ui/views/controls/tabbed_pane/tabbed_pane.h" 10 #include "ui/views/test/views_test_base.h" 11 12 namespace views { 13 14 namespace { 15 16 // A view for testing that takes a fixed preferred size upon construction. 17 class FixedSizeView : public View { 18 public: 19 explicit FixedSizeView(const gfx::Size& size) 20 : size_(size) {} 21 22 // Overridden from View: 23 virtual gfx::Size GetPreferredSize() OVERRIDE { 24 return size_; 25 } 26 27 private: 28 const gfx::Size size_; 29 30 DISALLOW_COPY_AND_ASSIGN(FixedSizeView); 31 }; 32 33 typedef ViewsTestBase TabbedPaneTest; 34 35 // Tests TabbedPane::GetPreferredSize() and TabbedPane::Layout(). 36 TEST_F(TabbedPaneTest, SizeAndLayout) { 37 scoped_ptr<TabbedPane> tabbed_pane(new TabbedPane(false)); 38 View* child1 = new FixedSizeView(gfx::Size(20, 10)); 39 tabbed_pane->AddTab(ASCIIToUTF16("tab1"), child1); 40 View* child2 = new FixedSizeView(gfx::Size(5, 5)); 41 tabbed_pane->AddTab(ASCIIToUTF16("tab2"), child2); 42 tabbed_pane->SelectTabAt(0); 43 44 // 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 46 // tabbed pane has a border that used up extra space. Therefore the preferred 47 // width is larger than the largest child. 48 gfx::Size pref(tabbed_pane->GetPreferredSize()); 49 EXPECT_GE(pref.width(), 20); 50 EXPECT_GT(pref.height(), 10); 51 52 // The bounds of our children should be smaller than the tabbed pane's bounds. 53 tabbed_pane->SetBounds(0, 0, 100, 200); 54 RunPendingMessages(); 55 gfx::Rect bounds(child1->bounds()); 56 EXPECT_GT(bounds.width(), 0); 57 // The |tabbed_pane| has no border. Therefore the children should be as wide 58 // as the |tabbed_pane|. 59 EXPECT_LE(bounds.width(), 100); 60 EXPECT_GT(bounds.height(), 0); 61 EXPECT_LT(bounds.height(), 200); 62 63 // If we switch to the other tab, it should get assigned the same bounds. 64 tabbed_pane->SelectTabAt(1); 65 EXPECT_EQ(bounds, child2->bounds()); 66 } 67 68 TEST_F(TabbedPaneTest, AddAndSelect) { 69 scoped_ptr<TabbedPane> tabbed_pane(new TabbedPane(false)); 70 // Add several tabs; only the first should be a selected automatically. 71 for (int i = 0; i < 3; ++i) { 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 } 77 78 // Select each tab. 79 for (int i = 0; i < tabbed_pane->GetTabCount(); ++i) { 80 tabbed_pane->SelectTabAt(i); 81 EXPECT_EQ(i, tabbed_pane->selected_tab_index()); 82 } 83 84 // Add a tab at index 0, it should not be selected automatically. 85 View* tab0 = new View(); 86 tabbed_pane->AddTabAtIndex(0, ASCIIToUTF16("tab0"), tab0); 87 EXPECT_NE(tab0, tabbed_pane->GetSelectedTab()); 88 EXPECT_NE(0, tabbed_pane->selected_tab_index()); 89 } 90 91 } // namespace 92 93 } // namespace views 94