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

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: Address comments (msw): Remove WidgetDelegateImpl. 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 class TabbedPaneTest : public ViewsTestBase {
33 public WidgetDelegate {
34 public: 34 public:
35 TabbedPaneTest() {} 35 TabbedPaneTest() {}
36 36
37 TabbedPane* tabbed_pane_; 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.
42
43 #if defined(OS_WIN) && !defined(USE_AURA)
44 TabbedPane* tabbed_pane_win_; // Owned by the |widget_|'s root View.
45 #endif
38 46
39 private: 47 private:
40 virtual void SetUp() OVERRIDE { 48 virtual void SetUp() OVERRIDE;
41 ViewsTestBase::SetUp();
42 tabbed_pane_ = new TabbedPane();
43 tabbed_pane_->set_use_native_win_control(true);
44 window_ = Widget::CreateWindowWithBounds(this, gfx::Rect(0, 0, 100, 100));
45 window_->Show();
46 }
47 49
48 virtual void TearDown() OVERRIDE { 50 scoped_ptr<Widget> widget_;
49 window_->Close();
50 ViewsTestBase::TearDown();
51 }
52
53 virtual views::View* GetContentsView() OVERRIDE {
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 51
65 DISALLOW_COPY_AND_ASSIGN(TabbedPaneTest); 52 DISALLOW_COPY_AND_ASSIGN(TabbedPaneTest);
66 }; 53 };
67 54
68 // Tests that TabbedPane::GetPreferredSize() and TabbedPane::Layout(). 55 void TabbedPaneTest::SetUp() {
69 TEST_F(TabbedPaneTest, SizeAndLayout) { 56 ViewsTestBase::SetUp();
57 tabbed_pane_ = new TabbedPane();
58 widget_.reset(new Widget());
59 Widget::InitParams params(Widget::InitParams::TYPE_POPUP);
60 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
61 params.bounds = gfx::Rect(0, 0, 100, 100);
62 widget_->Init(params);
63 // In order to properly initialize the |TabbedPane| it must be added to a
64 // parent view (see the ViewHierarchyChanged method of the |TabbedPane|).
65 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 }
73
74 void TabbedPaneTest::TestSizeAndLayout(TabbedPane* tabbed_pane) {
70 View* child1 = new FixedSizeView(gfx::Size(20, 10)); 75 View* child1 = new FixedSizeView(gfx::Size(20, 10));
71 tabbed_pane_->AddTab(ASCIIToUTF16("tab1"), child1); 76 tabbed_pane->AddTab(ASCIIToUTF16("tab1"), child1);
72 View* child2 = new FixedSizeView(gfx::Size(5, 5)); 77 View* child2 = new FixedSizeView(gfx::Size(5, 5));
73 tabbed_pane_->AddTab(ASCIIToUTF16("tab2"), child2); 78 tabbed_pane->AddTab(ASCIIToUTF16("tab2"), child2);
74 tabbed_pane_->SelectTabAt(0); 79 tabbed_pane->SelectTabAt(0);
75 80
76 // Check that the preferred size is larger than the largest child. 81 // Check that the preferred size is larger than the largest child.
77 gfx::Size pref(tabbed_pane_->GetPreferredSize()); 82 gfx::Size pref(tabbed_pane->GetPreferredSize());
78 EXPECT_GT(pref.width(), 20); 83 // The |tabbed_pane_| has no border. Therefore it should be as wide as the
msw 2012/07/27 18:09:18 Sorry for changing my advice, but this comment sho
markusheintz_ 2012/07/30 10:15:44 Uups I merged the two comments. It's not possible
84 // widest tab.
85 EXPECT_GE(pref.width(), 20);
79 EXPECT_GT(pref.height(), 10); 86 EXPECT_GT(pref.height(), 10);
80 87
81 // The bounds of our children should be smaller than the tabbed pane's bounds. 88 // The bounds of our children should be smaller than the tabbed pane's bounds.
82 tabbed_pane_->SetBounds(0, 0, 100, 200); 89 tabbed_pane->SetBounds(0, 0, 100, 200);
83 RunPendingMessages(); 90 RunPendingMessages();
84 gfx::Rect bounds(child1->bounds()); 91 gfx::Rect bounds(child1->bounds());
85 EXPECT_GT(bounds.width(), 0); 92 EXPECT_GT(bounds.width(), 0);
86 EXPECT_LT(bounds.width(), 100); 93 // The |tabbed_pane_| has no border. Therefore the children should be as wide
94 // as the |tabbed_pane_|.
95 EXPECT_LE(bounds.width(), 100);
87 EXPECT_GT(bounds.height(), 0); 96 EXPECT_GT(bounds.height(), 0);
88 EXPECT_LT(bounds.height(), 200); 97 EXPECT_LT(bounds.height(), 200);
89 98
90 // If we switch to the other tab, it should get assigned the same bounds. 99 // If we switch to the other tab, it should get assigned the same bounds.
91 tabbed_pane_->SelectTabAt(1); 100 tabbed_pane->SelectTabAt(1);
92 EXPECT_EQ(bounds, child2->bounds()); 101 EXPECT_EQ(bounds, child2->bounds());
93 } 102 }
94 103
95 TEST_F(TabbedPaneTest, AddRemove) { 104 void TabbedPaneTest::TestAddRemove(TabbedPane* tabbed_pane) {
96 View* tab0 = new View; 105 View* tab0 = new View;
97 tabbed_pane_->AddTab(ASCIIToUTF16("tab0"), tab0); 106 tabbed_pane->AddTab(ASCIIToUTF16("tab0"), tab0);
98 EXPECT_EQ(tab0, tabbed_pane_->GetSelectedTab()); 107 EXPECT_EQ(tab0, tabbed_pane->GetSelectedTab());
99 EXPECT_EQ(0, tabbed_pane_->GetSelectedTabIndex()); 108 EXPECT_EQ(0, tabbed_pane->GetSelectedTabIndex());
100 109
101 // Add more 3 tabs. 110 // Add more 3 tabs.
102 tabbed_pane_->AddTab(ASCIIToUTF16("tab1"), new View); 111 tabbed_pane->AddTab(ASCIIToUTF16("tab1"), new View);
103 tabbed_pane_->AddTab(ASCIIToUTF16("tab2"), new View); 112 tabbed_pane->AddTab(ASCIIToUTF16("tab2"), new View);
104 tabbed_pane_->AddTab(ASCIIToUTF16("tab3"), new View); 113 tabbed_pane->AddTab(ASCIIToUTF16("tab3"), new View);
105 EXPECT_EQ(4, tabbed_pane_->GetTabCount()); 114 EXPECT_EQ(4, tabbed_pane->GetTabCount());
106 115
107 // Note: AddTab() doesn't select a tab if the tabbed pane isn't empty. 116 // Note: AddTab() doesn't select a tab if the tabbed pane isn't empty.
108 117
109 // Select the last one. 118 // Select the last one.
110 tabbed_pane_->SelectTabAt(tabbed_pane_->GetTabCount() - 1); 119 tabbed_pane->SelectTabAt(tabbed_pane->GetTabCount() - 1);
111 EXPECT_EQ(3, tabbed_pane_->GetSelectedTabIndex()); 120 EXPECT_EQ(3, tabbed_pane->GetSelectedTabIndex());
112 121
113 // Remove the last one. 122 // Remove the last one.
114 delete tabbed_pane_->RemoveTabAtIndex(3); 123 delete tabbed_pane->RemoveTabAtIndex(3);
115 EXPECT_EQ(3, tabbed_pane_->GetTabCount()); 124 EXPECT_EQ(3, tabbed_pane->GetTabCount());
116 125
117 // After removing the last tab, check if the tabbed pane selected the previous 126 // After removing the last tab, check if the tabbed pane selected the previous
118 // tab. 127 // tab.
119 EXPECT_EQ(2, tabbed_pane_->GetSelectedTabIndex()); 128 EXPECT_EQ(2, tabbed_pane->GetSelectedTabIndex());
120 129
121 tabbed_pane_->AddTabAtIndex(0, ASCIIToUTF16("tab4"), new View, true); 130 tabbed_pane->AddTabAtIndex(0, ASCIIToUTF16("tab4"), new View, true);
122 131
123 // Assert that even adding a new tab, the tabbed pane doesn't change the 132 // 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. 133 // 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. 134 // The last tab should remains selected, instead of the tab added at index 0.
126 EXPECT_EQ(3, tabbed_pane_->GetSelectedTabIndex()); 135 EXPECT_EQ(3, tabbed_pane->GetSelectedTabIndex());
127 136
128 // Now change the selected tab. 137 // Now change the selected tab.
129 tabbed_pane_->SelectTabAt(1); 138 tabbed_pane->SelectTabAt(1);
130 EXPECT_EQ(1, tabbed_pane_->GetSelectedTabIndex()); 139 EXPECT_EQ(1, tabbed_pane->GetSelectedTabIndex());
131 140
132 // Remove the first one. 141 // Remove the first one.
133 delete tabbed_pane_->RemoveTabAtIndex(0); 142 delete tabbed_pane->RemoveTabAtIndex(0);
134 EXPECT_EQ(0, tabbed_pane_->GetSelectedTabIndex()); 143 EXPECT_EQ(0, tabbed_pane->GetSelectedTabIndex());
144 }
145
146 // Tests that TabbedPane::GetPreferredSize() and TabbedPane::Layout().
msw 2012/07/27 18:09:18 nit: remove "that" or extrapolate.
markusheintz_ 2012/07/30 10:15:44 Done.
147 TEST_F(TabbedPaneTest, SizeAndLayout) {
148 TestSizeAndLayout(tabbed_pane_);
149 // TODO(markusheintz): Once replacing NativeTabbedPaneWin with
150 // NativeTabbedPaneView is completed (http://crbug.com/138059), then the
151 // TestSizeAndLayout method should be inlined here again and the "ifdef" part
152 // should be deleted.
153 #if defined(OS_WIN) && !defined(USE_AURA)
154 TestSizeAndLayout(tabbed_pane_win_);
155 #endif
156 }
157
158 TEST_F(TabbedPaneTest, AddRemove) {
159 TestAddRemove(tabbed_pane_);
160 // TODO(markusheintz): Once replacing NativeTabbedPaneWin with
161 // NativeTabbedPaneView is completed (http://crbug.com/138059), then the
162 // TestAddRemove method should be inlined here again and the "ifdef" part
163 // should be deleted.
164 #if defined(OS_WIN) && !defined(USE_AURA)
165 TestAddRemove(tabbed_pane_win_);
166 #endif
135 } 167 }
136 168
137 } // namespace views 169 } // 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