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.
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/views/test/views_test_helper.h"
17 #include "ui/views/widget/widget.h"
18 #include "ui/views/window/dialog_delegate.h"
22 class DesktopMediaPickerViewsTest
: public testing::Test
{
24 DesktopMediaPickerViewsTest() {}
25 virtual ~DesktopMediaPickerViewsTest() {}
27 virtual void SetUp() OVERRIDE
{
28 bool enable_pixel_output
= false;
29 ui::ContextFactory
* context_factory
=
30 ui::InitializeContextFactoryForTests(enable_pixel_output
);
32 ViewsTestHelper::Create(base::MessageLoopForUI::current(),
34 test_helper_
->SetUp();
36 Widget::InitParams
params(Widget::InitParams::TYPE_WINDOW
);
37 params
.context
= test_helper_
->GetContext();
38 params
.ownership
= Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
39 parent_widget_
.reset(new Widget
);
40 parent_widget_
->Init(params
);
42 media_list_
= new FakeDesktopMediaList();
43 scoped_ptr
<FakeDesktopMediaList
> media_list(media_list_
);
45 base::string16 app_name
= base::ASCIIToUTF16("foo");
47 picker_views_
.reset(new DesktopMediaPickerViews());
48 picker_views_
->Show(NULL
,
50 parent_widget_
->GetNativeWindow(),
53 media_list
.PassAs
<DesktopMediaList
>(),
54 base::Bind(&DesktopMediaPickerViewsTest::OnPickerDone
,
55 base::Unretained(this)));
58 virtual void TearDown() OVERRIDE
{
59 test_helper_
->TearDown();
60 ui::TerminateContextFactoryForTests();
63 DesktopMediaPickerDialogView
* GetPickerDialogView() const {
64 return picker_views_
->GetDialogViewForTesting();
67 MOCK_METHOD1(OnPickerDone
, void(content::DesktopMediaID
));
70 content::TestBrowserThreadBundle thread_bundle_
;
71 scoped_ptr
<views::ViewsTestHelper
> test_helper_
;
72 FakeDesktopMediaList
* media_list_
;
73 scoped_ptr
<Widget
> parent_widget_
;
74 scoped_ptr
<DesktopMediaPickerViews
> picker_views_
;
77 TEST_F(DesktopMediaPickerViewsTest
, DoneCallbackCalledWhenWindowClosed
) {
78 EXPECT_CALL(*this, OnPickerDone(content::DesktopMediaID()));
80 GetPickerDialogView()->GetWidget()->Close();
81 base::RunLoop().RunUntilIdle();
84 TEST_F(DesktopMediaPickerViewsTest
, DoneCallbackCalledOnOkButtonPressed
) {
85 const int kFakeId
= 222;
87 OnPickerDone(content::DesktopMediaID(
88 content::DesktopMediaID::TYPE_WINDOW
, kFakeId
)));
90 media_list_
->AddSource(kFakeId
);
93 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
95 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnFocus();
97 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
99 GetPickerDialogView()->Accept();
100 base::RunLoop().RunUntilIdle();
103 TEST_F(DesktopMediaPickerViewsTest
, DoneCallbackCalledOnDoubleClick
) {
104 const int kFakeId
= 222;
106 OnPickerDone(content::DesktopMediaID(
107 content::DesktopMediaID::TYPE_WINDOW
, kFakeId
)));
109 media_list_
->AddSource(kFakeId
);
111 ui::MouseEvent
double_click(ui::ET_MOUSE_PRESSED
,
114 ui::EF_LEFT_MOUSE_BUTTON
| ui::EF_IS_DOUBLE_CLICK
,
115 ui::EF_LEFT_MOUSE_BUTTON
);
117 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnMousePressed(
119 base::RunLoop().RunUntilIdle();
122 TEST_F(DesktopMediaPickerViewsTest
, DoneCallbackCalledOnDoubleTap
) {
123 const int kFakeId
= 222;
125 OnPickerDone(content::DesktopMediaID(
126 content::DesktopMediaID::TYPE_WINDOW
, kFakeId
)));
128 media_list_
->AddSource(kFakeId
);
130 ui::GestureEvent
double_tap(
135 ui::GestureEventDetails(ui::ET_GESTURE_TAP
, 2, 0));
137 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnGestureEvent(
139 base::RunLoop().RunUntilIdle();
142 TEST_F(DesktopMediaPickerViewsTest
, CancelButtonAlwaysEnabled
) {
144 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL
));
147 // Verifies that the MediaSourceView is added or removed when |media_list_| is
149 TEST_F(DesktopMediaPickerViewsTest
, AddAndRemoveMediaSource
) {
150 // No media source at first.
151 EXPECT_EQ(NULL
, GetPickerDialogView()->GetMediaSourceViewForTesting(0));
153 for (int i
= 0; i
< 3; ++i
) {
154 media_list_
->AddSource(i
);
155 EXPECT_TRUE(GetPickerDialogView()->GetMediaSourceViewForTesting(i
));
158 for (int i
= 2; i
>= 0; --i
) {
159 media_list_
->RemoveSource(i
);
160 EXPECT_EQ(NULL
, GetPickerDialogView()->GetMediaSourceViewForTesting(i
));
164 // Verifies that focusing the MediaSourceView marks it selected and the
165 // original selected MediaSourceView gets unselected.
166 TEST_F(DesktopMediaPickerViewsTest
, FocusMediaSourceViewToSelect
) {
167 media_list_
->AddSource(0);
168 media_list_
->AddSource(1);
170 DesktopMediaSourceView
* source_view_0
=
171 GetPickerDialogView()->GetMediaSourceViewForTesting(0);
173 DesktopMediaSourceView
* source_view_1
=
174 GetPickerDialogView()->GetMediaSourceViewForTesting(1);
176 EXPECT_FALSE(source_view_0
->is_selected());
177 EXPECT_FALSE(source_view_1
->is_selected());
179 source_view_0
->OnFocus();
180 EXPECT_TRUE(source_view_0
->is_selected());
182 // Removing the focus does not undo the selection.
183 source_view_0
->OnBlur();
184 EXPECT_TRUE(source_view_0
->is_selected());
186 source_view_1
->OnFocus();
187 EXPECT_FALSE(source_view_0
->is_selected());
188 EXPECT_TRUE(source_view_1
->is_selected());
191 TEST_F(DesktopMediaPickerViewsTest
, OkButtonDisabledWhenNoSelection
) {
192 media_list_
->AddSource(111);
195 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
197 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnFocus();
199 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));
201 media_list_
->RemoveSource(0);
203 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK
));