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 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
|
// Copyright (c) 2011 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>
#import "base/mac/scoped_nsobject.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/cocoa/cocoa_test_helper.h"
#import "chrome/browser/ui/cocoa/tabs/media_indicator_button_cocoa.h"
#import "chrome/browser/ui/cocoa/tabs/tab_controller.h"
#import "chrome/browser/ui/cocoa/tabs/tab_controller_target.h"
#import "chrome/browser/ui/cocoa/tabs/tab_strip_drag_controller.h"
#import "chrome/browser/ui/cocoa/tabs/tab_view.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/resources/grit/ui_resources.h"
// Implements the target interface for the tab, which gets sent messages when
// the tab is clicked on by the user and when its close box is clicked.
@interface TabControllerTestTarget : NSObject<TabControllerTarget> {
@private
bool selected_;
bool closed_;
base::scoped_nsobject<TabStripDragController> dragController_;
}
- (bool)selected;
- (bool)closed;
@end
@implementation TabControllerTestTarget
- (id)init {
if ((self = [super init])) {
dragController_.reset(
[[TabStripDragController alloc] initWithTabStripController:nil]);
}
return self;
}
- (bool)selected {
return selected_;
}
- (bool)closed {
return closed_;
}
- (void)selectTab:(id)sender {
selected_ = true;
}
- (void)closeTab:(id)sender {
closed_ = true;
}
- (void)mouseTimer:(NSTimer*)timer {
// Fire the mouseUp to break the TabView drag loop.
NSEvent* current = [NSApp currentEvent];
NSWindow* window = [timer userInfo];
NSEvent* up = [NSEvent mouseEventWithType:NSLeftMouseUp
location:[current locationInWindow]
modifierFlags:0
timestamp:[current timestamp]
windowNumber:[window windowNumber]
context:nil
eventNumber:0
clickCount:1
pressure:1.0];
[window postEvent:up atStart:YES];
}
- (void)commandDispatch:(TabStripModel::ContextMenuCommand)command
forController:(TabController*)controller {
}
- (BOOL)isCommandEnabled:(TabStripModel::ContextMenuCommand)command
forController:(TabController*)controller {
return NO;
}
- (ui::SimpleMenuModel*)contextMenuModelForController:(TabController*)controller
menuDelegate:(ui::SimpleMenuModel::Delegate*)delegate {
ui::SimpleMenuModel* model = new ui::SimpleMenuModel(delegate);
model->AddItem(1, base::ASCIIToUTF16("Hello World"));
model->AddItem(2, base::ASCIIToUTF16("Allays"));
model->AddItem(3, base::ASCIIToUTF16("Chromium"));
return model;
}
- (id<TabDraggingEventTarget>)dragController {
return dragController_.get();
}
@end
namespace {
CGFloat LeftMargin(NSRect superFrame, NSRect subFrame) {
return NSMinX(subFrame) - NSMinX(superFrame);
}
CGFloat RightMargin(NSRect superFrame, NSRect subFrame) {
return NSMaxX(superFrame) - NSMaxX(subFrame);
}
// The dragging code in TabView makes heavy use of autorelease pools so
// inherit from CocoaTest to have one created for us.
class TabControllerTest : public CocoaTest {
public:
TabControllerTest() { }
static void CheckForExpectedLayoutAndVisibilityOfSubviews(
const TabController* controller) {
// Check whether subviews should be visible when they are supposed to be,
// given Tab size and TabRendererData state.
const TabMediaState indicatorState =
[[controller mediaIndicatorButton] showingMediaState];
if ([controller mini]) {
EXPECT_EQ(1, [controller iconCapacity]);
if (indicatorState != TAB_MEDIA_STATE_NONE) {
EXPECT_FALSE([controller shouldShowIcon]);
EXPECT_TRUE([controller shouldShowMediaIndicator]);
} else {
EXPECT_TRUE([controller shouldShowIcon]);
EXPECT_FALSE([controller shouldShowMediaIndicator]);
}
EXPECT_FALSE([controller shouldShowCloseButton]);
} else if ([controller selected]) {
EXPECT_TRUE([controller shouldShowCloseButton]);
switch ([controller iconCapacity]) {
case 0:
case 1:
EXPECT_FALSE([controller shouldShowIcon]);
EXPECT_FALSE([controller shouldShowMediaIndicator]);
break;
case 2:
if (indicatorState != TAB_MEDIA_STATE_NONE) {
EXPECT_FALSE([controller shouldShowIcon]);
EXPECT_TRUE([controller shouldShowMediaIndicator]);
} else {
EXPECT_TRUE([controller shouldShowIcon]);
EXPECT_FALSE([controller shouldShowMediaIndicator]);
}
break;
default:
EXPECT_LE(3, [controller iconCapacity]);
EXPECT_TRUE([controller shouldShowIcon]);
if (indicatorState != TAB_MEDIA_STATE_NONE)
EXPECT_TRUE([controller shouldShowMediaIndicator]);
else
EXPECT_FALSE([controller shouldShowMediaIndicator]);
break;
}
} else { // Tab not selected/active and not mini tab.
switch ([controller iconCapacity]) {
case 0:
EXPECT_FALSE([controller shouldShowCloseButton]);
EXPECT_FALSE([controller shouldShowIcon]);
EXPECT_FALSE([controller shouldShowMediaIndicator]);
break;
case 1:
EXPECT_FALSE([controller shouldShowCloseButton]);
if (indicatorState != TAB_MEDIA_STATE_NONE) {
EXPECT_FALSE([controller shouldShowIcon]);
EXPECT_TRUE([controller shouldShowMediaIndicator]);
} else {
EXPECT_TRUE([controller shouldShowIcon]);
EXPECT_FALSE([controller shouldShowMediaIndicator]);
}
break;
default:
EXPECT_LE(2, [controller iconCapacity]);
EXPECT_TRUE([controller shouldShowIcon]);
if (indicatorState != TAB_MEDIA_STATE_NONE)
EXPECT_TRUE([controller shouldShowMediaIndicator]);
else
EXPECT_FALSE([controller shouldShowMediaIndicator]);
break;
}
}
// Make sure the NSView's "isHidden" state jives with the "shouldShowXXX."
EXPECT_TRUE([controller shouldShowIcon] ==
(!![controller iconView] && ![[controller iconView] isHidden]));
EXPECT_TRUE([controller mini] == [[controller tabView] titleHidden]);
EXPECT_TRUE([controller shouldShowMediaIndicator] ==
![[controller mediaIndicatorButton] isHidden]);
EXPECT_TRUE([controller shouldShowCloseButton] !=
[[controller closeButton] isHidden]);
// Check positioning of elements with respect to each other, and that they
// are fully within the tab frame.
const NSRect tabFrame = [[controller view] frame];
const NSRect titleFrame = [[controller tabView] titleFrame];
if ([controller shouldShowIcon]) {
const NSRect iconFrame = [[controller iconView] frame];
EXPECT_LE(NSMinX(tabFrame), NSMinX(iconFrame));
if (NSWidth(titleFrame) > 0)
EXPECT_LE(NSMaxX(iconFrame), NSMinX(titleFrame));
EXPECT_LE(NSMinY(tabFrame), NSMinY(iconFrame));
EXPECT_LE(NSMaxY(iconFrame), NSMaxY(tabFrame));
}
if ([controller shouldShowIcon] && [controller shouldShowMediaIndicator]) {
EXPECT_LE(NSMaxX([[controller iconView] frame]),
NSMinX([[controller mediaIndicatorButton] frame]));
}
if ([controller shouldShowMediaIndicator]) {
const NSRect mediaIndicatorFrame =
[[controller mediaIndicatorButton] frame];
if (NSWidth(titleFrame) > 0)
EXPECT_LE(NSMaxX(titleFrame), NSMinX(mediaIndicatorFrame));
EXPECT_LE(NSMaxX(mediaIndicatorFrame), NSMaxX(tabFrame));
EXPECT_LE(NSMinY(tabFrame), NSMinY(mediaIndicatorFrame));
EXPECT_LE(NSMaxY(mediaIndicatorFrame), NSMaxY(tabFrame));
}
if ([controller shouldShowMediaIndicator] &&
[controller shouldShowCloseButton]) {
EXPECT_LE(NSMaxX([[controller mediaIndicatorButton] frame]),
NSMinX([[controller closeButton] frame]));
}
if ([controller shouldShowCloseButton]) {
const NSRect closeButtonFrame = [[controller closeButton] frame];
if (NSWidth(titleFrame) > 0)
EXPECT_LE(NSMaxX(titleFrame), NSMinX(closeButtonFrame));
EXPECT_LE(NSMaxX(closeButtonFrame), NSMaxX(tabFrame));
EXPECT_LE(NSMinY(tabFrame), NSMinY(closeButtonFrame));
EXPECT_LE(NSMaxY(closeButtonFrame), NSMaxY(tabFrame));
}
}
};
// Tests creating the controller, sticking it in a window, and removing it.
TEST_F(TabControllerTest, Creation) {
NSWindow* window = test_window();
base::scoped_nsobject<TabController> controller([[TabController alloc] init]);
[[window contentView] addSubview:[controller view]];
EXPECT_TRUE([controller tabView]);
EXPECT_EQ([[controller view] window], window);
[[controller view] display]; // Test drawing to ensure nothing leaks/crashes.
[[controller view] removeFromSuperview];
}
// Tests sending it a close message and ensuring that the target/action get
// called. Mimics the user clicking on the close button in the tab.
TEST_F(TabControllerTest, Close) {
NSWindow* window = test_window();
base::scoped_nsobject<TabController> controller([[TabController alloc] init]);
[[window contentView] addSubview:[controller view]];
base::scoped_nsobject<TabControllerTestTarget> target(
[[TabControllerTestTarget alloc] init]);
EXPECT_FALSE([target closed]);
[controller setTarget:target];
EXPECT_EQ(target.get(), [controller target]);
[controller closeTab:nil];
EXPECT_TRUE([target closed]);
[[controller view] removeFromSuperview];
}
// Tests setting the |selected| property via code.
TEST_F(TabControllerTest, APISelection) {
NSWindow* window = test_window();
base::scoped_nsobject<TabController> controller([[TabController alloc] init]);
[[window contentView] addSubview:[controller view]];
EXPECT_FALSE([controller selected]);
[controller setSelected:YES];
EXPECT_TRUE([controller selected]);
[[controller view] removeFromSuperview];
}
// Tests setting the |loading| property via code.
TEST_F(TabControllerTest, Loading) {
NSWindow* window = test_window();
base::scoped_nsobject<TabController> controller([[TabController alloc] init]);
[[window contentView] addSubview:[controller view]];
EXPECT_EQ(kTabDone, [controller loadingState]);
[controller setLoadingState:kTabWaiting];
EXPECT_EQ(kTabWaiting, [controller loadingState]);
[controller setLoadingState:kTabLoading];
EXPECT_EQ(kTabLoading, [controller loadingState]);
[controller setLoadingState:kTabDone];
EXPECT_EQ(kTabDone, [controller loadingState]);
[[controller view] removeFromSuperview];
}
// Tests selecting the tab with the mouse click and ensuring the target/action
// get called.
TEST_F(TabControllerTest, UserSelection) {
NSWindow* window = test_window();
// Create a tab at a known location in the window that we can click on
// to activate selection.
base::scoped_nsobject<TabController> controller([[TabController alloc] init]);
[[window contentView] addSubview:[controller view]];
NSRect frame = [[controller view] frame];
frame.size.width = [TabController minTabWidth];
frame.origin = NSZeroPoint;
[[controller view] setFrame:frame];
// Set the target and action.
base::scoped_nsobject<TabControllerTestTarget> target(
[[TabControllerTestTarget alloc] init]);
EXPECT_FALSE([target selected]);
[controller setTarget:target];
[controller setAction:@selector(selectTab:)];
EXPECT_EQ(target.get(), [controller target]);
EXPECT_EQ(@selector(selectTab:), [controller action]);
// In order to track a click, we have to fake a mouse down and a mouse
// up, but the down goes into a tight drag loop. To break the loop, we have
// to fire a timer that sends a mouse up event while the "drag" is ongoing.
[NSTimer scheduledTimerWithTimeInterval:0.1
target:target.get()
selector:@selector(mouseTimer:)
userInfo:window
repeats:NO];
NSEvent* current = [NSApp currentEvent];
NSPoint click_point = NSMakePoint(frame.size.width / 2,
frame.size.height / 2);
NSEvent* down = [NSEvent mouseEventWithType:NSLeftMouseDown
location:click_point
modifierFlags:0
timestamp:[current timestamp]
windowNumber:[window windowNumber]
context:nil
eventNumber:0
clickCount:1
pressure:1.0];
[[controller view] mouseDown:down];
// Check our target was told the tab got selected.
EXPECT_TRUE([target selected]);
[[controller view] removeFromSuperview];
}
TEST_F(TabControllerTest, IconCapacity) {
NSWindow* window = test_window();
base::scoped_nsobject<TabController> controller([[TabController alloc] init]);
[[window contentView] addSubview:[controller view]];
int cap = [controller iconCapacity];
EXPECT_GE(cap, 1);
NSRect frame = [[controller view] frame];
frame.size.width += 500;
[[controller view] setFrame:frame];
int newcap = [controller iconCapacity];
EXPECT_GT(newcap, cap);
}
TEST_F(TabControllerTest, ShouldShowIcon) {
NSWindow* window = test_window();
base::scoped_nsobject<TabController> controller([[TabController alloc] init]);
[[window contentView] addSubview:[controller view]];
int cap = [controller iconCapacity];
EXPECT_GT(cap, 0);
// Tab is minimum width, both icon and close box should be hidden.
NSRect frame = [[controller view] frame];
frame.size.width = [TabController minTabWidth];
[[controller view] setFrame:frame];
EXPECT_FALSE([controller shouldShowIcon]);
EXPECT_FALSE([controller shouldShowCloseButton]);
// Setting the icon when tab is at min width should not show icon (bug 18359).
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
base::scoped_nsobject<NSImage> image(
rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON).CopyNSImage());
[controller setIconImage:image];
NSView* newIcon = [controller iconView];
EXPECT_TRUE([newIcon isHidden]);
// Tab is at active minimum width. Since it's active, the close box
// should be visible.
[controller setActive:YES];
frame = [[controller view] frame];
frame.size.width = [TabController minActiveTabWidth];
[[controller view] setFrame:frame];
EXPECT_FALSE([controller shouldShowIcon]);
EXPECT_TRUE([newIcon isHidden]);
EXPECT_TRUE([controller shouldShowCloseButton]);
// Test expanding the tab to max width and ensure the icon and close box
// get put back, even when de-activated.
frame.size.width = [TabController maxTabWidth];
[[controller view] setFrame:frame];
EXPECT_TRUE([controller shouldShowIcon]);
EXPECT_FALSE([newIcon isHidden]);
EXPECT_TRUE([controller shouldShowCloseButton]);
[controller setActive:NO];
EXPECT_TRUE([controller shouldShowIcon]);
EXPECT_TRUE([controller shouldShowCloseButton]);
cap = [controller iconCapacity];
EXPECT_GT(cap, 0);
}
TEST_F(TabControllerTest, Menu) {
NSWindow* window = test_window();
base::scoped_nsobject<TabController> controller([[TabController alloc] init]);
base::scoped_nsobject<TabControllerTestTarget> target(
[[TabControllerTestTarget alloc] init]);
[controller setTarget:target];
[[window contentView] addSubview:[controller view]];
int cap = [controller iconCapacity];
EXPECT_GT(cap, 0);
// Asking the view for its menu should yield a valid menu.
NSMenu* menu = [[controller view] menu];
EXPECT_TRUE(menu);
EXPECT_EQ(3, [menu numberOfItems]);
}
// Tests that the title field is correctly positioned and sized when the
// view is resized.
TEST_F(TabControllerTest, TitleViewLayout) {
NSWindow* window = test_window();
base::scoped_nsobject<TabController> controller([[TabController alloc] init]);
[[window contentView] addSubview:[controller view]];
NSRect tabFrame = [[controller view] frame];
tabFrame.size.width = [TabController maxTabWidth];
[[controller view] setFrame:tabFrame];
const NSRect originalTabFrame = [[controller view] frame];
const NSRect originalIconFrame = [[controller iconView] frame];
const NSRect originalCloseFrame = [[controller closeButton] frame];
const NSRect originalTitleFrame = [[controller tabView] titleFrame];
// Sanity check the start state.
EXPECT_FALSE([[controller iconView] isHidden]);
EXPECT_FALSE([[controller closeButton] isHidden]);
EXPECT_GT(NSWidth([[controller view] frame]),
NSWidth([[controller tabView] titleFrame]));
// Resize the tab so that that the it shrinks.
tabFrame.size.width = [TabController minTabWidth];
[[controller view] setFrame:tabFrame];
// The icon view and close button should be hidden and the title view should
// be resize to take up their space.
EXPECT_TRUE([[controller iconView] isHidden]);
EXPECT_TRUE([[controller closeButton] isHidden]);
EXPECT_GT(NSWidth([[controller view] frame]),
NSWidth([[controller tabView] titleFrame]));
EXPECT_EQ(LeftMargin(originalTabFrame, originalIconFrame),
LeftMargin([[controller view] frame],
[[controller tabView] titleFrame]));
EXPECT_EQ(RightMargin(originalTabFrame, originalCloseFrame),
RightMargin([[controller view] frame],
[[controller tabView] titleFrame]));
// Resize the tab so that that the it grows.
tabFrame.size.width = static_cast<int>([TabController maxTabWidth] * 0.75);
[[controller view] setFrame:tabFrame];
// The icon view and close button should be visible again and the title view
// should be resized to make room for them.
EXPECT_FALSE([[controller iconView] isHidden]);
EXPECT_FALSE([[controller closeButton] isHidden]);
EXPECT_GT(NSWidth([[controller view] frame]),
NSWidth([[controller tabView] titleFrame]));
EXPECT_EQ(LeftMargin(originalTabFrame, originalTitleFrame),
LeftMargin([[controller view] frame],
[[controller tabView] titleFrame]));
EXPECT_EQ(RightMargin(originalTabFrame, originalTitleFrame),
RightMargin([[controller view] frame],
[[controller tabView] titleFrame]));
}
// A comprehensive test of the layout and visibility of all elements (favicon,
// throbber indicators, titile text, media indicator button, and close button)
// over all relevant combinations of tab state. This test overlaps with parts
// of the other tests above.
// Flaky: https://code.google.com/p/chromium/issues/detail?id=311668
TEST_F(TabControllerTest, DISABLED_LayoutAndVisibilityOfSubviews) {
static const TabMediaState kMediaStatesToTest[] = {
TAB_MEDIA_STATE_NONE, TAB_MEDIA_STATE_CAPTURING,
TAB_MEDIA_STATE_AUDIO_PLAYING, TAB_MEDIA_STATE_AUDIO_MUTING
};
NSWindow* const window = test_window();
// Create TabController instance and place its view into the test window.
base::scoped_nsobject<TabController> controller([[TabController alloc] init]);
[[window contentView] addSubview:[controller view]];
// Create favicon.
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
base::scoped_nsobject<NSImage> favicon(
rb.GetNativeImageNamed(IDR_DEFAULT_FAVICON).CopyNSImage());
// Trigger TabController to auto-create the MediaIndicatorButton.
[controller setMediaState:TAB_MEDIA_STATE_AUDIO_PLAYING];
[controller setMediaState:TAB_MEDIA_STATE_NONE];
base::scoped_nsobject<MediaIndicatorButton> mediaIndicatorButton(
[[controller mediaIndicatorButton] retain]);
ASSERT_TRUE(mediaIndicatorButton.get());
// Perform layout over all possible combinations, checking for correct
// results.
for (int isMiniTab = 0; isMiniTab < 2; ++isMiniTab) {
for (int isActiveTab = 0; isActiveTab < 2; ++isActiveTab) {
for (size_t mediaStateIndex = 0;
mediaStateIndex < arraysize(kMediaStatesToTest);
++mediaStateIndex) {
const TabMediaState mediaState = kMediaStatesToTest[mediaStateIndex];
SCOPED_TRACE(::testing::Message()
<< (isActiveTab ? "Active" : "Inactive") << ' '
<< (isMiniTab ? "Mini " : "")
<< "Tab with media indicator state " << mediaState);
// Simulate what tab_strip_controller would do to set up the
// TabController state.
[controller setMini:(isMiniTab ? YES : NO)];
[controller setActive:(isActiveTab ? YES : NO)];
[controller setIconImage:favicon];
[controller setMediaState:mediaState];
[controller updateVisibility];
// Test layout for every width from maximum to minimum.
NSRect tabFrame = [[controller view] frame];
int minWidth;
if (isMiniTab) {
tabFrame.size.width = minWidth = [TabController miniTabWidth];
} else {
tabFrame.size.width = [TabController maxTabWidth];
minWidth = isActiveTab ? [TabController minActiveTabWidth] :
[TabController minTabWidth];
}
while (NSWidth(tabFrame) >= minWidth) {
SCOPED_TRACE(::testing::Message() << "width=" << tabFrame.size.width);
[[controller view] setFrame:tabFrame];
CheckForExpectedLayoutAndVisibilityOfSubviews(controller);
--tabFrame.size.width;
}
}
}
}
}
} // namespace
|