OLD | NEW |
---|---|
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 @interface TabStripControllerForMediaTesting : TabStripController { | |
34 // Keeps media state of tabs in browser for testing purpose. | |
35 std::map<content::WebContents*, TabMediaState> contents_media_state_maps; | |
Robert Sesek
2015/11/09 23:41:25
naming: contentsMediaStateMaps_
| |
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 | |
32 @interface TestTabStripControllerDelegate | 52 @interface TestTabStripControllerDelegate |
33 : NSObject<TabStripControllerDelegate> { | 53 : NSObject<TabStripControllerDelegate> { |
34 } | 54 } |
35 @end | 55 @end |
36 | 56 |
37 @implementation TestTabStripControllerDelegate | 57 @implementation TestTabStripControllerDelegate |
38 - (void)onActivateTabWithContents:(WebContents*)contents { | 58 - (void)onActivateTabWithContents:(WebContents*)contents { |
39 } | 59 } |
40 - (void)onTabChanged:(TabStripModelObserver::TabChangeType)change | 60 - (void)onTabChanged:(TabStripModelObserver::TabChangeType)change |
41 withContents:(WebContents*)contents { | 61 withContents:(WebContents*)contents { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 | 115 |
96 NSWindow* window = browser()->window()->GetNativeWindow(); | 116 NSWindow* window = browser()->window()->GetNativeWindow(); |
97 NSView* parent = [window contentView]; | 117 NSView* parent = [window contentView]; |
98 NSRect content_frame = [parent frame]; | 118 NSRect content_frame = [parent frame]; |
99 | 119 |
100 // Create the "switch view" (view that gets changed out when a tab | 120 // Create the "switch view" (view that gets changed out when a tab |
101 // switches). | 121 // switches). |
102 NSRect switch_frame = NSMakeRect(0, 0, content_frame.size.width, 500); | 122 NSRect switch_frame = NSMakeRect(0, 0, content_frame.size.width, 500); |
103 base::scoped_nsobject<NSView> switch_view( | 123 base::scoped_nsobject<NSView> switch_view( |
104 [[NSView alloc] initWithFrame:switch_frame]); | 124 [[NSView alloc] initWithFrame:switch_frame]); |
105 [parent addSubview:switch_view.get()]; | 125 switch_view_ = switch_view; |
126 [parent addSubview:switch_view_.get()]; | |
106 | 127 |
107 // Create the tab strip view. It's expected to have a child button in it | 128 // 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. | 129 // already as the "new tab" button so create that too. |
109 NSRect strip_frame = NSMakeRect(0, NSMaxY(switch_frame), | 130 NSRect strip_frame = NSMakeRect(0, NSMaxY(switch_frame), |
110 content_frame.size.width, 30); | 131 content_frame.size.width, 30); |
111 tab_strip_.reset( | 132 tab_strip_.reset( |
112 [[TabStripView alloc] initWithFrame:strip_frame]); | 133 [[TabStripView alloc] initWithFrame:strip_frame]); |
113 [parent addSubview:tab_strip_.get()]; | 134 [parent addSubview:tab_strip_.get()]; |
114 NSRect button_frame = NSMakeRect(0, 0, 15, 15); | 135 NSRect button_frame = NSMakeRect(0, 0, 15, 15); |
115 base::scoped_nsobject<NewTabButton> new_tab_button( | 136 base::scoped_nsobject<NewTabButton> new_tab_button( |
(...skipping 12 matching lines...) Expand all Loading... | |
128 } | 149 } |
129 | 150 |
130 void TearDown() override { | 151 void TearDown() override { |
131 // The call to CocoaTest::TearDown() deletes the Browser and TabStripModel | 152 // The call to CocoaTest::TearDown() deletes the Browser and TabStripModel |
132 // objects, so we first have to delete the controller, which refers to them. | 153 // objects, so we first have to delete the controller, which refers to them. |
133 controller_.reset(); | 154 controller_.reset(); |
134 model_ = NULL; | 155 model_ = NULL; |
135 CocoaProfileTest::TearDown(); | 156 CocoaProfileTest::TearDown(); |
136 } | 157 } |
137 | 158 |
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 | |
138 TabView* CreateTab() { | 170 TabView* CreateTab() { |
139 SiteInstance* instance = SiteInstance::Create(profile()); | 171 SiteInstance* instance = SiteInstance::Create(profile()); |
140 WebContents* web_contents = WebContents::Create( | 172 WebContents* web_contents = WebContents::Create( |
141 content::WebContents::CreateParams(profile(), instance)); | 173 content::WebContents::CreateParams(profile(), instance)); |
142 model_->AppendWebContents(web_contents, true); | 174 model_->AppendWebContents(web_contents, true); |
143 const NSUInteger tab_count = [controller_.get() viewsCount]; | 175 const NSUInteger tab_count = [controller_.get() viewsCount]; |
144 return static_cast<TabView*>([controller_.get() viewAtIndex:tab_count - 1]); | 176 return static_cast<TabView*>([controller_.get() viewAtIndex:tab_count - 1]); |
145 } | 177 } |
146 | 178 |
147 // Closes all tabs and unrefs the tabstrip and then posts a NSLeftMouseUp | 179 // Closes all tabs and unrefs the tabstrip and then posts a NSLeftMouseUp |
148 // event which should end the nested drag event loop. | 180 // event which should end the nested drag event loop. |
149 void CloseTabsAndEndDrag() { | 181 void CloseTabsAndEndDrag() { |
150 // Simulate a close of the browser window. | 182 // Simulate a close of the browser window. |
151 model_->CloseAllTabs(); | 183 model_->CloseAllTabs(); |
152 controller_.reset(); | 184 controller_.reset(); |
153 tab_strip_.reset(); | 185 tab_strip_.reset(); |
154 // Schedule a NSLeftMouseUp to end the nested drag event loop. | 186 // Schedule a NSLeftMouseUp to end the nested drag event loop. |
155 NSEvent* event = | 187 NSEvent* event = |
156 cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, 0); | 188 cocoa_test_event_utils::MouseEventWithType(NSLeftMouseUp, 0); |
157 [NSApp postEvent:event atStart:NO]; | 189 [NSApp postEvent:event atStart:NO]; |
158 } | 190 } |
159 | 191 |
160 scoped_ptr<TestTabStripModelDelegate> delegate_; | 192 scoped_ptr<TestTabStripModelDelegate> delegate_; |
161 TabStripModel* model_; | 193 TabStripModel* model_; |
162 base::scoped_nsobject<TestTabStripControllerDelegate> controller_delegate_; | 194 base::scoped_nsobject<TestTabStripControllerDelegate> controller_delegate_; |
163 base::scoped_nsobject<TabStripController> controller_; | 195 base::scoped_nsobject<TabStripController> controller_; |
164 base::scoped_nsobject<TabStripView> tab_strip_; | 196 base::scoped_nsobject<TabStripView> tab_strip_; |
197 base::scoped_nsobject<NSView> switch_view_; | |
165 }; | 198 }; |
166 | 199 |
167 // Test adding and removing tabs and making sure that views get added to | 200 // Test adding and removing tabs and making sure that views get added to |
168 // the tab strip. | 201 // the tab strip. |
169 TEST_F(TabStripControllerTest, AddRemoveTabs) { | 202 TEST_F(TabStripControllerTest, AddRemoveTabs) { |
170 EXPECT_TRUE(model_->empty()); | 203 EXPECT_TRUE(model_->empty()); |
171 CreateTab(); | 204 CreateTab(); |
172 EXPECT_EQ(model_->count(), 1); | 205 EXPECT_EQ(model_->count(), 1); |
173 } | 206 } |
174 | 207 |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
342 EXPECT_EQ(tab2, value); | 375 EXPECT_EQ(tab2, value); |
343 | 376 |
344 model_->ActivateTabAt(0, false); | 377 model_->ActivateTabAt(0, false); |
345 EXPECT_TRUE([tab1 controller].selected); | 378 EXPECT_TRUE([tab1 controller].selected); |
346 EXPECT_FALSE([tab2 controller].selected); | 379 EXPECT_FALSE([tab2 controller].selected); |
347 value = | 380 value = |
348 [tab_strip_ accessibilityAttributeValue:NSAccessibilityValueAttribute]; | 381 [tab_strip_ accessibilityAttributeValue:NSAccessibilityValueAttribute]; |
349 EXPECT_EQ(tab1, value); | 382 EXPECT_EQ(tab1, value); |
350 } | 383 } |
351 | 384 |
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 | |
352 } // namespace | 468 } // namespace |
OLD | NEW |