Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: third_party/WebKit/Source/modules/media_controls/MediaControlsImplTest.cpp

Issue 2943983003: chrome/blink: Add functionality for in-product help for media elements. (Closed)
Patch Set: move IPH to MediaDownloadInProductManager Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "modules/media_controls/MediaControlsImpl.h" 5 #include "modules/media_controls/MediaControlsImpl.h"
6 6
7 #include <limits> 7 #include <limits>
8 #include <memory> 8 #include <memory>
9 9
10 #include "build/build_config.h" 10 #include "build/build_config.h"
11 #include "core/HTMLNames.h" 11 #include "core/HTMLNames.h"
12 #include "core/css/StylePropertySet.h" 12 #include "core/css/StylePropertySet.h"
13 #include "core/dom/Document.h" 13 #include "core/dom/Document.h"
14 #include "core/dom/ElementTraversal.h" 14 #include "core/dom/ElementTraversal.h"
15 #include "core/dom/StyleEngine.h" 15 #include "core/dom/StyleEngine.h"
16 #include "core/events/Event.h" 16 #include "core/events/Event.h"
17 #include "core/frame/Settings.h" 17 #include "core/frame/Settings.h"
18 #include "core/geometry/DOMRect.h" 18 #include "core/geometry/DOMRect.h"
19 #include "core/html/HTMLElement.h" 19 #include "core/html/HTMLElement.h"
20 #include "core/html/HTMLVideoElement.h" 20 #include "core/html/HTMLVideoElement.h"
21 #include "core/input/EventHandler.h" 21 #include "core/input/EventHandler.h"
22 #include "core/layout/LayoutObject.h" 22 #include "core/layout/LayoutObject.h"
23 #include "core/loader/EmptyClients.h" 23 #include "core/loader/EmptyClients.h"
24 #include "core/testing/DummyPageHolder.h" 24 #include "core/testing/DummyPageHolder.h"
25 #include "modules/media_controls/MediaDownloadInProductHelpManager.h"
25 #include "modules/media_controls/elements/MediaControlCurrentTimeDisplayElement. h" 26 #include "modules/media_controls/elements/MediaControlCurrentTimeDisplayElement. h"
26 #include "modules/media_controls/elements/MediaControlDownloadButtonElement.h" 27 #include "modules/media_controls/elements/MediaControlDownloadButtonElement.h"
27 #include "modules/media_controls/elements/MediaControlTimelineElement.h" 28 #include "modules/media_controls/elements/MediaControlTimelineElement.h"
28 #include "modules/media_controls/elements/MediaControlVolumeSliderElement.h" 29 #include "modules/media_controls/elements/MediaControlVolumeSliderElement.h"
29 #include "modules/remoteplayback/HTMLMediaElementRemotePlayback.h" 30 #include "modules/remoteplayback/HTMLMediaElementRemotePlayback.h"
30 #include "modules/remoteplayback/RemotePlayback.h" 31 #include "modules/remoteplayback/RemotePlayback.h"
31 #include "platform/RuntimeEnabledFeatures.h" 32 #include "platform/RuntimeEnabledFeatures.h"
32 #include "platform/heap/Handle.h" 33 #include "platform/heap/Handle.h"
33 #include "platform/testing/EmptyWebMediaPlayer.h" 34 #include "platform/testing/EmptyWebMediaPlayer.h"
34 #include "platform/testing/HistogramTester.h" 35 #include "platform/testing/HistogramTester.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 WebTimeRanges Seekable() const override { return seekable_; } 70 WebTimeRanges Seekable() const override { return seekable_; }
70 bool HasVideo() const override { return true; } 71 bool HasVideo() const override { return true; }
71 72
72 WebTimeRanges seekable_; 73 WebTimeRanges seekable_;
73 }; 74 };
74 75
75 class MockLayoutObject : public LayoutObject { 76 class MockLayoutObject : public LayoutObject {
76 public: 77 public:
77 MockLayoutObject(Node* node) : LayoutObject(node) {} 78 MockLayoutObject(Node* node) : LayoutObject(node) {}
78 79
80 void SetVisible(bool visible) { visible_ = visible; }
81
79 const char* GetName() const override { return "MockLayoutObject"; } 82 const char* GetName() const override { return "MockLayoutObject"; }
80 void UpdateLayout() override {} 83 void UpdateLayout() override {}
81 FloatRect LocalBoundingBoxRectForAccessibility() const override { 84 FloatRect LocalBoundingBoxRectForAccessibility() const override {
82 return FloatRect(); 85 return FloatRect();
83 } 86 }
87 void AbsoluteQuads(Vector<FloatQuad>& quads,
88 MapCoordinatesFlags mode) const override {
89 if (!visible_)
90 return;
91 quads.push_back(FloatQuad(FloatRect(0.f, 0.f, 10.f, 10.f)));
92 }
93
94 private:
95 bool visible_ = false;
84 }; 96 };
85 97
86 class StubLocalFrameClientForImpl : public EmptyLocalFrameClient { 98 class StubLocalFrameClientForImpl : public EmptyLocalFrameClient {
87 public: 99 public:
88 static StubLocalFrameClientForImpl* Create() { 100 static StubLocalFrameClientForImpl* Create() {
89 return new StubLocalFrameClientForImpl; 101 return new StubLocalFrameClientForImpl;
90 } 102 }
91 103
92 std::unique_ptr<WebMediaPlayer> CreateWebMediaPlayer( 104 std::unique_ptr<WebMediaPlayer> CreateWebMediaPlayer(
93 HTMLMediaElement&, 105 HTMLMediaElement&,
(...skipping 10 matching lines...) Expand all
104 116
105 Element* GetElementByShadowPseudoId(Node& root_node, 117 Element* GetElementByShadowPseudoId(Node& root_node,
106 const char* shadow_pseudo_id) { 118 const char* shadow_pseudo_id) {
107 for (Element& element : ElementTraversal::DescendantsOf(root_node)) { 119 for (Element& element : ElementTraversal::DescendantsOf(root_node)) {
108 if (element.ShadowPseudoId() == shadow_pseudo_id) 120 if (element.ShadowPseudoId() == shadow_pseudo_id)
109 return &element; 121 return &element;
110 } 122 }
111 return nullptr; 123 return nullptr;
112 } 124 }
113 125
126 MediaControlDownloadButtonElement& GetDownloadButton(
127 MediaControlsImpl& controls) {
128 Element* element = GetElementByShadowPseudoId(
129 controls, "-internal-media-controls-download-button");
130 return static_cast<MediaControlDownloadButtonElement&>(*element);
131 }
132
114 bool IsElementVisible(Element& element) { 133 bool IsElementVisible(Element& element) {
115 const StylePropertySet* inline_style = element.InlineStyle(); 134 const StylePropertySet* inline_style = element.InlineStyle();
116 135
117 if (!inline_style) 136 if (!inline_style)
118 return true; 137 return true;
119 138
120 if (inline_style->GetPropertyValue(CSSPropertyDisplay) == "none") 139 if (inline_style->GetPropertyValue(CSSPropertyDisplay) == "none")
121 return false; 140 return false;
122 141
123 if (inline_style->HasProperty(CSSPropertyOpacity) && 142 if (inline_style->HasProperty(CSSPropertyOpacity) &&
(...skipping 27 matching lines...) Expand all
151 170
152 InitializePage(); 171 InitializePage();
153 } 172 }
154 173
155 void InitializePage() { 174 void InitializePage() {
156 Page::PageClients clients; 175 Page::PageClients clients;
157 FillWithEmptyClients(clients); 176 FillWithEmptyClients(clients);
158 clients.chrome_client = new MockChromeClientForImpl(); 177 clients.chrome_client = new MockChromeClientForImpl();
159 page_holder_ = DummyPageHolder::Create( 178 page_holder_ = DummyPageHolder::Create(
160 IntSize(800, 600), &clients, StubLocalFrameClientForImpl::Create()); 179 IntSize(800, 600), &clients, StubLocalFrameClientForImpl::Create());
180 GetDocument().GetSettings()->SetMediaDownloadInProductHelpEnabled(
181 EnableDownloadInProductHelp());
161 182
162 GetDocument().write("<video>"); 183 GetDocument().write("<video>");
163 HTMLVideoElement& video = 184 HTMLVideoElement& video =
164 toHTMLVideoElement(*GetDocument().QuerySelector("video")); 185 toHTMLVideoElement(*GetDocument().QuerySelector("video"));
165 media_controls_ = static_cast<MediaControlsImpl*>(video.GetMediaControls()); 186 media_controls_ = static_cast<MediaControlsImpl*>(video.GetMediaControls());
166 187
167 // If scripts are not enabled, controls will always be shown. 188 // If scripts are not enabled, controls will always be shown.
168 page_holder_->GetFrame().GetSettings()->SetScriptEnabled(true); 189 page_holder_->GetFrame().GetSettings()->SetScriptEnabled(true);
169 } 190 }
170 191
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 } 241 }
221 242
222 void MouseDownAt(WebFloatPoint pos); 243 void MouseDownAt(WebFloatPoint pos);
223 void MouseMoveTo(WebFloatPoint pos); 244 void MouseMoveTo(WebFloatPoint pos);
224 void MouseUpAt(WebFloatPoint pos); 245 void MouseUpAt(WebFloatPoint pos);
225 246
226 bool HasAvailabilityCallbacks(RemotePlayback* remote_playback) { 247 bool HasAvailabilityCallbacks(RemotePlayback* remote_playback) {
227 return !remote_playback->availability_callbacks_.IsEmpty(); 248 return !remote_playback->availability_callbacks_.IsEmpty();
228 } 249 }
229 250
251 virtual bool EnableDownloadInProductHelp() { return false; }
252
230 private: 253 private:
231 std::unique_ptr<DummyPageHolder> page_holder_; 254 std::unique_ptr<DummyPageHolder> page_holder_;
232 Persistent<MediaControlsImpl> media_controls_; 255 Persistent<MediaControlsImpl> media_controls_;
233 HistogramTester histogram_tester_; 256 HistogramTester histogram_tester_;
234 }; 257 };
235 258
236 void MediaControlsImplTest::MouseDownAt(WebFloatPoint pos) { 259 void MediaControlsImplTest::MouseDownAt(WebFloatPoint pos) {
237 WebMouseEvent mouse_down_event(WebInputEvent::kMouseDown, 260 WebMouseEvent mouse_down_event(WebInputEvent::kMouseDown,
238 pos /* client pos */, pos /* screen pos */, 261 pos /* client pos */, pos /* screen pos */,
239 WebPointerProperties::Button::kLeft, 1, 262 WebPointerProperties::Button::kLeft, 1,
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after
506 MediaControls(), "-internal-media-controls-download-button"); 529 MediaControls(), "-internal-media-controls-download-button");
507 ASSERT_NE(nullptr, download_button); 530 ASSERT_NE(nullptr, download_button);
508 531
509 // Download button should not be displayed for HLS streams. 532 // Download button should not be displayed for HLS streams.
510 MediaControls().MediaElement().SetSrc("https://example.com/foo.m3u8"); 533 MediaControls().MediaElement().SetSrc("https://example.com/foo.m3u8");
511 testing::RunPendingTasks(); 534 testing::RunPendingTasks();
512 SimulateLoadedMetadata(); 535 SimulateLoadedMetadata();
513 EXPECT_FALSE(IsElementVisible(*download_button)); 536 EXPECT_FALSE(IsElementVisible(*download_button));
514 } 537 }
515 538
539 TEST_F(MediaControlsImplTest, DownloadButtonInProductHelpDisabled) {
540 EXPECT_FALSE(MediaControls().DownloadInProductHelp());
541 }
542
543 class MediaControlsImplInProductHelpTest : public MediaControlsImplTest {
mlamouri (slow - plz ping) 2017/08/16 13:09:30 Would it make sense to create a different unit tes
Khushal 2017/08/16 19:01:22 There is a lot test setup here that I wanted to re
544 public:
545 void SetUp() override {
546 MediaControlsImplTest::SetUp();
547 ASSERT_TRUE(MediaControls().DownloadInProductHelp());
548 }
549
550 MediaDownloadInProductHelpManager& Manager() {
551 return *MediaControls().DownloadInProductHelp();
552 }
553
554 void Play() { MediaControls().OnPlay(); }
555
556 bool EnableDownloadInProductHelp() override { return true; }
557 };
558
559 TEST_F(MediaControlsImplInProductHelpTest, DownloadButtonInProductHelp_Button) {
560 EnsureSizing();
561
562 // Inject the LayoutObject for the button to override the rect returned in
563 // visual viewport.
564 MediaControlDownloadButtonElement& button =
565 GetDownloadButton(MediaControls());
566 MockLayoutObject layout_object(&button);
567 layout_object.SetVisible(true);
568 button.SetLayoutObject(&layout_object);
569
570 MediaControls().MediaElement().SetSrc("https://example.com/foo.mp4");
571 testing::RunPendingTasks();
572 SimulateLoadedMetadata();
573 Play();
574
575 // Load above should have made the button wanted, which should trigger showing
576 // in-product help.
577 ASSERT_TRUE(Manager().CanShowInProductHelp());
578 EXPECT_TRUE(Manager().IsShowingInProductHelp());
579
580 // Disable the download button, which dismisses the in-product-help.
581 button.SetIsWanted(false);
582 EXPECT_FALSE(Manager().IsShowingInProductHelp());
583
584 // Toggle again. In-product help is shown only once.
585 button.SetIsWanted(true);
586 EXPECT_FALSE(Manager().IsShowingInProductHelp());
587
588 button.SetLayoutObject(nullptr);
589 }
590
591 TEST_F(MediaControlsImplInProductHelpTest,
592 DownloadButtonInProductHelp_ControlsVisibility) {
593 EnsureSizing();
594
595 // Inject the LayoutObject for the button to override the rect returned in
596 // visual viewport.
597 MediaControlDownloadButtonElement& button =
598 GetDownloadButton(MediaControls());
599 MockLayoutObject layout_object(&button);
600 layout_object.SetVisible(true);
601 button.SetLayoutObject(&layout_object);
602
603 // The in-product-help should not be shown while the controls are hidden.
604 MediaControls().Hide();
605 MediaControls().MediaElement().SetSrc("https://example.com/foo.mp4");
606 testing::RunPendingTasks();
607 SimulateLoadedMetadata();
608 Play();
609
610 ASSERT_TRUE(button.IsWanted());
611 EXPECT_FALSE(Manager().IsShowingInProductHelp());
612
613 // Showing the controls initiates showing in-product-help.
614 MediaControls().MaybeShow();
615 EXPECT_TRUE(Manager().IsShowingInProductHelp());
616
617 // Hiding the controls dismissed in-product-help.
618 MediaControls().Hide();
619 EXPECT_FALSE(Manager().IsShowingInProductHelp());
620
621 button.SetLayoutObject(nullptr);
622 }
623
624 TEST_F(MediaControlsImplInProductHelpTest,
625 DownloadButtonInProductHelp_ButtonVisibility) {
626 EnsureSizing();
627
628 // Inject the LayoutObject for the button to override the rect returned in
629 // visual viewport.
630 MediaControlDownloadButtonElement& button =
631 GetDownloadButton(MediaControls());
632 MockLayoutObject layout_object(&button);
633 button.SetLayoutObject(&layout_object);
634
635 // The in-product-help should not be shown while the button is hidden.
636 layout_object.SetVisible(false);
637 MediaControls().MediaElement().SetSrc("https://example.com/foo.mp4");
638 testing::RunPendingTasks();
639 SimulateLoadedMetadata();
640 Play();
641
642 ASSERT_TRUE(button.IsWanted());
643 EXPECT_FALSE(Manager().IsShowingInProductHelp());
644
645 // Make the button visible to show in-product-help.
646 layout_object.SetVisible(true);
647 button.SetIsWanted(false);
648 button.SetIsWanted(true);
649 EXPECT_TRUE(Manager().IsShowingInProductHelp());
650
651 button.SetLayoutObject(nullptr);
652 }
653
516 TEST_F(MediaControlsImplTest, TimelineSeekToRoundedEnd) { 654 TEST_F(MediaControlsImplTest, TimelineSeekToRoundedEnd) {
517 EnsureSizing(); 655 EnsureSizing();
518 656
519 // Tests the case where the real length of the video, |exact_duration|, gets 657 // Tests the case where the real length of the video, |exact_duration|, gets
520 // rounded up slightly to |rounded_up_duration| when setting the timeline's 658 // rounded up slightly to |rounded_up_duration| when setting the timeline's
521 // |max| attribute (crbug.com/695065). 659 // |max| attribute (crbug.com/695065).
522 double exact_duration = 596.586667; 660 double exact_duration = 596.586667;
523 double rounded_up_duration = 596.587; 661 double rounded_up_duration = 596.587;
524 LoadMediaWithDuration(exact_duration); 662 LoadMediaWithDuration(exact_duration);
525 663
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after
852 { 990 {
853 Persistent<HTMLMediaElement> video_holder = element; 991 Persistent<HTMLMediaElement> video_holder = element;
854 page_holder->GetDocument().body()->RemoveChild(element); 992 page_holder->GetDocument().body()->RemoveChild(element);
855 page_holder->GetDocument().body()->AppendChild(video_holder.Get()); 993 page_holder->GetDocument().body()->AppendChild(video_holder.Get());
856 EXPECT_TRUE(remote_playback->HasEventListeners()); 994 EXPECT_TRUE(remote_playback->HasEventListeners());
857 EXPECT_TRUE(HasAvailabilityCallbacks(remote_playback)); 995 EXPECT_TRUE(HasAvailabilityCallbacks(remote_playback));
858 } 996 }
859 } 997 }
860 998
861 } // namespace blink 999 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698