| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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 #include "chrome/browser/media/combined_desktop_media_list.h" | |
| 6 | |
| 7 #include "base/message_loop/message_loop.h" | |
| 8 #include "base/run_loop.h" | |
| 9 #include "base/single_thread_task_runner.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "chrome/browser/media/webrtc/desktop_media_list_base.h" | |
| 12 #include "chrome/browser/media/webrtc/desktop_media_list_observer.h" | |
| 13 #include "content/public/test/test_browser_thread.h" | |
| 14 #include "testing/gmock/include/gmock/gmock.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 #include "ui/gfx/image/image_skia.h" | |
| 17 | |
| 18 using testing::DoAll; | |
| 19 using content::DesktopMediaID; | |
| 20 | |
| 21 static const int kThumbnailSize = 50; | |
| 22 static const int kDefaultSourceCount = 2; | |
| 23 | |
| 24 // Create a greyscale image with certain size and grayscale value. | |
| 25 gfx::ImageSkia CreateGrayscaleImage(gfx::Size size, uint8_t greyscale_value) { | |
| 26 SkBitmap result; | |
| 27 result.allocN32Pixels(size.width(), size.height(), true); | |
| 28 | |
| 29 result.lockPixels(); | |
| 30 uint8_t* pixels_data = reinterpret_cast<uint8_t*>(result.getPixels()); | |
| 31 | |
| 32 // Set greyscale value for all pixels. | |
| 33 for (int y = 0; y < result.height(); ++y) { | |
| 34 for (int x = 0; x < result.width(); ++x) { | |
| 35 pixels_data[result.rowBytes() * y + x * result.bytesPerPixel()] = | |
| 36 greyscale_value; | |
| 37 pixels_data[result.rowBytes() * y + x * result.bytesPerPixel() + 1] = | |
| 38 greyscale_value; | |
| 39 pixels_data[result.rowBytes() * y + x * result.bytesPerPixel() + 2] = | |
| 40 greyscale_value; | |
| 41 pixels_data[result.rowBytes() * y + x * result.bytesPerPixel() + 3] = | |
| 42 0xff; | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 result.unlockPixels(); | |
| 47 | |
| 48 return gfx::ImageSkia::CreateFrom1xBitmap(result); | |
| 49 } | |
| 50 | |
| 51 // Fake Implementation of DesktopMediaListBase. | |
| 52 class FakeDesktopMediaListBaseImpl : public DesktopMediaListBase { | |
| 53 public: | |
| 54 explicit FakeDesktopMediaListBaseImpl(DesktopMediaID::Type type) | |
| 55 : DesktopMediaListBase(base::TimeDelta()), | |
| 56 media_type_(type) { | |
| 57 SetThumbnailSize(gfx::Size(kThumbnailSize, kThumbnailSize)); | |
| 58 | |
| 59 for (int i = 0; i < kDefaultSourceCount; ++i) | |
| 60 AddFakeSource(i, base::UTF8ToUTF16("Test media"), i); | |
| 61 } | |
| 62 | |
| 63 ~FakeDesktopMediaListBaseImpl() override {} | |
| 64 | |
| 65 void AddFakeSource(int index, base::string16 title, int greyscale_value) { | |
| 66 DesktopMediaID id(media_type_, index + 1); | |
| 67 fake_sources_.push_back(DesktopMediaListBase::SourceDescription(id, title)); | |
| 68 fake_thumbnails_.push_back( | |
| 69 CreateGrayscaleImage(gfx::Size(10, 10), greyscale_value)); | |
| 70 current_thumbnail_map_[id.id] = greyscale_value; | |
| 71 } | |
| 72 | |
| 73 void RemoveFakeSource(int index) { | |
| 74 if (static_cast<size_t>(index) >= fake_sources_.size()) | |
| 75 return; | |
| 76 | |
| 77 current_thumbnail_map_.erase(fake_sources_[index].id.id); | |
| 78 fake_sources_.erase(fake_sources_.begin() + index); | |
| 79 fake_thumbnails_.erase(fake_thumbnails_.begin() + index); | |
| 80 } | |
| 81 | |
| 82 void Refresh() override { | |
| 83 UpdateSourcesList(fake_sources_); | |
| 84 | |
| 85 // Update thumbnails. | |
| 86 for (size_t i = 0; i < fake_sources_.size(); i++) { | |
| 87 // only update when a thumbnail is added or changed. | |
| 88 const int id = fake_sources_[i].id.id; | |
| 89 if (!refreshed_thumbnail_map_.count(id) || | |
| 90 (refreshed_thumbnail_map_[id] != current_thumbnail_map_[id])) { | |
| 91 UpdateSourceThumbnail(fake_sources_[i].id, fake_thumbnails_[i]); | |
| 92 } | |
| 93 } | |
| 94 refreshed_thumbnail_map_ = current_thumbnail_map_; | |
| 95 } | |
| 96 | |
| 97 std::vector<DesktopMediaListBase::SourceDescription> fake_sources_; | |
| 98 std::vector<gfx::ImageSkia> fake_thumbnails_; | |
| 99 DesktopMediaID::Type media_type_; | |
| 100 // The current and last refrehed maps of source id and thumbnail's greyscale. | |
| 101 // They are used for detect the thumbnail add or change. | |
| 102 std::map<int, int> current_thumbnail_map_, refreshed_thumbnail_map_; | |
| 103 | |
| 104 DISALLOW_COPY_AND_ASSIGN(FakeDesktopMediaListBaseImpl); | |
| 105 }; | |
| 106 | |
| 107 class MockObserver : public DesktopMediaListObserver { | |
| 108 public: | |
| 109 MOCK_METHOD2(OnSourceAdded, void(DesktopMediaList* list, int index)); | |
| 110 MOCK_METHOD2(OnSourceRemoved, void(DesktopMediaList* list, int index)); | |
| 111 MOCK_METHOD3(OnSourceMoved, | |
| 112 void(DesktopMediaList* list, int old_index, int new_index)); | |
| 113 MOCK_METHOD2(OnSourceNameChanged, void(DesktopMediaList* list, int index)); | |
| 114 MOCK_METHOD2(OnSourceThumbnailChanged, | |
| 115 void(DesktopMediaList* list, int index)); | |
| 116 | |
| 117 void VerifyAndClearExpectations() { | |
| 118 testing::Mock::VerifyAndClearExpectations(this); | |
| 119 } | |
| 120 }; | |
| 121 | |
| 122 ACTION_P2(CheckListSize, list, expected_list_size) { | |
| 123 EXPECT_EQ(expected_list_size, list->GetSourceCount()); | |
| 124 } | |
| 125 | |
| 126 ACTION_P(QuitMessageLoop, message_loop) { | |
| 127 message_loop->task_runner()->PostTask( | |
| 128 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | |
| 129 } | |
| 130 | |
| 131 class CombinedDesktopMediaListTest : public testing::Test { | |
| 132 public: | |
| 133 CombinedDesktopMediaListTest() | |
| 134 : ui_thread_(content::BrowserThread::UI, &message_loop_) { | |
| 135 list1_ = new FakeDesktopMediaListBaseImpl(DesktopMediaID::TYPE_SCREEN); | |
| 136 list2_ = new FakeDesktopMediaListBaseImpl(DesktopMediaID::TYPE_WINDOW); | |
| 137 | |
| 138 std::unique_ptr<DesktopMediaList> list1(list1_); | |
| 139 std::unique_ptr<DesktopMediaList> list2(list2_); | |
| 140 | |
| 141 std::vector<std::unique_ptr<DesktopMediaList>> lists; | |
| 142 lists.push_back(std::move(list1)); | |
| 143 lists.push_back(std::move(list2)); | |
| 144 | |
| 145 combined_list_.reset(new CombinedDesktopMediaList(lists)); | |
| 146 } | |
| 147 | |
| 148 // StartUpdating() and verify the first call of refresh(). | |
| 149 void InitializeAndVerify() { | |
| 150 { | |
| 151 testing::InSequence dummy; | |
| 152 | |
| 153 // list1_'s refresh. | |
| 154 for (int i = 0; i < kDefaultSourceCount; ++i) { | |
| 155 EXPECT_CALL(observer_, OnSourceAdded(combined_list_.get(), i)) | |
| 156 .WillOnce(CheckListSize(combined_list_.get(), i + 1)); | |
| 157 } | |
| 158 | |
| 159 for (int i = 0; i < kDefaultSourceCount; ++i) { | |
| 160 EXPECT_CALL(observer_, | |
| 161 OnSourceThumbnailChanged(combined_list_.get(), i)); | |
| 162 } | |
| 163 | |
| 164 // list2_'s refresh. | |
| 165 for (int i = kDefaultSourceCount; i < 2 * kDefaultSourceCount; ++i) { | |
| 166 EXPECT_CALL(observer_, OnSourceAdded(combined_list_.get(), i)) | |
| 167 .WillOnce(CheckListSize(combined_list_.get(), i + 1)); | |
| 168 } | |
| 169 | |
| 170 for (int i = kDefaultSourceCount; i < 2 * kDefaultSourceCount - 1; ++i) { | |
| 171 EXPECT_CALL(observer_, | |
| 172 OnSourceThumbnailChanged(combined_list_.get(), i)); | |
| 173 } | |
| 174 | |
| 175 EXPECT_CALL(observer_, | |
| 176 OnSourceThumbnailChanged(combined_list_.get(), | |
| 177 2 * kDefaultSourceCount - 1)) | |
| 178 .WillOnce(QuitMessageLoop(&message_loop_)); | |
| 179 } | |
| 180 | |
| 181 combined_list_->StartUpdating(&observer_); | |
| 182 base::RunLoop().Run(); | |
| 183 | |
| 184 // list1_'s sources. | |
| 185 for (int i = 0; i < kDefaultSourceCount; ++i) { | |
| 186 EXPECT_EQ(combined_list_->GetSource(i).id.type, | |
| 187 content::DesktopMediaID::TYPE_SCREEN); | |
| 188 EXPECT_EQ(combined_list_->GetSource(i).id.id, i + 1); | |
| 189 } | |
| 190 | |
| 191 // list2_'s sources. | |
| 192 for (int i = kDefaultSourceCount; i < 2 * kDefaultSourceCount; i++) { | |
| 193 EXPECT_EQ(combined_list_->GetSource(i).id.type, | |
| 194 content::DesktopMediaID::TYPE_WINDOW); | |
| 195 EXPECT_EQ(combined_list_->GetSource(i).id.id, | |
| 196 i - kDefaultSourceCount + 1); | |
| 197 } | |
| 198 | |
| 199 observer_.VerifyAndClearExpectations(); | |
| 200 } | |
| 201 | |
| 202 protected: | |
| 203 // Must be listed before |combined_list_|, so it's destroyed last. | |
| 204 MockObserver observer_; | |
| 205 FakeDesktopMediaListBaseImpl* list1_; | |
| 206 FakeDesktopMediaListBaseImpl* list2_; | |
| 207 std::unique_ptr<CombinedDesktopMediaList> combined_list_; | |
| 208 | |
| 209 base::MessageLoop message_loop_; | |
| 210 content::TestBrowserThread ui_thread_; | |
| 211 | |
| 212 DISALLOW_COPY_AND_ASSIGN(CombinedDesktopMediaListTest); | |
| 213 }; | |
| 214 | |
| 215 TEST_F(CombinedDesktopMediaListTest, AddSource) { | |
| 216 InitializeAndVerify(); | |
| 217 | |
| 218 int index = kDefaultSourceCount; | |
| 219 list1_->AddFakeSource(index, base::UTF8ToUTF16("Test media"), index); | |
| 220 | |
| 221 EXPECT_CALL(observer_, OnSourceAdded(combined_list_.get(), index)) | |
| 222 .WillOnce( | |
| 223 CheckListSize(combined_list_.get(), 2 * kDefaultSourceCount + 1)); | |
| 224 EXPECT_CALL(observer_, OnSourceThumbnailChanged(combined_list_.get(), index)) | |
| 225 .WillOnce(QuitMessageLoop(&message_loop_)); | |
| 226 | |
| 227 list1_->Refresh(); | |
| 228 base::RunLoop().Run(); | |
| 229 | |
| 230 list2_->AddFakeSource(index, base::UTF8ToUTF16("Test media"), index); | |
| 231 | |
| 232 EXPECT_CALL(observer_, | |
| 233 OnSourceAdded(combined_list_.get(), 2 * kDefaultSourceCount + 1)) | |
| 234 .WillOnce( | |
| 235 CheckListSize(combined_list_.get(), 2 * kDefaultSourceCount + 2)); | |
| 236 EXPECT_CALL(observer_, OnSourceThumbnailChanged(combined_list_.get(), | |
| 237 2 * kDefaultSourceCount + 1)) | |
| 238 .WillOnce(QuitMessageLoop(&message_loop_)); | |
| 239 | |
| 240 list2_->Refresh(); | |
| 241 base::RunLoop().Run(); | |
| 242 | |
| 243 // Verify last source for list1_ and first source for list2_. | |
| 244 EXPECT_EQ(combined_list_->GetSource(index).id.type, | |
| 245 content::DesktopMediaID::TYPE_SCREEN); | |
| 246 EXPECT_EQ(combined_list_->GetSource(index).id.id, index + 1); | |
| 247 EXPECT_EQ(combined_list_->GetSource(index + 1).id.type, | |
| 248 content::DesktopMediaID::TYPE_WINDOW); | |
| 249 EXPECT_EQ(combined_list_->GetSource(index + 1).id.id, 1); | |
| 250 } | |
| 251 | |
| 252 TEST_F(CombinedDesktopMediaListTest, RemoveSource) { | |
| 253 InitializeAndVerify(); | |
| 254 | |
| 255 int index = kDefaultSourceCount - 1; | |
| 256 list1_->RemoveFakeSource(index); | |
| 257 | |
| 258 EXPECT_CALL(observer_, OnSourceRemoved(combined_list_.get(), index)) | |
| 259 .WillOnce(DoAll( | |
| 260 CheckListSize(combined_list_.get(), 2 * kDefaultSourceCount - 1), | |
| 261 QuitMessageLoop(&message_loop_))); | |
| 262 | |
| 263 list1_->Refresh(); | |
| 264 base::RunLoop().Run(); | |
| 265 | |
| 266 list2_->RemoveFakeSource(index); | |
| 267 | |
| 268 EXPECT_CALL(observer_, OnSourceRemoved(combined_list_.get(), | |
| 269 2 * kDefaultSourceCount - 2)) | |
| 270 .WillOnce(DoAll( | |
| 271 CheckListSize(combined_list_.get(), 2 * kDefaultSourceCount - 2), | |
| 272 QuitMessageLoop(&message_loop_))); | |
| 273 | |
| 274 list2_->Refresh(); | |
| 275 base::RunLoop().Run(); | |
| 276 | |
| 277 // Verify last source for list1_ and first source for list2_. | |
| 278 EXPECT_EQ(combined_list_->GetSource(index - 1).id.type, | |
| 279 content::DesktopMediaID::TYPE_SCREEN); | |
| 280 EXPECT_EQ(combined_list_->GetSource(index - 1).id.id, index); | |
| 281 EXPECT_EQ(combined_list_->GetSource(index).id.type, | |
| 282 content::DesktopMediaID::TYPE_WINDOW); | |
| 283 EXPECT_EQ(combined_list_->GetSource(index).id.id, 1); | |
| 284 } | |
| 285 | |
| 286 TEST_F(CombinedDesktopMediaListTest, MoveSource) { | |
| 287 InitializeAndVerify(); | |
| 288 | |
| 289 // Swap sources. | |
| 290 list1_->RemoveFakeSource(kDefaultSourceCount - 1); | |
| 291 list1_->RemoveFakeSource(kDefaultSourceCount - 2); | |
| 292 list1_->AddFakeSource(kDefaultSourceCount - 1, | |
| 293 base::UTF8ToUTF16("Test media"), | |
| 294 kDefaultSourceCount - 1); | |
| 295 list1_->AddFakeSource(kDefaultSourceCount - 2, | |
| 296 base::UTF8ToUTF16("Test media"), | |
| 297 kDefaultSourceCount - 2); | |
| 298 | |
| 299 EXPECT_CALL(observer_, | |
| 300 OnSourceMoved(combined_list_.get(), kDefaultSourceCount - 1, | |
| 301 kDefaultSourceCount - 2)) | |
| 302 .WillOnce(QuitMessageLoop(&message_loop_)); | |
| 303 | |
| 304 list1_->Refresh(); | |
| 305 base::RunLoop().Run(); | |
| 306 | |
| 307 // Swap sources. | |
| 308 list2_->RemoveFakeSource(kDefaultSourceCount - 1); | |
| 309 list2_->RemoveFakeSource(kDefaultSourceCount - 2); | |
| 310 list2_->AddFakeSource(kDefaultSourceCount - 1, | |
| 311 base::UTF8ToUTF16("Test media"), | |
| 312 kDefaultSourceCount - 1); | |
| 313 list2_->AddFakeSource(kDefaultSourceCount - 2, | |
| 314 base::UTF8ToUTF16("Test media"), | |
| 315 kDefaultSourceCount - 2); | |
| 316 | |
| 317 EXPECT_CALL(observer_, | |
| 318 OnSourceMoved(combined_list_.get(), 2 * kDefaultSourceCount - 1, | |
| 319 2 * kDefaultSourceCount - 2)) | |
| 320 .WillOnce(QuitMessageLoop(&message_loop_)); | |
| 321 | |
| 322 list2_->Refresh(); | |
| 323 base::RunLoop().Run(); | |
| 324 } | |
| 325 | |
| 326 TEST_F(CombinedDesktopMediaListTest, UpdateTitle) { | |
| 327 InitializeAndVerify(); | |
| 328 | |
| 329 // Change title. | |
| 330 list1_->RemoveFakeSource(kDefaultSourceCount - 1); | |
| 331 list1_->AddFakeSource(kDefaultSourceCount - 1, | |
| 332 base::UTF8ToUTF16("New test media"), | |
| 333 kDefaultSourceCount - 1); | |
| 334 | |
| 335 EXPECT_CALL(observer_, OnSourceNameChanged(combined_list_.get(), | |
| 336 kDefaultSourceCount - 1)) | |
| 337 .WillOnce(QuitMessageLoop(&message_loop_)); | |
| 338 | |
| 339 list1_->Refresh(); | |
| 340 base::RunLoop().Run(); | |
| 341 | |
| 342 // Change title. | |
| 343 list2_->RemoveFakeSource(kDefaultSourceCount - 1); | |
| 344 list2_->AddFakeSource(kDefaultSourceCount - 1, | |
| 345 base::UTF8ToUTF16("New test media"), | |
| 346 kDefaultSourceCount - 1); | |
| 347 | |
| 348 EXPECT_CALL(observer_, OnSourceNameChanged(combined_list_.get(), | |
| 349 2 * kDefaultSourceCount - 1)) | |
| 350 .WillOnce(QuitMessageLoop(&message_loop_)); | |
| 351 | |
| 352 list2_->Refresh(); | |
| 353 base::RunLoop().Run(); | |
| 354 | |
| 355 EXPECT_EQ(combined_list_->GetSource(kDefaultSourceCount - 1).name, | |
| 356 base::UTF8ToUTF16("New test media")); | |
| 357 EXPECT_EQ(combined_list_->GetSource(2 * kDefaultSourceCount - 1).name, | |
| 358 base::UTF8ToUTF16("New test media")); | |
| 359 } | |
| 360 | |
| 361 TEST_F(CombinedDesktopMediaListTest, UpdateThumbnail) { | |
| 362 InitializeAndVerify(); | |
| 363 | |
| 364 // Change thumbnail. | |
| 365 list1_->RemoveFakeSource(kDefaultSourceCount - 1); | |
| 366 list1_->AddFakeSource(kDefaultSourceCount - 1, | |
| 367 base::UTF8ToUTF16("Test media"), 100); | |
| 368 | |
| 369 EXPECT_CALL(observer_, OnSourceThumbnailChanged(combined_list_.get(), | |
| 370 kDefaultSourceCount - 1)) | |
| 371 .WillOnce(QuitMessageLoop(&message_loop_)); | |
| 372 | |
| 373 list1_->Refresh(); | |
| 374 base::RunLoop().Run(); | |
| 375 | |
| 376 // Change thumbnail. | |
| 377 list2_->RemoveFakeSource(kDefaultSourceCount - 1); | |
| 378 list2_->AddFakeSource(kDefaultSourceCount - 1, | |
| 379 base::UTF8ToUTF16("Test media"), 100); | |
| 380 | |
| 381 EXPECT_CALL(observer_, OnSourceThumbnailChanged(combined_list_.get(), | |
| 382 2 * kDefaultSourceCount - 1)) | |
| 383 .WillOnce(QuitMessageLoop(&message_loop_)); | |
| 384 | |
| 385 list2_->Refresh(); | |
| 386 base::RunLoop().Run(); | |
| 387 } | |
| OLD | NEW |