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 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
11 #include "content/public/test/test_browser_thread_bundle.h"
12 #include "testing/gtest_mac.h"
14 @interface DesktopMediaPickerController (ExposedForTesting)
15 - (IKImageBrowserView*)sourceBrowser;
16 - (NSButton*)okButton;
20 @implementation DesktopMediaPickerController (ExposedForTesting)
21 - (IKImageBrowserView*)sourceBrowser {
22 return sourceBrowser_;
25 - (NSButton*)okButton {
29 - (NSButton*)cancelButton {
38 class FakeDesktopMediaPickerModel : public DesktopMediaPickerModel {
40 FakeDesktopMediaPickerModel() : observer_(NULL) {
43 void AddSource(int id) {
45 content::DesktopMediaID(content::DesktopMediaID::TYPE_WINDOW, id),
46 base::Int64ToString16(id));
48 sources_.push_back(source);
49 observer_->OnSourceAdded(sources_.size() - 1);
52 void RemoveSource(int index) {
53 sources_.erase(sources_.begin() + index);
54 observer_->OnSourceRemoved(sources_.size() - 1);
57 void SetSourceThumbnail(int index) {
58 sources_[index].thumbnail = thumbnail_;
59 observer_->OnSourceThumbnailChanged(index);
62 void SetSourceName(int index, string16 name) {
63 sources_[index].name = name;
64 observer_->OnSourceNameChanged(index);
67 // DesktopMediaPickerModel implementation:
68 virtual void SetUpdatePeriod(base::TimeDelta period) OVERRIDE {
71 virtual void SetThumbnailSize(const gfx::Size& thumbnail_size) OVERRIDE {
74 virtual void SetViewDialogWindowId(
75 content::DesktopMediaID::Id dialog_id) OVERRIDE {
78 virtual void StartUpdating(Observer* observer) OVERRIDE {
82 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 150, 150);
84 bitmap.eraseRGB(0, 255, 0);
85 thumbnail_ = gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
88 virtual int source_count() const OVERRIDE {
89 return sources_.size();
92 virtual const Source& source(int index) const OVERRIDE {
93 return sources_[index];
97 std::vector<Source> sources_;
99 gfx::ImageSkia thumbnail_;
102 class DesktopMediaPickerControllerTest : public CocoaTest {
104 DesktopMediaPickerControllerTest() : callback_called_(false), model_(NULL) {
107 virtual void SetUp() OVERRIDE {
110 model_ = new FakeDesktopMediaPickerModel();
112 DesktopMediaPicker::DoneCallback callback =
113 base::Bind(&DesktopMediaPickerControllerTest::OnResult,
114 base::Unretained(this));
117 [[DesktopMediaPickerController alloc]
118 initWithModel:scoped_ptr<DesktopMediaPickerModel>(model_)
120 appName:ASCIIToUTF16("Screenshare Test")]);
123 virtual void TearDown() OVERRIDE {
125 CocoaTest::TearDown();
128 bool WaitForCallback() {
129 if (!callback_called_) {
130 base::RunLoop().RunUntilIdle();
132 return callback_called_;
136 void OnResult(content::DesktopMediaID source) {
137 EXPECT_FALSE(callback_called_);
138 callback_called_ = true;
139 source_reported_ = source;
142 content::TestBrowserThreadBundle thread_bundle_;
143 bool callback_called_;
144 content::DesktopMediaID source_reported_;
145 FakeDesktopMediaPickerModel* model_;
146 base::scoped_nsobject<DesktopMediaPickerController> controller_;
149 TEST_F(DesktopMediaPickerControllerTest, ShowAndDismiss) {
150 [controller_ showWindow:nil];
152 model_->AddSource(0);
153 model_->AddSource(1);
154 model_->SetSourceThumbnail(1);
156 NSArray* items = [controller_ items];
157 EXPECT_EQ(2U, [items count]);
158 EXPECT_NSEQ(@"0", [[items objectAtIndex:0] imageTitle]);
159 EXPECT_EQ(nil, [[items objectAtIndex:0] imageRepresentation]);
160 EXPECT_NSEQ(@"1", [[items objectAtIndex:1] imageTitle]);
161 EXPECT_TRUE([[items objectAtIndex:1] imageRepresentation] != nil);
164 TEST_F(DesktopMediaPickerControllerTest, ClickOK) {
165 [controller_ showWindow:nil];
167 model_->AddSource(0);
168 model_->SetSourceThumbnail(0);
169 model_->AddSource(1);
170 model_->SetSourceThumbnail(1);
172 EXPECT_EQ(2U, [[controller_ items] count]);
173 EXPECT_FALSE([[controller_ okButton] isEnabled]);
175 NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:1];
176 [[controller_ sourceBrowser] setSelectionIndexes:indexSet
177 byExtendingSelection:NO];
178 EXPECT_TRUE([[controller_ okButton] isEnabled]);
180 [[controller_ okButton] performClick:nil];
181 EXPECT_TRUE(WaitForCallback());
182 EXPECT_EQ(model_->source(1).id, source_reported_);
185 TEST_F(DesktopMediaPickerControllerTest, ClickCancel) {
186 [controller_ showWindow:nil];
188 model_->AddSource(0);
189 model_->SetSourceThumbnail(0);
190 model_->AddSource(1);
191 model_->SetSourceThumbnail(1);
193 [[controller_ cancelButton] performClick:nil];
194 EXPECT_TRUE(WaitForCallback());
195 EXPECT_EQ(content::DesktopMediaID(), source_reported_);
198 TEST_F(DesktopMediaPickerControllerTest, CloseWindow) {
199 [controller_ showWindow:nil];
201 model_->AddSource(0);
202 model_->SetSourceThumbnail(0);
203 model_->AddSource(1);
204 model_->SetSourceThumbnail(1);
207 EXPECT_TRUE(WaitForCallback());
208 EXPECT_EQ(content::DesktopMediaID(), source_reported_);
211 TEST_F(DesktopMediaPickerControllerTest, UpdateThumbnail) {
212 [controller_ showWindow:nil];
214 model_->AddSource(0);
215 model_->SetSourceThumbnail(0);
216 model_->AddSource(1);
217 model_->SetSourceThumbnail(1);
219 NSArray* items = [controller_ items];
220 EXPECT_EQ(2U, [items count]);
221 NSUInteger version = [[items objectAtIndex:0] imageVersion];
223 model_->SetSourceThumbnail(0);
224 EXPECT_NE(version, [[items objectAtIndex:0] imageVersion]);
227 TEST_F(DesktopMediaPickerControllerTest, UpdateName) {
228 [controller_ showWindow:nil];
230 model_->AddSource(0);
231 model_->SetSourceThumbnail(0);
232 model_->AddSource(1);
233 model_->SetSourceThumbnail(1);
235 NSArray* items = [controller_ items];
236 EXPECT_EQ(2U, [items count]);
237 NSUInteger version = [[items objectAtIndex:0] imageVersion];
239 model_->SetSourceThumbnail(0);
240 EXPECT_NE(version, [[items objectAtIndex:0] imageVersion]);
243 TEST_F(DesktopMediaPickerControllerTest, RemoveSource) {
244 [controller_ showWindow:nil];
246 model_->AddSource(0);
247 model_->AddSource(1);
248 model_->AddSource(2);
249 model_->SetSourceName(1, ASCIIToUTF16("foo"));
251 NSArray* items = [controller_ items];
252 EXPECT_EQ(3U, [items count]);
253 EXPECT_NSEQ(@"foo", [[items objectAtIndex:1] imageTitle]);