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