Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / views / controls / tabbed_pane / tabbed_pane_unittest.cc
blob72ea86caf0064ae6553b93f1b45ce5bc8cb0f279
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.
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"
12 using base::ASCIIToUTF16;
14 namespace views {
16 namespace {
18 // A view for testing that takes a fixed preferred size upon construction.
19 class FixedSizeView : public View {
20 public:
21 explicit FixedSizeView(const gfx::Size& size)
22 : size_(size) {}
24 // Overridden from View:
25 gfx::Size GetPreferredSize() const override { return size_; }
27 private:
28 const gfx::Size size_;
30 DISALLOW_COPY_AND_ASSIGN(FixedSizeView);
33 typedef ViewsTestBase TabbedPaneTest;
35 // Tests TabbedPane::GetPreferredSize() and TabbedPane::Layout().
36 TEST_F(TabbedPaneTest, SizeAndLayout) {
37 scoped_ptr<TabbedPane> tabbed_pane(new TabbedPane());
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);
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);
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);
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());
68 TEST_F(TabbedPaneTest, AddAndSelect) {
69 scoped_ptr<TabbedPane> tabbed_pane(new TabbedPane());
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());
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());
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());
91 } // namespace
93 } // namespace views