Make ViewsTestHelpers DCHECK that all Widgets are destroyed before unit test tear...
[chromium-blink-merge.git] / chrome / browser / ui / views / desktop_media_picker_views_unittest.cc
blobd43e36c9b598a9e4c2ff9002c9014a709d4b480c
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/events/event_utils.h"
16 #include "ui/views/test/scoped_views_test_helper.h"
17 #include "ui/views/widget/widget.h"
18 #include "ui/views/window/dialog_client_view.h"
19 #include "ui/views/window/dialog_delegate.h"
21 namespace views {
23 class DesktopMediaPickerViewsTest : public testing::Test {
24 public:
25 DesktopMediaPickerViewsTest() {}
26 ~DesktopMediaPickerViewsTest() override {}
28 void SetUp() override {
29 media_list_ = new FakeDesktopMediaList();
30 scoped_ptr<FakeDesktopMediaList> media_list(media_list_);
32 base::string16 app_name = base::ASCIIToUTF16("foo");
34 picker_views_.reset(new DesktopMediaPickerViews());
35 picker_views_->Show(NULL,
36 test_helper_.GetContext(),
37 NULL,
38 app_name,
39 app_name,
40 media_list.Pass(),
41 base::Bind(&DesktopMediaPickerViewsTest::OnPickerDone,
42 base::Unretained(this)));
45 void TearDown() override {
46 if (GetPickerDialogView()) {
47 EXPECT_CALL(*this, OnPickerDone(content::DesktopMediaID()));
48 GetPickerDialogView()->GetWidget()->CloseNow();
52 DesktopMediaPickerDialogView* GetPickerDialogView() const {
53 return picker_views_->GetDialogViewForTesting();
56 MOCK_METHOD1(OnPickerDone, void(content::DesktopMediaID));
58 protected:
59 content::TestBrowserThreadBundle thread_bundle_;
60 views::ScopedViewsTestHelper test_helper_;
61 FakeDesktopMediaList* media_list_;
62 scoped_ptr<DesktopMediaPickerViews> picker_views_;
65 TEST_F(DesktopMediaPickerViewsTest, DoneCallbackCalledWhenWindowClosed) {
66 EXPECT_CALL(*this, OnPickerDone(content::DesktopMediaID()));
68 GetPickerDialogView()->GetWidget()->Close();
69 base::RunLoop().RunUntilIdle();
72 TEST_F(DesktopMediaPickerViewsTest, DoneCallbackCalledOnOkButtonPressed) {
73 const int kFakeId = 222;
74 EXPECT_CALL(*this,
75 OnPickerDone(content::DesktopMediaID(
76 content::DesktopMediaID::TYPE_WINDOW, kFakeId)));
77 media_list_->AddSource(kFakeId);
79 EXPECT_FALSE(
80 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
82 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnFocus();
83 EXPECT_TRUE(
84 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
86 GetPickerDialogView()->GetDialogClientView()->AcceptWindow();
87 base::RunLoop().RunUntilIdle();
90 TEST_F(DesktopMediaPickerViewsTest, DoneCallbackCalledOnDoubleClick) {
91 const int kFakeId = 222;
92 EXPECT_CALL(*this,
93 OnPickerDone(content::DesktopMediaID(
94 content::DesktopMediaID::TYPE_WINDOW, kFakeId)));
96 media_list_->AddSource(kFakeId);
98 ui::MouseEvent double_click(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(),
99 ui::EventTimeForNow(),
100 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_IS_DOUBLE_CLICK,
101 ui::EF_LEFT_MOUSE_BUTTON);
103 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnMousePressed(
104 double_click);
105 base::RunLoop().RunUntilIdle();
108 TEST_F(DesktopMediaPickerViewsTest, DoneCallbackCalledOnDoubleTap) {
109 const int kFakeId = 222;
110 EXPECT_CALL(*this,
111 OnPickerDone(content::DesktopMediaID(
112 content::DesktopMediaID::TYPE_WINDOW, kFakeId)));
114 media_list_->AddSource(kFakeId);
115 ui::GestureEventDetails details(ui::ET_GESTURE_TAP);
116 details.set_tap_count(2);
117 ui::GestureEvent double_tap(10, 10, 0, base::TimeDelta(), details);
119 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnGestureEvent(
120 &double_tap);
121 base::RunLoop().RunUntilIdle();
124 TEST_F(DesktopMediaPickerViewsTest, CancelButtonAlwaysEnabled) {
125 EXPECT_TRUE(
126 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
129 // Verifies that the MediaSourceView is added or removed when |media_list_| is
130 // updated.
131 TEST_F(DesktopMediaPickerViewsTest, AddAndRemoveMediaSource) {
132 // No media source at first.
133 EXPECT_EQ(NULL, GetPickerDialogView()->GetMediaSourceViewForTesting(0));
135 for (int i = 0; i < 3; ++i) {
136 media_list_->AddSource(i);
137 EXPECT_TRUE(GetPickerDialogView()->GetMediaSourceViewForTesting(i));
140 for (int i = 2; i >= 0; --i) {
141 media_list_->RemoveSource(i);
142 EXPECT_EQ(NULL, GetPickerDialogView()->GetMediaSourceViewForTesting(i));
146 // Verifies that focusing the MediaSourceView marks it selected and the
147 // original selected MediaSourceView gets unselected.
148 TEST_F(DesktopMediaPickerViewsTest, FocusMediaSourceViewToSelect) {
149 media_list_->AddSource(0);
150 media_list_->AddSource(1);
152 DesktopMediaSourceView* source_view_0 =
153 GetPickerDialogView()->GetMediaSourceViewForTesting(0);
155 DesktopMediaSourceView* source_view_1 =
156 GetPickerDialogView()->GetMediaSourceViewForTesting(1);
158 EXPECT_FALSE(source_view_0->is_selected());
159 EXPECT_FALSE(source_view_1->is_selected());
161 source_view_0->OnFocus();
162 EXPECT_TRUE(source_view_0->is_selected());
164 // Removing the focus does not undo the selection.
165 source_view_0->OnBlur();
166 EXPECT_TRUE(source_view_0->is_selected());
168 source_view_1->OnFocus();
169 EXPECT_FALSE(source_view_0->is_selected());
170 EXPECT_TRUE(source_view_1->is_selected());
173 TEST_F(DesktopMediaPickerViewsTest, OkButtonDisabledWhenNoSelection) {
174 media_list_->AddSource(111);
176 EXPECT_FALSE(
177 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
179 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnFocus();
180 EXPECT_TRUE(
181 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
183 media_list_->RemoveSource(0);
184 EXPECT_FALSE(
185 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
188 } // namespace views