Chromium Code Reviews| 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 type. | |
|
msw
2016/05/05 22:59:49
nit: types
qiangchen
2016/05/05 23:36:27
Done.
| |
| 138 EXPECT_EQ(source_type == DesktopMediaID::TYPE_SCREEN, | |
| 139 source_view_0->is_selected()); | |
| 140 EXPECT_FALSE(source_view_1->is_selected()); | |
| 141 | |
| 142 // Source view 0 is selected with mouse click. | |
| 143 ui::MouseEvent press(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | |
| 144 ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0); | |
| 145 | |
| 146 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnMousePressed( | |
| 147 press); | |
| 148 | |
| 149 EXPECT_TRUE(source_view_0->is_selected()); | |
| 150 EXPECT_FALSE(source_view_1->is_selected()); | |
| 151 | |
| 152 // Source view 1 is selected and source view 0 is unselected with mouse | |
| 153 // click. | |
| 154 GetPickerDialogView()->GetMediaSourceViewForTesting(1)->OnMousePressed( | |
| 155 press); | |
| 156 | |
| 157 EXPECT_FALSE(source_view_0->is_selected()); | |
| 158 EXPECT_TRUE(source_view_1->is_selected()); | |
| 159 } | |
| 160 } | |
| 161 | |
| 162 TEST_F(DesktopMediaPickerViewsTest, DoneCallbackCalledOnDoubleClick) { | |
| 163 const DesktopMediaID kFakeId(DesktopMediaID::TYPE_WEB_CONTENTS, 222); | |
| 164 EXPECT_CALL(*this, OnPickerDone(kFakeId)); | |
| 165 | |
| 166 media_lists_[DesktopMediaID::TYPE_WEB_CONTENTS]->AddSourceByFullMediaID( | |
| 167 kFakeId); | |
| 168 EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_WEB_CONTENTS)); | |
| 169 GetPickerDialogView()->GetCheckboxForTesting()->SetChecked(false); | |
| 170 | |
| 171 ui::MouseEvent double_click(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), | |
| 172 ui::EventTimeForNow(), | |
| 173 ui::EF_LEFT_MOUSE_BUTTON | ui::EF_IS_DOUBLE_CLICK, | |
| 174 ui::EF_LEFT_MOUSE_BUTTON); | |
| 175 | |
| 176 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnMousePressed( | |
| 177 double_click); | |
| 178 base::RunLoop().RunUntilIdle(); | |
| 179 } | |
| 180 | |
| 181 TEST_F(DesktopMediaPickerViewsTest, DoneCallbackCalledOnDoubleTap) { | |
| 182 const DesktopMediaID kFakeId(DesktopMediaID::TYPE_SCREEN, 222); | |
| 183 EXPECT_CALL(*this, OnPickerDone(kFakeId)); | |
| 184 | |
| 185 EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_SCREEN)); | |
| 186 GetPickerDialogView()->GetCheckboxForTesting()->SetChecked(false); | |
| 187 | |
| 188 media_lists_[DesktopMediaID::TYPE_SCREEN]->AddSourceByFullMediaID(kFakeId); | |
| 189 ui::GestureEventDetails details(ui::ET_GESTURE_TAP); | |
| 190 details.set_tap_count(2); | |
| 191 ui::GestureEvent double_tap(10, 10, 0, base::TimeDelta(), details); | |
| 192 | |
| 193 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnGestureEvent( | |
| 194 &double_tap); | |
| 195 base::RunLoop().RunUntilIdle(); | |
| 196 } | |
| 197 | |
| 198 TEST_F(DesktopMediaPickerViewsTest, CancelButtonAlwaysEnabled) { | |
| 199 EXPECT_TRUE( | |
| 200 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); | |
| 201 } | |
| 202 | |
| 203 // Verifies that the MediaSourceView is added or removed when |media_list_| is | |
| 204 // updated. | |
| 205 TEST_F(DesktopMediaPickerViewsTest, AddAndRemoveMediaSource) { | |
| 206 for (auto source_type : kSourceTypes) { | |
| 207 EXPECT_TRUE(ClickSourceTypeButton(source_type)); | |
| 208 // No media source at first. | |
| 209 EXPECT_FALSE(GetPickerDialogView()->GetMediaSourceViewForTesting(0)); | |
| 210 | |
| 211 for (int i = 0; i < 3; ++i) { | |
| 212 media_lists_[source_type]->AddSourceByFullMediaID( | |
| 213 DesktopMediaID(source_type, i)); | |
| 214 EXPECT_TRUE(GetPickerDialogView()->GetMediaSourceViewForTesting(i)); | |
| 215 } | |
| 216 | |
| 217 for (int i = 2; i >= 0; --i) { | |
| 218 media_lists_[source_type]->RemoveSource(i); | |
| 219 EXPECT_FALSE(GetPickerDialogView()->GetMediaSourceViewForTesting(i)); | |
| 220 } | |
| 221 } | |
| 222 } | |
| 223 | |
| 224 // Verifies that focusing the MediaSourceView marks it selected and the | |
| 225 // original selected MediaSourceView gets unselected. | |
| 226 TEST_F(DesktopMediaPickerViewsTest, FocusMediaSourceViewToSelect) { | |
| 227 for (auto source_type : kSourceTypes) { | |
| 228 ClickSourceTypeButton(source_type); | |
| 229 media_lists_[source_type]->AddSourceByFullMediaID( | |
| 230 DesktopMediaID(source_type, 0)); | |
| 231 media_lists_[source_type]->AddSourceByFullMediaID( | |
| 232 DesktopMediaID(source_type, 1)); | |
| 233 | |
| 234 DesktopMediaSourceView* source_view_0 = | |
| 235 GetPickerDialogView()->GetMediaSourceViewForTesting(0); | |
| 236 | |
| 237 DesktopMediaSourceView* source_view_1 = | |
| 238 GetPickerDialogView()->GetMediaSourceViewForTesting(1); | |
| 239 | |
| 240 source_view_0->OnFocus(); | |
| 241 EXPECT_TRUE(source_view_0->is_selected()); | |
| 242 | |
| 243 // Removing the focus does not undo the selection. | |
| 244 source_view_0->OnBlur(); | |
| 245 EXPECT_TRUE(source_view_0->is_selected()); | |
| 246 | |
| 247 source_view_1->OnFocus(); | |
| 248 EXPECT_FALSE(source_view_0->is_selected()); | |
| 249 EXPECT_TRUE(source_view_1->is_selected()); | |
| 250 } | |
| 251 } | |
| 252 | |
| 253 TEST_F(DesktopMediaPickerViewsTest, OkButtonDisabledWhenNoSelection) { | |
| 254 for (auto source_type : kSourceTypes) { | |
| 255 EXPECT_TRUE(ClickSourceTypeButton(source_type)); | |
| 256 media_lists_[source_type]->AddSourceByFullMediaID( | |
| 257 DesktopMediaID(source_type, 111)); | |
| 258 | |
| 259 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnFocus(); | |
| 260 EXPECT_TRUE( | |
| 261 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); | |
| 262 | |
| 263 media_lists_[source_type]->RemoveSource(0); | |
| 264 EXPECT_FALSE( | |
| 265 GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); | |
| 266 } | |
| 267 } | |
| 268 | |
| 269 // Verifies that the MediaListView gets the initial focus. | |
| 270 TEST_F(DesktopMediaPickerViewsTest, ListViewHasInitialFocus) { | |
| 271 EXPECT_TRUE(GetPickerDialogView()->GetMediaListViewForTesting()->HasFocus()); | |
| 272 } | |
| 273 | |
| 274 // Verifies the visible status of audio checkbox. | |
| 275 TEST_F(DesktopMediaPickerViewsTest, AudioCheckboxState) { | |
| 276 bool expect_value = false; | |
| 277 EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_SCREEN)); | |
| 278 #if defined(OS_WIN) || defined(USE_CRAS) | |
| 279 expect_value = true; | |
| 280 #endif | |
| 281 EXPECT_EQ(expect_value, | |
| 282 GetPickerDialogView()->GetCheckboxForTesting()->visible()); | |
| 283 | |
| 284 EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_WINDOW)); | |
| 285 EXPECT_FALSE(GetPickerDialogView()->GetCheckboxForTesting()->visible()); | |
| 286 | |
| 287 EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_WEB_CONTENTS)); | |
| 288 EXPECT_TRUE(GetPickerDialogView()->GetCheckboxForTesting()->visible()); | |
| 289 } | |
| 290 | |
| 291 // Verifies that audio share information is recorded in the ID if the checkbox | |
| 292 // is checked. | |
| 293 TEST_F(DesktopMediaPickerViewsTest, DoneWithAudioShare) { | |
| 294 DesktopMediaID originId(DesktopMediaID::TYPE_WEB_CONTENTS, 222); | |
| 295 DesktopMediaID returnId = originId; | |
| 296 returnId.audio_share = true; | |
| 297 | |
| 298 // This matches the real workflow that when a source is generated in | |
| 299 // media_list, its |audio_share| bit is not set. The bit is set by the picker | |
| 300 // UI if the audio checkbox is checked. | |
| 301 EXPECT_CALL(*this, OnPickerDone(returnId)); | |
| 302 media_lists_[DesktopMediaID::TYPE_WEB_CONTENTS]->AddSourceByFullMediaID( | |
| 303 originId); | |
| 304 | |
| 305 EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_WEB_CONTENTS)); | |
| 306 GetPickerDialogView()->GetCheckboxForTesting()->SetChecked(true); | |
| 307 GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnFocus(); | |
| 308 | |
| 309 GetPickerDialogView()->GetDialogClientView()->AcceptWindow(); | |
| 310 base::RunLoop().RunUntilIdle(); | |
| 311 } | |
| 312 | |
| 313 } // namespace views | |
| OLD | NEW |