OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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. |
| 4 |
| 5 #import "chrome/browser/ui/cocoa/media_picker/desktop_media_picker_controller.h" |
| 6 |
| 7 #include "base/bind.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" |
| 13 #include "third_party/webrtc/modules/desktop_capture/screen_capturer.h" |
| 14 #include "third_party/webrtc/modules/desktop_capture/window_capturer.h" |
| 15 |
| 16 @interface DesktopMediaPickerController (ExposedForTesting) |
| 17 - (IKImageBrowserView*)sourceBrowser; |
| 18 - (NSButton*)okButton; |
| 19 - (NSArray*)items; |
| 20 @end |
| 21 |
| 22 @implementation DesktopMediaPickerController (ExposedForTesting) |
| 23 - (IKImageBrowserView*)sourceBrowser { |
| 24 return sourceBrowser_; |
| 25 } |
| 26 |
| 27 - (NSButton*)okButton { |
| 28 return okButton_; |
| 29 } |
| 30 |
| 31 - (NSButton*)cancelButton { |
| 32 return cancelButton_; |
| 33 } |
| 34 |
| 35 - (NSArray*)items { |
| 36 return items_; |
| 37 } |
| 38 @end |
| 39 |
| 40 class FakeDesktopMediaPickerModel : public DesktopMediaPickerModel { |
| 41 public: |
| 42 FakeDesktopMediaPickerModel() |
| 43 : DesktopMediaPickerModel(scoped_ptr<webrtc::ScreenCapturer>(), |
| 44 scoped_ptr<webrtc::WindowCapturer>()), |
| 45 observer_(NULL) { |
| 46 } |
| 47 |
| 48 virtual void StartUpdating(Observer* observer) OVERRIDE { |
| 49 observer_ = observer; |
| 50 |
| 51 SkBitmap bitmap; |
| 52 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 150, 150); |
| 53 bitmap.allocPixels(); |
| 54 bitmap.eraseRGB(0, 255, 0); |
| 55 thumbnail_ = gfx::ImageSkia::CreateFrom1xBitmap(bitmap); |
| 56 } |
| 57 |
| 58 Observer* observer() { |
| 59 return observer_; |
| 60 } |
| 61 |
| 62 void AddSource(int id) { |
| 63 Source source( |
| 64 content::DesktopMediaID(content::DesktopMediaID::TYPE_WINDOW, id), |
| 65 base::Int64ToString16(id)); |
| 66 |
| 67 sources_.push_back(source); |
| 68 observer_->OnSourceAdded(sources_.size() - 1); |
| 69 } |
| 70 |
| 71 void RemoveSource(int index) { |
| 72 sources_.erase(sources_.begin() + index); |
| 73 observer_->OnSourceRemoved(sources_.size() - 1); |
| 74 } |
| 75 |
| 76 void SetSourceThumbnail(int index) { |
| 77 sources_[index].thumbnail = thumbnail_; |
| 78 observer_->OnSourceThumbnailChanged(index); |
| 79 } |
| 80 |
| 81 void SetSourceName(int index, string16 name) { |
| 82 sources_[index].name = name; |
| 83 observer_->OnSourceNameChanged(index); |
| 84 } |
| 85 |
| 86 private: |
| 87 Observer* observer_; |
| 88 gfx::ImageSkia thumbnail_; |
| 89 }; |
| 90 |
| 91 class DesktopMediaPickerControllerTest : public CocoaTest { |
| 92 public: |
| 93 DesktopMediaPickerControllerTest() : callback_called_(false), model_(NULL) { |
| 94 } |
| 95 |
| 96 virtual void SetUp() OVERRIDE { |
| 97 CocoaTest::SetUp(); |
| 98 |
| 99 model_ = new FakeDesktopMediaPickerModel(); |
| 100 |
| 101 DesktopMediaPicker::DoneCallback callback = |
| 102 base::Bind(&DesktopMediaPickerControllerTest::OnResult, |
| 103 base::Unretained(this)); |
| 104 |
| 105 controller_.reset( |
| 106 [[DesktopMediaPickerController alloc] |
| 107 initWithModel:scoped_ptr<DesktopMediaPickerModel>(model_) |
| 108 callback:callback |
| 109 appName:ASCIIToUTF16("Screenshare Test")]); |
| 110 } |
| 111 |
| 112 virtual void TearDown() OVERRIDE { |
| 113 controller_.reset(); |
| 114 CocoaTest::TearDown(); |
| 115 } |
| 116 |
| 117 bool WaitForCallback() { |
| 118 if (!callback_called_) { |
| 119 base::RunLoop().RunUntilIdle(); |
| 120 } |
| 121 return callback_called_; |
| 122 } |
| 123 |
| 124 protected: |
| 125 void OnResult(content::DesktopMediaID source) { |
| 126 EXPECT_FALSE(callback_called_); |
| 127 callback_called_ = true; |
| 128 source_reported_ = source; |
| 129 } |
| 130 |
| 131 content::TestBrowserThreadBundle thread_bundle_; |
| 132 bool callback_called_; |
| 133 content::DesktopMediaID source_reported_; |
| 134 FakeDesktopMediaPickerModel* model_; |
| 135 base::scoped_nsobject<DesktopMediaPickerController> controller_; |
| 136 }; |
| 137 |
| 138 TEST_F(DesktopMediaPickerControllerTest, ShowAndDismiss) { |
| 139 [controller_ showWindow:nil]; |
| 140 |
| 141 model_->AddSource(0); |
| 142 model_->AddSource(1); |
| 143 model_->SetSourceThumbnail(1); |
| 144 |
| 145 NSArray* items = [controller_ items]; |
| 146 EXPECT_EQ(2U, [items count]); |
| 147 EXPECT_NSEQ(@"0", [[items objectAtIndex:0] imageTitle]); |
| 148 EXPECT_EQ(nil, [[items objectAtIndex:0] imageRepresentation]); |
| 149 EXPECT_NSEQ(@"1", [[items objectAtIndex:1] imageTitle]); |
| 150 EXPECT_TRUE([[items objectAtIndex:1] imageRepresentation] != nil); |
| 151 } |
| 152 |
| 153 TEST_F(DesktopMediaPickerControllerTest, ClickOK) { |
| 154 [controller_ showWindow:nil]; |
| 155 |
| 156 model_->AddSource(0); |
| 157 model_->SetSourceThumbnail(0); |
| 158 model_->AddSource(1); |
| 159 model_->SetSourceThumbnail(1); |
| 160 |
| 161 EXPECT_EQ(2U, [[controller_ items] count]); |
| 162 EXPECT_FALSE([[controller_ okButton] isEnabled]); |
| 163 |
| 164 NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:1]; |
| 165 [[controller_ sourceBrowser] setSelectionIndexes:indexSet |
| 166 byExtendingSelection:NO]; |
| 167 EXPECT_TRUE([[controller_ okButton] isEnabled]); |
| 168 |
| 169 [[controller_ okButton] performClick:nil]; |
| 170 EXPECT_TRUE(WaitForCallback()); |
| 171 EXPECT_EQ(model_->source(1).id, source_reported_); |
| 172 } |
| 173 |
| 174 TEST_F(DesktopMediaPickerControllerTest, ClickCancel) { |
| 175 [controller_ showWindow:nil]; |
| 176 |
| 177 model_->AddSource(0); |
| 178 model_->SetSourceThumbnail(0); |
| 179 model_->AddSource(1); |
| 180 model_->SetSourceThumbnail(1); |
| 181 |
| 182 [[controller_ cancelButton] performClick:nil]; |
| 183 EXPECT_TRUE(WaitForCallback()); |
| 184 EXPECT_EQ(content::DesktopMediaID(), source_reported_); |
| 185 } |
| 186 |
| 187 TEST_F(DesktopMediaPickerControllerTest, CloseWindow) { |
| 188 [controller_ showWindow:nil]; |
| 189 |
| 190 model_->AddSource(0); |
| 191 model_->SetSourceThumbnail(0); |
| 192 model_->AddSource(1); |
| 193 model_->SetSourceThumbnail(1); |
| 194 |
| 195 [controller_ close]; |
| 196 EXPECT_TRUE(WaitForCallback()); |
| 197 EXPECT_EQ(content::DesktopMediaID(), source_reported_); |
| 198 } |
| 199 |
| 200 TEST_F(DesktopMediaPickerControllerTest, UpdateThumbnail) { |
| 201 [controller_ showWindow:nil]; |
| 202 |
| 203 model_->AddSource(0); |
| 204 model_->SetSourceThumbnail(0); |
| 205 model_->AddSource(1); |
| 206 model_->SetSourceThumbnail(1); |
| 207 |
| 208 NSArray* items = [controller_ items]; |
| 209 EXPECT_EQ(2U, [items count]); |
| 210 NSUInteger version = [[items objectAtIndex:0] imageVersion]; |
| 211 |
| 212 model_->SetSourceThumbnail(0); |
| 213 EXPECT_NE(version, [[items objectAtIndex:0] imageVersion]); |
| 214 } |
| 215 |
| 216 TEST_F(DesktopMediaPickerControllerTest, UpdateName) { |
| 217 [controller_ showWindow:nil]; |
| 218 |
| 219 model_->AddSource(0); |
| 220 model_->SetSourceThumbnail(0); |
| 221 model_->AddSource(1); |
| 222 model_->SetSourceThumbnail(1); |
| 223 |
| 224 NSArray* items = [controller_ items]; |
| 225 EXPECT_EQ(2U, [items count]); |
| 226 NSUInteger version = [[items objectAtIndex:0] imageVersion]; |
| 227 |
| 228 model_->SetSourceThumbnail(0); |
| 229 EXPECT_NE(version, [[items objectAtIndex:0] imageVersion]); |
| 230 } |
| 231 |
| 232 TEST_F(DesktopMediaPickerControllerTest, RemoveSource) { |
| 233 [controller_ showWindow:nil]; |
| 234 |
| 235 model_->AddSource(0); |
| 236 model_->AddSource(1); |
| 237 model_->AddSource(2); |
| 238 model_->SetSourceName(1, ASCIIToUTF16("foo")); |
| 239 |
| 240 NSArray* items = [controller_ items]; |
| 241 EXPECT_EQ(3U, [items count]); |
| 242 EXPECT_NSEQ(@"foo", [[items objectAtIndex:1] imageTitle]); |
| 243 } |
OLD | NEW |