Chromium Code Reviews| Index: chrome/browser/media/native_desktop_media_list_unittest.cc |
| diff --git a/chrome/browser/media/native_desktop_media_list_unittest.cc b/chrome/browser/media/native_desktop_media_list_unittest.cc |
| index 07ba3ecd93ed232f88ea9633033e812f18900853..287f6798054dc4fcd51b55910072369331430f27 100644 |
| --- a/chrome/browser/media/native_desktop_media_list_unittest.cc |
| +++ b/chrome/browser/media/native_desktop_media_list_unittest.cc |
| @@ -7,10 +7,10 @@ |
| #include <stddef.h> |
| #include <stdint.h> |
| #include <string.h> |
| +#include <vector> |
| #include "base/location.h" |
| #include "base/macros.h" |
| -#include "base/message_loop/message_loop.h" |
| #include "base/single_thread_task_runner.h" |
| #include "base/strings/utf_string_conversions.h" |
| #include "base/synchronization/lock.h" |
| @@ -21,12 +21,25 @@ |
| #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
| #include "third_party/webrtc/modules/desktop_capture/screen_capturer.h" |
| #include "third_party/webrtc/modules/desktop_capture/window_capturer.h" |
| +#include "ui/aura/window.h" |
| +#include "ui/aura/window_tree_host.h" |
| +#include "ui/views/test/views_test_base.h" |
| +#include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h" |
| +#include "ui/views/widget/widget.h" |
| +using content::DesktopMediaID; |
| using testing::_; |
| using testing::DoAll; |
| namespace { |
| +static const int kDefaultWindowCount = 2; |
| +#if defined(USE_AURA) |
| +static const int kDefaultAuraCount = 1; |
| +#else |
| +static const int kDefaultAuraCount = 0; |
| +#endif |
| + |
| class MockObserver : public DesktopMediaListObserver { |
| public: |
| MOCK_METHOD2(OnSourceAdded, void(DesktopMediaList* list, int index)); |
| @@ -135,6 +148,8 @@ class FakeWindowCapturer : public webrtc::WindowCapturer { |
| DISALLOW_COPY_AND_ASSIGN(FakeWindowCapturer); |
| }; |
| +} // namespace |
| + |
| ACTION_P2(CheckListSize, model, expected_list_size) { |
| EXPECT_EQ(expected_list_size, model->GetSourceCount()); |
| } |
| @@ -144,38 +159,149 @@ ACTION_P(QuitMessageLoop, message_loop) { |
| FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| } |
| -class DesktopMediaListTest : public testing::Test { |
| +class NativeDesktopMediaListTest : public views::ViewsTestBase { |
| public: |
| - DesktopMediaListTest() |
| - : window_capturer_(NULL), |
| - ui_thread_(content::BrowserThread::UI, |
| - &message_loop_) { |
| + NativeDesktopMediaListTest() |
| + : ui_thread_(content::BrowserThread::UI, message_loop()) {} |
| + |
| + void TearDown() override { |
| + for (size_t i = 0; i < desktop_widgets_.size(); i++) |
| + desktop_widgets_[i].reset(); |
| + |
| + ViewsTestBase::TearDown(); |
| } |
| - void CreateWithDefaultCapturers() { |
| - window_capturer_ = new FakeWindowCapturer(); |
| + void CreateWithCapturers(bool screen, bool window) { |
| + webrtc::ScreenCapturer* screen_capturer = nullptr; |
| + if (screen) |
| + screen_capturer = new FakeScreenCapturer(); |
| + |
| + if (window) |
| + window_capturer_ = new FakeWindowCapturer(); |
| + else |
| + window_capturer_ = nullptr; |
| + |
| model_.reset(new NativeDesktopMediaList( |
| - scoped_ptr<webrtc::ScreenCapturer>(new FakeScreenCapturer()), |
| + scoped_ptr<webrtc::ScreenCapturer>(screen_capturer), |
| scoped_ptr<webrtc::WindowCapturer>(window_capturer_))); |
| // Set update period to reduce the time it takes to run tests. |
| - model_->SetUpdatePeriod(base::TimeDelta::FromMilliseconds(0)); |
| + model_->SetUpdatePeriod(base::TimeDelta::FromMilliseconds(5)); |
| + } |
| + |
| + void AddNativeWindow(int id) { |
| + webrtc::WindowCapturer::Window window; |
| + window.id = id; |
| + window.title = "Test window"; |
| + window_list_.push_back(window); |
| + } |
| + |
| +#if defined(USE_AURA) |
| + std::unique_ptr<views::Widget> CreateDesktopWidget() { |
| + std::unique_ptr<views::Widget> widget(new views::Widget); |
| + views::Widget::InitParams params; |
| + params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; |
| + params.accept_events = false; |
| + params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| + params.native_widget = new views::DesktopNativeWidgetAura(widget.get()); |
| + params.bounds = gfx::Rect(0, 0, 20, 20); |
| + widget->Init(params); |
| + widget->Show(); |
| + return widget; |
| } |
| - webrtc::WindowCapturer::WindowList AddWindowsAndVerify( |
| - size_t count, bool window_only) { |
| - webrtc::WindowCapturer::WindowList list; |
| - for (size_t i = 0; i < count; ++i) { |
| - webrtc::WindowCapturer::Window window; |
| - window.id = i + 1; |
| - window.title = "Test window"; |
| - list.push_back(window); |
| + void AddAuraWindow() { |
| + webrtc::WindowCapturer::Window window; |
| + window.title = "Test window"; |
| + // Create a aura native widow through a widget. |
| + desktop_widgets_.push_back(CreateDesktopWidget()); |
| + // Get the native window's id. |
| + aura::Window* aura_window = desktop_widgets_.back()->GetNativeWindow(); |
| + gfx::AcceleratedWidget widget = |
| + aura_window->GetHost()->GetAcceleratedWidget(); |
| +#if defined(OS_WIN) |
| + window.id = reinterpret_cast<DesktopMediaID::Id>(widget); |
| +#else |
| + window.id = widget; |
| +#endif |
| + // Get the aura window's id. |
| + DesktopMediaID aura_id = DesktopMediaID::RegisterAuraWindow( |
| + DesktopMediaID::TYPE_WINDOW, aura_window); |
| + native_aura_id_map_[window.id] = aura_id.aura_id; |
| + |
| + window_list_.push_back(window); |
| + } |
| + |
| + void RemoveAuraWindow(int index) { |
| + DCHECK_LT(index, static_cast<int>(desktop_widgets_.size())); |
| + |
| + // Get the native window's id. |
| + aura::Window* aura_window = desktop_widgets_[index]->GetNativeWindow(); |
| + gfx::AcceleratedWidget widget = |
| + aura_window->GetHost()->GetAcceleratedWidget(); |
| +#if defined(OS_WIN) |
| + int native_id = reinterpret_cast<DesktopMediaID::Id>(widget); |
| +#else |
| + int native_id = widget; |
| +#endif |
| + // Remove the widget and assoicate aura window. |
| + desktop_widgets_[index].reset(); |
|
Sergey Ulanov
2016/04/07 18:17:31
don't need this. Since it's a vector of scoped_ptr
GeorgeZ
2016/04/08 13:15:23
Done.
|
| + desktop_widgets_.erase(desktop_widgets_.begin() + index); |
| + // Remove the aura window from the window list. |
| + size_t i; |
| + for (i = 0; i < window_list_.size(); i++) { |
| + if (window_list_[i].id == native_id) |
| + break; |
| + } |
|
Sergey Ulanov
2016/04/07 18:17:30
check here that i < window_list_.size()
GeorgeZ
2016/04/08 13:15:22
Done.
|
| + window_list_.erase(window_list_.begin() + i); |
| + native_aura_id_map_.erase(native_id); |
| + } |
| + |
| +#endif // defined(USE_AURA) |
| + |
| + void AddWindowsAndVerify(bool screen, |
| + size_t window_count, |
| + size_t aura_count, |
| + bool has_view_dialog) { |
| + // Create model_. |
| + CreateWithCapturers(screen, window_count > 0); |
| + |
| +#if !defined(USE_AURA) |
| + aura_count = 0; |
| +#endif |
| + if (aura_count >= window_count) |
| + aura_count = window_count - 1; |
| + |
| + if (window_count == 0) |
| + has_view_dialog = false; |
| + |
| + // Set up widows. |
| + size_t aura_window_first_index = window_count - aura_count; |
| + for (size_t i = 0; i < window_count; ++i) { |
| + if (i < aura_window_first_index) { |
| + AddNativeWindow(i + 1); |
| + } else { |
| +#if defined(USE_AURA) |
| + AddAuraWindow(); |
| +#endif |
| + } |
| + } |
| + |
| + if (window_capturer_) |
| + window_capturer_->SetWindowList(window_list_); |
| + |
| + // Set view dialog window ID as the first window id. |
| + if (has_view_dialog) { |
| + DesktopMediaID dialog_window_id(DesktopMediaID::TYPE_WINDOW, |
| + window_list_[0].id); |
| + model_->SetViewDialogWindowId(dialog_window_id); |
| + window_count--; |
| + aura_window_first_index--; |
| } |
| - window_capturer_->SetWindowList(list); |
| { |
| testing::InSequence dummy; |
| - size_t source_count = window_only ? count : count + 1; |
| + size_t source_count = screen ? window_count + 1 : window_count; |
| for (size_t i = 0; i < source_count; ++i) { |
| EXPECT_CALL(observer_, OnSourceAdded(model_.get(), i)) |
| .WillOnce(CheckListSize(model_.get(), static_cast<int>(i + 1))); |
| @@ -185,21 +311,32 @@ class DesktopMediaListTest : public testing::Test { |
| } |
| EXPECT_CALL(observer_, |
| OnSourceThumbnailChanged(model_.get(), source_count - 1)) |
| - .WillOnce(QuitMessageLoop(&message_loop_)); |
| + .WillOnce(QuitMessageLoop(message_loop())); |
| } |
| model_->StartUpdating(&observer_); |
| - message_loop_.Run(); |
| + message_loop()->Run(); |
| - for (size_t i = 0; i < count; ++i) { |
| - size_t source_index = window_only ? i : i + 1; |
| + if (screen) { |
| + EXPECT_EQ(model_->GetSource(0).id.type, DesktopMediaID::TYPE_SCREEN); |
| + EXPECT_EQ(model_->GetSource(0).id.id, 0); |
| + } |
| + |
| + for (size_t i = 0; i < window_count; ++i) { |
| + size_t source_index = screen ? i + 1 : i; |
| EXPECT_EQ(model_->GetSource(source_index).id.type, |
| - content::DesktopMediaID::TYPE_WINDOW); |
| - EXPECT_EQ(model_->GetSource(source_index).id.id, static_cast<int>(i + 1)); |
| + DesktopMediaID::TYPE_WINDOW); |
| EXPECT_EQ(model_->GetSource(source_index).name, |
| - base::UTF8ToUTF16("Test window")); |
| + base::UTF8ToUTF16("Test window")); |
| + int index = has_view_dialog ? i + 1 : i; |
| + int native_id = window_list_[index].id; |
| + EXPECT_EQ(model_->GetSource(source_index).id.id, native_id); |
| +#if defined(USE_AURA) |
| + if (i >= aura_window_first_index) |
| + EXPECT_EQ(model_->GetSource(source_index).id.aura_id, |
| + native_aura_id_map_[native_id]); |
| +#endif |
| } |
| testing::Mock::VerifyAndClearExpectations(&observer_); |
| - return list; |
| } |
| protected: |
| @@ -209,154 +346,156 @@ class DesktopMediaListTest : public testing::Test { |
| // Owned by |model_|; |
| FakeWindowCapturer* window_capturer_; |
| + webrtc::WindowCapturer::WindowList window_list_; |
| + std::vector<std::unique_ptr<views::Widget>> desktop_widgets_; |
| + std::map<DesktopMediaID::Id, DesktopMediaID::Id> native_aura_id_map_; |
| scoped_ptr<NativeDesktopMediaList> model_; |
| - base::MessageLoop message_loop_; |
| content::TestBrowserThread ui_thread_; |
| - DISALLOW_COPY_AND_ASSIGN(DesktopMediaListTest); |
| + DISALLOW_COPY_AND_ASSIGN(NativeDesktopMediaListTest); |
| }; |
| -TEST_F(DesktopMediaListTest, InitialSourceList) { |
| - CreateWithDefaultCapturers(); |
| - webrtc::WindowCapturer::WindowList list = AddWindowsAndVerify(1, false); |
| +TEST_F(NativeDesktopMediaListTest, WindowsOnly) { |
| + AddWindowsAndVerify(false, kDefaultWindowCount, kDefaultAuraCount, false); |
| +} |
| - EXPECT_EQ(model_->GetSource(0).id.type, content::DesktopMediaID::TYPE_SCREEN); |
| - EXPECT_EQ(model_->GetSource(0).id.id, 0); |
| +TEST_F(NativeDesktopMediaListTest, ScreenOnly) { |
| + AddWindowsAndVerify(true, 0, 0, false); |
| } |
| // Verifies that the window specified with SetViewDialogWindowId() is filtered |
| // from the results. |
| -TEST_F(DesktopMediaListTest, Filtering) { |
| - CreateWithDefaultCapturers(); |
| - webrtc::WindowCapturer::WindowList list = AddWindowsAndVerify(2, false); |
| - |
| - EXPECT_EQ(model_->GetSource(0).id.type, content::DesktopMediaID::TYPE_SCREEN); |
| - EXPECT_EQ(model_->GetSource(0).id.id, 0); |
| +TEST_F(NativeDesktopMediaListTest, Filtering) { |
| + AddWindowsAndVerify(true, kDefaultWindowCount, kDefaultAuraCount, true); |
| } |
| -TEST_F(DesktopMediaListTest, WindowsOnly) { |
| - window_capturer_ = new FakeWindowCapturer(); |
| - model_.reset(new NativeDesktopMediaList( |
| - scoped_ptr<webrtc::ScreenCapturer>(), |
| - scoped_ptr<webrtc::WindowCapturer>(window_capturer_))); |
| - AddWindowsAndVerify(1, true); |
| -} |
| +TEST_F(NativeDesktopMediaListTest, AddNativeWindow) { |
| + AddWindowsAndVerify(true, kDefaultWindowCount, kDefaultAuraCount, false); |
| -TEST_F(DesktopMediaListTest, ScreenOnly) { |
| - model_.reset(new NativeDesktopMediaList( |
| - scoped_ptr<webrtc::ScreenCapturer>(new FakeScreenCapturer), |
| - scoped_ptr<webrtc::WindowCapturer>())); |
| - |
| - { |
| - testing::InSequence dummy; |
| - EXPECT_CALL(observer_, OnSourceAdded(model_.get(), 0)) |
| - .WillOnce(CheckListSize(model_.get(), 1)); |
| - EXPECT_CALL(observer_, OnSourceThumbnailChanged(model_.get(), 0)) |
| - .WillOnce(QuitMessageLoop(&message_loop_)); |
| - } |
| - model_->StartUpdating(&observer_); |
| + const int index = kDefaultWindowCount + 1; |
| + EXPECT_CALL(observer_, OnSourceAdded(model_.get(), index)) |
| + .WillOnce(DoAll(CheckListSize(model_.get(), index + 1), |
| + QuitMessageLoop(message_loop()))); |
| - message_loop_.Run(); |
| + webrtc::WindowCapturer::Window window; |
| + AddNativeWindow(index); |
| + window_capturer_->SetWindowList(window_list_); |
| + |
| + message_loop()->Run(); |
| - EXPECT_EQ(model_->GetSource(0).id.type, content::DesktopMediaID::TYPE_SCREEN); |
| + EXPECT_EQ(model_->GetSource(index).id.type, DesktopMediaID::TYPE_WINDOW); |
| + EXPECT_EQ(model_->GetSource(index).id.id, index); |
| } |
| -TEST_F(DesktopMediaListTest, AddWindow) { |
| - CreateWithDefaultCapturers(); |
| - webrtc::WindowCapturer::WindowList list = AddWindowsAndVerify(1, false); |
| +#if defined(USE_AURA) |
| +TEST_F(NativeDesktopMediaListTest, AddAuraWindow) { |
| + AddWindowsAndVerify(true, kDefaultWindowCount, kDefaultAuraCount, false); |
| - EXPECT_CALL(observer_, OnSourceAdded(model_.get(), 2)) |
| - .WillOnce(DoAll(CheckListSize(model_.get(), 3), |
| - QuitMessageLoop(&message_loop_))); |
| + const int index = kDefaultWindowCount + 1; |
| + EXPECT_CALL(observer_, OnSourceAdded(model_.get(), index)) |
| + .WillOnce(DoAll(CheckListSize(model_.get(), index + 1), |
| + QuitMessageLoop(message_loop()))); |
| - webrtc::WindowCapturer::Window window; |
| - window.id = 10; // id=0 is invalid. |
| - window.title = "Test window 10"; |
| - list.push_back(window); |
| - window_capturer_->SetWindowList(list); |
| + AddAuraWindow(); |
| + window_capturer_->SetWindowList(window_list_); |
| + |
| + message_loop()->Run(); |
| + |
| + int native_id = window_list_.back().id; |
| + EXPECT_EQ(model_->GetSource(index).id.type, DesktopMediaID::TYPE_WINDOW); |
| + EXPECT_EQ(model_->GetSource(index).id.id, native_id); |
| + EXPECT_EQ(model_->GetSource(index).id.aura_id, |
| + native_aura_id_map_[native_id]); |
| +} |
| +#endif // defined(USE_AURA) |
| + |
| +TEST_F(NativeDesktopMediaListTest, RemoveNativeWindow) { |
| + AddWindowsAndVerify(true, kDefaultWindowCount, kDefaultAuraCount, false); |
| - message_loop_.Run(); |
| + EXPECT_CALL(observer_, OnSourceRemoved(model_.get(), 1)) |
| + .WillOnce(DoAll(CheckListSize(model_.get(), kDefaultWindowCount), |
| + QuitMessageLoop(message_loop()))); |
| - EXPECT_EQ(model_->GetSource(2).id.type, content::DesktopMediaID::TYPE_WINDOW); |
| - EXPECT_EQ(model_->GetSource(2).id.id, 10); |
| + window_list_.erase(window_list_.begin()); |
| + window_capturer_->SetWindowList(window_list_); |
| + |
| + message_loop()->Run(); |
| } |
| -TEST_F(DesktopMediaListTest, RemoveWindow) { |
| - CreateWithDefaultCapturers(); |
| - webrtc::WindowCapturer::WindowList list = AddWindowsAndVerify(2, false); |
| +#if defined(USE_AURA) |
| +TEST_F(NativeDesktopMediaListTest, RemoveAuraWindow) { |
| + AddWindowsAndVerify(true, kDefaultWindowCount, kDefaultAuraCount, false); |
| - EXPECT_CALL(observer_, OnSourceRemoved(model_.get(), 2)) |
| - .WillOnce(DoAll(CheckListSize(model_.get(), 2), |
| - QuitMessageLoop(&message_loop_))); |
| + int aura_window_start_index = kDefaultWindowCount - kDefaultAuraCount + 1; |
| + EXPECT_CALL(observer_, OnSourceRemoved(model_.get(), aura_window_start_index)) |
| + .WillOnce(DoAll(CheckListSize(model_.get(), kDefaultWindowCount), |
| + QuitMessageLoop(message_loop()))); |
| - list.erase(list.begin() + 1); |
| - window_capturer_->SetWindowList(list); |
| + RemoveAuraWindow(0); |
| + window_capturer_->SetWindowList(window_list_); |
| - message_loop_.Run(); |
| + message_loop()->Run(); |
| } |
| +#endif // defined(USE_AURA) |
| -TEST_F(DesktopMediaListTest, RemoveAllWindows) { |
| - CreateWithDefaultCapturers(); |
| - webrtc::WindowCapturer::WindowList list = AddWindowsAndVerify(2, false); |
| +TEST_F(NativeDesktopMediaListTest, RemoveAllWindows) { |
| + AddWindowsAndVerify(true, kDefaultWindowCount, kDefaultAuraCount, false); |
| testing::InSequence seq; |
| - EXPECT_CALL(observer_, OnSourceRemoved(model_.get(), 1)) |
| - .WillOnce(CheckListSize(model_.get(), 2)); |
| + for (int i = 0; i < kDefaultWindowCount - 1; i++) { |
| + EXPECT_CALL(observer_, OnSourceRemoved(model_.get(), 1)) |
| + .WillOnce(CheckListSize(model_.get(), kDefaultWindowCount - i)); |
| + } |
| EXPECT_CALL(observer_, OnSourceRemoved(model_.get(), 1)) |
| .WillOnce(DoAll(CheckListSize(model_.get(), 1), |
| - QuitMessageLoop(&message_loop_))); |
| + QuitMessageLoop(message_loop()))); |
| - list.erase(list.begin(), list.end()); |
| - window_capturer_->SetWindowList(list); |
| + window_list_.clear(); |
| + window_capturer_->SetWindowList(window_list_); |
| - message_loop_.Run(); |
| + message_loop()->Run(); |
| } |
| -TEST_F(DesktopMediaListTest, UpdateTitle) { |
| - CreateWithDefaultCapturers(); |
| - webrtc::WindowCapturer::WindowList list = AddWindowsAndVerify(1, false); |
| +TEST_F(NativeDesktopMediaListTest, UpdateTitle) { |
| + AddWindowsAndVerify(true, kDefaultWindowCount, kDefaultAuraCount, false); |
| EXPECT_CALL(observer_, OnSourceNameChanged(model_.get(), 1)) |
| - .WillOnce(QuitMessageLoop(&message_loop_)); |
| + .WillOnce(QuitMessageLoop(message_loop())); |
| const std::string kTestTitle = "New Title"; |
| + window_list_[0].title = kTestTitle; |
| + window_capturer_->SetWindowList(window_list_); |
| - list[0].title = kTestTitle; |
| - window_capturer_->SetWindowList(list); |
| - |
| - message_loop_.Run(); |
| + message_loop()->Run(); |
| EXPECT_EQ(model_->GetSource(1).name, base::UTF8ToUTF16(kTestTitle)); |
| } |
| -TEST_F(DesktopMediaListTest, UpdateThumbnail) { |
| - CreateWithDefaultCapturers(); |
| - AddWindowsAndVerify(2, false); |
| +TEST_F(NativeDesktopMediaListTest, UpdateThumbnail) { |
| + AddWindowsAndVerify(true, kDefaultWindowCount, kDefaultAuraCount, false); |
| EXPECT_CALL(observer_, OnSourceThumbnailChanged(model_.get(), 1)) |
| - .WillOnce(QuitMessageLoop(&message_loop_)); |
| + .WillOnce(QuitMessageLoop(message_loop())); |
| + |
| // Update frame for the window and verify that we get notification about it. |
| - window_capturer_->SetNextFrameValue(1, 1); |
| + window_capturer_->SetNextFrameValue(1, 10); |
| - message_loop_.Run(); |
| + message_loop()->Run(); |
| } |
| -TEST_F(DesktopMediaListTest, MoveWindow) { |
| - CreateWithDefaultCapturers(); |
| - webrtc::WindowCapturer::WindowList list = AddWindowsAndVerify(2, false); |
| +TEST_F(NativeDesktopMediaListTest, MoveWindow) { |
| + AddWindowsAndVerify(true, kDefaultWindowCount, kDefaultAuraCount, false); |
| EXPECT_CALL(observer_, OnSourceMoved(model_.get(), 2, 1)) |
| - .WillOnce(DoAll(CheckListSize(model_.get(), 3), |
| - QuitMessageLoop(&message_loop_))); |
| + .WillOnce(DoAll(CheckListSize(model_.get(), kDefaultWindowCount + 1), |
| + QuitMessageLoop(message_loop()))); |
| // Swap the two windows. |
| - webrtc::WindowCapturer::Window temp = list[0]; |
| - list[0] = list[1]; |
| - list[1] = temp; |
| - window_capturer_->SetWindowList(list); |
| + webrtc::WindowCapturer::Window temp = window_list_[0]; |
| + window_list_[0] = window_list_[1]; |
| + window_list_[1] = temp; |
| + window_capturer_->SetWindowList(window_list_); |
| - message_loop_.Run(); |
| + message_loop()->Run(); |
| } |
| - |
| -} // namespace |