Chromium Code Reviews| Index: chrome/browser/ui/views/desktop_capture/desktop_media_picker_views_unittest.cc |
| diff --git a/chrome/browser/ui/views/desktop_capture/desktop_media_picker_views_unittest.cc b/chrome/browser/ui/views/desktop_capture/desktop_media_picker_views_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..995d0af86c2d187ae0f0ea0bb720fabd00f7031a |
| --- /dev/null |
| +++ b/chrome/browser/ui/views/desktop_capture/desktop_media_picker_views_unittest.cc |
| @@ -0,0 +1,331 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/views/desktop_capture/desktop_media_picker_views.h" |
| + |
| +#include <map> |
| +#include <utility> |
| + |
| +#include "base/bind.h" |
| +#include "base/run_loop.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/media/fake_desktop_media_list.h" |
| +#include "components/web_modal/test_web_contents_modal_dialog_host.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "ui/aura/window.h" |
| +#include "ui/events/event_utils.h" |
| +#include "ui/views/controls/button/checkbox.h" |
| +#include "ui/views/controls/button/label_button.h" |
|
msw
2016/05/02 23:49:58
Remove this.
qiangchen
2016/05/05 22:17:54
Done.
|
| +#include "ui/views/controls/tabbed_pane/tabbed_pane.h" |
| +#include "ui/views/test/scoped_views_test_helper.h" |
| +#include "ui/views/widget/widget.h" |
| +#include "ui/views/window/dialog_client_view.h" |
| +#include "ui/views/window/dialog_delegate.h" |
| + |
| +using content::DesktopMediaID; |
| + |
| +namespace views { |
|
msw
2016/05/02 23:49:59
nit: add a blank line below.
qiangchen
2016/05/05 22:17:54
Done.
|
| +const std::vector<DesktopMediaID::Type> kSourceTypes = { |
| + DesktopMediaID::TYPE_SCREEN, DesktopMediaID::TYPE_WINDOW, |
| + DesktopMediaID::TYPE_WEB_CONTENTS}; |
| + |
| +class DesktopMediaPickerViewsTest : public testing::Test { |
| + public: |
| + DesktopMediaPickerViewsTest() {} |
| + ~DesktopMediaPickerViewsTest() override {} |
| + |
| + void SetUp() override { |
| + media_lists_[DesktopMediaID::TYPE_SCREEN] = new FakeDesktopMediaList(); |
| + media_lists_[DesktopMediaID::TYPE_WINDOW] = new FakeDesktopMediaList(); |
| + media_lists_[DesktopMediaID::TYPE_WEB_CONTENTS] = |
| + new FakeDesktopMediaList(); |
| + std::unique_ptr<FakeDesktopMediaList> screen_list( |
| + media_lists_[DesktopMediaID::TYPE_SCREEN]); |
| + std::unique_ptr<FakeDesktopMediaList> window_list( |
| + media_lists_[DesktopMediaID::TYPE_WINDOW]); |
| + std::unique_ptr<FakeDesktopMediaList> tab_list( |
| + media_lists_[DesktopMediaID::TYPE_WEB_CONTENTS]); |
| + |
| + base::string16 app_name = base::ASCIIToUTF16("foo"); |
| + |
| + picker_views_.reset(new DesktopMediaPickerViews()); |
| + picker_views_->Show(NULL, test_helper_.GetContext(), NULL, app_name, |
|
msw
2016/05/02 23:49:58
nit: nullptr here and elsewhere
qiangchen
2016/05/05 22:17:54
Done.
|
| + app_name, std::move(screen_list), |
| + std::move(window_list), std::move(tab_list), true, |
| + base::Bind(&DesktopMediaPickerViewsTest::OnPickerDone, |
| + base::Unretained(this))); |
| + } |
| + |
| + void TearDown() override { |
| + if (GetPickerDialogView()) { |
| + EXPECT_CALL(*this, OnPickerDone(content::DesktopMediaID())); |
| + GetPickerDialogView()->GetWidget()->CloseNow(); |
| + } |
| + } |
| + |
| + DesktopMediaPickerDialogView* GetPickerDialogView() const { |
| + return picker_views_->GetDialogViewForTesting(); |
| + } |
| + |
| + bool ClickSourceTypeButton(DesktopMediaID::Type source_type) { |
| + int index = |
| + GetPickerDialogView()->GetIndexOfSourceTypeForTesting(source_type); |
| + |
| + if (index == -1) |
| + return false; |
| + |
| + GetPickerDialogView()->GetPaneForTesting()->SelectTabAt(index); |
| + return true; |
| + } |
| + |
| + MOCK_METHOD1(OnPickerDone, void(content::DesktopMediaID)); |
| + |
| + protected: |
| + content::TestBrowserThreadBundle thread_bundle_; |
| + views::ScopedViewsTestHelper test_helper_; |
| + std::map<DesktopMediaID::Type, FakeDesktopMediaList*> media_lists_; |
| + std::unique_ptr<DesktopMediaPickerViews> picker_views_; |
| +}; |
| + |
| +TEST_F(DesktopMediaPickerViewsTest, DoneCallbackCalledWhenWindowClosed) { |
| + EXPECT_CALL(*this, OnPickerDone(content::DesktopMediaID())); |
| + |
| + GetPickerDialogView()->GetWidget()->Close(); |
| + base::RunLoop().RunUntilIdle(); |
| +} |
| + |
| +TEST_F(DesktopMediaPickerViewsTest, DoneCallbackCalledOnOkButtonPressed) { |
| + const DesktopMediaID kFakeId(DesktopMediaID::TYPE_WINDOW, 222); |
| + EXPECT_CALL(*this, OnPickerDone(kFakeId)); |
| + |
| + media_lists_[DesktopMediaID::TYPE_WINDOW]->AddSourceByFullMediaID(kFakeId); |
| + GetPickerDialogView()->GetCheckboxForTesting()->SetChecked(true); |
| + |
| + EXPECT_FALSE( |
| + GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); |
| + |
| + EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_WINDOW)); |
| + |
| + GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnFocus(); |
| + |
| + EXPECT_TRUE( |
| + GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); |
| + |
| + GetPickerDialogView()->GetDialogClientView()->AcceptWindow(); |
| + base::RunLoop().RunUntilIdle(); |
| +} |
| + |
| +// Verifies that a MediaSourceView is selected with mouse left click and |
| +// original selected MediaSourceView gets unselected. |
| +TEST_F(DesktopMediaPickerViewsTest, SelectMediaSourceViewOnSingleClick) { |
| + for (auto source_type : kSourceTypes) { |
| + EXPECT_TRUE(ClickSourceTypeButton(source_type)); |
| + media_lists_[source_type]->AddSourceByFullMediaID( |
| + DesktopMediaID(source_type, 0)); |
| + media_lists_[source_type]->AddSourceByFullMediaID( |
| + DesktopMediaID(source_type, 1)); |
| + |
| + DesktopMediaSourceView* source_view_0 = |
| + GetPickerDialogView()->GetMediaSourceViewForTesting(0); |
| + |
| + DesktopMediaSourceView* source_view_1 = |
| + GetPickerDialogView()->GetMediaSourceViewForTesting(1); |
| + |
| + // Default choose the first screen for screen share. |
|
msw
2016/05/02 23:49:59
nit: "By default, the first screen should be selec
qiangchen
2016/05/05 22:17:54
Done.
|
| + if (source_type == DesktopMediaID::TYPE_SCREEN) { |
|
msw
2016/05/02 23:49:58
replace this block with EXPECT_EQ(source_type == D
qiangchen
2016/05/05 22:17:54
Done.
Good idea.
|
| + EXPECT_TRUE(source_view_0->is_selected()); |
| + } else { |
| + EXPECT_FALSE(source_view_0->is_selected()); |
| + } |
| + EXPECT_FALSE(source_view_1->is_selected()); |
| + |
| + // Source view 0 is selected with mouse click. |
| + ui::MouseEvent press(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), |
| + ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0); |
| + |
| + GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnMousePressed( |
| + press); |
| + |
| + EXPECT_TRUE(source_view_0->is_selected()); |
| + EXPECT_FALSE(source_view_1->is_selected()); |
| + |
| + // Source view 1 is selected and source view 0 is unselected with mouse |
| + // click. |
| + GetPickerDialogView()->GetMediaSourceViewForTesting(1)->OnMousePressed( |
| + press); |
| + |
| + EXPECT_FALSE(source_view_0->is_selected()); |
| + EXPECT_TRUE(source_view_1->is_selected()); |
| + } |
| +} |
| + |
| +TEST_F(DesktopMediaPickerViewsTest, DoneCallbackCalledOnDoubleClick) { |
| + const DesktopMediaID kFakeId(DesktopMediaID::TYPE_WEB_CONTENTS, 222); |
| + EXPECT_CALL(*this, OnPickerDone(kFakeId)); |
| + |
| + media_lists_[DesktopMediaID::TYPE_WEB_CONTENTS]->AddSourceByFullMediaID( |
| + kFakeId); |
| + EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_WEB_CONTENTS)); |
| + GetPickerDialogView()->GetCheckboxForTesting()->SetChecked(false); |
| + |
| + ui::MouseEvent double_click(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), |
| + ui::EventTimeForNow(), |
| + ui::EF_LEFT_MOUSE_BUTTON | ui::EF_IS_DOUBLE_CLICK, |
| + ui::EF_LEFT_MOUSE_BUTTON); |
| + |
| + GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnMousePressed( |
| + double_click); |
| + base::RunLoop().RunUntilIdle(); |
| +} |
| + |
| +TEST_F(DesktopMediaPickerViewsTest, DoneCallbackCalledOnDoubleTap) { |
| + const DesktopMediaID kFakeId(DesktopMediaID::TYPE_SCREEN, 222); |
| + EXPECT_CALL(*this, OnPickerDone(kFakeId)); |
| + |
| + EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_SCREEN)); |
| + GetPickerDialogView()->GetCheckboxForTesting()->SetChecked(false); |
| + |
| + media_lists_[DesktopMediaID::TYPE_SCREEN]->AddSourceByFullMediaID(kFakeId); |
| + ui::GestureEventDetails details(ui::ET_GESTURE_TAP); |
| + details.set_tap_count(2); |
| + ui::GestureEvent double_tap(10, 10, 0, base::TimeDelta(), details); |
| + |
| + GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnGestureEvent( |
| + &double_tap); |
| + base::RunLoop().RunUntilIdle(); |
| +} |
| + |
| +TEST_F(DesktopMediaPickerViewsTest, CancelButtonAlwaysEnabled) { |
| + EXPECT_TRUE( |
| + GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL)); |
| +} |
| + |
| +// Verifies that the MediaSourceView is added or removed when |media_list_| is |
| +// updated. |
| +TEST_F(DesktopMediaPickerViewsTest, AddAndRemoveMediaSource) { |
| + for (auto source_type : kSourceTypes) { |
| + EXPECT_TRUE(ClickSourceTypeButton(source_type)); |
| + // No media source at first. |
| + EXPECT_EQ(NULL, GetPickerDialogView()->GetMediaSourceViewForTesting(0)); |
|
msw
2016/05/02 23:49:59
nit: EXPECT_FALSE here and line 222?
qiangchen
2016/05/05 22:17:54
Done.
|
| + |
| + for (int i = 0; i < 3; ++i) { |
| + media_lists_[source_type]->AddSourceByFullMediaID( |
| + DesktopMediaID(source_type, i)); |
| + EXPECT_TRUE(GetPickerDialogView()->GetMediaSourceViewForTesting(i)); |
| + } |
| + |
| + for (int i = 2; i >= 0; --i) { |
| + media_lists_[source_type]->RemoveSource(i); |
| + EXPECT_EQ(NULL, GetPickerDialogView()->GetMediaSourceViewForTesting(i)); |
| + } |
| + } |
| +} |
| + |
| +// Verifies that focusing the MediaSourceView marks it selected and the |
| +// original selected MediaSourceView gets unselected. |
| +TEST_F(DesktopMediaPickerViewsTest, FocusMediaSourceViewToSelect) { |
| + for (auto source_type : kSourceTypes) { |
| + ClickSourceTypeButton(source_type); |
| + media_lists_[source_type]->AddSourceByFullMediaID( |
| + DesktopMediaID(source_type, 0)); |
| + media_lists_[source_type]->AddSourceByFullMediaID( |
| + DesktopMediaID(source_type, 1)); |
| + |
| + DesktopMediaSourceView* source_view_0 = |
| + GetPickerDialogView()->GetMediaSourceViewForTesting(0); |
| + |
| + DesktopMediaSourceView* source_view_1 = |
| + GetPickerDialogView()->GetMediaSourceViewForTesting(1); |
| + |
| + // Default choose the first screen for screen share. |
|
msw
2016/05/02 23:49:58
Lines 243-249 are redundant with line 137-143, rem
qiangchen
2016/05/05 22:17:54
Done.
|
| + if (source_type == DesktopMediaID::TYPE_SCREEN) { |
| + EXPECT_TRUE(source_view_0->is_selected()); |
| + } else { |
| + EXPECT_FALSE(source_view_0->is_selected()); |
| + } |
| + EXPECT_FALSE(source_view_1->is_selected()); |
| + |
| + source_view_0->OnFocus(); |
| + EXPECT_TRUE(source_view_0->is_selected()); |
| + |
| + // Removing the focus does not undo the selection. |
| + source_view_0->OnBlur(); |
| + EXPECT_TRUE(source_view_0->is_selected()); |
| + |
| + source_view_1->OnFocus(); |
| + EXPECT_FALSE(source_view_0->is_selected()); |
| + EXPECT_TRUE(source_view_1->is_selected()); |
| + } |
| +} |
| + |
| +TEST_F(DesktopMediaPickerViewsTest, OkButtonDisabledWhenNoSelection) { |
| + for (auto source_type : kSourceTypes) { |
| + EXPECT_TRUE(ClickSourceTypeButton(source_type)); |
| + media_lists_[source_type]->AddSourceByFullMediaID( |
| + DesktopMediaID(source_type, 111)); |
| + |
| + GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnFocus(); |
| + EXPECT_TRUE( |
| + GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); |
| + |
| + media_lists_[source_type]->RemoveSource(0); |
| + EXPECT_FALSE( |
| + GetPickerDialogView()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK)); |
| + } |
| +} |
| + |
| +// Verifies that the MediaListView get the initial focus. |
|
msw
2016/05/02 23:49:58
nit: gets
qiangchen
2016/05/05 22:17:54
Done.
|
| +TEST_F(DesktopMediaPickerViewsTest, ListViewHasInitialFocus) { |
| + EXPECT_TRUE(GetPickerDialogView()->GetMediaListViewForTesting()->HasFocus()); |
| +} |
| + |
| +// Verifies the visible status of audio checkbox. |
| +TEST_F(DesktopMediaPickerViewsTest, AudioCheckboxState) { |
| + bool expect_value; |
|
msw
2016/05/02 23:49:58
nit: init this to false, remove the #else case bel
qiangchen
2016/05/05 22:17:54
Done.
|
| + EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_SCREEN)); |
| +#if defined(OS_WIN) || defined(USE_CRAS) |
| + expect_value = true; |
| +#else |
| + expect_value = false; |
| +#endif |
| + EXPECT_EQ(expect_value, |
| + GetPickerDialogView()->GetCheckboxForTesting()->visible()); |
| + |
| + EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_WINDOW)); |
| + EXPECT_EQ(false, GetPickerDialogView()->GetCheckboxForTesting()->visible()); |
| + |
| + EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_WEB_CONTENTS)); |
| + EXPECT_EQ(true, GetPickerDialogView()->GetCheckboxForTesting()->visible()); |
| +} |
| + |
| +// Verifies that audio share information is recorded in the ID if the checkbox |
| +// is checked. |
| +TEST_F(DesktopMediaPickerViewsTest, DoneWithAudioShare) { |
| + DesktopMediaID originId(DesktopMediaID::TYPE_WEB_CONTENTS, 222); |
| + DesktopMediaID returnId = originId; |
| + returnId.audio_share = true; |
| + |
| + // It is also the real workflow when a source is generated in media_list, |
|
msw
2016/05/02 23:49:59
nit: s/It is also/This matches/
qiangchen
2016/05/05 22:17:54
Done.
|
| + // its |audio_share| bit is not set. The bit is set by the picker UI if the |
| + // audio checkbox is checked. |
| + EXPECT_CALL(*this, OnPickerDone(returnId)); |
| + media_lists_[DesktopMediaID::TYPE_WEB_CONTENTS]->AddSourceByFullMediaID( |
| + originId); |
| + |
| + EXPECT_TRUE(ClickSourceTypeButton(DesktopMediaID::TYPE_WEB_CONTENTS)); |
| + GetPickerDialogView()->GetCheckboxForTesting()->SetChecked(true); |
| + |
| + ui::MouseEvent double_click(ui::ET_MOUSE_PRESSED, gfx::Point(), gfx::Point(), |
|
msw
2016/05/02 23:49:58
Instead of double clicking here, just do GetPicker
qiangchen
2016/05/05 22:17:54
Done.
|
| + ui::EventTimeForNow(), |
| + ui::EF_LEFT_MOUSE_BUTTON | ui::EF_IS_DOUBLE_CLICK, |
| + ui::EF_LEFT_MOUSE_BUTTON); |
| + |
| + GetPickerDialogView()->GetMediaSourceViewForTesting(0)->OnMousePressed( |
| + double_click); |
| + base::RunLoop().RunUntilIdle(); |
| +} |
| + |
| +} // namespace views |