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