1 // Copyright 2013 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/webrtc/desktop_media_list_ash.h"
6
7 #include "base/location.h"
8 #include "base/macros.h"
9 #include "base/run_loop.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/threading/thread_task_runner_handle.h"
12 #include "build/build_config.h"
13 #include "chrome/browser/media/webrtc/desktop_media_list_observer.h"
14 #include "chrome/test/base/chrome_ash_test_base.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/aura/window.h"
18
19 int kThumbnailSize = 100;
20
21 using testing::AtLeast;
22 using testing::DoDefault;
23
24 class MockDesktopMediaListObserver : public DesktopMediaListObserver {
25 public:
26 MOCK_METHOD2(OnSourceAdded, void(DesktopMediaList* list, int index));
27 MOCK_METHOD2(OnSourceRemoved, void(DesktopMediaList* list, int index));
28 MOCK_METHOD3(OnSourceMoved,
29 void(DesktopMediaList* list, int old_index, int new_index));
30 MOCK_METHOD2(OnSourceNameChanged, void(DesktopMediaList* list, int index));
31 MOCK_METHOD2(OnSourceThumbnailChanged,
32 void(DesktopMediaList* list, int index));
33 };
34
35 class DesktopMediaListAshTest : public ChromeAshTestBase {
36 public:
DesktopMediaListAshTest()37 DesktopMediaListAshTest() {}
~DesktopMediaListAshTest()38 ~DesktopMediaListAshTest() override {}
39
TearDown()40 void TearDown() override {
41 // Reset the unique_ptr so the list stops refreshing.
42 list_.reset();
43 ChromeAshTestBase::TearDown();
44 }
45
CreateList(content::DesktopMediaID::Type type)46 void CreateList(content::DesktopMediaID::Type type) {
47 list_.reset(new DesktopMediaListAsh(type));
48 list_->SetThumbnailSize(gfx::Size(kThumbnailSize, kThumbnailSize));
49
50 // Set update period to reduce the time it takes to run tests.
51 list_->SetUpdatePeriod(base::TimeDelta::FromMilliseconds(1));
52 }
53
54 protected:
55 MockDesktopMediaListObserver observer_;
56 std::unique_ptr<DesktopMediaListAsh> list_;
57 DISALLOW_COPY_AND_ASSIGN(DesktopMediaListAshTest);
58 };
59
ACTION(QuitMessageLoop)60 ACTION(QuitMessageLoop) {
61 base::ThreadTaskRunnerHandle::Get()->PostTask(
62 FROM_HERE, base::RunLoop::QuitCurrentWhenIdleClosureDeprecated());
63 }
64
TEST_F(DesktopMediaListAshTest,ScreenOnly)65 TEST_F(DesktopMediaListAshTest, ScreenOnly) {
66 CreateList(content::DesktopMediaID::TYPE_SCREEN);
67
68 std::unique_ptr<aura::Window> window(CreateTestWindowInShellWithId(0));
69
70 EXPECT_CALL(observer_, OnSourceAdded(list_.get(), 0));
71 EXPECT_CALL(observer_, OnSourceThumbnailChanged(list_.get(), 0))
72 .WillOnce(QuitMessageLoop())
73 .WillRepeatedly(DoDefault());
74
75 list_->StartUpdating(&observer_);
76 base::RunLoop().Run();
77 }
78
TEST_F(DesktopMediaListAshTest,WindowOnly)79 TEST_F(DesktopMediaListAshTest, WindowOnly) {
80 CreateList(content::DesktopMediaID::TYPE_WINDOW);
81
82 std::unique_ptr<aura::Window> window(CreateTestWindowInShellWithId(0));
83
84 EXPECT_CALL(observer_, OnSourceAdded(list_.get(), 0));
85 EXPECT_CALL(observer_, OnSourceThumbnailChanged(list_.get(), 0))
86 .WillOnce(QuitMessageLoop())
87 .WillRepeatedly(DoDefault());
88 EXPECT_CALL(observer_, OnSourceRemoved(list_.get(), 0))
89 .WillOnce(QuitMessageLoop());
90
91 list_->StartUpdating(&observer_);
92 base::RunLoop().Run();
93 window.reset();
94 base::RunLoop().Run();
95 }
96