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

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

Issue 1412803012: Revert "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: 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
« no previous file with comments | « chrome/browser/ui/cocoa/tabs/tab_strip_controller.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
13 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h" 12 #include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
14 #import "chrome/browser/ui/cocoa/new_tab_button.h" 13 #import "chrome/browser/ui/cocoa/new_tab_button.h"
15 #import "chrome/browser/ui/cocoa/tabs/tab_controller.h" 14 #import "chrome/browser/ui/cocoa/tabs/tab_controller.h"
16 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h" 15 #import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
17 #import "chrome/browser/ui/cocoa/tabs/tab_strip_view.h" 16 #import "chrome/browser/ui/cocoa/tabs/tab_strip_view.h"
18 #import "chrome/browser/ui/cocoa/tabs/tab_view.h" 17 #import "chrome/browser/ui/cocoa/tabs/tab_view.h"
19 #include "chrome/browser/ui/tabs/tab_utils.h" 18 #include "chrome/browser/ui/tabs/tab_utils.h"
20 #include "chrome/browser/ui/tabs/test_tab_strip_model_delegate.h" 19 #include "chrome/browser/ui/tabs/test_tab_strip_model_delegate.h"
21 #include "chrome/test/base/testing_profile.h" 20 #include "chrome/test/base/testing_profile.h"
22 #include "content/public/browser/site_instance.h" 21 #include "content/public/browser/site_instance.h"
23 #include "content/public/browser/web_contents.h" 22 #include "content/public/browser/web_contents.h"
24 #include "content/public/common/media_stream_request.h" 23 #include "content/public/common/media_stream_request.h"
25 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
26 #import "testing/gtest_mac.h" 25 #import "testing/gtest_mac.h"
27 #include "testing/platform_test.h" 26 #include "testing/platform_test.h"
28 #include "ui/events/test/cocoa_test_event_utils.h" 27 #include "ui/events/test/cocoa_test_event_utils.h"
29 28
30 using content::SiteInstance; 29 using content::SiteInstance;
31 using content::WebContents; 30 using content::WebContents;
32 31
33 @interface TabStripControllerForMediaTesting : TabStripController {
34 // Keeps media state of tabs in browser for testing purpose.
35 std::map<content::WebContents*, TabMediaState> contents_media_state_maps;
36 }
37 @end
38
39 @implementation TabStripControllerForMediaTesting
40 // Returns the media state of each tab from the map we are keeping.
41 - (TabMediaState)mediaStateForContents:(content::WebContents*)contents {
42 return contents_media_state_maps[contents];
43 }
44
45 - (void)setMediaStateForContents:(content::WebContents*)contents
46 withMediaState:(TabMediaState)media_state {
47 contents_media_state_maps[contents] = media_state;
48 }
49
50 @end
51
52 @interface TestTabStripControllerDelegate 32 @interface TestTabStripControllerDelegate
53 : NSObject<TabStripControllerDelegate> { 33 : NSObject<TabStripControllerDelegate> {
54 } 34 }
55 @end 35 @end
56 36
57 @implementation TestTabStripControllerDelegate 37 @implementation TestTabStripControllerDelegate
58 - (void)onActivateTabWithContents:(WebContents*)contents { 38 - (void)onActivateTabWithContents:(WebContents*)contents {
59 } 39 }
60 - (void)onTabChanged:(TabStripModelObserver::TabChangeType)change 40 - (void)onTabChanged:(TabStripModelObserver::TabChangeType)change
61 withContents:(WebContents*)contents { 41 withContents:(WebContents*)contents {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 95
116 NSWindow* window = browser()->window()->GetNativeWindow(); 96 NSWindow* window = browser()->window()->GetNativeWindow();
117 NSView* parent = [window contentView]; 97 NSView* parent = [window contentView];
118 NSRect content_frame = [parent frame]; 98 NSRect content_frame = [parent frame];
119 99
120 // Create the "switch view" (view that gets changed out when a tab 100 // Create the "switch view" (view that gets changed out when a tab
121 // switches). 101 // switches).
122 NSRect switch_frame = NSMakeRect(0, 0, content_frame.size.width, 500); 102 NSRect switch_frame = NSMakeRect(0, 0, content_frame.size.width, 500);
123 base::scoped_nsobject<NSView> switch_view( 103 base::scoped_nsobject<NSView> switch_view(
124 [[NSView alloc] initWithFrame:switch_frame]); 104 [[NSView alloc] initWithFrame:switch_frame]);
125 switch_view_ = switch_view; 105 [parent addSubview:switch_view.get()];
126 [parent addSubview:switch_view_.get()];
127 106
128 // Create the tab strip view. It's expected to have a child button in it 107 // Create the tab strip view. It's expected to have a child button in it
129 // already as the "new tab" button so create that too. 108 // already as the "new tab" button so create that too.
130 NSRect strip_frame = NSMakeRect(0, NSMaxY(switch_frame), 109 NSRect strip_frame = NSMakeRect(0, NSMaxY(switch_frame),
131 content_frame.size.width, 30); 110 content_frame.size.width, 30);
132 tab_strip_.reset( 111 tab_strip_.reset(
133 [[TabStripView alloc] initWithFrame:strip_frame]); 112 [[TabStripView alloc] initWithFrame:strip_frame]);
134 [parent addSubview:tab_strip_.get()]; 113 [parent addSubview:tab_strip_.get()];
135 NSRect button_frame = NSMakeRect(0, 0, 15, 15); 114 NSRect button_frame = NSMakeRect(0, 0, 15, 15);
136 base::scoped_nsobject<NewTabButton> new_tab_button( 115 base::scoped_nsobject<NewTabButton> new_tab_button(
(...skipping 12 matching lines...) Expand all
149 } 128 }
150 129
151 void TearDown() override { 130 void TearDown() override {
152 // The call to CocoaTest::TearDown() deletes the Browser and TabStripModel 131 // The call to CocoaTest::TearDown() deletes the Browser and TabStripModel
153 // objects, so we first have to delete the controller, which refers to them. 132 // objects, so we first have to delete the controller, which refers to them.
154 controller_.reset(); 133 controller_.reset();
155 model_ = NULL; 134 model_ = NULL;
156 CocoaProfileTest::TearDown(); 135 CocoaProfileTest::TearDown();
157 } 136 }
158 137
159 // Return a derived TabStripController.
160 TabStripControllerForMediaTesting* InitTabStripControllerForMediaTesting() {
161 TabStripControllerForMediaTesting* c =
162 [[TabStripControllerForMediaTesting alloc]
163 initWithView:static_cast<TabStripView*>(tab_strip_.get())
164 switchView:switch_view_.get()
165 browser:browser()
166 delegate:controller_delegate_.get()];
167 return c;
168 }
169
170 TabView* CreateTab() { 138 TabView* CreateTab() {
171 SiteInstance* instance = SiteInstance::Create(profile()); 139 SiteInstance* instance = SiteInstance::Create(profile());
172 WebContents* web_contents = WebContents::Create( 140 WebContents* web_contents = WebContents::Create(
173 content::WebContents::CreateParams(profile(), instance)); 141 content::WebContents::CreateParams(profile(), instance));
174 model_->AppendWebContents(web_contents, true); 142 model_->AppendWebContents(web_contents, true);
175 const NSUInteger tab_count = [controller_.get() viewsCount]; 143 const NSUInteger tab_count = [controller_.get() viewsCount];
176 return static_cast<TabView*>([controller_.get() viewAtIndex:tab_count - 1]); 144 return static_cast<TabView*>([controller_.get() viewAtIndex:tab_count - 1]);
177 } 145 }
178 146
179 // Closes all tabs and unrefs the tabstrip and then posts a NSLeftMouseUp 147 // Closes all tabs and unrefs the tabstrip and then posts a NSLeftMouseUp
180 // event which should end the nested drag event loop. 148 // event which should end the nested drag event loop.
181 void CloseTabsAndEndDrag() { 149 void CloseTabsAndEndDrag() {
182 // Simulate a close of the browser window. 150 // Simulate a close of the browser window.
183 model_->CloseAllTabs(); 151 model_->CloseAllTabs();
184 controller_.reset(); 152 controller_.reset();
185 tab_strip_.reset(); 153 tab_strip_.reset();
186 // Schedule a NSLeftMouseUp to end the nested drag event loop. 154 // Schedule a NSLeftMouseUp to end the nested drag event loop.
187 NSEvent* event = 155 NSEvent* event =
188 cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, 0); 156 cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, 0);
189 [NSApp postEvent:event atStart:NO]; 157 [NSApp postEvent:event atStart:NO];
190 } 158 }
191 159
192 scoped_ptr<TestTabStripModelDelegate> delegate_; 160 scoped_ptr<TestTabStripModelDelegate> delegate_;
193 TabStripModel* model_; 161 TabStripModel* model_;
194 base::scoped_nsobject<TestTabStripControllerDelegate> controller_delegate_; 162 base::scoped_nsobject<TestTabStripControllerDelegate> controller_delegate_;
195 base::scoped_nsobject<TabStripController> controller_; 163 base::scoped_nsobject<TabStripController> controller_;
196 base::scoped_nsobject<TabStripView> tab_strip_; 164 base::scoped_nsobject<TabStripView> tab_strip_;
197 base::scoped_nsobject<NSView> switch_view_;
198 }; 165 };
199 166
200 // Test adding and removing tabs and making sure that views get added to 167 // Test adding and removing tabs and making sure that views get added to
201 // the tab strip. 168 // the tab strip.
202 TEST_F(TabStripControllerTest, AddRemoveTabs) { 169 TEST_F(TabStripControllerTest, AddRemoveTabs) {
203 EXPECT_TRUE(model_->empty()); 170 EXPECT_TRUE(model_->empty());
204 CreateTab(); 171 CreateTab();
205 EXPECT_EQ(model_->count(), 1); 172 EXPECT_EQ(model_->count(), 1);
206 } 173 }
207 174
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 EXPECT_EQ(tab2, value); 342 EXPECT_EQ(tab2, value);
376 343
377 model_->ActivateTabAt(0, false); 344 model_->ActivateTabAt(0, false);
378 EXPECT_TRUE([tab1 controller].selected); 345 EXPECT_TRUE([tab1 controller].selected);
379 EXPECT_FALSE([tab2 controller].selected); 346 EXPECT_FALSE([tab2 controller].selected);
380 value = 347 value =
381 [tab_strip_ accessibilityAttributeValue:NSAccessibilityValueAttribute]; 348 [tab_strip_ accessibilityAttributeValue:NSAccessibilityValueAttribute];
382 EXPECT_EQ(tab1, value); 349 EXPECT_EQ(tab1, value);
383 } 350 }
384 351
385 TEST_F(TabStripControllerTest, CorrectWindowFromUpdateWindowMediaState) {
386 controller_.reset(InitTabStripControllerForMediaTesting());
387 NSWindow* window = [tab_strip_ window];
388 BrowserWindowController* window_controller =
389 [BrowserWindowController browserWindowControllerForWindow:window];
390 TabStripControllerForMediaTesting* tabStripControllerForTesting =
391 static_cast<TabStripControllerForMediaTesting*>(controller_);
392
393 TabView* const tab1 = CreateTab();
394 TabView* const tab2 = CreateTab();
395
396 // tab2 should be the selected one.
397 EXPECT_FALSE([tab1 controller].selected);
398 EXPECT_TRUE([tab2 controller].selected);
399 WebContents* const contents_at_tab1 = model_->GetActiveWebContents();
400
401 [tabStripControllerForTesting
402 setMediaStateForContents:contents_at_tab1
403 withMediaState:TAB_MEDIA_STATE_AUDIO_PLAYING];
404 // Make sure the overriden from base controller correctly handles media
405 // status of tabs.
406 EXPECT_EQ(TAB_MEDIA_STATE_AUDIO_PLAYING,
407 [controller_ mediaStateForContents:contents_at_tab1]);
408 [controller_ updateWindowMediaState:TAB_MEDIA_STATE_AUDIO_PLAYING
409 on:contents_at_tab1];
410 // Because we have one tab playing, and the other one's media state is none,
411 // window media state should be AUDIO_PLAYING.
412 EXPECT_EQ(TAB_MEDIA_STATE_AUDIO_PLAYING, [window_controller mediaState]);
413
414 model_->ActivateTabAt(0, false);
415 // tab1 should be the selected one now.
416 EXPECT_TRUE([tab1 controller].selected);
417 EXPECT_FALSE([tab2 controller].selected);
418 WebContents* const contents_at_tab0 = model_->GetActiveWebContents();
419
420 [tabStripControllerForTesting
421 setMediaStateForContents:contents_at_tab0
422 withMediaState:TAB_MEDIA_STATE_AUDIO_MUTING];
423 [controller_ updateWindowMediaState:TAB_MEDIA_STATE_AUDIO_MUTING
424 on:contents_at_tab0];
425 // We have two tabs. One is playing and the other one is muting. The window
426 // media state should be still AUDIO_PLAYING.
427 EXPECT_EQ(TAB_MEDIA_STATE_AUDIO_PLAYING, [window_controller mediaState]);
428
429 [tabStripControllerForTesting
430 setMediaStateForContents:contents_at_tab1
431 withMediaState:TAB_MEDIA_STATE_AUDIO_MUTING];
432 [controller_ updateWindowMediaState:TAB_MEDIA_STATE_AUDIO_MUTING
433 on:contents_at_tab1];
434 // Now both tabs are muting, the window media state should be AUDIO_MUTING.
435 EXPECT_EQ(TAB_MEDIA_STATE_AUDIO_MUTING, [window_controller mediaState]);
436
437 [tabStripControllerForTesting
438 setMediaStateForContents:contents_at_tab0
439 withMediaState:TAB_MEDIA_STATE_AUDIO_PLAYING];
440 [controller_ updateWindowMediaState:TAB_MEDIA_STATE_AUDIO_PLAYING
441 on:contents_at_tab0];
442 // Among those tabs which were muting, one is started playing, the window
443 // media state should be playing.
444 EXPECT_EQ(TAB_MEDIA_STATE_AUDIO_PLAYING, [window_controller mediaState]);
445
446 // Mute it again for further testing.
447 [tabStripControllerForTesting
448 setMediaStateForContents:contents_at_tab0
449 withMediaState:TAB_MEDIA_STATE_AUDIO_MUTING];
450 [controller_ updateWindowMediaState:TAB_MEDIA_STATE_AUDIO_MUTING
451 on:contents_at_tab0];
452
453 [tabStripControllerForTesting setMediaStateForContents:contents_at_tab1
454 withMediaState:TAB_MEDIA_STATE_NONE];
455 [controller_ updateWindowMediaState:TAB_MEDIA_STATE_NONE on:contents_at_tab1];
456 // One of the tabs is muting, the other one is none. So window media state
457 // should be MUTING.
458 EXPECT_EQ(TAB_MEDIA_STATE_AUDIO_MUTING, [window_controller mediaState]);
459
460 [tabStripControllerForTesting setMediaStateForContents:contents_at_tab0
461 withMediaState:TAB_MEDIA_STATE_NONE];
462 [controller_ updateWindowMediaState:TAB_MEDIA_STATE_NONE on:contents_at_tab0];
463 // Neither of tabs playing nor muting, so the window media state should be
464 // NONE.
465 EXPECT_EQ(TAB_MEDIA_STATE_NONE, [window_controller mediaState]);
466 }
467
468 } // namespace 352 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/ui/cocoa/tabs/tab_strip_controller.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698