Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / ui / views / desktop_media_picker_views_unittest.cc
blob0e2e3f179bf9f12e93ae48c2a30437c6af63f653
1 // Copyright 2014 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.
5 #include "base/bind.h"
6 #include "base/run_loop.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/media/fake_desktop_media_list.h"
9 #include "chrome/browser/ui/views/desktop_media_picker_views.h"
10 #include "components/web_modal/test_web_contents_modal_dialog_host.h"
11 #include "content/public/test/test_browser_thread_bundle.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/aura/window.h"
15 #include "ui/compositor/test/context_factories_for_test.h"
16 #include "ui/events/event_utils.h"
17 #include "ui/views/test/views_test_helper.h"
18 #include "ui/views/widget/widget.h"
19 #include "ui/views/window/dialog_delegate.h"
21 namespace views {
23 class DesktopMediaPickerViewsTest : public testing::Test {
24 public:
25 DesktopMediaPickerViewsTest() {}
26 virtual ~DesktopMediaPickerViewsTest() {}
28 virtual void SetUp() override {
29 bool enable_pixel_output = false;
30 ui::ContextFactory* context_factory =
31 ui::InitializeContextFactoryForTests(enable_pixel_output);
32 test_helper_.reset(
33 ViewsTestHelper::Create(base::MessageLoopForUI::current(),
34 context_factory));
35 test_helper_->SetUp();
37 Widget::InitParams params(Widget::InitParams::TYPE_WINDOW);
38 params.context = test_helper_->GetContext();
39 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
40 parent_widget_.reset(new Widget);
41 parent_widget_->Init(params);
43 media_list_ = new FakeDesktopMediaList();
44 scoped_ptr<FakeDesktopMediaList> media_list(media_list_);
46 base::string16 app_name = base::ASCIIToUTF16("foo");
48 picker_views_.reset(new DesktopMediaPickerViews());
49 picker_views_->Show(NULL,
50 parent_widget_->GetNativeWindow(),
51 NULL,
52 app_name,
53 app_name,
54 media_list.Pass(),
55 base::Bind(&DesktopMediaPickerViewsTest::OnPickerDone,
56 base::Unretained(this)));
59 virtual void TearDown() override {
60 test_helper_->TearDown();
61 ui::TerminateContextFactoryForTests();
64 DesktopMediaPickerDialogView* GetPickerDialogView() const {
65 return picker_views_->GetDialogViewForTesting();
68 MOCK_METHOD1(OnPickerDone, void(content::DesktopMediaID));
70 protected:
71 content::TestBrowserThreadBundle thread_bundle_;
72 scoped_ptr<views::ViewsTestHelper> test_helper_;
73 FakeDesktopMediaList* media_list_;
74 scoped_ptr<Widget> parent_widget_;
75 scoped_ptr<DesktopMediaPickerViews> picker_views_;
78 TEST_F(DesktopMediaPickerViewsTest, DoneCallbackCalledWhenWindowClosed) {
79 EXPECT_CALL(*this, OnPickerDone(content::DesktopMediaID()));
81 GetPickerDialogView()->GetWidget()->Close();
82 base::RunLoop().RunUntilIdle();
85 TEST_F(DesktopMediaPickerViewsTest, DoneCallbackCalledOnOkButtonPressed) {
86 const int kFakeId = 222;
87 EXPECT_CALL(*this,
88 OnPickerDone(content::DesktopMediaID(
89 content::DesktopMediaID::TYPE_WINDOW, kFakeId)));
91 media_list_->AddSource(kFakeId);
93 EXPECT_FALSE(
94 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
96 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnFocus();
97 EXPECT_TRUE(
98 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
100 GetPickerDialogView()->Accept();
101 base::RunLoop().RunUntilIdle();
104 TEST_F(DesktopMediaPickerViewsTest, DoneCallbackCalledOnDoubleClick) {
105 const int kFakeId = 222;
106 EXPECT_CALL(*this,
107 OnPickerDone(content::DesktopMediaID(
108 content::DesktopMediaID::TYPE_WINDOW, kFakeId)));
110 media_list_->AddSource(kFakeId);
112 ui::MouseEvent double_click(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(),
113 ui::EventTimeForNow(),
114 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_IS_DOUBLE_CLICK,
115 ui::EF_LEFT_MOUSE_BUTTON);
117 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnMousePressed(
118 double_click);
119 base::RunLoop().RunUntilIdle();
122 TEST_F(DesktopMediaPickerViewsTest, DoneCallbackCalledOnDoubleTap) {
123 const int kFakeId = 222;
124 EXPECT_CALL(*this,
125 OnPickerDone(content::DesktopMediaID(
126 content::DesktopMediaID::TYPE_WINDOW, kFakeId)));
128 media_list_->AddSource(kFakeId);
129 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
130 details.set_tap_count(2);
131 ui::GestureEvent double_tap(10, 10, 0, base::TimeDelta(), details);
133 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnGestureEvent(
134 &double_tap);
135 base::RunLoop().RunUntilIdle();
138 TEST_F(DesktopMediaPickerViewsTest, CancelButtonAlwaysEnabled) {
139 EXPECT_TRUE(
140 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
143 // Verifies that the MediaSourceView is added or removed when |media_list_| is
144 // updated.
145 TEST_F(DesktopMediaPickerViewsTest, AddAndRemoveMediaSource) {
146 // No media source at first.
147 EXPECT_EQ(NULL, GetPickerDialogView()->GetMediaSourceViewForTesting(0));
149 for (int i = 0; i < 3; ++i) {
150 media_list_->AddSource(i);
151 EXPECT_TRUE(GetPickerDialogView()->GetMediaSourceViewForTesting(i));
154 for (int i = 2; i >= 0; --i) {
155 media_list_->RemoveSource(i);
156 EXPECT_EQ(NULL, GetPickerDialogView()->GetMediaSourceViewForTesting(i));
160 // Verifies that focusing the MediaSourceView marks it selected and the
161 // original selected MediaSourceView gets unselected.
162 TEST_F(DesktopMediaPickerViewsTest, FocusMediaSourceViewToSelect) {
163 media_list_->AddSource(0);
164 media_list_->AddSource(1);
166 DesktopMediaSourceView* source_view_0 =
167 GetPickerDialogView()->GetMediaSourceViewForTesting(0);
169 DesktopMediaSourceView* source_view_1 =
170 GetPickerDialogView()->GetMediaSourceViewForTesting(1);
172 EXPECT_FALSE(source_view_0->is_selected());
173 EXPECT_FALSE(source_view_1->is_selected());
175 source_view_0->OnFocus();
176 EXPECT_TRUE(source_view_0->is_selected());
178 // Removing the focus does not undo the selection.
179 source_view_0->OnBlur();
180 EXPECT_TRUE(source_view_0->is_selected());
182 source_view_1->OnFocus();
183 EXPECT_FALSE(source_view_0->is_selected());
184 EXPECT_TRUE(source_view_1->is_selected());
187 TEST_F(DesktopMediaPickerViewsTest, OkButtonDisabledWhenNoSelection) {
188 media_list_->AddSource(111);
190 EXPECT_FALSE(
191 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
193 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnFocus();
194 EXPECT_TRUE(
195 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
197 media_list_->RemoveSource(0);
198 EXPECT_FALSE(
199 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
202 } // namespace views