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

Side by Side Diff: chrome/browser/ui/cocoa/toolbar/toolbar_controller_unittest.mm

Issue 1305143008: [Mac] Implement LocationBarViewMac::UpdateLocationBarVisibility() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing reviewer feedback Created 5 years, 1 month 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
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 #import <Cocoa/Cocoa.h> 5 #import <Cocoa/Cocoa.h>
6 6
7 #import "base/mac/scoped_nsobject.h" 7 #import "base/mac/scoped_nsobject.h"
8 #include "base/prefs/pref_service.h" 8 #include "base/prefs/pref_service.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "chrome/app/chrome_command_ids.h" 10 #include "chrome/app/chrome_command_ids.h"
11 #include "chrome/browser/command_updater.h" 11 #include "chrome/browser/command_updater.h"
12 #include "chrome/browser/ui/browser.h" 12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_command_controller.h" 13 #include "chrome/browser/ui/browser_command_controller.h"
14 #include "chrome/browser/ui/browser_commands.h" 14 #include "chrome/browser/ui/browser_commands.h"
15 #include "chrome/browser/ui/browser_list.h" 15 #include "chrome/browser/ui/browser_list.h"
16 #include "chrome/browser/ui/browser_list_observer.h" 16 #include "chrome/browser/ui/browser_list_observer.h"
17 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" 17 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
18 #import "chrome/browser/ui/cocoa/image_button_cell.h" 18 #import "chrome/browser/ui/cocoa/image_button_cell.h"
19 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" 19 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
20 #import "chrome/browser/ui/cocoa/view_resizer_pong.h"
20 #include "chrome/common/pref_names.h" 21 #include "chrome/common/pref_names.h"
21 #include "chrome/test/base/testing_profile.h" 22 #include "chrome/test/base/testing_profile.h"
22 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
23 #import "testing/gtest_mac.h" 24 #import "testing/gtest_mac.h"
24 #include "testing/platform_test.h" 25 #include "testing/platform_test.h"
25 26
26 // An NSView that fakes out hitTest:. 27 // An NSView that fakes out hitTest:.
27 @interface HitView : NSView { 28 @interface HitView : NSView {
28 id hitTestReturn_; 29 id hitTestReturn_;
29 } 30 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 } 63 }
63 @end 64 @end
64 65
65 namespace { 66 namespace {
66 67
67 class ToolbarControllerTest : public CocoaProfileTest { 68 class ToolbarControllerTest : public CocoaProfileTest {
68 public: 69 public:
69 70
70 // Indexes that match the ordering returned by the private ToolbarController 71 // Indexes that match the ordering returned by the private ToolbarController
71 // |-toolbarViews| method. 72 // |-toolbarViews| method.
72 enum { 73 enum SubviewIndex {
73 kBackIndex, kForwardIndex, kReloadIndex, kHomeIndex, 74 kBackIndex, kForwardIndex, kReloadIndex, kHomeIndex,
74 kWrenchIndex, kLocationIndex, kBrowserActionContainerViewIndex 75 kWrenchIndex, kLocationIndex, kBrowserActionContainerViewIndex
75 }; 76 };
76 77
77 void SetUp() override { 78 void SetUp() override {
78 CocoaProfileTest::SetUp(); 79 CocoaProfileTest::SetUp();
79 ASSERT_TRUE(browser()); 80 ASSERT_TRUE(browser());
80 81
82 resizeDelegate_.reset([[ViewResizerPong alloc] init]);
83
81 CommandUpdater* updater = 84 CommandUpdater* updater =
82 browser()->command_controller()->command_updater(); 85 browser()->command_controller()->command_updater();
83 // The default state for the commands is true, set a couple to false to 86 // The default state for the commands is true, set a couple to false to
84 // ensure they get picked up correct on initialization 87 // ensure they get picked up correct on initialization
85 updater->UpdateCommandEnabled(IDC_BACK, false); 88 updater->UpdateCommandEnabled(IDC_BACK, false);
86 updater->UpdateCommandEnabled(IDC_FORWARD, false); 89 updater->UpdateCommandEnabled(IDC_FORWARD, false);
87 bar_.reset([[TestToolbarController alloc] 90 bar_.reset([[TestToolbarController alloc]
88 initWithCommands:browser()->command_controller()->command_updater() 91 initWithCommands:browser()->command_controller()->command_updater()
89 profile:profile() 92 profile:profile()
90 browser:browser()]); 93 browser:browser()
94 resizeDelegate:resizeDelegate_.get()]);
91 EXPECT_TRUE([bar_ view]); 95 EXPECT_TRUE([bar_ view]);
92 NSView* parent = [test_window() contentView]; 96 NSView* parent = [test_window() contentView];
93 [parent addSubview:[bar_ view]]; 97 [parent addSubview:[bar_ view]];
94 } 98 }
95 99
96 void TearDown() override { 100 void TearDown() override {
97 bar_.reset(); // browser() must outlive the ToolbarController. 101 bar_.reset(); // browser() must outlive the ToolbarController.
98 CocoaProfileTest::TearDown(); 102 CocoaProfileTest::TearDown();
99 } 103 }
100 104
101 // Make sure the enabled state of the view is the same as the corresponding 105 // Make sure the enabled state of the view is the same as the corresponding
102 // command in the updater. The views are in the declaration order of outlets. 106 // command in the updater. The views are in the declaration order of outlets.
103 void CompareState(CommandUpdater* updater, NSArray* views) { 107 void CompareState(CommandUpdater* updater, NSArray* views) {
104 EXPECT_EQ(updater->IsCommandEnabled(IDC_BACK), 108 EXPECT_EQ(updater->IsCommandEnabled(IDC_BACK),
105 [[views objectAtIndex:kBackIndex] isEnabled] ? true : false); 109 [[views objectAtIndex:kBackIndex] isEnabled] ? true : false);
106 EXPECT_EQ(updater->IsCommandEnabled(IDC_FORWARD), 110 EXPECT_EQ(updater->IsCommandEnabled(IDC_FORWARD),
107 [[views objectAtIndex:kForwardIndex] isEnabled] ? true : false); 111 [[views objectAtIndex:kForwardIndex] isEnabled] ? true : false);
108 EXPECT_EQ(updater->IsCommandEnabled(IDC_RELOAD), 112 EXPECT_EQ(updater->IsCommandEnabled(IDC_RELOAD),
109 [[views objectAtIndex:kReloadIndex] isEnabled] ? true : false); 113 [[views objectAtIndex:kReloadIndex] isEnabled] ? true : false);
110 EXPECT_EQ(updater->IsCommandEnabled(IDC_HOME), 114 EXPECT_EQ(updater->IsCommandEnabled(IDC_HOME),
111 [[views objectAtIndex:kHomeIndex] isEnabled] ? true : false); 115 [[views objectAtIndex:kHomeIndex] isEnabled] ? true : false);
112 } 116 }
113 117
118 NSView* GetSubviewAt(SubviewIndex index) {
119 return [[bar_ toolbarViews] objectAtIndex:index];
120 }
121
122 base::scoped_nsobject<ViewResizerPong> resizeDelegate_;
114 base::scoped_nsobject<TestToolbarController> bar_; 123 base::scoped_nsobject<TestToolbarController> bar_;
115 }; 124 };
116 125
117 TEST_VIEW(ToolbarControllerTest, [bar_ view]) 126 TEST_VIEW(ToolbarControllerTest, [bar_ view])
118 127
119 // Test the initial state that everything is sync'd up 128 // Test the initial state that everything is sync'd up
120 TEST_F(ToolbarControllerTest, InitialState) { 129 TEST_F(ToolbarControllerTest, InitialState) {
121 CommandUpdater* updater = browser()->command_controller()->command_updater(); 130 CommandUpdater* updater = browser()->command_controller()->command_updater();
122 CompareState(updater, [bar_ toolbarViews]); 131 CompareState(updater, [bar_ toolbarViews]);
123 } 132 }
124 133
125 // Make sure a "titlebar only" toolbar with location bar works. 134 // Make sure a "titlebar only" toolbar with location bar works.
126 TEST_F(ToolbarControllerTest, TitlebarOnly) { 135 TEST_F(ToolbarControllerTest, TitlebarOnly) {
127 NSView* view = [bar_ view]; 136 NSView* view = [bar_ view];
128 137
129 [bar_ setHasToolbar:NO hasLocationBar:YES]; 138 [bar_ setHasToolbar:NO hasLocationBar:YES];
130 EXPECT_NE(view, [bar_ view]); 139 EXPECT_EQ(view, [bar_ view]);
131 140
132 // Simulate a popup going fullscreen and back by performing the reparenting 141 // Simulate a popup going fullscreen and back by performing the reparenting
133 // that happens during fullscreen transitions 142 // that happens during fullscreen transitions
134 NSView* superview = [view superview]; 143 NSView* superview = [view superview];
135 [view removeFromSuperview]; 144 [view removeFromSuperview];
136 [superview addSubview:view]; 145 [superview addSubview:view];
137 146
138 [bar_ setHasToolbar:YES hasLocationBar:YES]; 147 [bar_ setHasToolbar:YES hasLocationBar:YES];
tapted 2015/10/26 07:55:34 Hm - does this make sense? I think we say that the
dominickn 2015/10/26 23:31:32 From the blame and log, it doesn't look like it's
139 EXPECT_EQ(view, [bar_ view]); 148 EXPECT_EQ(view, [bar_ view]);
140 149
141 // Leave it off to make sure that's fine 150 // Leave it off to make sure that's fine
142 [bar_ setHasToolbar:NO hasLocationBar:YES]; 151 [bar_ setHasToolbar:NO hasLocationBar:YES];
143 } 152 }
144 153
154 // Test updateVisibility with location bar only; this method is used by bookmark
155 // apps, and should never be called when the toolbar is enabled. Ensure that the
156 // buttons remain in the correct state.
157 TEST_F(ToolbarControllerTest, UpdateVisibility) {
158 NSView* view = [bar_ view];
159
160 // Test the escapable states first.
161 [bar_ setHasToolbar:YES hasLocationBar:YES];
162 EXPECT_FALSE([view isHidden]);
163 EXPECT_FALSE([GetSubviewAt(kLocationIndex) isHidden]);
164 EXPECT_FALSE([GetSubviewAt(kBackIndex) isHidden]);
165 EXPECT_FALSE([GetSubviewAt(kForwardIndex) isHidden]);
166 EXPECT_FALSE([GetSubviewAt(kReloadIndex) isHidden]);
167 EXPECT_FALSE([GetSubviewAt(kWrenchIndex) isHidden]);
168 EXPECT_TRUE([GetSubviewAt(kHomeIndex) isHidden]);
169 EXPECT_TRUE([GetSubviewAt(kBrowserActionContainerViewIndex) isHidden]);
170
171 // For NO/NO, only the top level toolbar view is hidden.
172 [bar_ setHasToolbar:NO hasLocationBar:NO];
173 EXPECT_TRUE([view isHidden]);
174 EXPECT_FALSE([GetSubviewAt(kLocationIndex) isHidden]);
175 EXPECT_FALSE([GetSubviewAt(kBackIndex) isHidden]);
176 EXPECT_FALSE([GetSubviewAt(kForwardIndex) isHidden]);
177 EXPECT_FALSE([GetSubviewAt(kReloadIndex) isHidden]);
178 EXPECT_FALSE([GetSubviewAt(kWrenchIndex) isHidden]);
179 EXPECT_TRUE([GetSubviewAt(kHomeIndex) isHidden]);
180 EXPECT_TRUE([GetSubviewAt(kBrowserActionContainerViewIndex) isHidden]);
181
182 [bar_ setHasToolbar:NO hasLocationBar:YES];
183 EXPECT_FALSE([view isHidden]);
184 EXPECT_FALSE([GetSubviewAt(kLocationIndex) isHidden]);
185 EXPECT_TRUE([GetSubviewAt(kBackIndex) isHidden]);
186 EXPECT_TRUE([GetSubviewAt(kForwardIndex) isHidden]);
187 EXPECT_TRUE([GetSubviewAt(kReloadIndex) isHidden]);
188 EXPECT_TRUE([GetSubviewAt(kWrenchIndex) isHidden]);
189 EXPECT_TRUE([GetSubviewAt(kHomeIndex) isHidden]);
190 EXPECT_TRUE([GetSubviewAt(kBrowserActionContainerViewIndex) isHidden]);
191
192 // Maintain visible state.
193 [bar_ updateVisibility:YES withAnimation:NO];
194 EXPECT_GT([[bar_ view] frame].size.height, 0);
195 EXPECT_FALSE([view isHidden]);
196 EXPECT_FALSE([GetSubviewAt(kLocationIndex) isHidden]);
197 EXPECT_TRUE([GetSubviewAt(kBackIndex) isHidden]);
198 EXPECT_TRUE([GetSubviewAt(kForwardIndex) isHidden]);
199 EXPECT_TRUE([GetSubviewAt(kReloadIndex) isHidden]);
200 EXPECT_TRUE([GetSubviewAt(kWrenchIndex) isHidden]);
201 EXPECT_TRUE([GetSubviewAt(kHomeIndex) isHidden]);
202 EXPECT_TRUE([GetSubviewAt(kBrowserActionContainerViewIndex) isHidden]);
203
204 // Hide the toolbar and ensure it is not visible.
205 [bar_ updateVisibility:NO withAnimation:NO];
206 EXPECT_TRUE([view isHidden]);
207 EXPECT_EQ(0, [resizeDelegate_ height]);
208 EXPECT_EQ(0, [[bar_ view] frame].size.height);
209
210 // Try to show the home button.
211 [bar_ showOptionalHomeButton];
212
213 // Re-show the bar. Buttons should remain hidden, including the home button.
214 [bar_ updateVisibility:YES withAnimation:NO];
215 EXPECT_GT([resizeDelegate_ height], 0);
216 EXPECT_GT([[bar_ view] frame].size.height, 0);
217 EXPECT_FALSE([view isHidden]);
218 EXPECT_FALSE([GetSubviewAt(kLocationIndex) isHidden]);
219 EXPECT_TRUE([GetSubviewAt(kBackIndex) isHidden]);
220 EXPECT_TRUE([GetSubviewAt(kForwardIndex) isHidden]);
221 EXPECT_TRUE([GetSubviewAt(kReloadIndex) isHidden]);
222 EXPECT_TRUE([GetSubviewAt(kWrenchIndex) isHidden]);
223 EXPECT_TRUE([GetSubviewAt(kHomeIndex) isHidden]);
224 EXPECT_TRUE([GetSubviewAt(kBrowserActionContainerViewIndex) isHidden]);
225 }
226
145 // Make sure it works in the completely undecorated case. 227 // Make sure it works in the completely undecorated case.
146 TEST_F(ToolbarControllerTest, NoLocationBar) { 228 TEST_F(ToolbarControllerTest, NoLocationBar) {
147 NSView* view = [bar_ view]; 229 NSView* view = [bar_ view];
148 230
149 [bar_ setHasToolbar:NO hasLocationBar:NO]; 231 [bar_ setHasToolbar:NO hasLocationBar:NO];
150 EXPECT_NE(view, [bar_ view]);
151 EXPECT_TRUE([[bar_ view] isHidden]); 232 EXPECT_TRUE([[bar_ view] isHidden]);
152 233
153 // Simulate a popup going fullscreen and back by performing the reparenting 234 // Simulate a popup going fullscreen and back by performing the reparenting
154 // that happens during fullscreen transitions 235 // that happens during fullscreen transitions
155 NSView* superview = [view superview]; 236 NSView* superview = [view superview];
156 [view removeFromSuperview]; 237 [view removeFromSuperview];
157 [superview addSubview:view]; 238 [superview addSubview:view];
158 } 239 }
159 240
160 // Make some changes to the enabled state of a few of the buttons and ensure 241 // Make some changes to the enabled state of a few of the buttons and ensure
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 BrowserRemovedObserver observer; 379 BrowserRemovedObserver observer;
299 // This is normally called by BrowserWindowController, but since |bar_| is not 380 // This is normally called by BrowserWindowController, but since |bar_| is not
300 // owned by one, call it here. 381 // owned by one, call it here.
301 [bar_ browserWillBeDestroyed]; 382 [bar_ browserWillBeDestroyed];
302 CloseBrowserWindow(); 383 CloseBrowserWindow();
303 observer.WaitUntilBrowserRemoved(); 384 observer.WaitUntilBrowserRemoved();
304 // |bar_| is released in TearDown(). 385 // |bar_| is released in TearDown().
305 } 386 }
306 387
307 } // namespace 388 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698