1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <Cocoa/Cocoa.h>
#include "base/command_line.h"
#import "base/mac/scoped_nsobject.h"
#include "base/macros.h"
#include "base/run_loop.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/command_updater.h"
#include "chrome/browser/extensions/extension_action_test_util.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_command_controller.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_list_observer.h"
#import "chrome/browser/ui/cocoa/image_button_cell.h"
#import "chrome/browser/ui/cocoa/extensions/browser_actions_controller.h"
#import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
#import "chrome/browser/ui/cocoa/location_bar/translate_decoration.h"
#include "chrome/browser/ui/cocoa/test/cocoa_profile_test.h"
#include "chrome/browser/ui/cocoa/test/scoped_force_rtl_mac.h"
#import "chrome/browser/ui/cocoa/toolbar/toolbar_controller.h"
#include "chrome/browser/ui/toolbar/toolbar_actions_bar.h"
#include "chrome/browser/ui/toolbar/toolbar_actions_model.h"
#import "chrome/browser/ui/cocoa/view_resizer_pong.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "components/prefs/pref_service.h"
#include "extensions/browser/extension_system.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"
// An NSView that fakes out hitTest:.
@interface HitView : NSView {
id hitTestReturn_;
}
@end
@implementation HitView
- (void)setHitTestReturn:(id)rtn {
hitTestReturn_ = rtn;
}
- (NSView *)hitTest:(NSPoint)aPoint {
return hitTestReturn_;
}
@end
// Records the last command id and enabled state it has received so it can be
// queried by the tests to see if we got a notification or not.
@interface TestToolbarController : ToolbarController {
@private
int lastCommand_; // Id of last received state change.
bool lastState_; // State of last received state change.
}
@property(nonatomic, readonly) int lastCommand;
@property(nonatomic, readonly) bool lastState;
@end
@implementation TestToolbarController
@synthesize lastCommand = lastCommand_;
@synthesize lastState = lastState_;
- (void)enabledStateChangedForCommand:(int)command enabled:(bool)enabled {
[super enabledStateChangedForCommand:command enabled:enabled];
lastCommand_ = command;
lastState_ = enabled;
}
@end
namespace {
class ToolbarControllerTest : public CocoaProfileTest {
public:
// Indexes that match the ordering returned by the private ToolbarController
// |-toolbarViews| method.
enum SubviewIndex {
kBackIndex,
kForwardIndex,
kReloadIndex,
kHomeIndex,
kLocationIndex,
kBrowserActionContainerViewIndex,
kAppMenuIndex
};
void SetUp() override {
CocoaProfileTest::SetUp();
ASSERT_TRUE(browser());
// Add an extension so the browser action container view
// is visible and has a real size/position.
extensions::TestExtensionSystem* extension_system =
static_cast<extensions::TestExtensionSystem*>(
extensions::ExtensionSystem::Get(profile()));
extension_system->CreateExtensionService(
base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
scoped_refptr<const extensions::Extension> extension =
extensions::extension_action_test_util::CreateActionExtension(
"ABC", extensions::extension_action_test_util::BROWSER_ACTION);
extensions::ExtensionSystem::Get(profile())
->extension_service()
->AddExtension(extension.get());
ToolbarActionsModel* model =
extensions::extension_action_test_util::CreateToolbarModelForProfile(
profile());
model->SetVisibleIconCount(1);
resizeDelegate_.reset([[ViewResizerPong alloc] init]);
CommandUpdater* updater =
browser()->command_controller()->command_updater();
// The default state for the commands is true, set a couple to false to
// ensure they get picked up correct on initialization
updater->UpdateCommandEnabled(IDC_BACK, false);
updater->UpdateCommandEnabled(IDC_FORWARD, false);
bar_.reset([[TestToolbarController alloc]
initWithCommands:browser()->command_controller()->command_updater()
profile:profile()
browser:browser()
resizeDelegate:resizeDelegate_.get()]);
EXPECT_TRUE([bar_ view]);
NSView* parent = [test_window() contentView];
[parent addSubview:[bar_ view]];
// Nudge a few things to ensure the browser actions container gets
// laid out.
[bar_ createBrowserActionButtons];
[[bar_ browserActionsController] update];
[bar_ toolbarFrameChanged];
}
void TearDown() override {
// Releasing ToolbarController doesn't actually free it at this point, since
// the NSViewController retains a reference to it from the nib loading.
// As browser() is released in the superclass TearDown, call
// -[ToolbarController browserWillBeDestroyed] to prevent a use after free
// issue on the |browser_| pointer in LocationBarViewMac when
// ToolbarController is actually freed (some time after this method is run).
[bar_ browserWillBeDestroyed];
bar_.reset();
CocoaProfileTest::TearDown();
}
// Make sure the enabled state of the view is the same as the corresponding
// command in the updater. The views are in the declaration order of outlets.
void CompareState(CommandUpdater* updater, NSArray* views) {
EXPECT_EQ(updater->IsCommandEnabled(IDC_BACK),
[[views objectAtIndex:kBackIndex] isEnabled] ? true : false);
EXPECT_EQ(updater->IsCommandEnabled(IDC_FORWARD),
[[views objectAtIndex:kForwardIndex] isEnabled] ? true : false);
EXPECT_EQ(updater->IsCommandEnabled(IDC_RELOAD),
[[views objectAtIndex:kReloadIndex] isEnabled] ? true : false);
EXPECT_EQ(updater->IsCommandEnabled(IDC_HOME),
[[views objectAtIndex:kHomeIndex] isEnabled] ? true : false);
}
NSView* GetSubviewAt(SubviewIndex index) {
return [[bar_ toolbarViews] objectAtIndex:index];
}
base::scoped_nsobject<ViewResizerPong> resizeDelegate_;
base::scoped_nsobject<TestToolbarController> bar_;
};
TEST_VIEW(ToolbarControllerTest, [bar_ view])
// Test the initial state that everything is sync'd up
TEST_F(ToolbarControllerTest, InitialState) {
CommandUpdater* updater = browser()->command_controller()->command_updater();
CompareState(updater, [bar_ toolbarViews]);
}
// Make sure a "titlebar only" toolbar with location bar works.
TEST_F(ToolbarControllerTest, TitlebarOnly) {
NSView* view = [bar_ view];
[bar_ setHasToolbar:NO hasLocationBar:YES];
EXPECT_EQ(view, [bar_ view]);
// Simulate a popup going fullscreen and back by performing the reparenting
// that happens during fullscreen transitions
NSView* superview = [view superview];
[view removeFromSuperview];
[superview addSubview:view];
EXPECT_EQ(view, [bar_ view]);
}
// Test updateVisibility with location bar only; this method is used by bookmark
// apps, and should never be called when the toolbar is enabled. Ensure that the
// buttons remain in the correct state.
TEST_F(ToolbarControllerTest, UpdateVisibility) {
NSView* view = [bar_ view];
// Test the escapable states first.
[bar_ setHasToolbar:YES hasLocationBar:YES];
EXPECT_GT([[bar_ view] frame].size.height, 0);
EXPECT_GT([[bar_ view] frame].size.height,
[GetSubviewAt(kLocationIndex) frame].size.height);
EXPECT_GT([[bar_ view] frame].size.width,
[GetSubviewAt(kLocationIndex) frame].size.width);
EXPECT_FALSE([view isHidden]);
EXPECT_FALSE([GetSubviewAt(kLocationIndex) isHidden]);
EXPECT_FALSE([GetSubviewAt(kBackIndex) isHidden]);
EXPECT_FALSE([GetSubviewAt(kForwardIndex) isHidden]);
EXPECT_FALSE([GetSubviewAt(kReloadIndex) isHidden]);
EXPECT_FALSE([GetSubviewAt(kAppMenuIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kHomeIndex) isHidden]);
EXPECT_FALSE([GetSubviewAt(kBrowserActionContainerViewIndex) isHidden]);
// For NO/NO, only the top level toolbar view is hidden.
[bar_ setHasToolbar:NO hasLocationBar:NO];
EXPECT_TRUE([view isHidden]);
EXPECT_FALSE([GetSubviewAt(kLocationIndex) isHidden]);
EXPECT_FALSE([GetSubviewAt(kBackIndex) isHidden]);
EXPECT_FALSE([GetSubviewAt(kForwardIndex) isHidden]);
EXPECT_FALSE([GetSubviewAt(kReloadIndex) isHidden]);
EXPECT_FALSE([GetSubviewAt(kAppMenuIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kHomeIndex) isHidden]);
EXPECT_FALSE([GetSubviewAt(kBrowserActionContainerViewIndex) isHidden]);
// Now test the inescapable state.
[bar_ setHasToolbar:NO hasLocationBar:YES];
EXPECT_GT([[bar_ view] frame].size.height, 0);
EXPECT_EQ([[bar_ view] frame].size.height,
[GetSubviewAt(kLocationIndex) frame].size.height);
EXPECT_EQ([[bar_ view] frame].size.width,
[GetSubviewAt(kLocationIndex) frame].size.width);
EXPECT_FALSE([view isHidden]);
EXPECT_FALSE([GetSubviewAt(kLocationIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kBackIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kForwardIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kReloadIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kAppMenuIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kHomeIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kBrowserActionContainerViewIndex) isHidden]);
// Maintain visible state.
[bar_ updateVisibility:YES withAnimation:NO];
EXPECT_GT([[bar_ view] frame].size.height, 0);
EXPECT_EQ([[bar_ view] frame].size.height,
[GetSubviewAt(kLocationIndex) frame].size.height);
EXPECT_EQ([[bar_ view] frame].size.width,
[GetSubviewAt(kLocationIndex) frame].size.width);
EXPECT_FALSE([view isHidden]);
EXPECT_FALSE([GetSubviewAt(kLocationIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kBackIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kForwardIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kReloadIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kAppMenuIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kHomeIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kBrowserActionContainerViewIndex) isHidden]);
// Hide the toolbar and ensure it has height 0.
[bar_ updateVisibility:NO withAnimation:NO];
EXPECT_FALSE([view isHidden]);
EXPECT_EQ(0, [resizeDelegate_ height]);
EXPECT_EQ(0, [[bar_ view] frame].size.height);
// Try to show the home button.
[bar_ showOptionalHomeButton];
// Re-show the bar. Buttons should remain hidden, including the home button.
[bar_ updateVisibility:YES withAnimation:NO];
EXPECT_GT([resizeDelegate_ height], 0);
EXPECT_GT([[bar_ view] frame].size.height, 0);
EXPECT_EQ([[bar_ view] frame].size.height,
[GetSubviewAt(kLocationIndex) frame].size.height);
EXPECT_EQ([[bar_ view] frame].size.width,
[GetSubviewAt(kLocationIndex) frame].size.width);
EXPECT_FALSE([view isHidden]);
EXPECT_FALSE([GetSubviewAt(kLocationIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kBackIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kForwardIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kReloadIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kAppMenuIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kHomeIndex) isHidden]);
EXPECT_TRUE([GetSubviewAt(kBrowserActionContainerViewIndex) isHidden]);
}
// Make sure it works in the completely undecorated case.
TEST_F(ToolbarControllerTest, NoLocationBar) {
NSView* view = [bar_ view];
[bar_ setHasToolbar:NO hasLocationBar:NO];
EXPECT_TRUE([[bar_ view] isHidden]);
// Simulate a popup going fullscreen and back by performing the reparenting
// that happens during fullscreen transitions
NSView* superview = [view superview];
[view removeFromSuperview];
[superview addSubview:view];
}
// Make some changes to the enabled state of a few of the buttons and ensure
// that we're still in sync.
TEST_F(ToolbarControllerTest, UpdateEnabledState) {
EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_BACK));
EXPECT_FALSE(chrome::IsCommandEnabled(browser(), IDC_FORWARD));
chrome::UpdateCommandEnabled(browser(), IDC_BACK, true);
chrome::UpdateCommandEnabled(browser(), IDC_FORWARD, true);
CommandUpdater* updater = browser()->command_controller()->command_updater();
CompareState(updater, [bar_ toolbarViews]);
// Change an unwatched command and ensure the last state does not change.
updater->UpdateCommandEnabled(IDC_MinimumLabelValue, false);
EXPECT_EQ([bar_ lastCommand], IDC_FORWARD);
EXPECT_EQ([bar_ lastState], true);
}
// Focus the location bar and make sure that it's the first responder.
TEST_F(ToolbarControllerTest, FocusLocation) {
NSWindow* window = test_window();
[window makeFirstResponder:[window contentView]];
EXPECT_EQ([window firstResponder], [window contentView]);
[bar_ focusLocationBar:YES];
EXPECT_NE([window firstResponder], [window contentView]);
NSView* locationBar = [[bar_ toolbarViews] objectAtIndex:kLocationIndex];
EXPECT_EQ([window firstResponder], [(id)locationBar currentEditor]);
}
TEST_F(ToolbarControllerTest, LoadingState) {
// In its initial state, the reload button has a tag of
// IDC_RELOAD. When loading, it should be IDC_STOP.
NSButton* reload = [[bar_ toolbarViews] objectAtIndex:kReloadIndex];
EXPECT_EQ([reload tag], IDC_RELOAD);
[bar_ setIsLoading:YES force:YES];
EXPECT_EQ([reload tag], IDC_STOP);
[bar_ setIsLoading:NO force:YES];
EXPECT_EQ([reload tag], IDC_RELOAD);
}
// Check that toggling the state of the home button changes the visible
// state of the home button and moves the other items accordingly.
TEST_F(ToolbarControllerTest, ToggleHome) {
PrefService* prefs = profile()->GetPrefs();
bool showHome = prefs->GetBoolean(prefs::kShowHomeButton);
NSView* homeButton = [[bar_ toolbarViews] objectAtIndex:kHomeIndex];
EXPECT_EQ(showHome, ![homeButton isHidden]);
NSView* locationBar = [[bar_ toolbarViews] objectAtIndex:kLocationIndex];
NSRect originalLocationBarFrame = [locationBar frame];
// Toggle the pref and make sure the button changed state and the other
// views moved.
prefs->SetBoolean(prefs::kShowHomeButton, !showHome);
EXPECT_EQ(showHome, [homeButton isHidden]);
EXPECT_NE(NSMinX(originalLocationBarFrame), NSMinX([locationBar frame]));
EXPECT_NE(NSWidth(originalLocationBarFrame), NSWidth([locationBar frame]));
}
// Ensure that we don't toggle the buttons when we have a strip marked as not
// having the full toolbar. Also ensure that the location bar doesn't change
// size.
TEST_F(ToolbarControllerTest, DontToggleWhenNoToolbar) {
[bar_ setHasToolbar:NO hasLocationBar:YES];
NSView* homeButton = [[bar_ toolbarViews] objectAtIndex:kHomeIndex];
NSView* locationBar = [[bar_ toolbarViews] objectAtIndex:kLocationIndex];
NSRect locationBarFrame = [locationBar frame];
EXPECT_EQ([homeButton isHidden], YES);
[bar_ showOptionalHomeButton];
EXPECT_EQ([homeButton isHidden], YES);
NSRect newLocationBarFrame = [locationBar frame];
EXPECT_NSEQ(locationBarFrame, newLocationBarFrame);
newLocationBarFrame = [locationBar frame];
EXPECT_NSEQ(locationBarFrame, newLocationBarFrame);
}
TEST_F(ToolbarControllerTest, BookmarkBubblePoint) {
const NSPoint starPoint = [bar_ bookmarkBubblePoint];
const NSRect barFrame =
[[bar_ view] convertRect:[[bar_ view] bounds] toView:nil];
// Make sure the star is completely inside the location bar.
EXPECT_TRUE(NSPointInRect(starPoint, barFrame));
}
TEST_F(ToolbarControllerTest, TranslateBubblePoint) {
LocationBarViewMac* locationBarBridge = [bar_ locationBarBridge];
LocationBarDecoration* decoration = locationBarBridge->translate_decoration();
const NSPoint translatePoint =
locationBarBridge->GetBubblePointForDecoration(decoration);
const NSRect barFrame =
[[bar_ view] convertRect:[[bar_ view] bounds] toView:nil];
EXPECT_TRUE(NSPointInRect(translatePoint, barFrame));
}
TEST_F(ToolbarControllerTest, HoverButtonForEvent) {
base::scoped_nsobject<HitView> view(
[[HitView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)]);
NSView* toolbarView = [bar_ view];
[bar_ setView:view];
NSEvent* event = [NSEvent mouseEventWithType:NSMouseMoved
location:NSMakePoint(10,10)
modifierFlags:0
timestamp:0
windowNumber:0
context:nil
eventNumber:0
clickCount:0
pressure:0.0];
// NOT a match.
[view setHitTestReturn:bar_.get()];
EXPECT_FALSE([bar_ hoverButtonForEvent:event]);
// Not yet...
base::scoped_nsobject<NSButton> button([[NSButton alloc] init]);
[view setHitTestReturn:button];
EXPECT_FALSE([bar_ hoverButtonForEvent:event]);
// Now!
base::scoped_nsobject<ImageButtonCell> cell(
[[ImageButtonCell alloc] init]);
[button setCell:cell.get()];
EXPECT_TRUE([bar_ hoverButtonForEvent:nil]);
// Restore the original view so that
// -[ToolbarController browserWillBeDestroyed] will run correctly.
[bar_ setView:toolbarView];
}
// Test that subviews are ordered left to right
TEST_F(ToolbarControllerTest, ElementOrder) {
NSArray* views = [bar_ toolbarViews];
for (size_t i = 1; i < [views count]; i++) {
NSView* previousSubview = views[i - 1];
NSView* subview = views[i];
EXPECT_LE(NSMinX([previousSubview frame]), NSMinX([subview frame]));
}
}
class ToolbarControllerRTLTest : public ToolbarControllerTest {
public:
ToolbarControllerRTLTest() {}
private:
cocoa_l10n_util::ScopedForceRTLMac rtl_;
DISALLOW_COPY_AND_ASSIGN(ToolbarControllerRTLTest);
};
// Test that subviews are ordered right to left
TEST_F(ToolbarControllerRTLTest, ElementOrder) {
NSArray* views = [[[bar_ toolbarViews] reverseObjectEnumerator] allObjects];
for (size_t i = 1; i < [views count]; i++) {
NSView* previousSubview = views[i - 1];
NSView* subview = views[i];
EXPECT_LE(NSMinX([previousSubview frame]), NSMinX([subview frame]));
}
}
class BrowserRemovedObserver : public chrome::BrowserListObserver {
public:
BrowserRemovedObserver() { BrowserList::AddObserver(this); }
~BrowserRemovedObserver() override { BrowserList::RemoveObserver(this); }
void WaitUntilBrowserRemoved() { run_loop_.Run(); }
void OnBrowserRemoved(Browser* browser) override { run_loop_.Quit(); }
private:
base::RunLoop run_loop_;
DISALLOW_COPY_AND_ASSIGN(BrowserRemovedObserver);
};
} // namespace
|