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