File: tab_strip_controller_unittest.mm

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (495 lines) | stat: -rw-r--r-- 19,426 bytes parent folder | download
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
// 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/bind_helpers.h"
#include "base/mac/scoped_nsautorelease_pool.h"
#include "chrome/browser/media/webrtc/media_capture_devices_dispatcher.h"
#include "chrome/browser/media/webrtc/media_stream_capture_indicator.h"
#include "chrome/browser/ui/browser_window.h"
#import "chrome/browser/ui/cocoa/browser_window_controller.h"
#import "chrome/browser/ui/cocoa/new_tab_button.h"
#import "chrome/browser/ui/cocoa/tabs/tab_controller.h"
#import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
#import "chrome/browser/ui/cocoa/tabs/tab_strip_view.h"
#import "chrome/browser/ui/cocoa/tabs/tab_view.h"
#include "chrome/browser/ui/cocoa/test/cocoa_profile_test.h"
#include "chrome/browser/ui/tabs/tab_utils.h"
#include "chrome/browser/ui/tabs/test_tab_strip_model_delegate.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/media_stream_request.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"
#include "ui/events/test/cocoa_test_event_utils.h"

using content::SiteInstance;
using content::WebContents;

@interface TabStripControllerForAlertTesting : TabStripController {
  // Keeps alert state of tabs in browser for testing purpose.
  std::map<content::WebContents*, TabAlertState> contentsAlertStateMaps_;
}
@end

@implementation TabStripControllerForAlertTesting
// Returns the alert state of each tab from the map we are keeping.
- (TabAlertState)alertStateForContents:(content::WebContents*)contents {
  return contentsAlertStateMaps_[contents];
}

- (void)setAlertStateForContents:(content::WebContents*)contents
                  withAlertState:(TabAlertState)alert_state {
  contentsAlertStateMaps_[contents] = alert_state;
}

@end

@interface TestTabStripControllerDelegate
    : NSObject<TabStripControllerDelegate> {
}
@end

@implementation TestTabStripControllerDelegate
- (void)onActivateTabWithContents:(WebContents*)contents {
}
- (void)onTabChanged:(TabStripModelObserver::TabChangeType)change
        withContents:(WebContents*)contents {
}
- (void)onTabDetachedWithContents:(WebContents*)contents {
}
- (void)onTabInsertedInForeground:(BOOL)inForeground {
}
@end


// Helper class for invoking a base::Closure via
// -performSelector:withObject:afterDelay:.
@interface TestClosureRunner : NSObject {
 @private
  base::Closure closure_;
}
- (id)initWithClosure:(const base::Closure&)closure;
- (void)scheduleDelayedRun;
- (void)run;
@end

@implementation TestClosureRunner
- (id)initWithClosure:(const base::Closure&)closure {
  if (self) {
    closure_ = closure;
  }
  return self;
}
- (void)scheduleDelayedRun {
  [self performSelector:@selector(run) withObject:nil afterDelay:0];
}
- (void)run {
  closure_.Run();
}
@end

@interface TabStripController (Test)

- (void)mouseMoved:(NSEvent*)event;

@end

@implementation TabView (Test)

- (TabController*)controller {
  return controller_;
}

@end

namespace {

class TabStripControllerTest : public CocoaProfileTest {
 public:
  void SetUp() override {
    CocoaProfileTest::SetUp();
    ASSERT_TRUE(browser());

    NSWindow* window = browser()->window()->GetNativeWindow();
    NSView* parent = [window contentView];
    NSRect content_frame = [parent frame];

    // Create the "switch view" (view that gets changed out when a tab
    // switches).
    NSRect switch_frame = NSMakeRect(0, 0, content_frame.size.width, 500);
    base::scoped_nsobject<NSView> switch_view(
        [[NSView alloc] initWithFrame:switch_frame]);
    switch_view_ = switch_view;
    [parent addSubview:switch_view_.get()];

    // Create the tab strip view. It's expected to have a child button in it
    // already as the "new tab" button so create that too.
    NSRect strip_frame = NSMakeRect(0, NSMaxY(switch_frame),
                                    content_frame.size.width, 30);
    tab_strip_.reset(
        [[TabStripView alloc] initWithFrame:strip_frame]);
    [parent addSubview:tab_strip_.get()];
    NSRect button_frame = NSMakeRect(0, 0, 15, 15);
    base::scoped_nsobject<NewTabButton> new_tab_button(
        [[NewTabButton alloc] initWithFrame:button_frame]);
    [tab_strip_ addSubview:new_tab_button.get()];
    [tab_strip_ setNewTabButton:new_tab_button.get()];

    delegate_.reset(new TestTabStripModelDelegate());
    model_ = browser()->tab_strip_model();
    controller_delegate_.reset([TestTabStripControllerDelegate alloc]);
    controller_.reset([[TabStripController alloc]
                      initWithView:static_cast<TabStripView*>(tab_strip_.get())
                        switchView:switch_view.get()
                           browser:browser()
                          delegate:controller_delegate_.get()]);
  }

  void TearDown() override {
    // The call to CocoaTest::TearDown() deletes the Browser and TabStripModel
    // objects, so we first have to delete the controller, which refers to them.
    controller_.reset();
    model_ = NULL;
    CocoaProfileTest::TearDown();
  }

  // Return a derived TabStripController.
  TabStripControllerForAlertTesting* InitTabStripControllerForAlertTesting() {
    TabStripControllerForAlertTesting* c =
        [[TabStripControllerForAlertTesting alloc]
            initWithView:static_cast<TabStripView*>(tab_strip_.get())
              switchView:switch_view_.get()
                 browser:browser()
                delegate:controller_delegate_.get()];
    return c;
  }

  TabView* CreateTab() {
    WebContents* web_contents =
        WebContents::Create(content::WebContents::CreateParams(
            profile(), SiteInstance::Create(profile())));
    model_->AppendWebContents(web_contents, true);
    const NSUInteger tab_count = [controller_.get() viewsCount];
    return static_cast<TabView*>([controller_.get() viewAtIndex:tab_count - 1]);
  }

  // Closes all tabs and unrefs the tabstrip and then posts a NSLeftMouseUp
  // event which should end the nested drag event loop.
  void CloseTabsAndEndDrag() {
    // Simulate a close of the browser window.
    model_->CloseAllTabs();
    controller_.reset();
    tab_strip_.reset();
    // Schedule a NSLeftMouseUp to end the nested drag event loop.
    NSEvent* event =
        cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, 0);
    [NSApp postEvent:event atStart:NO];
  }

  std::unique_ptr<TestTabStripModelDelegate> delegate_;
  TabStripModel* model_;
  base::scoped_nsobject<TestTabStripControllerDelegate> controller_delegate_;
  base::scoped_nsobject<TabStripController> controller_;
  base::scoped_nsobject<TabStripView> tab_strip_;
  base::scoped_nsobject<NSView> switch_view_;
};

// Test adding and removing tabs and making sure that views get added to
// the tab strip.
TEST_F(TabStripControllerTest, AddRemoveTabs) {
  EXPECT_TRUE(model_->empty());
  CreateTab();
  EXPECT_EQ(model_->count(), 1);
}

// Clicking a selected (but inactive) tab should activate it.
TEST_F(TabStripControllerTest, ActivateSelectedButInactiveTab) {
  TabView* tab0 = CreateTab();
  TabView* tab1 = CreateTab();
  model_->ToggleSelectionAt(0);
  EXPECT_TRUE([[tab0 controller] selected]);
  EXPECT_TRUE([[tab1 controller] selected]);

  [controller_ selectTab:tab1];
  EXPECT_EQ(1, model_->active_index());
}

// Toggling (cmd-click) a selected (but inactive) tab should deselect it.
TEST_F(TabStripControllerTest, DeselectInactiveTab) {
  TabView* tab0 = CreateTab();
  TabView* tab1 = CreateTab();
  model_->ToggleSelectionAt(0);
  EXPECT_TRUE([[tab0 controller] selected]);
  EXPECT_TRUE([[tab1 controller] selected]);

  model_->ToggleSelectionAt(1);
  EXPECT_TRUE([[tab0 controller] selected]);
  EXPECT_FALSE([[tab1 controller] selected]);
}

TEST_F(TabStripControllerTest, SelectTab) {
  // TODO(pinkerton): Implement http://crbug.com/10899
}

TEST_F(TabStripControllerTest, RearrangeTabs) {
  // TODO(pinkerton): Implement http://crbug.com/10899
}

TEST_F(TabStripControllerTest, CorrectMouseHoverBehavior) {
  TabView* tab1 = CreateTab();
  TabView* tab2 = CreateTab();

  EXPECT_FALSE([tab1 controller].selected);
  EXPECT_TRUE([tab2 controller].selected);

  // Check that there's no hovered tab yet.
  EXPECT_FALSE([controller_ hoveredTab]);

  // Set up mouse event on overlap of tab1 + tab2.
  const CGFloat min_y = NSMinY([tab_strip_.get() frame]) + 1;
  const CGFloat tab_overlap = [TabStripController tabOverlap];

  // Hover over overlap between tab 1 and 2.
  NSRect tab1_frame = [tab1 frame];
  NSPoint tab_overlap_point =
      NSMakePoint(NSMaxX(tab1_frame) - tab_overlap / 2, min_y);

  NSEvent* event = cocoa_test_event_utils::MouseEventAtPoint(tab_overlap_point,
                                                             NSMouseMoved,
                                                             0);
  [controller_.get() mouseMoved:event];
  EXPECT_EQ(tab2, [controller_ hoveredTab]);

  // Hover over tab 1.
  NSPoint hover_point = NSMakePoint(NSMidX(tab1_frame), min_y);
  event =
      cocoa_test_event_utils::MouseEventAtPoint(hover_point, NSMouseMoved, 0);
  [controller_.get() mouseMoved:event];
  EXPECT_EQ(tab1, [controller_ hoveredTab]);

  // Hover over tab 2.
  NSRect tab2_frame = [tab2 frame];
  hover_point = NSMakePoint(NSMidX(tab2_frame), min_y);
  event =
      cocoa_test_event_utils::MouseEventAtPoint(hover_point, NSMouseMoved, 0);
  [controller_.get() mouseMoved:event];
  EXPECT_EQ(tab2, [controller_ hoveredTab]);
}

TEST_F(TabStripControllerTest, CorrectTitleAndToolTipTextFromSetTabTitle) {
  using content::MediaStreamDevice;
  using content::MediaStreamDevices;
  using content::MediaStreamUI;

  TabView* const tab = CreateTab();
  TabController* const tabController = [tab controller];
  WebContents* const contents = model_->GetActiveWebContents();

  // For the duration of the test, assume the tab has been hovered. This adds a
  // subview containing the actual source of the tooltip.
  [controller_ setHoveredTab:tab];
  // Note -[NSView hitTest:] takes superview coordinates. Then, find a spot that
  // is outside the mask image, but inside the tab.
  NSPoint centerPoint = NSMakePoint(5, NSMidY([tab bounds]));
  NSPoint hitPoint = [tab convertPoint:centerPoint
                                toView:[tab_strip_ superview]];
  NSView* toolTipView = [tab_strip_ hitTest:hitPoint];
  EXPECT_TRUE(toolTipView);
  EXPECT_NE(toolTipView, tab);

  // Initially, tab title and tooltip text are equivalent.
  EXPECT_EQ(TabAlertState::NONE,
            chrome::GetTabAlertStateForContents(contents));
  [controller_ setTabTitle:tabController withContents:contents];
  NSString* const baseTitle = [tabController title];
  EXPECT_NSEQ(baseTitle, [tabController toolTip]);
  EXPECT_NSEQ([tabController toolTip], [toolTipView toolTip]);

  // Simulate the start of tab video capture.  Tab title remains the same, but
  // the tooltip text should include the following appended: 1) a line break;
  // 2) a non-empty string with a localized description of the alert state.
  scoped_refptr<MediaStreamCaptureIndicator> indicator =
      MediaCaptureDevicesDispatcher::GetInstance()->
          GetMediaStreamCaptureIndicator();
  const MediaStreamDevice dummyVideoCaptureDevice(
      content::MEDIA_TAB_VIDEO_CAPTURE, "dummy_id", "dummy name");
  std::unique_ptr<MediaStreamUI> streamUi(indicator->RegisterMediaStream(
      contents, MediaStreamDevices(1, dummyVideoCaptureDevice)));
  streamUi->OnStarted(base::Bind(&base::DoNothing));
  EXPECT_EQ(TabAlertState::TAB_CAPTURING,
            chrome::GetTabAlertStateForContents(contents));
  [controller_ setTabTitle:tabController withContents:contents];
  EXPECT_NSEQ(baseTitle, [tabController title]);
  NSString* const toolTipText = [tabController toolTip];
  EXPECT_NSEQ(toolTipText, [toolTipView toolTip]);
  if ([baseTitle length] > 0) {
    EXPECT_TRUE(NSEqualRanges(NSMakeRange(0, [baseTitle length]),
                              [toolTipText rangeOfString:baseTitle]));
    EXPECT_TRUE(NSEqualRanges(NSMakeRange([baseTitle length], 1),
                              [toolTipText rangeOfString:@"\n"]));
    EXPECT_LT([baseTitle length] + 1, [toolTipText length]);
  } else {
    EXPECT_LT(0u, [toolTipText length]);
  }

  // Simulate the end of tab video capture.  Tab title and tooltip should become
  // equivalent again.
  streamUi.reset();
  EXPECT_EQ(TabAlertState::NONE,
            chrome::GetTabAlertStateForContents(contents));
  [controller_ setTabTitle:tabController withContents:contents];
  EXPECT_NSEQ(baseTitle, [tabController title]);
  EXPECT_NSEQ(baseTitle, [tabController toolTip]);
  EXPECT_NSEQ(baseTitle, [toolTipView toolTip]);
}

TEST_F(TabStripControllerTest, TabCloseDuringDrag) {
  TabController* tab;
  // The TabController gets autoreleased when created, but is owned by the
  // tab strip model. Use a ScopedNSAutoreleasePool to get a truly weak ref
  // to it to test that -maybeStartDrag:forTab: can handle that properly.
  {
    base::mac::ScopedNSAutoreleasePool pool;
    tab = [CreateTab() controller];
  }

  // Schedule a task to close all the tabs and stop the drag, before the call to
  // -maybeStartDrag:forTab:, which starts a nested event loop. This task will
  // run in that nested event loop, which shouldn't crash.
  base::scoped_nsobject<TestClosureRunner> runner([[TestClosureRunner alloc]
      initWithClosure:base::Bind(&TabStripControllerTest::CloseTabsAndEndDrag,
                                 base::Unretained(this))]);
  [runner scheduleDelayedRun];

  NSEvent* event =
      cocoa_test_event_utils::LeftMouseDownAtPoint(NSZeroPoint);
  [[controller_ dragController] maybeStartDrag:event forTab:tab];
}

TEST_F(TabStripControllerTest, ViewAccessibility_Contents) {
  NSArray* attrs = [tab_strip_ accessibilityAttributeNames];
  ASSERT_TRUE([attrs containsObject:NSAccessibilityContentsAttribute]);

  // Create two tabs and ensure they exist in the contents array.
  TabView* tab1 = CreateTab();
  TabView* tab2 = CreateTab();
  NSObject* contents =
      [tab_strip_ accessibilityAttributeValue:NSAccessibilityContentsAttribute];
  DCHECK([contents isKindOfClass:[NSArray class]]);
  NSArray* contentsArray = static_cast<NSArray*>(contents);
  ASSERT_TRUE([contentsArray containsObject:tab1]);
  ASSERT_TRUE([contentsArray containsObject:tab2]);
}

TEST_F(TabStripControllerTest, ViewAccessibility_Value) {
  NSArray* attrs = [tab_strip_ accessibilityAttributeNames];
  ASSERT_TRUE([attrs containsObject:NSAccessibilityValueAttribute]);

  // Create two tabs and ensure the active one gets returned.
  TabView* tab1 = CreateTab();
  TabView* tab2 = CreateTab();
  EXPECT_FALSE([tab1 controller].selected);
  EXPECT_TRUE([tab2 controller].selected);
  NSObject* value =
      [tab_strip_ accessibilityAttributeValue:NSAccessibilityValueAttribute];
  EXPECT_EQ(tab2, value);

  model_->ActivateTabAt(0, false);
  EXPECT_TRUE([tab1 controller].selected);
  EXPECT_FALSE([tab2 controller].selected);
  value =
      [tab_strip_ accessibilityAttributeValue:NSAccessibilityValueAttribute];
  EXPECT_EQ(tab1, value);
}

TEST_F(TabStripControllerTest, CorrectWindowFromUpdateWindowAlertState) {
  controller_.reset(InitTabStripControllerForAlertTesting());
  NSWindow* window = [tab_strip_ window];
  BrowserWindowController* window_controller =
      [BrowserWindowController browserWindowControllerForWindow:window];
  TabStripControllerForAlertTesting* tabStripControllerForTesting =
      static_cast<TabStripControllerForAlertTesting*>(controller_);

  TabView* const tab1 = CreateTab();
  TabView* const tab2 = CreateTab();

  // tab2 should be the selected one.
  EXPECT_FALSE([tab1 controller].selected);
  EXPECT_TRUE([tab2 controller].selected);
  WebContents* const contents_at_tab1 = model_->GetActiveWebContents();

  [tabStripControllerForTesting
      setAlertStateForContents:contents_at_tab1
                withAlertState:TabAlertState::AUDIO_PLAYING];
  // Make sure the overriden from base controller correctly handles alert
  // status of tabs.
  EXPECT_EQ(TabAlertState::AUDIO_PLAYING,
            [controller_ alertStateForContents:contents_at_tab1]);
  [controller_ updateWindowAlertState:TabAlertState::AUDIO_PLAYING
                       forWebContents:contents_at_tab1];
  // Because we have one tab playing, and the other one's alert state is none,
  // window alert state should be AUDIO_PLAYING.
  EXPECT_EQ(TabAlertState::AUDIO_PLAYING, [window_controller alertState]);

  model_->ActivateTabAt(0, false);
  // tab1 should be the selected one now.
  EXPECT_TRUE([tab1 controller].selected);
  EXPECT_FALSE([tab2 controller].selected);
  WebContents* const contents_at_tab0 = model_->GetActiveWebContents();

  [tabStripControllerForTesting
      setAlertStateForContents:contents_at_tab0
                withAlertState:TabAlertState::AUDIO_MUTING];
  [controller_ updateWindowAlertState:TabAlertState::AUDIO_MUTING
                       forWebContents:contents_at_tab0];
  // We have two tabs. One is playing and the other one is muting. The window
  // alert state should be still AUDIO_PLAYING.
  EXPECT_EQ(TabAlertState::AUDIO_PLAYING, [window_controller alertState]);

  [tabStripControllerForTesting
      setAlertStateForContents:contents_at_tab1
                withAlertState:TabAlertState::AUDIO_MUTING];
  [controller_ updateWindowAlertState:TabAlertState::AUDIO_MUTING
                       forWebContents:contents_at_tab1];
  // Now both tabs are muting, the window alert state should be AUDIO_MUTING.
  EXPECT_EQ(TabAlertState::AUDIO_MUTING, [window_controller alertState]);

  [tabStripControllerForTesting
      setAlertStateForContents:contents_at_tab0
                withAlertState:TabAlertState::AUDIO_PLAYING];
  [controller_ updateWindowAlertState:TabAlertState::AUDIO_PLAYING
                       forWebContents:contents_at_tab0];
  // Among those tabs which were muting, one is started playing, the window
  // alert state should be playing.
  EXPECT_EQ(TabAlertState::AUDIO_PLAYING, [window_controller alertState]);

  // Mute it again for further testing.
  [tabStripControllerForTesting
      setAlertStateForContents:contents_at_tab0
                withAlertState:TabAlertState::AUDIO_MUTING];
  [controller_ updateWindowAlertState:TabAlertState::AUDIO_MUTING
                       forWebContents:contents_at_tab0];

  [tabStripControllerForTesting setAlertStateForContents:contents_at_tab1
                                          withAlertState:TabAlertState::NONE];
  [controller_ updateWindowAlertState:TabAlertState::NONE
                       forWebContents:contents_at_tab1];
  // One of the tabs is muting, the other one is none. So window alert state
  // should be MUTING.
  EXPECT_EQ(TabAlertState::AUDIO_MUTING, [window_controller alertState]);

  [tabStripControllerForTesting setAlertStateForContents:contents_at_tab0
                                          withAlertState:TabAlertState::NONE];
  [controller_ updateWindowAlertState:TabAlertState::NONE
                       forWebContents:contents_at_tab0];
  // Neither of tabs playing nor muting, so the window alert state should be
  // NONE.
  EXPECT_EQ(TabAlertState::NONE, [window_controller alertState]);
}

}  // namespace