1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/media_picker/desktop_media_picker_controller.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/run_loop.h"
10 #include "chrome/browser/media/desktop_media_list_observer.h"
11 #include "chrome/browser/media/fake_desktop_media_list.h"
12 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "testing/gtest_mac.h"
16 @interface DesktopMediaPickerController (ExposedForTesting)
17 - (IKImageBrowserView*)sourceBrowser;
18 - (NSButton*)okButton;
22 @implementation DesktopMediaPickerController (ExposedForTesting)
23 - (IKImageBrowserView*)sourceBrowser {
24 return sourceBrowser_;
27 - (NSButton*)okButton {
31 - (NSButton*)cancelButton {
40 class DesktopMediaPickerControllerTest : public CocoaTest {
42 DesktopMediaPickerControllerTest()
43 : callback_called_(false), media_list_(NULL) {
46 virtual void SetUp() OVERRIDE {
49 media_list_ = new FakeDesktopMediaList();
51 DesktopMediaPicker::DoneCallback callback =
52 base::Bind(&DesktopMediaPickerControllerTest::OnResult,
53 base::Unretained(this));
56 [[DesktopMediaPickerController alloc]
57 initWithMediaList:scoped_ptr<DesktopMediaList>(media_list_)
60 appName:base::ASCIIToUTF16("Screenshare Test")]);
63 virtual void TearDown() OVERRIDE {
65 CocoaTest::TearDown();
68 bool WaitForCallback() {
69 if (!callback_called_) {
70 base::RunLoop().RunUntilIdle();
72 return callback_called_;
76 void OnResult(content::DesktopMediaID source) {
77 EXPECT_FALSE(callback_called_);
78 callback_called_ = true;
79 source_reported_ = source;
82 content::TestBrowserThreadBundle thread_bundle_;
83 bool callback_called_;
84 content::DesktopMediaID source_reported_;
85 FakeDesktopMediaList* media_list_;
86 base::scoped_nsobject<DesktopMediaPickerController> controller_;
89 TEST_F(DesktopMediaPickerControllerTest, ShowAndDismiss) {
90 [controller_ showWindow:nil];
92 media_list_->AddSource(0);
93 media_list_->AddSource(1);
94 media_list_->SetSourceThumbnail(1);
96 NSArray* items = [controller_ items];
97 EXPECT_EQ(2U, [items count]);
98 EXPECT_NSEQ(@"0", [[items objectAtIndex:0] imageTitle]);
99 EXPECT_EQ(nil, [[items objectAtIndex:0] imageRepresentation]);
100 EXPECT_NSEQ(@"1", [[items objectAtIndex:1] imageTitle]);
101 EXPECT_TRUE([[items objectAtIndex:1] imageRepresentation] != nil);
104 TEST_F(DesktopMediaPickerControllerTest, ClickOK) {
105 [controller_ showWindow:nil];
107 media_list_->AddSource(0);
108 media_list_->SetSourceThumbnail(0);
109 media_list_->AddSource(1);
110 media_list_->SetSourceThumbnail(1);
112 EXPECT_EQ(2U, [[controller_ items] count]);
113 EXPECT_FALSE([[controller_ okButton] isEnabled]);
115 NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:1];
116 [[controller_ sourceBrowser] setSelectionIndexes:indexSet
117 byExtendingSelection:NO];
118 EXPECT_TRUE([[controller_ okButton] isEnabled]);
120 [[controller_ okButton] performClick:nil];
121 EXPECT_TRUE(WaitForCallback());
122 EXPECT_EQ(media_list_->GetSource(1).id, source_reported_);
125 TEST_F(DesktopMediaPickerControllerTest, ClickCancel) {
126 [controller_ showWindow:nil];
128 media_list_->AddSource(0);
129 media_list_->SetSourceThumbnail(0);
130 media_list_->AddSource(1);
131 media_list_->SetSourceThumbnail(1);
133 [[controller_ cancelButton] performClick:nil];
134 EXPECT_TRUE(WaitForCallback());
135 EXPECT_EQ(content::DesktopMediaID(), source_reported_);
138 TEST_F(DesktopMediaPickerControllerTest, CloseWindow) {
139 [controller_ showWindow:nil];
141 media_list_->AddSource(0);
142 media_list_->SetSourceThumbnail(0);
143 media_list_->AddSource(1);
144 media_list_->SetSourceThumbnail(1);
147 EXPECT_TRUE(WaitForCallback());
148 EXPECT_EQ(content::DesktopMediaID(), source_reported_);
151 TEST_F(DesktopMediaPickerControllerTest, UpdateThumbnail) {
152 [controller_ showWindow:nil];
154 media_list_->AddSource(0);
155 media_list_->SetSourceThumbnail(0);
156 media_list_->AddSource(1);
157 media_list_->SetSourceThumbnail(1);
159 NSArray* items = [controller_ items];
160 EXPECT_EQ(2U, [items count]);
161 NSUInteger version = [[items objectAtIndex:0] imageVersion];
163 media_list_->SetSourceThumbnail(0);
164 EXPECT_NE(version, [[items objectAtIndex:0] imageVersion]);
167 TEST_F(DesktopMediaPickerControllerTest, UpdateName) {
168 [controller_ showWindow:nil];
170 media_list_->AddSource(0);
171 media_list_->SetSourceThumbnail(0);
172 media_list_->AddSource(1);
173 media_list_->SetSourceThumbnail(1);
175 NSArray* items = [controller_ items];
176 EXPECT_EQ(2U, [items count]);
177 NSUInteger version = [[items objectAtIndex:0] imageVersion];
179 media_list_->SetSourceThumbnail(0);
180 EXPECT_NE(version, [[items objectAtIndex:0] imageVersion]);
183 TEST_F(DesktopMediaPickerControllerTest, RemoveSource) {
184 [controller_ showWindow:nil];
186 media_list_->AddSource(0);
187 media_list_->AddSource(1);
188 media_list_->AddSource(2);
189 media_list_->SetSourceName(1, base::ASCIIToUTF16("foo"));
191 NSArray* items = [controller_ items];
192 EXPECT_EQ(3U, [items count]);
193 EXPECT_NSEQ(@"foo", [[items objectAtIndex:1] imageTitle]);