OLD | NEW |
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 #include "base/command_line.h" | |
8 #import "base/mac/scoped_nsobject.h" | 7 #import "base/mac/scoped_nsobject.h" |
9 #include "base/macros.h" | 8 #include "base/macros.h" |
10 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
11 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
12 #include "chrome/browser/command_updater.h" | 11 #include "chrome/browser/command_updater.h" |
13 #include "chrome/browser/extensions/extension_action_test_util.h" | |
14 #include "chrome/browser/extensions/extension_service.h" | |
15 #include "chrome/browser/extensions/test_extension_system.h" | |
16 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
17 #include "chrome/browser/ui/browser_command_controller.h" | 13 #include "chrome/browser/ui/browser_command_controller.h" |
18 #include "chrome/browser/ui/browser_commands.h" | 14 #include "chrome/browser/ui/browser_commands.h" |
19 #include "chrome/browser/ui/browser_list.h" | 15 #include "chrome/browser/ui/browser_list.h" |
20 #include "chrome/browser/ui/browser_list_observer.h" | 16 #include "chrome/browser/ui/browser_list_observer.h" |
21 #import "chrome/browser/ui/cocoa/image_button_cell.h" | 17 #import "chrome/browser/ui/cocoa/image_button_cell.h" |
22 #import "chrome/browser/ui/cocoa/extensions/browser_actions_controller.h" | |
23 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" | 18 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h" |
24 #import "chrome/browser/ui/cocoa/location_bar/translate_decoration.h" | 19 #import "chrome/browser/ui/cocoa/location_bar/translate_decoration.h" |
25 #include "chrome/browser/ui/cocoa/test/cocoa_profile_test.h" | 20 #include "chrome/browser/ui/cocoa/test/cocoa_profile_test.h" |
26 #include "chrome/browser/ui/cocoa/test/scoped_force_rtl_mac.h" | |
27 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" | 21 #import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h" |
28 #include "chrome/browser/ui/toolbar/toolbar_actions_bar.h" | |
29 #include "chrome/browser/ui/toolbar/toolbar_actions_model.h" | |
30 #import "chrome/browser/ui/cocoa/view_resizer_pong.h" | 22 #import "chrome/browser/ui/cocoa/view_resizer_pong.h" |
31 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
32 #include "chrome/test/base/testing_profile.h" | 24 #include "chrome/test/base/testing_profile.h" |
33 #include "components/prefs/pref_service.h" | 25 #include "components/prefs/pref_service.h" |
34 #include "extensions/browser/extension_system.h" | |
35 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
36 #import "testing/gtest_mac.h" | 27 #import "testing/gtest_mac.h" |
37 #include "testing/platform_test.h" | 28 #include "testing/platform_test.h" |
38 | 29 |
39 // An NSView that fakes out hitTest:. | 30 // An NSView that fakes out hitTest:. |
40 @interface HitView : NSView { | 31 @interface HitView : NSView { |
41 id hitTestReturn_; | 32 id hitTestReturn_; |
42 } | 33 } |
43 @end | 34 @end |
44 | 35 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 @end | 67 @end |
77 | 68 |
78 namespace { | 69 namespace { |
79 | 70 |
80 class ToolbarControllerTest : public CocoaProfileTest { | 71 class ToolbarControllerTest : public CocoaProfileTest { |
81 public: | 72 public: |
82 | 73 |
83 // Indexes that match the ordering returned by the private ToolbarController | 74 // Indexes that match the ordering returned by the private ToolbarController |
84 // |-toolbarViews| method. | 75 // |-toolbarViews| method. |
85 enum SubviewIndex { | 76 enum SubviewIndex { |
86 kBackIndex, | 77 kBackIndex, kForwardIndex, kReloadIndex, kHomeIndex, |
87 kForwardIndex, | 78 kAppMenuIndex, kLocationIndex, kBrowserActionContainerViewIndex |
88 kReloadIndex, | |
89 kHomeIndex, | |
90 kLocationIndex, | |
91 kBrowserActionContainerViewIndex, | |
92 kAppMenuIndex | |
93 }; | 79 }; |
94 | 80 |
95 void SetUp() override { | 81 void SetUp() override { |
96 CocoaProfileTest::SetUp(); | 82 CocoaProfileTest::SetUp(); |
97 ASSERT_TRUE(browser()); | 83 ASSERT_TRUE(browser()); |
98 | 84 |
99 // Add an extension so the browser action container view | |
100 // is visible and has a real size/position. | |
101 extensions::TestExtensionSystem* extension_system = | |
102 static_cast<extensions::TestExtensionSystem*>( | |
103 extensions::ExtensionSystem::Get(profile())); | |
104 extension_system->CreateExtensionService( | |
105 base::CommandLine::ForCurrentProcess(), base::FilePath(), false); | |
106 scoped_refptr<const extensions::Extension> extension = | |
107 extensions::extension_action_test_util::CreateActionExtension( | |
108 "ABC", extensions::extension_action_test_util::BROWSER_ACTION); | |
109 extensions::ExtensionSystem::Get(profile()) | |
110 ->extension_service() | |
111 ->AddExtension(extension.get()); | |
112 ToolbarActionsModel* model = | |
113 extensions::extension_action_test_util::CreateToolbarModelForProfile( | |
114 profile()); | |
115 model->SetVisibleIconCount(1); | |
116 | |
117 resizeDelegate_.reset([[ViewResizerPong alloc] init]); | 85 resizeDelegate_.reset([[ViewResizerPong alloc] init]); |
118 | 86 |
119 CommandUpdater* updater = | 87 CommandUpdater* updater = |
120 browser()->command_controller()->command_updater(); | 88 browser()->command_controller()->command_updater(); |
121 // The default state for the commands is true, set a couple to false to | 89 // The default state for the commands is true, set a couple to false to |
122 // ensure they get picked up correct on initialization | 90 // ensure they get picked up correct on initialization |
123 updater->UpdateCommandEnabled(IDC_BACK, false); | 91 updater->UpdateCommandEnabled(IDC_BACK, false); |
124 updater->UpdateCommandEnabled(IDC_FORWARD, false); | 92 updater->UpdateCommandEnabled(IDC_FORWARD, false); |
125 bar_.reset([[TestToolbarController alloc] | 93 bar_.reset([[TestToolbarController alloc] |
126 initWithCommands:browser()->command_controller()->command_updater() | 94 initWithCommands:browser()->command_controller()->command_updater() |
127 profile:profile() | 95 profile:profile() |
128 browser:browser() | 96 browser:browser() |
129 resizeDelegate:resizeDelegate_.get()]); | 97 resizeDelegate:resizeDelegate_.get()]); |
130 EXPECT_TRUE([bar_ view]); | 98 EXPECT_TRUE([bar_ view]); |
131 NSView* parent = [test_window() contentView]; | 99 NSView* parent = [test_window() contentView]; |
132 [parent addSubview:[bar_ view]]; | 100 [parent addSubview:[bar_ view]]; |
133 | |
134 // Nudge a few things to ensure the browser actions container gets | |
135 // laid out. | |
136 [bar_ createBrowserActionButtons]; | |
137 [[bar_ browserActionsController] update]; | |
138 [bar_ toolbarFrameChanged]; | |
139 } | 101 } |
140 | 102 |
141 void TearDown() override { | 103 void TearDown() override { |
142 // Releasing ToolbarController doesn't actually free it at this point, since | 104 // Releasing ToolbarController doesn't actually free it at this point, since |
143 // the NSViewController retains a reference to it from the nib loading. | 105 // the NSViewController retains a reference to it from the nib loading. |
144 // As browser() is released in the superclass TearDown, call | 106 // As browser() is released in the superclass TearDown, call |
145 // -[ToolbarController browserWillBeDestroyed] to prevent a use after free | 107 // -[ToolbarController browserWillBeDestroyed] to prevent a use after free |
146 // issue on the |browser_| pointer in LocationBarViewMac when | 108 // issue on the |browser_| pointer in LocationBarViewMac when |
147 // ToolbarController is actually freed (some time after this method is run). | 109 // ToolbarController is actually freed (some time after this method is run). |
148 [bar_ browserWillBeDestroyed]; | 110 [bar_ browserWillBeDestroyed]; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 [GetSubviewAt(kLocationIndex) frame].size.height); | 170 [GetSubviewAt(kLocationIndex) frame].size.height); |
209 EXPECT_GT([[bar_ view] frame].size.width, | 171 EXPECT_GT([[bar_ view] frame].size.width, |
210 [GetSubviewAt(kLocationIndex) frame].size.width); | 172 [GetSubviewAt(kLocationIndex) frame].size.width); |
211 EXPECT_FALSE([view isHidden]); | 173 EXPECT_FALSE([view isHidden]); |
212 EXPECT_FALSE([GetSubviewAt(kLocationIndex) isHidden]); | 174 EXPECT_FALSE([GetSubviewAt(kLocationIndex) isHidden]); |
213 EXPECT_FALSE([GetSubviewAt(kBackIndex) isHidden]); | 175 EXPECT_FALSE([GetSubviewAt(kBackIndex) isHidden]); |
214 EXPECT_FALSE([GetSubviewAt(kForwardIndex) isHidden]); | 176 EXPECT_FALSE([GetSubviewAt(kForwardIndex) isHidden]); |
215 EXPECT_FALSE([GetSubviewAt(kReloadIndex) isHidden]); | 177 EXPECT_FALSE([GetSubviewAt(kReloadIndex) isHidden]); |
216 EXPECT_FALSE([GetSubviewAt(kAppMenuIndex) isHidden]); | 178 EXPECT_FALSE([GetSubviewAt(kAppMenuIndex) isHidden]); |
217 EXPECT_TRUE([GetSubviewAt(kHomeIndex) isHidden]); | 179 EXPECT_TRUE([GetSubviewAt(kHomeIndex) isHidden]); |
218 EXPECT_FALSE([GetSubviewAt(kBrowserActionContainerViewIndex) isHidden]); | 180 EXPECT_TRUE([GetSubviewAt(kBrowserActionContainerViewIndex) isHidden]); |
219 | 181 |
220 // For NO/NO, only the top level toolbar view is hidden. | 182 // For NO/NO, only the top level toolbar view is hidden. |
221 [bar_ setHasToolbar:NO hasLocationBar:NO]; | 183 [bar_ setHasToolbar:NO hasLocationBar:NO]; |
222 EXPECT_TRUE([view isHidden]); | 184 EXPECT_TRUE([view isHidden]); |
223 EXPECT_FALSE([GetSubviewAt(kLocationIndex) isHidden]); | 185 EXPECT_FALSE([GetSubviewAt(kLocationIndex) isHidden]); |
224 EXPECT_FALSE([GetSubviewAt(kBackIndex) isHidden]); | 186 EXPECT_FALSE([GetSubviewAt(kBackIndex) isHidden]); |
225 EXPECT_FALSE([GetSubviewAt(kForwardIndex) isHidden]); | 187 EXPECT_FALSE([GetSubviewAt(kForwardIndex) isHidden]); |
226 EXPECT_FALSE([GetSubviewAt(kReloadIndex) isHidden]); | 188 EXPECT_FALSE([GetSubviewAt(kReloadIndex) isHidden]); |
227 EXPECT_FALSE([GetSubviewAt(kAppMenuIndex) isHidden]); | 189 EXPECT_FALSE([GetSubviewAt(kAppMenuIndex) isHidden]); |
228 EXPECT_TRUE([GetSubviewAt(kHomeIndex) isHidden]); | 190 EXPECT_TRUE([GetSubviewAt(kHomeIndex) isHidden]); |
229 EXPECT_FALSE([GetSubviewAt(kBrowserActionContainerViewIndex) isHidden]); | 191 EXPECT_TRUE([GetSubviewAt(kBrowserActionContainerViewIndex) isHidden]); |
230 | 192 |
231 // Now test the inescapable state. | 193 // Now test the inescapable state. |
232 [bar_ setHasToolbar:NO hasLocationBar:YES]; | 194 [bar_ setHasToolbar:NO hasLocationBar:YES]; |
233 EXPECT_GT([[bar_ view] frame].size.height, 0); | 195 EXPECT_GT([[bar_ view] frame].size.height, 0); |
234 EXPECT_EQ([[bar_ view] frame].size.height, | 196 EXPECT_EQ([[bar_ view] frame].size.height, |
235 [GetSubviewAt(kLocationIndex) frame].size.height); | 197 [GetSubviewAt(kLocationIndex) frame].size.height); |
236 EXPECT_EQ([[bar_ view] frame].size.width, | 198 EXPECT_EQ([[bar_ view] frame].size.width, |
237 [GetSubviewAt(kLocationIndex) frame].size.width); | 199 [GetSubviewAt(kLocationIndex) frame].size.width); |
238 EXPECT_FALSE([view isHidden]); | 200 EXPECT_FALSE([view isHidden]); |
239 EXPECT_FALSE([GetSubviewAt(kLocationIndex) isHidden]); | 201 EXPECT_FALSE([GetSubviewAt(kLocationIndex) isHidden]); |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 base::scoped_nsobject<ImageButtonCell> cell( | 384 base::scoped_nsobject<ImageButtonCell> cell( |
423 [[ImageButtonCell alloc] init]); | 385 [[ImageButtonCell alloc] init]); |
424 [button setCell:cell.get()]; | 386 [button setCell:cell.get()]; |
425 EXPECT_TRUE([bar_ hoverButtonForEvent:nil]); | 387 EXPECT_TRUE([bar_ hoverButtonForEvent:nil]); |
426 | 388 |
427 // Restore the original view so that | 389 // Restore the original view so that |
428 // -[ToolbarController browserWillBeDestroyed] will run correctly. | 390 // -[ToolbarController browserWillBeDestroyed] will run correctly. |
429 [bar_ setView:toolbarView]; | 391 [bar_ setView:toolbarView]; |
430 } | 392 } |
431 | 393 |
432 // Test that subviews are ordered left to right | |
433 TEST_F(ToolbarControllerTest, ElementOrder) { | |
434 NSArray* views = [bar_ toolbarViews]; | |
435 for (size_t i = 1; i < [views count]; i++) { | |
436 NSView* previousSubview = views[i - 1]; | |
437 NSView* subview = views[i]; | |
438 EXPECT_LE(NSMinX([previousSubview frame]), NSMinX([subview frame])); | |
439 } | |
440 } | |
441 | |
442 class ToolbarControllerRTLTest : public ToolbarControllerTest { | |
443 public: | |
444 ToolbarControllerRTLTest() {} | |
445 | |
446 private: | |
447 cocoa_l10n_util::ScopedForceRTLMac rtl_; | |
448 | |
449 DISALLOW_COPY_AND_ASSIGN(ToolbarControllerRTLTest); | |
450 }; | |
451 | |
452 // Test that subviews are ordered right to left | |
453 TEST_F(ToolbarControllerRTLTest, ElementOrder) { | |
454 NSArray* views = [[[bar_ toolbarViews] reverseObjectEnumerator] allObjects]; | |
455 for (size_t i = 1; i < [views count]; i++) { | |
456 NSView* previousSubview = views[i - 1]; | |
457 NSView* subview = views[i]; | |
458 EXPECT_LE(NSMinX([previousSubview frame]), NSMinX([subview frame])); | |
459 } | |
460 } | |
461 | |
462 class BrowserRemovedObserver : public chrome::BrowserListObserver { | 394 class BrowserRemovedObserver : public chrome::BrowserListObserver { |
463 public: | 395 public: |
464 BrowserRemovedObserver() { BrowserList::AddObserver(this); } | 396 BrowserRemovedObserver() { BrowserList::AddObserver(this); } |
465 ~BrowserRemovedObserver() override { BrowserList::RemoveObserver(this); } | 397 ~BrowserRemovedObserver() override { BrowserList::RemoveObserver(this); } |
466 void WaitUntilBrowserRemoved() { run_loop_.Run(); } | 398 void WaitUntilBrowserRemoved() { run_loop_.Run(); } |
467 void OnBrowserRemoved(Browser* browser) override { run_loop_.Quit(); } | 399 void OnBrowserRemoved(Browser* browser) override { run_loop_.Quit(); } |
468 | 400 |
469 private: | 401 private: |
470 base::RunLoop run_loop_; | 402 base::RunLoop run_loop_; |
471 | 403 |
472 DISALLOW_COPY_AND_ASSIGN(BrowserRemovedObserver); | 404 DISALLOW_COPY_AND_ASSIGN(BrowserRemovedObserver); |
473 }; | 405 }; |
474 | 406 |
475 } // namespace | 407 } // namespace |
OLD | NEW |