Chromium Code Reviews| Index: chrome/browser/media/tab_desktop_media_list_unittest.cc |
| diff --git a/chrome/browser/media/tab_desktop_media_list_unittest.cc b/chrome/browser/media/tab_desktop_media_list_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9b16572eefaa64527d3d557068ae6cb16bd7813d |
| --- /dev/null |
| +++ b/chrome/browser/media/tab_desktop_media_list_unittest.cc |
| @@ -0,0 +1,236 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/media/tab_desktop_media_list.h" |
| + |
| +#include "base/message_loop/message_loop.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/media/desktop_media_list_observer.h" |
| +#include "content/public/test/test_browser_thread.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "ui/gfx/image/image.h" |
| + |
| +using testing::DoAll; |
| +using content::DesktopMediaID; |
| + |
| +namespace { |
| + |
| +static const int kThumbnailSize = 50; // >=20. |
| +static const int kDefaultSouceCount = 2; // >=2. |
|
qiangchen
2016/01/26 20:43:10
kDefaultSourceCount
GeorgeZ
2016/01/27 00:17:39
Done.
|
| + |
| +// Create a greyscale image with certain size and grayscale value. |
| +gfx::Image CreateGrayscaleImage(gfx::Size size, int greyscale_value) { |
| + SkBitmap result; |
| + result.allocN32Pixels(size.width(), size.height(), true); |
| + |
| + result.lockPixels(); |
| + uint8_t* pixels_data = reinterpret_cast<uint8_t*>(result.getPixels()); |
| + |
| + // Set greyscale value for all pixels. |
| + for (int y = 0; y < result.height(); ++y) { |
| + for (int x = 0; x < result.width(); ++x) { |
| + pixels_data[result.rowBytes() * y + x * result.bytesPerPixel()] = |
| + greyscale_value; |
| + pixels_data[result.rowBytes() * y + x * result.bytesPerPixel() + 1] = |
| + greyscale_value; |
| + pixels_data[result.rowBytes() * y + x * result.bytesPerPixel() + 2] = |
| + greyscale_value; |
| + pixels_data[result.rowBytes() * y + x * result.bytesPerPixel() + 3] = |
| + 0xff; |
| + } |
| + } |
| + |
| + result.unlockPixels(); |
| + |
| + return gfx::Image::CreateFrom1xBitmap(result); |
| +} |
| + |
| +class MockObserver : public DesktopMediaListObserver { |
| + public: |
| + MOCK_METHOD2(OnSourceAdded, void(DesktopMediaList* list, int index)); |
| + MOCK_METHOD2(OnSourceRemoved, void(DesktopMediaList* list, int index)); |
| + MOCK_METHOD3(OnSourceMoved, |
| + void(DesktopMediaList* list, int old_index, int new_index)); |
| + MOCK_METHOD2(OnSourceNameChanged, void(DesktopMediaList* list, int index)); |
| + MOCK_METHOD2(OnSourceThumbnailChanged, |
| + void(DesktopMediaList* list, int index)); |
| +}; |
| + |
| +ACTION_P2(CheckListSize, list, expected_list_size) { |
| + EXPECT_EQ(expected_list_size, list->GetSourceCount()); |
| +} |
| + |
| +ACTION_P(QuitMessageLoop, message_loop) { |
| + message_loop->task_runner()->PostTask( |
| + FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| +} |
| + |
| +class TabDesktopMediaListTest : public testing::Test { |
| + public: |
| + TabDesktopMediaListTest() |
| + : ui_thread_(content::BrowserThread::UI, &message_loop_) {} |
| + |
| + void CreateDefaultList() { |
| + list_.reset(new TabDesktopMediaList()); |
| + list_->SetThumbnailSize(gfx::Size(kThumbnailSize, kThumbnailSize)); |
| + |
| + // Set update period to reduce the time it takes to run tests. |
| + // >0 to avoid unit test failure. |
| + list_->SetUpdatePeriod(base::TimeDelta::FromMilliseconds(1)); |
| + } |
| + |
| + void AddFakeSource(int index, base::string16 title, int greyscale_value) { |
| + DesktopMediaID id(DesktopMediaID::TYPE_WEB_CONTENTS, 0, |
| + content::WebContentsMediaCaptureId(index + 1, index + 1)); |
| + fake_sources_.push_back(DesktopMediaListBase::SourceDescription(id, title)); |
| + fake_favicons_.push_back( |
| + CreateGrayscaleImage(gfx::Size(10, 10), greyscale_value)); |
| + } |
| + |
| + void RemoveFakeSource(int index) { |
| + if (static_cast<size_t>(index) >= fake_sources_.size()) |
| + return; |
| + |
| + fake_sources_.erase(fake_sources_.begin() + index); |
| + fake_favicons_.erase(fake_favicons_.begin() + index); |
| + } |
| + |
| + void InitializeAndVerify() { |
| + CreateDefaultList(); |
| + |
| + for (int i = 0; i < kDefaultSouceCount; ++i) |
| + AddFakeSource(i, base::UTF8ToUTF16("Test tab"), static_cast<uint8_t>(i)); |
| + |
| + list_->SetFakeSourcesForTesting(fake_sources_, fake_favicons_); |
| + |
| + { |
| + testing::InSequence dummy; |
| + for (int i = 0; i < kDefaultSouceCount; ++i) { |
| + EXPECT_CALL(observer_, OnSourceAdded(list_.get(), i)) |
| + .WillOnce(CheckListSize(list_.get(), i + 1)); |
| + } |
| + for (int i = 0; i < kDefaultSouceCount - 1; ++i) { |
| + EXPECT_CALL(observer_, OnSourceThumbnailChanged(list_.get(), i)); |
| + } |
| + EXPECT_CALL(observer_, |
| + OnSourceThumbnailChanged(list_.get(), kDefaultSouceCount - 1)) |
| + .WillOnce(QuitMessageLoop(&message_loop_)); |
| + } |
| + |
| + list_->StartUpdating(&observer_); |
| + message_loop_.Run(); |
| + |
| + for (int i = 0; i < kDefaultSouceCount; ++i) { |
| + EXPECT_EQ(list_->GetSource(i).id.type, |
| + content::DesktopMediaID::TYPE_WEB_CONTENTS); |
| + EXPECT_EQ(list_->GetSource(i).id.web_contents_id.render_process_id, |
| + i + 1); |
| + EXPECT_EQ(list_->GetSource(i).id.web_contents_id.main_render_frame_id, |
| + i + 1); |
| + EXPECT_EQ(list_->GetSource(i).name, base::UTF8ToUTF16("Test tab")); |
| + } |
| + |
| + testing::Mock::VerifyAndClearExpectations(&observer_); |
|
qiangchen
2016/01/26 20:43:10
Again, make this to a function of observer_.
GeorgeZ
2016/01/27 00:17:39
Done.
|
| + } |
| + |
| + protected: |
| + std::vector<DesktopMediaListBase::SourceDescription> fake_sources_; |
| + std::vector<gfx::Image> fake_favicons_; |
| + |
| + // Must be listed before |list_|, so it's destroyed last. |
| + MockObserver observer_; |
| + scoped_ptr<TabDesktopMediaList> list_; |
| + |
| + base::MessageLoop message_loop_; |
| + content::TestBrowserThread ui_thread_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(TabDesktopMediaListTest); |
| +}; |
| + |
| +TEST_F(TabDesktopMediaListTest, AddTab) { |
| + InitializeAndVerify(); |
| + |
| + int index = kDefaultSouceCount; |
| + AddFakeSource(index, base::UTF8ToUTF16("Test tab"), |
| + static_cast<uint8_t>(index)); |
| + list_->SetFakeSourcesForTesting(fake_sources_, fake_favicons_); |
| + |
| + EXPECT_CALL(observer_, OnSourceAdded(list_.get(), index)) |
| + .WillOnce(CheckListSize(list_.get(), kDefaultSouceCount + 1)); |
| + EXPECT_CALL(observer_, OnSourceThumbnailChanged(list_.get(), index)) |
| + .WillOnce(QuitMessageLoop(&message_loop_)); |
| + |
| + message_loop_.Run(); |
| + |
| + EXPECT_EQ(list_->GetSource(index).id.web_contents_id.render_process_id, |
| + index + 1); |
| + EXPECT_EQ(list_->GetSource(index).id.web_contents_id.main_render_frame_id, |
| + index + 1); |
| +} |
| + |
| +TEST_F(TabDesktopMediaListTest, RemoveTab) { |
| + InitializeAndVerify(); |
| + |
| + int index = kDefaultSouceCount - 1; |
| + RemoveFakeSource(index); |
| + list_->SetFakeSourcesForTesting(fake_sources_, fake_favicons_); |
| + |
| + EXPECT_CALL(observer_, OnSourceRemoved(list_.get(), index)) |
| + .WillOnce(DoAll(CheckListSize(list_.get(), kDefaultSouceCount - 1), |
| + QuitMessageLoop(&message_loop_))); |
| + |
| + message_loop_.Run(); |
| +} |
| + |
| +TEST_F(TabDesktopMediaListTest, MoveTab) { |
| + InitializeAndVerify(); |
| + |
| + // Swap the two sources. |
| + DCHECK_GE(kDefaultSouceCount, 2); |
| + RemoveFakeSource(kDefaultSouceCount - 1); |
| + RemoveFakeSource(kDefaultSouceCount - 2); |
| + AddFakeSource(kDefaultSouceCount - 1, base::UTF8ToUTF16("Test tab"), |
| + static_cast<uint8_t>(kDefaultSouceCount - 1)); |
| + AddFakeSource(kDefaultSouceCount - 2, base::UTF8ToUTF16("Test tab"), |
| + static_cast<uint8_t>(kDefaultSouceCount - 2)); |
| + list_->SetFakeSourcesForTesting(fake_sources_, fake_favicons_); |
| + |
| + EXPECT_CALL(observer_, OnSourceMoved(list_.get(), kDefaultSouceCount - 1, |
| + kDefaultSouceCount - 2)) |
| + .WillOnce(DoAll(CheckListSize(list_.get(), kDefaultSouceCount), |
| + QuitMessageLoop(&message_loop_))); |
| + |
| + message_loop_.Run(); |
| +} |
| + |
| +TEST_F(TabDesktopMediaListTest, UpdateTitle) { |
| + InitializeAndVerify(); |
| + |
| + fake_sources_[0].name = base::UTF8ToUTF16("New test tab"); |
| + list_->SetFakeSourcesForTesting(fake_sources_, fake_favicons_); |
| + |
| + EXPECT_CALL(observer_, OnSourceNameChanged(list_.get(), 0)) |
| + .WillOnce(QuitMessageLoop(&message_loop_)); |
| + |
| + message_loop_.Run(); |
| + |
| + EXPECT_EQ(list_->GetSource(0).name, base::UTF8ToUTF16("New test tab")); |
| +} |
| + |
| +TEST_F(TabDesktopMediaListTest, UpdateThumbnail) { |
| + InitializeAndVerify(); |
| + |
| + fake_favicons_[0] = |
| + CreateGrayscaleImage(gfx::Size(kThumbnailSize, kThumbnailSize), 100); |
| + list_->SetFakeSourcesForTesting(fake_sources_, fake_favicons_); |
| + |
| + EXPECT_CALL(observer_, OnSourceThumbnailChanged(list_.get(), 0)) |
| + .WillOnce(QuitMessageLoop(&message_loop_)); |
| + |
| + message_loop_.Run(); |
| +} |
| + |
| +} // namespace |