Chromium Code Reviews| 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/tab_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_observer.h" | |
| 11 #include "content/public/test/test_browser_thread.h" | |
| 12 #include "testing/gmock/include/gmock/gmock.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 #include "ui/gfx/image/image.h" | |
| 15 | |
| 16 using testing::DoAll; | |
| 17 using content::DesktopMediaID; | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 static const int kThumbnailSize = 50; // >=20. | |
| 22 static const int kDefaultSouceCount = 2; // >=2. | |
|
qiangchen
2016/01/26 20:43:10
kDefaultSourceCount
GeorgeZ
2016/01/27 00:17:39
Done.
| |
| 23 | |
| 24 // Create a greyscale image with certain size and grayscale value. | |
| 25 gfx::Image CreateGrayscaleImage(gfx::Size size, int 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::Image::CreateFrom1xBitmap(result); | |
| 49 } | |
| 50 | |
| 51 class MockObserver : public DesktopMediaListObserver { | |
| 52 public: | |
| 53 MOCK_METHOD2(OnSourceAdded, void(DesktopMediaList* list, int index)); | |
| 54 MOCK_METHOD2(OnSourceRemoved, void(DesktopMediaList* list, int index)); | |
| 55 MOCK_METHOD3(OnSourceMoved, | |
| 56 void(DesktopMediaList* list, int old_index, int new_index)); | |
| 57 MOCK_METHOD2(OnSourceNameChanged, void(DesktopMediaList* list, int index)); | |
| 58 MOCK_METHOD2(OnSourceThumbnailChanged, | |
| 59 void(DesktopMediaList* list, int index)); | |
| 60 }; | |
| 61 | |
| 62 ACTION_P2(CheckListSize, list, expected_list_size) { | |
| 63 EXPECT_EQ(expected_list_size, list->GetSourceCount()); | |
| 64 } | |
| 65 | |
| 66 ACTION_P(QuitMessageLoop, message_loop) { | |
| 67 message_loop->task_runner()->PostTask( | |
| 68 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); | |
| 69 } | |
| 70 | |
| 71 class TabDesktopMediaListTest : public testing::Test { | |
| 72 public: | |
| 73 TabDesktopMediaListTest() | |
| 74 : ui_thread_(content::BrowserThread::UI, &message_loop_) {} | |
| 75 | |
| 76 void CreateDefaultList() { | |
| 77 list_.reset(new TabDesktopMediaList()); | |
| 78 list_->SetThumbnailSize(gfx::Size(kThumbnailSize, kThumbnailSize)); | |
| 79 | |
| 80 // Set update period to reduce the time it takes to run tests. | |
| 81 // >0 to avoid unit test failure. | |
| 82 list_->SetUpdatePeriod(base::TimeDelta::FromMilliseconds(1)); | |
| 83 } | |
| 84 | |
| 85 void AddFakeSource(int index, base::string16 title, int greyscale_value) { | |
| 86 DesktopMediaID id(DesktopMediaID::TYPE_WEB_CONTENTS, 0, | |
| 87 content::WebContentsMediaCaptureId(index + 1, index + 1)); | |
| 88 fake_sources_.push_back(DesktopMediaListBase::SourceDescription(id, title)); | |
| 89 fake_favicons_.push_back( | |
| 90 CreateGrayscaleImage(gfx::Size(10, 10), greyscale_value)); | |
| 91 } | |
| 92 | |
| 93 void RemoveFakeSource(int index) { | |
| 94 if (static_cast<size_t>(index) >= fake_sources_.size()) | |
| 95 return; | |
| 96 | |
| 97 fake_sources_.erase(fake_sources_.begin() + index); | |
| 98 fake_favicons_.erase(fake_favicons_.begin() + index); | |
| 99 } | |
| 100 | |
| 101 void InitializeAndVerify() { | |
| 102 CreateDefaultList(); | |
| 103 | |
| 104 for (int i = 0; i < kDefaultSouceCount; ++i) | |
| 105 AddFakeSource(i, base::UTF8ToUTF16("Test tab"), static_cast<uint8_t>(i)); | |
| 106 | |
| 107 list_->SetFakeSourcesForTesting(fake_sources_, fake_favicons_); | |
| 108 | |
| 109 { | |
| 110 testing::InSequence dummy; | |
| 111 for (int i = 0; i < kDefaultSouceCount; ++i) { | |
| 112 EXPECT_CALL(observer_, OnSourceAdded(list_.get(), i)) | |
| 113 .WillOnce(CheckListSize(list_.get(), i + 1)); | |
| 114 } | |
| 115 for (int i = 0; i < kDefaultSouceCount - 1; ++i) { | |
| 116 EXPECT_CALL(observer_, OnSourceThumbnailChanged(list_.get(), i)); | |
| 117 } | |
| 118 EXPECT_CALL(observer_, | |
| 119 OnSourceThumbnailChanged(list_.get(), kDefaultSouceCount - 1)) | |
| 120 .WillOnce(QuitMessageLoop(&message_loop_)); | |
| 121 } | |
| 122 | |
| 123 list_->StartUpdating(&observer_); | |
| 124 message_loop_.Run(); | |
| 125 | |
| 126 for (int i = 0; i < kDefaultSouceCount; ++i) { | |
| 127 EXPECT_EQ(list_->GetSource(i).id.type, | |
| 128 content::DesktopMediaID::TYPE_WEB_CONTENTS); | |
| 129 EXPECT_EQ(list_->GetSource(i).id.web_contents_id.render_process_id, | |
| 130 i + 1); | |
| 131 EXPECT_EQ(list_->GetSource(i).id.web_contents_id.main_render_frame_id, | |
| 132 i + 1); | |
| 133 EXPECT_EQ(list_->GetSource(i).name, base::UTF8ToUTF16("Test tab")); | |
| 134 } | |
| 135 | |
| 136 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.
| |
| 137 } | |
| 138 | |
| 139 protected: | |
| 140 std::vector<DesktopMediaListBase::SourceDescription> fake_sources_; | |
| 141 std::vector<gfx::Image> fake_favicons_; | |
| 142 | |
| 143 // Must be listed before |list_|, so it's destroyed last. | |
| 144 MockObserver observer_; | |
| 145 scoped_ptr<TabDesktopMediaList> list_; | |
| 146 | |
| 147 base::MessageLoop message_loop_; | |
| 148 content::TestBrowserThread ui_thread_; | |
| 149 | |
| 150 DISALLOW_COPY_AND_ASSIGN(TabDesktopMediaListTest); | |
| 151 }; | |
| 152 | |
| 153 TEST_F(TabDesktopMediaListTest, AddTab) { | |
| 154 InitializeAndVerify(); | |
| 155 | |
| 156 int index = kDefaultSouceCount; | |
| 157 AddFakeSource(index, base::UTF8ToUTF16("Test tab"), | |
| 158 static_cast<uint8_t>(index)); | |
| 159 list_->SetFakeSourcesForTesting(fake_sources_, fake_favicons_); | |
| 160 | |
| 161 EXPECT_CALL(observer_, OnSourceAdded(list_.get(), index)) | |
| 162 .WillOnce(CheckListSize(list_.get(), kDefaultSouceCount + 1)); | |
| 163 EXPECT_CALL(observer_, OnSourceThumbnailChanged(list_.get(), index)) | |
| 164 .WillOnce(QuitMessageLoop(&message_loop_)); | |
| 165 | |
| 166 message_loop_.Run(); | |
| 167 | |
| 168 EXPECT_EQ(list_->GetSource(index).id.web_contents_id.render_process_id, | |
| 169 index + 1); | |
| 170 EXPECT_EQ(list_->GetSource(index).id.web_contents_id.main_render_frame_id, | |
| 171 index + 1); | |
| 172 } | |
| 173 | |
| 174 TEST_F(TabDesktopMediaListTest, RemoveTab) { | |
| 175 InitializeAndVerify(); | |
| 176 | |
| 177 int index = kDefaultSouceCount - 1; | |
| 178 RemoveFakeSource(index); | |
| 179 list_->SetFakeSourcesForTesting(fake_sources_, fake_favicons_); | |
| 180 | |
| 181 EXPECT_CALL(observer_, OnSourceRemoved(list_.get(), index)) | |
| 182 .WillOnce(DoAll(CheckListSize(list_.get(), kDefaultSouceCount - 1), | |
| 183 QuitMessageLoop(&message_loop_))); | |
| 184 | |
| 185 message_loop_.Run(); | |
| 186 } | |
| 187 | |
| 188 TEST_F(TabDesktopMediaListTest, MoveTab) { | |
| 189 InitializeAndVerify(); | |
| 190 | |
| 191 // Swap the two sources. | |
| 192 DCHECK_GE(kDefaultSouceCount, 2); | |
| 193 RemoveFakeSource(kDefaultSouceCount - 1); | |
| 194 RemoveFakeSource(kDefaultSouceCount - 2); | |
| 195 AddFakeSource(kDefaultSouceCount - 1, base::UTF8ToUTF16("Test tab"), | |
| 196 static_cast<uint8_t>(kDefaultSouceCount - 1)); | |
| 197 AddFakeSource(kDefaultSouceCount - 2, base::UTF8ToUTF16("Test tab"), | |
| 198 static_cast<uint8_t>(kDefaultSouceCount - 2)); | |
| 199 list_->SetFakeSourcesForTesting(fake_sources_, fake_favicons_); | |
| 200 | |
| 201 EXPECT_CALL(observer_, OnSourceMoved(list_.get(), kDefaultSouceCount - 1, | |
| 202 kDefaultSouceCount - 2)) | |
| 203 .WillOnce(DoAll(CheckListSize(list_.get(), kDefaultSouceCount), | |
| 204 QuitMessageLoop(&message_loop_))); | |
| 205 | |
| 206 message_loop_.Run(); | |
| 207 } | |
| 208 | |
| 209 TEST_F(TabDesktopMediaListTest, UpdateTitle) { | |
| 210 InitializeAndVerify(); | |
| 211 | |
| 212 fake_sources_[0].name = base::UTF8ToUTF16("New test tab"); | |
| 213 list_->SetFakeSourcesForTesting(fake_sources_, fake_favicons_); | |
| 214 | |
| 215 EXPECT_CALL(observer_, OnSourceNameChanged(list_.get(), 0)) | |
| 216 .WillOnce(QuitMessageLoop(&message_loop_)); | |
| 217 | |
| 218 message_loop_.Run(); | |
| 219 | |
| 220 EXPECT_EQ(list_->GetSource(0).name, base::UTF8ToUTF16("New test tab")); | |
| 221 } | |
| 222 | |
| 223 TEST_F(TabDesktopMediaListTest, UpdateThumbnail) { | |
| 224 InitializeAndVerify(); | |
| 225 | |
| 226 fake_favicons_[0] = | |
| 227 CreateGrayscaleImage(gfx::Size(kThumbnailSize, kThumbnailSize), 100); | |
| 228 list_->SetFakeSourcesForTesting(fake_sources_, fake_favicons_); | |
| 229 | |
| 230 EXPECT_CALL(observer_, OnSourceThumbnailChanged(list_.get(), 0)) | |
| 231 .WillOnce(QuitMessageLoop(&message_loop_)); | |
| 232 | |
| 233 message_loop_.Run(); | |
| 234 } | |
| 235 | |
| 236 } // namespace | |
| OLD | NEW |