Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: ui/views/controls/tabbed_pane/tabbed_pane_unittest.cc

Issue 10831009: Change the GetSelectedTab method of the NativeTabbedPaneView to return the tab contents. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: " Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/views/controls/tabbed_pane/native_tabbed_pane_views.cc ('k') | ui/views/views.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
7 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
8 #include "ui/views/controls/tabbed_pane/tabbed_pane.h" 9 #include "ui/views/controls/tabbed_pane/tabbed_pane.h"
9 #include "ui/views/test/views_test_base.h" 10 #include "ui/views/test/views_test_base.h"
10 #include "ui/views/widget/widget.h" 11 #include "ui/views/widget/widget.h"
11 #include "ui/views/widget/widget_delegate.h" 12 #include "ui/views/widget/widget_delegate.h"
12 13
13 namespace views { 14 namespace views {
14 15
15 // 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.
16 class FixedSizeView : public View { 17 class FixedSizeView : public View {
17 public: 18 public:
18 explicit FixedSizeView(const gfx::Size& size) 19 explicit FixedSizeView(const gfx::Size& size)
19 : size_(size) {} 20 : size_(size) {}
20 21
21 // Overridden from View: 22 // Overridden from View:
22 virtual gfx::Size GetPreferredSize() OVERRIDE { 23 virtual gfx::Size GetPreferredSize() OVERRIDE {
23 return size_; 24 return size_;
24 } 25 }
25 26
26 private: 27 private:
27 const gfx::Size size_; 28 const gfx::Size size_;
28 29
29 DISALLOW_COPY_AND_ASSIGN(FixedSizeView); 30 DISALLOW_COPY_AND_ASSIGN(FixedSizeView);
30 }; 31 };
31 32
32 class TabbedPaneTest : public ViewsTestBase, 33 #if defined(OS_WIN) && !defined(USE_AURA)
33 public WidgetDelegate { 34 class WidgetDelegateImpl : public WidgetDelegate {
msw 2012/07/26 19:29:30 None of this Widget and contents view wrangling sh
markusheintz_ 2012/07/26 21:33:52 The default implementation of WidgetDelegate is an
msw 2012/07/26 22:19:05 Try following the pattern in ui/views/view_unittes
35 public:
36 WidgetDelegateImpl(View* contents_view) : contents_view_(contents_view) {}
37 virtual ~WidgetDelegateImpl() {}
38
39 virtual views::View* GetContentsView() OVERRIDE {
40 return contents_view_.get();
41 }
42 virtual views::Widget* GetWidget() OVERRIDE {
43 return contents_view_->GetWidget();
44 }
45 virtual const views::Widget* GetWidget() const OVERRIDE {
46 return contents_view_->GetWidget();
47 }
48
49 private:
50 scoped_ptr<View> contents_view_;
51 };
52 #endif
53
54 class TabbedPaneTest : public ViewsTestBase {
34 public: 55 public:
35 TabbedPaneTest() {} 56 TabbedPaneTest() {}
36 57
37 TabbedPane* tabbed_pane_; 58 void TestSizeAndLayout(TabbedPane* tabbed_pane);
59
60 void TestAddRemove(TabbedPane* tabbed_pane);
61
62 TabbedPane* tabbed_pane_; // Owned by |tabbed_pane_container_|
msw 2012/07/26 19:29:30 nit: append a period.
markusheintz_ 2012/07/26 21:33:52 Done.
63
64 #if defined(OS_WIN) && !defined(USE_AURA)
65 TabbedPane* tabbed_pane_win_; // Owned by the |window_|s |WidgetDelegate|.
msw 2012/07/26 19:29:30 nit: "|window_|'s"
markusheintz_ 2012/07/26 21:33:52 Done.
66 Widget* window_;
67 #endif
38 68
39 private: 69 private:
40 virtual void SetUp() OVERRIDE { 70 virtual void SetUp() OVERRIDE {
msw 2012/07/26 19:29:30 Move this definition out-of-line.
markusheintz_ 2012/07/26 21:33:52 Done.
41 ViewsTestBase::SetUp(); 71 ViewsTestBase::SetUp();
42 tabbed_pane_ = new TabbedPane(); 72 tabbed_pane_ = new TabbedPane();
43 tabbed_pane_->set_use_native_win_control(true); 73 // In order to proper initialize the |TabbedPane| it must be added to a
msw 2012/07/26 19:29:30 nit: "properly" and "|TabbedPane|, "
markusheintz_ 2012/07/26 21:33:52 Done.
44 window_ = Widget::CreateWindowWithBounds(this, gfx::Rect(0, 0, 100, 100)); 74 // parent view (see the ViewHierarchyChanged method of the |TabbedPane|.
msw 2012/07/26 19:29:30 nit: close the parentheses.
markusheintz_ 2012/07/26 21:33:52 Done.
75 tabbed_pane_container_.reset(new View());
76 tabbed_pane_container_->AddChildView(tabbed_pane_);
77 #if defined(OS_WIN) && !defined(USE_AURA)
78 tabbed_pane_win_ = new TabbedPane();
79 tabbed_pane_win_->set_use_native_win_control(true);
80 window_ = Widget::CreateWindowWithBounds(
msw 2012/07/26 19:29:30 The Widget should be safe to use regardless of pla
81 new WidgetDelegateImpl(tabbed_pane_win_), gfx::Rect(0, 0, 100, 100));
45 window_->Show(); 82 window_->Show();
83 #endif
46 } 84 }
47 85
86 #if defined(OS_WIN) && !defined(USE_AURA)
48 virtual void TearDown() OVERRIDE { 87 virtual void TearDown() OVERRIDE {
49 window_->Close(); 88 window_->Close();
50 ViewsTestBase::TearDown(); 89 ViewsTestBase::TearDown();
51 } 90 }
91 #endif
52 92
53 virtual views::View* GetContentsView() OVERRIDE { 93 scoped_ptr<View> tabbed_pane_container_;
msw 2012/07/26 19:29:30 See the comment above about why this isn't necessa
54 return tabbed_pane_;
55 }
56 virtual views::Widget* GetWidget() OVERRIDE {
57 return tabbed_pane_->GetWidget();
58 }
59 virtual const views::Widget* GetWidget() const OVERRIDE {
60 return tabbed_pane_->GetWidget();
61 }
62
63 Widget* window_;
64 94
65 DISALLOW_COPY_AND_ASSIGN(TabbedPaneTest); 95 DISALLOW_COPY_AND_ASSIGN(TabbedPaneTest);
66 }; 96 };
67 97
68 // Tests that TabbedPane::GetPreferredSize() and TabbedPane::Layout(). 98 void TabbedPaneTest::TestSizeAndLayout(TabbedPane* tabbed_pane) {
69 TEST_F(TabbedPaneTest, SizeAndLayout) {
70 View* child1 = new FixedSizeView(gfx::Size(20, 10)); 99 View* child1 = new FixedSizeView(gfx::Size(20, 10));
71 tabbed_pane_->AddTab(ASCIIToUTF16("tab1"), child1); 100 tabbed_pane->AddTab(ASCIIToUTF16("tab1"), child1);
72 View* child2 = new FixedSizeView(gfx::Size(5, 5)); 101 View* child2 = new FixedSizeView(gfx::Size(5, 5));
73 tabbed_pane_->AddTab(ASCIIToUTF16("tab2"), child2); 102 tabbed_pane->AddTab(ASCIIToUTF16("tab2"), child2);
74 tabbed_pane_->SelectTabAt(0); 103 tabbed_pane->SelectTabAt(0);
75 104
76 // Check that the preferred size is larger than the largest child. 105 // Check that the preferred size is larger than the largest child.
77 gfx::Size pref(tabbed_pane_->GetPreferredSize()); 106 gfx::Size pref(tabbed_pane->GetPreferredSize());
78 EXPECT_GT(pref.width(), 20); 107 // The |tabbed_pane_| has no border. Therefore it should be as wide as the
108 // widest tab.
109 EXPECT_GE(pref.width(), 20);
79 EXPECT_GT(pref.height(), 10); 110 EXPECT_GT(pref.height(), 10);
80 111
81 // The bounds of our children should be smaller than the tabbed pane's bounds. 112 // The bounds of our children should be smaller than the tabbed pane's bounds.
82 tabbed_pane_->SetBounds(0, 0, 100, 200); 113 tabbed_pane->SetBounds(0, 0, 100, 200);
83 RunPendingMessages(); 114 RunPendingMessages();
84 gfx::Rect bounds(child1->bounds()); 115 gfx::Rect bounds(child1->bounds());
85 EXPECT_GT(bounds.width(), 0); 116 EXPECT_GT(bounds.width(), 0);
86 EXPECT_LT(bounds.width(), 100); 117 // The |tabbed_pane_| has no border. Therefore the children should be as wide
118 // as the |tabbed_pane_|.
119 EXPECT_LE(bounds.width(), 100);
87 EXPECT_GT(bounds.height(), 0); 120 EXPECT_GT(bounds.height(), 0);
88 EXPECT_LT(bounds.height(), 200); 121 EXPECT_LT(bounds.height(), 200);
89 122
90 // If we switch to the other tab, it should get assigned the same bounds. 123 // If we switch to the other tab, it should get assigned the same bounds.
91 tabbed_pane_->SelectTabAt(1); 124 tabbed_pane->SelectTabAt(1);
92 EXPECT_EQ(bounds, child2->bounds()); 125 EXPECT_EQ(bounds, child2->bounds());
93 } 126 }
94 127
95 TEST_F(TabbedPaneTest, AddRemove) { 128 void TabbedPaneTest::TestAddRemove(TabbedPane* tabbed_pane) {
96 View* tab0 = new View; 129 View* tab0 = new View;
97 tabbed_pane_->AddTab(ASCIIToUTF16("tab0"), tab0); 130 tabbed_pane->AddTab(ASCIIToUTF16("tab0"), tab0);
98 EXPECT_EQ(tab0, tabbed_pane_->GetSelectedTab()); 131 EXPECT_EQ(tab0, tabbed_pane->GetSelectedTab());
99 EXPECT_EQ(0, tabbed_pane_->GetSelectedTabIndex()); 132 EXPECT_EQ(0, tabbed_pane->GetSelectedTabIndex());
100 133
101 // Add more 3 tabs. 134 // Add more 3 tabs.
102 tabbed_pane_->AddTab(ASCIIToUTF16("tab1"), new View); 135 tabbed_pane->AddTab(ASCIIToUTF16("tab1"), new View);
103 tabbed_pane_->AddTab(ASCIIToUTF16("tab2"), new View); 136 tabbed_pane->AddTab(ASCIIToUTF16("tab2"), new View);
104 tabbed_pane_->AddTab(ASCIIToUTF16("tab3"), new View); 137 tabbed_pane->AddTab(ASCIIToUTF16("tab3"), new View);
105 EXPECT_EQ(4, tabbed_pane_->GetTabCount()); 138 EXPECT_EQ(4, tabbed_pane->GetTabCount());
106 139
107 // Note: AddTab() doesn't select a tab if the tabbed pane isn't empty. 140 // Note: AddTab() doesn't select a tab if the tabbed pane isn't empty.
108 141
109 // Select the last one. 142 // Select the last one.
110 tabbed_pane_->SelectTabAt(tabbed_pane_->GetTabCount() - 1); 143 tabbed_pane->SelectTabAt(tabbed_pane->GetTabCount() - 1);
111 EXPECT_EQ(3, tabbed_pane_->GetSelectedTabIndex()); 144 EXPECT_EQ(3, tabbed_pane->GetSelectedTabIndex());
112 145
113 // Remove the last one. 146 // Remove the last one.
114 delete tabbed_pane_->RemoveTabAtIndex(3); 147 delete tabbed_pane->RemoveTabAtIndex(3);
115 EXPECT_EQ(3, tabbed_pane_->GetTabCount()); 148 EXPECT_EQ(3, tabbed_pane->GetTabCount());
116 149
117 // After removing the last tab, check if the tabbed pane selected the previous 150 // After removing the last tab, check if the tabbed pane selected the previous
118 // tab. 151 // tab.
119 EXPECT_EQ(2, tabbed_pane_->GetSelectedTabIndex()); 152 EXPECT_EQ(2, tabbed_pane->GetSelectedTabIndex());
120 153
121 tabbed_pane_->AddTabAtIndex(0, ASCIIToUTF16("tab4"), new View, true); 154 tabbed_pane->AddTabAtIndex(0, ASCIIToUTF16("tab4"), new View, true);
122 155
123 // Assert that even adding a new tab, the tabbed pane doesn't change the 156 // Assert that even adding a new tab, the tabbed pane doesn't change the
124 // selection, i.e., it doesn't select the new one. 157 // selection, i.e., it doesn't select the new one.
125 // The last tab should remains selected, instead of the tab added at index 0. 158 // The last tab should remains selected, instead of the tab added at index 0.
126 EXPECT_EQ(3, tabbed_pane_->GetSelectedTabIndex()); 159 EXPECT_EQ(3, tabbed_pane->GetSelectedTabIndex());
127 160
128 // Now change the selected tab. 161 // Now change the selected tab.
129 tabbed_pane_->SelectTabAt(1); 162 tabbed_pane->SelectTabAt(1);
130 EXPECT_EQ(1, tabbed_pane_->GetSelectedTabIndex()); 163 EXPECT_EQ(1, tabbed_pane->GetSelectedTabIndex());
131 164
132 // Remove the first one. 165 // Remove the first one.
133 delete tabbed_pane_->RemoveTabAtIndex(0); 166 delete tabbed_pane->RemoveTabAtIndex(0);
134 EXPECT_EQ(0, tabbed_pane_->GetSelectedTabIndex()); 167 EXPECT_EQ(0, tabbed_pane->GetSelectedTabIndex());
168 }
169
170 // Tests that TabbedPane::GetPreferredSize() and TabbedPane::Layout().
171 TEST_F(TabbedPaneTest, SizeAndLayout) {
172 TestSizeAndLayout(tabbed_pane_);
173 #if defined(OS_WIN) && !defined(USE_AURA)
msw 2012/07/26 19:29:30 Please add a comment here (or maybe somewhere else
markusheintz_ 2012/07/26 21:33:52 Done.
174 TestSizeAndLayout(tabbed_pane_win_);
175 #endif
176 }
177
178 TEST_F(TabbedPaneTest, AddRemove) {
179 TestAddRemove(tabbed_pane_);
180 #if defined(OS_WIN) && !defined(USE_AURA)
181 TestAddRemove(tabbed_pane_win_);
182 #endif
135 } 183 }
136 184
137 } // namespace views 185 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/tabbed_pane/native_tabbed_pane_views.cc ('k') | ui/views/views.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698