| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/ui/views/desktop_capture/desktop_media_picker_views.h" | |
| 6 | |
| 7 #include <map> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 13 #include "chrome/browser/media/fake_desktop_media_list.h" | |
| 14 #include "components/web_modal/test_web_contents_modal_dialog_host.h" | |
| 15 #include "content/public/test/test_browser_thread_bundle.h" | |
| 16 #include "testing/gmock/include/gmock/gmock.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | |
| 18 #include "ui/aura/window.h" | |
| 19 #include "ui/events/event_utils.h" | |
| 20 #include "ui/views/controls/button/checkbox.h" | |
| 21 #include "ui/views/controls/tabbed_pane/tabbed_pane.h" | |
| 22 #include "ui/views/test/scoped_views_test_helper.h" | |
| 23 #include "ui/views/widget/widget.h" | |
| 24 #include "ui/views/window/dialog_client_view.h" | |
| 25 #include "ui/views/window/dialog_delegate.h" | |
| 26 | |
| 27 using content::DesktopMediaID; | |
| 28 | |
| 29 namespace views { | |
| 30 | |
| 31 const std::vector<DesktopMediaID::Type> kSourceTypes = { | |
| 32 DesktopMediaID::TYPE_SCREEN, DesktopMediaID::TYPE_WINDOW, | |
| 33 DesktopMediaID::TYPE_WEB_CONTENTS}; | |
| 34 | |
| 35 class DesktopMediaPickerViewsTest : public testing::Test { | |
| 36 public: | |
| 37 DesktopMediaPickerViewsTest() {} | |
| 38 ~DesktopMediaPickerViewsTest() override {} | |
| 39 | |
| 40 void SetUp() override { | |
| 41 media_lists_[DesktopMediaID::TYPE_SCREEN] = new FakeDesktopMediaList(); | |
| 42 media_lists_[DesktopMediaID::TYPE_WINDOW] = new FakeDesktopMediaList(); | |
| 43 media_lists_[DesktopMediaID::TYPE_WEB_CONTENTS] = | |
| 44 new FakeDesktopMediaList(); | |
| 45 std::unique_ptr<FakeDesktopMediaList> screen_list( | |
| 46 media_lists_[DesktopMediaID::TYPE_SCREEN]); | |
| 47 std::unique_ptr<FakeDesktopMediaList> window_list( | |
| 48 media_lists_[DesktopMediaID::TYPE_WINDOW]); | |
| 49 std::unique_ptr<FakeDesktopMediaList> tab_list( | |
| 50 media_lists_[DesktopMediaID::TYPE_WEB_CONTENTS]); | |
| 51 | |
| 52 base::string16 app_name = base::ASCIIToUTF16("foo"); | |
| 53 | |
| 54 picker_views_.reset(new DesktopMediaPickerViews()); | |
| 55 picker_views_->Show(nullptr, test_helper_.GetContext(), nullptr, app_name, | |
| 56 app_name, std::move(screen_list), | |
| 57 std::move(window_list), std::move(tab_list), true, | |
| 58 base::Bind(&DesktopMediaPickerViewsTest::OnPickerDone, | |
| 59 base::Unretained(this))); | |
| 60 } | |
| 61 | |
| 62 void TearDown() override { | |
| 63 if (GetPickerDialogView()) { | |
| 64 EXPECT_CALL(*this, OnPickerDone(content::DesktopMediaID())); | |
| 65 GetPickerDialogView()->GetWidget()->CloseNow(); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 DesktopMediaPickerDialogView* GetPickerDialogView() const { | |
| 70 return picker_views_->GetDialogViewForTesting(); | |
| 71 } | |
| 72 | |
| 73 bool ClickSourceTypeButton(DesktopMediaID::Type source_type) { | |
| 74 int index = | |
| 75 GetPickerDialogView()->GetIndexOfSourceTypeForTesting(source_type); | |
| 76 | |
| 77 if (index == -1) | |
| 78 return false; | |
| 79 | |
| 80 GetPickerDialogView()->GetPaneForTesting()->SelectTabAt(index); | |
| 81 return true; | |
| 82 } | |
| 83 | |
| 84 MOCK_METHOD1(OnPickerDone, void(content::DesktopMediaID)); | |
| 85 | |
| 86 protected: | |
| 87 content::TestBrowserThreadBundle thread_bundle_; | |
| 88 views::ScopedViewsTestHelper test_helper_; | |
| 89 std::map<DesktopMediaID::Type, FakeDesktopMediaList*> media_lists_; | |
| 90 std::unique_ptr<DesktopMediaPickerViews> picker_views_; | |
| 91 }; | |
| 92 | |
| 93 TEST_F(DesktopMediaPickerViewsTest, DoneCallbackCalledWhenWindowClosed) { | |
| 94 EXPECT_CALL(*this, OnPickerDone(content::DesktopMediaID())); | |
| 95 | |
| 96 GetPickerDialogView()->GetWidget()->Close(); | |
| 97 base::RunLoop().RunUntilIdle(); | |
| 98 } | |
| 99 | |
| 100 TEST_F(DesktopMediaPickerViewsTest, DoneCallbackCalledOnOkButtonPressed) { | |
| 101 const DesktopMediaID kFakeId(DesktopMediaID::TYPE_WINDOW, 222); | |
| 102 EXPECT_CALL(*this, OnPickerDone(kFakeId)); | |
| 103 | |
| 104 media_lists_[DesktopMediaID::TYPE_WINDOW]->AddSourceByFullMediaID(kFakeId); | |
| 105 GetPickerDialogView()->GetCheckboxForTesting()->SetChecked(true); | |
| 106 | |
| 107 EXPECT_FALSE( | |
| 108 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); | |
| 109 | |
| 110 EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_WINDOW)); | |
| 111 | |
| 112 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnFocus(); | |
| 113 | |
| 114 EXPECT_TRUE( | |
| 115 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); | |
| 116 | |
| 117 GetPickerDialogView()->GetDialogClientView()->AcceptWindow(); | |
| 118 base::RunLoop().RunUntilIdle(); | |
| 119 } | |
| 120 | |
| 121 // Verifies that a MediaSourceView is selected with mouse left click and | |
| 122 // original selected MediaSourceView gets unselected. | |
| 123 TEST_F(DesktopMediaPickerViewsTest, SelectMediaSourceViewOnSingleClick) { | |
| 124 for (auto source_type : kSourceTypes) { | |
| 125 EXPECT_TRUE(ClickSourceTypeButton(source_type)); | |
| 126 media_lists_[source_type]->AddSourceByFullMediaID( | |
| 127 DesktopMediaID(source_type, 0)); | |
| 128 media_lists_[source_type]->AddSourceByFullMediaID( | |
| 129 DesktopMediaID(source_type, 1)); | |
| 130 | |
| 131 DesktopMediaSourceView* source_view_0 = | |
| 132 GetPickerDialogView()->GetMediaSourceViewForTesting(0); | |
| 133 | |
| 134 DesktopMediaSourceView* source_view_1 = | |
| 135 GetPickerDialogView()->GetMediaSourceViewForTesting(1); | |
| 136 | |
| 137 // By default, the first screen is selected, but not for other sharing | |
| 138 // types. | |
| 139 EXPECT_EQ(source_type == DesktopMediaID::TYPE_SCREEN, | |
| 140 source_view_0->is_selected()); | |
| 141 EXPECT_FALSE(source_view_1->is_selected()); | |
| 142 | |
| 143 // Source view 0 is selected with mouse click. | |
| 144 ui::MouseEvent press(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | |
| 145 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0); | |
| 146 | |
| 147 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnMousePressed( | |
| 148 press); | |
| 149 | |
| 150 EXPECT_TRUE(source_view_0->is_selected()); | |
| 151 EXPECT_FALSE(source_view_1->is_selected()); | |
| 152 | |
| 153 // Source view 1 is selected and source view 0 is unselected with mouse | |
| 154 // click. | |
| 155 GetPickerDialogView()->GetMediaSourceViewForTesting(1)->OnMousePressed( | |
| 156 press); | |
| 157 | |
| 158 EXPECT_FALSE(source_view_0->is_selected()); | |
| 159 EXPECT_TRUE(source_view_1->is_selected()); | |
| 160 } | |
| 161 } | |
| 162 | |
| 163 TEST_F(DesktopMediaPickerViewsTest, DoneCallbackCalledOnDoubleClick) { | |
| 164 const DesktopMediaID kFakeId(DesktopMediaID::TYPE_WEB_CONTENTS, 222); | |
| 165 EXPECT_CALL(*this, OnPickerDone(kFakeId)); | |
| 166 | |
| 167 media_lists_[DesktopMediaID::TYPE_WEB_CONTENTS]->AddSourceByFullMediaID( | |
| 168 kFakeId); | |
| 169 EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_WEB_CONTENTS)); | |
| 170 GetPickerDialogView()->GetCheckboxForTesting()->SetChecked(false); | |
| 171 | |
| 172 ui::MouseEvent double_click(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | |
| 173 ui::EventTimeForNow(), | |
| 174 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_IS_DOUBLE_CLICK, | |
| 175 ui::EF_LEFT_MOUSE_BUTTON); | |
| 176 | |
| 177 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnMousePressed( | |
| 178 double_click); | |
| 179 base::RunLoop().RunUntilIdle(); | |
| 180 } | |
| 181 | |
| 182 TEST_F(DesktopMediaPickerViewsTest, DoneCallbackCalledOnDoubleTap) { | |
| 183 const DesktopMediaID kFakeId(DesktopMediaID::TYPE_SCREEN, 222); | |
| 184 EXPECT_CALL(*this, OnPickerDone(kFakeId)); | |
| 185 | |
| 186 EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_SCREEN)); | |
| 187 GetPickerDialogView()->GetCheckboxForTesting()->SetChecked(false); | |
| 188 | |
| 189 media_lists_[DesktopMediaID::TYPE_SCREEN]->AddSourceByFullMediaID(kFakeId); | |
| 190 ui::GestureEventDetails details(ui::ET_GESTURE_TAP); | |
| 191 details.set_tap_count(2); | |
| 192 ui::GestureEvent double_tap(10, 10, 0, base::TimeDelta(), details); | |
| 193 | |
| 194 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnGestureEvent( | |
| 195 &double_tap); | |
| 196 base::RunLoop().RunUntilIdle(); | |
| 197 } | |
| 198 | |
| 199 TEST_F(DesktopMediaPickerViewsTest, CancelButtonAlwaysEnabled) { | |
| 200 EXPECT_TRUE( | |
| 201 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); | |
| 202 } | |
| 203 | |
| 204 // Verifies that the MediaSourceView is added or removed when |media_list_| is | |
| 205 // updated. | |
| 206 TEST_F(DesktopMediaPickerViewsTest, AddAndRemoveMediaSource) { | |
| 207 for (auto source_type : kSourceTypes) { | |
| 208 EXPECT_TRUE(ClickSourceTypeButton(source_type)); | |
| 209 // No media source at first. | |
| 210 EXPECT_FALSE(GetPickerDialogView()->GetMediaSourceViewForTesting(0)); | |
| 211 | |
| 212 for (int i = 0; i < 3; ++i) { | |
| 213 media_lists_[source_type]->AddSourceByFullMediaID( | |
| 214 DesktopMediaID(source_type, i)); | |
| 215 EXPECT_TRUE(GetPickerDialogView()->GetMediaSourceViewForTesting(i)); | |
| 216 } | |
| 217 | |
| 218 for (int i = 2; i >= 0; --i) { | |
| 219 media_lists_[source_type]->RemoveSource(i); | |
| 220 EXPECT_FALSE(GetPickerDialogView()->GetMediaSourceViewForTesting(i)); | |
| 221 } | |
| 222 } | |
| 223 } | |
| 224 | |
| 225 // Verifies that focusing the MediaSourceView marks it selected and the | |
| 226 // original selected MediaSourceView gets unselected. | |
| 227 TEST_F(DesktopMediaPickerViewsTest, FocusMediaSourceViewToSelect) { | |
| 228 for (auto source_type : kSourceTypes) { | |
| 229 ClickSourceTypeButton(source_type); | |
| 230 media_lists_[source_type]->AddSourceByFullMediaID( | |
| 231 DesktopMediaID(source_type, 0)); | |
| 232 media_lists_[source_type]->AddSourceByFullMediaID( | |
| 233 DesktopMediaID(source_type, 1)); | |
| 234 | |
| 235 DesktopMediaSourceView* source_view_0 = | |
| 236 GetPickerDialogView()->GetMediaSourceViewForTesting(0); | |
| 237 | |
| 238 DesktopMediaSourceView* source_view_1 = | |
| 239 GetPickerDialogView()->GetMediaSourceViewForTesting(1); | |
| 240 | |
| 241 source_view_0->OnFocus(); | |
| 242 EXPECT_TRUE(source_view_0->is_selected()); | |
| 243 | |
| 244 // Removing the focus does not undo the selection. | |
| 245 source_view_0->OnBlur(); | |
| 246 EXPECT_TRUE(source_view_0->is_selected()); | |
| 247 | |
| 248 source_view_1->OnFocus(); | |
| 249 EXPECT_FALSE(source_view_0->is_selected()); | |
| 250 EXPECT_TRUE(source_view_1->is_selected()); | |
| 251 } | |
| 252 } | |
| 253 | |
| 254 TEST_F(DesktopMediaPickerViewsTest, OkButtonDisabledWhenNoSelection) { | |
| 255 for (auto source_type : kSourceTypes) { | |
| 256 EXPECT_TRUE(ClickSourceTypeButton(source_type)); | |
| 257 media_lists_[source_type]->AddSourceByFullMediaID( | |
| 258 DesktopMediaID(source_type, 111)); | |
| 259 | |
| 260 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnFocus(); | |
| 261 EXPECT_TRUE( | |
| 262 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); | |
| 263 | |
| 264 media_lists_[source_type]->RemoveSource(0); | |
| 265 EXPECT_FALSE( | |
| 266 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); | |
| 267 } | |
| 268 } | |
| 269 | |
| 270 // Verifies that the MediaListView gets the initial focus. | |
| 271 TEST_F(DesktopMediaPickerViewsTest, ListViewHasInitialFocus) { | |
| 272 EXPECT_TRUE(GetPickerDialogView()->GetMediaListViewForTesting()->HasFocus()); | |
| 273 } | |
| 274 | |
| 275 // Verifies the visible status of audio checkbox. | |
| 276 TEST_F(DesktopMediaPickerViewsTest, AudioCheckboxState) { | |
| 277 bool expect_value = false; | |
| 278 EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_SCREEN)); | |
| 279 #if defined(OS_WIN) || defined(USE_CRAS) | |
| 280 expect_value = true; | |
| 281 #endif | |
| 282 EXPECT_EQ(expect_value, | |
| 283 GetPickerDialogView()->GetCheckboxForTesting()->visible()); | |
| 284 | |
| 285 EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_WINDOW)); | |
| 286 EXPECT_FALSE(GetPickerDialogView()->GetCheckboxForTesting()->visible()); | |
| 287 | |
| 288 EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_WEB_CONTENTS)); | |
| 289 EXPECT_TRUE(GetPickerDialogView()->GetCheckboxForTesting()->visible()); | |
| 290 } | |
| 291 | |
| 292 // Verifies that audio share information is recorded in the ID if the checkbox | |
| 293 // is checked. | |
| 294 TEST_F(DesktopMediaPickerViewsTest, DoneWithAudioShare) { | |
| 295 DesktopMediaID originId(DesktopMediaID::TYPE_WEB_CONTENTS, 222); | |
| 296 DesktopMediaID returnId = originId; | |
| 297 returnId.audio_share = true; | |
| 298 | |
| 299 // This matches the real workflow that when a source is generated in | |
| 300 // media_list, its |audio_share| bit is not set. The bit is set by the picker | |
| 301 // UI if the audio checkbox is checked. | |
| 302 EXPECT_CALL(*this, OnPickerDone(returnId)); | |
| 303 media_lists_[DesktopMediaID::TYPE_WEB_CONTENTS]->AddSourceByFullMediaID( | |
| 304 originId); | |
| 305 | |
| 306 EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_WEB_CONTENTS)); | |
| 307 GetPickerDialogView()->GetCheckboxForTesting()->SetChecked(true); | |
| 308 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnFocus(); | |
| 309 | |
| 310 GetPickerDialogView()->GetDialogClientView()->AcceptWindow(); | |
| 311 base::RunLoop().RunUntilIdle(); | |
| 312 } | |
| 313 | |
| 314 } // namespace views | |
| OLD | NEW |