Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: chrome/browser/ui/cocoa/tabs/tab_strip_controller_unittest.mm

Issue 1412083002: Indicate in the Window menu which Chrome window has an active sound playing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: put Emojis in generated_resources.grd Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/bind_helpers.h" 7 #include "base/bind_helpers.h"
8 #include "base/mac/scoped_nsautorelease_pool.h" 8 #include "base/mac/scoped_nsautorelease_pool.h"
9 #include "chrome/browser/media/media_capture_devices_dispatcher.h" 9 #include "chrome/browser/media/media_capture_devices_dispatcher.h"
10 #include "chrome/browser/media/media_stream_capture_indicator.h" 10 #include "chrome/browser/media/media_stream_capture_indicator.h"
11 #include "chrome/browser/ui/browser_window.h" 11 #include "chrome/browser/ui/browser_window.h"
12 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
12 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" 13 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
13 #import "chrome/browser/ui/cocoa/new_tab_button.h" 14 #import "chrome/browser/ui/cocoa/new_tab_button.h"
14 #import "chrome/browser/ui/cocoa/tabs/tab_controller.h" 15 #import "chrome/browser/ui/cocoa/tabs/tab_controller.h"
15 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h" 16 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
16 #import "chrome/browser/ui/cocoa/tabs/tab_strip_view.h" 17 #import "chrome/browser/ui/cocoa/tabs/tab_strip_view.h"
17 #import "chrome/browser/ui/cocoa/tabs/tab_view.h" 18 #import "chrome/browser/ui/cocoa/tabs/tab_view.h"
18 #include "chrome/browser/ui/tabs/tab_utils.h" 19 #include "chrome/browser/ui/tabs/tab_utils.h"
19 #include "chrome/browser/ui/tabs/test_tab_strip_model_delegate.h" 20 #include "chrome/browser/ui/tabs/test_tab_strip_model_delegate.h"
20 #include "chrome/test/base/testing_profile.h" 21 #include "chrome/test/base/testing_profile.h"
21 #include "content/public/browser/site_instance.h" 22 #include "content/public/browser/site_instance.h"
22 #include "content/public/browser/web_contents.h" 23 #include "content/public/browser/web_contents.h"
23 #include "content/public/common/media_stream_request.h" 24 #include "content/public/common/media_stream_request.h"
24 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
25 #import "testing/gtest_mac.h" 26 #import "testing/gtest_mac.h"
26 #include "testing/platform_test.h" 27 #include "testing/platform_test.h"
27 #include "ui/events/test/cocoa_test_event_utils.h" 28 #include "ui/events/test/cocoa_test_event_utils.h"
28 29
29 using content::SiteInstance; 30 using content::SiteInstance;
30 using content::WebContents; 31 using content::WebContents;
31 32
33 // Keeps media state of tabs in browser for testing purpose.
34 static std::map<content::WebContents*, TabMediaState>
Robert Sesek 2015/10/28 18:45:58 Rather than keeping this in a static, store this i
35 contents_media_state_maps_;
36
37 @interface TabStripControllerForMediaTesting : TabStripController {
38 }
39 @end
40
41 @implementation TabStripControllerForMediaTesting
42 // Returns the media state of each tab from the map we are keeping.
43 - (TabMediaState)getMediaStateForContents:(content::WebContents*)contents {
44 return contents_media_state_maps_[contents];
45 }
46 @end
47
32 @interface TestTabStripControllerDelegate 48 @interface TestTabStripControllerDelegate
33 : NSObject<TabStripControllerDelegate> { 49 : NSObject<TabStripControllerDelegate> {
34 } 50 }
35 @end 51 @end
36 52
37 @implementation TestTabStripControllerDelegate 53 @implementation TestTabStripControllerDelegate
38 - (void)onActivateTabWithContents:(WebContents*)contents { 54 - (void)onActivateTabWithContents:(WebContents*)contents {
39 } 55 }
40 - (void)onTabChanged:(TabStripModelObserver::TabChangeType)change 56 - (void)onTabChanged:(TabStripModelObserver::TabChangeType)change
41 withContents:(WebContents*)contents { 57 withContents:(WebContents*)contents {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 111
96 NSWindow* window = browser()->window()->GetNativeWindow(); 112 NSWindow* window = browser()->window()->GetNativeWindow();
97 NSView* parent = [window contentView]; 113 NSView* parent = [window contentView];
98 NSRect content_frame = [parent frame]; 114 NSRect content_frame = [parent frame];
99 115
100 // Create the "switch view" (view that gets changed out when a tab 116 // Create the "switch view" (view that gets changed out when a tab
101 // switches). 117 // switches).
102 NSRect switch_frame = NSMakeRect(0, 0, content_frame.size.width, 500); 118 NSRect switch_frame = NSMakeRect(0, 0, content_frame.size.width, 500);
103 base::scoped_nsobject<NSView> switch_view( 119 base::scoped_nsobject<NSView> switch_view(
104 [[NSView alloc] initWithFrame:switch_frame]); 120 [[NSView alloc] initWithFrame:switch_frame]);
105 [parent addSubview:switch_view.get()]; 121 switch_view_ = switch_view;
122 [parent addSubview:switch_view_.get()];
106 123
107 // Create the tab strip view. It's expected to have a child button in it 124 // Create the tab strip view. It's expected to have a child button in it
108 // already as the "new tab" button so create that too. 125 // already as the "new tab" button so create that too.
109 NSRect strip_frame = NSMakeRect(0, NSMaxY(switch_frame), 126 NSRect strip_frame = NSMakeRect(0, NSMaxY(switch_frame),
110 content_frame.size.width, 30); 127 content_frame.size.width, 30);
111 tab_strip_.reset( 128 tab_strip_.reset(
112 [[TabStripView alloc] initWithFrame:strip_frame]); 129 [[TabStripView alloc] initWithFrame:strip_frame]);
113 [parent addSubview:tab_strip_.get()]; 130 [parent addSubview:tab_strip_.get()];
114 NSRect button_frame = NSMakeRect(0, 0, 15, 15); 131 NSRect button_frame = NSMakeRect(0, 0, 15, 15);
115 base::scoped_nsobject<NewTabButton> new_tab_button( 132 base::scoped_nsobject<NewTabButton> new_tab_button(
(...skipping 12 matching lines...) Expand all
128 } 145 }
129 146
130 void TearDown() override { 147 void TearDown() override {
131 // The call to CocoaTest::TearDown() deletes the Browser and TabStripModel 148 // The call to CocoaTest::TearDown() deletes the Browser and TabStripModel
132 // objects, so we first have to delete the controller, which refers to them. 149 // objects, so we first have to delete the controller, which refers to them.
133 controller_.reset(); 150 controller_.reset();
134 model_ = NULL; 151 model_ = NULL;
135 CocoaProfileTest::TearDown(); 152 CocoaProfileTest::TearDown();
136 } 153 }
137 154
155 // Return a derived TabStripController.
156 TabStripControllerForMediaTesting* InitTabStripControllerForMediaTesting() {
157 TabStripControllerForMediaTesting* c =
158 [[TabStripControllerForMediaTesting alloc]
159 initWithView:static_cast<TabStripView*>(tab_strip_.get())
160 switchView:switch_view_.get()
161 browser:browser()
162 delegate:controller_delegate_.get()];
163 return c;
164 }
165
138 TabView* CreateTab() { 166 TabView* CreateTab() {
139 SiteInstance* instance = SiteInstance::Create(profile()); 167 SiteInstance* instance = SiteInstance::Create(profile());
140 WebContents* web_contents = WebContents::Create( 168 WebContents* web_contents = WebContents::Create(
141 content::WebContents::CreateParams(profile(), instance)); 169 content::WebContents::CreateParams(profile(), instance));
142 model_->AppendWebContents(web_contents, true); 170 model_->AppendWebContents(web_contents, true);
143 const NSUInteger tab_count = [controller_.get() viewsCount]; 171 const NSUInteger tab_count = [controller_.get() viewsCount];
144 return static_cast<TabView*>([controller_.get() viewAtIndex:tab_count - 1]); 172 return static_cast<TabView*>([controller_.get() viewAtIndex:tab_count - 1]);
145 } 173 }
146 174
147 // Closes all tabs and unrefs the tabstrip and then posts a NSLeftMouseUp 175 // Closes all tabs and unrefs the tabstrip and then posts a NSLeftMouseUp
148 // event which should end the nested drag event loop. 176 // event which should end the nested drag event loop.
149 void CloseTabsAndEndDrag() { 177 void CloseTabsAndEndDrag() {
150 // Simulate a close of the browser window. 178 // Simulate a close of the browser window.
151 model_->CloseAllTabs(); 179 model_->CloseAllTabs();
152 controller_.reset(); 180 controller_.reset();
153 tab_strip_.reset(); 181 tab_strip_.reset();
154 // Schedule a NSLeftMouseUp to end the nested drag event loop. 182 // Schedule a NSLeftMouseUp to end the nested drag event loop.
155 NSEvent* event = 183 NSEvent* event =
156 cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, 0); 184 cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, 0);
157 [NSApp postEvent:event atStart:NO]; 185 [NSApp postEvent:event atStart:NO];
158 } 186 }
159 187
160 scoped_ptr<TestTabStripModelDelegate> delegate_; 188 scoped_ptr<TestTabStripModelDelegate> delegate_;
161 TabStripModel* model_; 189 TabStripModel* model_;
162 base::scoped_nsobject<TestTabStripControllerDelegate> controller_delegate_; 190 base::scoped_nsobject<TestTabStripControllerDelegate> controller_delegate_;
163 base::scoped_nsobject<TabStripController> controller_; 191 base::scoped_nsobject<TabStripController> controller_;
164 base::scoped_nsobject<TabStripView> tab_strip_; 192 base::scoped_nsobject<TabStripView> tab_strip_;
193 base::scoped_nsobject<NSView> switch_view_;
165 }; 194 };
166 195
167 // Test adding and removing tabs and making sure that views get added to 196 // Test adding and removing tabs and making sure that views get added to
168 // the tab strip. 197 // the tab strip.
169 TEST_F(TabStripControllerTest, AddRemoveTabs) { 198 TEST_F(TabStripControllerTest, AddRemoveTabs) {
170 EXPECT_TRUE(model_->empty()); 199 EXPECT_TRUE(model_->empty());
171 CreateTab(); 200 CreateTab();
172 EXPECT_EQ(model_->count(), 1); 201 EXPECT_EQ(model_->count(), 1);
173 } 202 }
174 203
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 EXPECT_EQ(tab2, value); 371 EXPECT_EQ(tab2, value);
343 372
344 model_->ActivateTabAt(0, false); 373 model_->ActivateTabAt(0, false);
345 EXPECT_TRUE([tab1 controller].selected); 374 EXPECT_TRUE([tab1 controller].selected);
346 EXPECT_FALSE([tab2 controller].selected); 375 EXPECT_FALSE([tab2 controller].selected);
347 value = 376 value =
348 [tab_strip_ accessibilityAttributeValue:NSAccessibilityValueAttribute]; 377 [tab_strip_ accessibilityAttributeValue:NSAccessibilityValueAttribute];
349 EXPECT_EQ(tab1, value); 378 EXPECT_EQ(tab1, value);
350 } 379 }
351 380
381 TEST_F(TabStripControllerTest, CorrectWindowFromUpdateWindowMediaState) {
382 controller_.reset(InitTabStripControllerForMediaTesting());
383 NSWindow* window = [tab_strip_ window];
384 BrowserWindowController* windowController =
Robert Sesek 2015/10/28 18:45:58 naming: Use under_scores for variable names, excep
385 [BrowserWindowController browserWindowControllerForWindow:window];
386
387 TabView* const tab1 = CreateTab();
388 TabView* const tab2 = CreateTab();
389
390 // tab2 should be the selected one.
391 EXPECT_FALSE([tab1 controller].selected);
392 EXPECT_TRUE([tab2 controller].selected);
393 WebContents* const contentsAtTab1 = model_->GetActiveWebContents();
394
395 contents_media_state_maps_[contentsAtTab1] = TAB_MEDIA_STATE_AUDIO_PLAYING;
396 // Make sure the overriden from base controller correctly handles media
397 // status of tabs.
398 EXPECT_EQ(TAB_MEDIA_STATE_AUDIO_PLAYING,
399 [controller_ getMediaStateForContents:contentsAtTab1]);
400 [controller_ updateWindowMediaState:TAB_MEDIA_STATE_AUDIO_PLAYING
401 on:contentsAtTab1];
402 // Because we have one tab playing, and the other one's media state is none,
403 // window media state should be AUDIO_PLAYING.
404 EXPECT_EQ(TAB_MEDIA_STATE_AUDIO_PLAYING, [windowController getMediaState]);
405
406 model_->ActivateTabAt(0, false);
407 // tab1 should be the selected one now.
408 EXPECT_TRUE([tab1 controller].selected);
409 EXPECT_FALSE([tab2 controller].selected);
410 WebContents* const contentsAtTab0 = model_->GetActiveWebContents();
411
412 contents_media_state_maps_[contentsAtTab0] = TAB_MEDIA_STATE_AUDIO_MUTING;
413 [controller_ updateWindowMediaState:TAB_MEDIA_STATE_AUDIO_MUTING
414 on:contentsAtTab0];
415 // We have two tabs. One is playing and the other one is muting. The window
416 // media state should be still AUDIO_PLAYING.
417 EXPECT_EQ(TAB_MEDIA_STATE_AUDIO_PLAYING, [windowController getMediaState]);
418
419 contents_media_state_maps_[contentsAtTab1] = TAB_MEDIA_STATE_AUDIO_MUTING;
420 [controller_ updateWindowMediaState:TAB_MEDIA_STATE_AUDIO_MUTING
421 on:contentsAtTab1];
422 // Now both tabs are muting, the window media state should be AUDIO_MUTING.
423 EXPECT_EQ(TAB_MEDIA_STATE_AUDIO_MUTING, [windowController getMediaState]);
424
425 contents_media_state_maps_[contentsAtTab0] = TAB_MEDIA_STATE_AUDIO_PLAYING;
426 [controller_ updateWindowMediaState:TAB_MEDIA_STATE_AUDIO_PLAYING
427 on:contentsAtTab0];
428 // Among those tabs which were muting, one is started playing, the window
429 // media state should be playing.
430 EXPECT_EQ(TAB_MEDIA_STATE_AUDIO_PLAYING, [windowController getMediaState]);
431
432 // Mute it again for further testing.
433 contents_media_state_maps_[contentsAtTab0] = TAB_MEDIA_STATE_AUDIO_MUTING;
434 [controller_ updateWindowMediaState:TAB_MEDIA_STATE_AUDIO_MUTING
435 on:contentsAtTab0];
436
437 contents_media_state_maps_[contentsAtTab1] = TAB_MEDIA_STATE_NONE;
438 [controller_ updateWindowMediaState:TAB_MEDIA_STATE_NONE on:contentsAtTab1];
439 // One of the tabs is muting, the other one is none. So window media state
440 // should be MUTING.
441 EXPECT_EQ(TAB_MEDIA_STATE_AUDIO_MUTING, [windowController getMediaState]);
442
443 contents_media_state_maps_[contentsAtTab0] = TAB_MEDIA_STATE_NONE;
444 [controller_ updateWindowMediaState:TAB_MEDIA_STATE_NONE on:contentsAtTab0];
445 // Neither of tabs playing nor muting, so the window media state should be
446 // NONE.
447 EXPECT_EQ(TAB_MEDIA_STATE_NONE, [windowController getMediaState]);
448 }
449
352 } // namespace 450 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698