| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 "core/html/AutoplayUmaHelper.h" |
| 6 |
| 7 #include "core/dom/Document.h" |
| 8 #include "core/html/HTMLMediaElement.h" |
| 9 #include "core/html/HTMLVideoElement.h" |
| 10 #include "core/testing/DummyPageHolder.h" |
| 11 |
| 12 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 namespace blink { |
| 16 |
| 17 using ::testing::Invoke; |
| 18 |
| 19 class MockAutoplayUmaHelper : public AutoplayUmaHelper { |
| 20 public: |
| 21 MockAutoplayUmaHelper(HTMLMediaElement* element) |
| 22 : AutoplayUmaHelper(element) { |
| 23 ON_CALL(*this, handleContextDestroyed()) |
| 24 .WillByDefault( |
| 25 Invoke(this, &MockAutoplayUmaHelper::reallyHandleContextDestroyed)); |
| 26 } |
| 27 |
| 28 void handlePlayingEvent() { AutoplayUmaHelper::handlePlayingEvent(); } |
| 29 |
| 30 MOCK_METHOD0(handleContextDestroyed, void()); |
| 31 |
| 32 // Making this a wrapper function to avoid calling the mocked version. |
| 33 void reallyHandleContextDestroyed() { |
| 34 AutoplayUmaHelper::handleContextDestroyed(); |
| 35 } |
| 36 }; |
| 37 |
| 38 class AutoplayUmaHelperTest : public testing::Test { |
| 39 protected: |
| 40 Document& document() { return m_pageHolder->document(); } |
| 41 |
| 42 HTMLMediaElement& mediaElement() { |
| 43 Element* element = document().getElementById("video"); |
| 44 DCHECK(element); |
| 45 return toHTMLVideoElement(*element); |
| 46 } |
| 47 |
| 48 MockAutoplayUmaHelper& umaHelper() { |
| 49 return *(static_cast<MockAutoplayUmaHelper*>( |
| 50 mediaElement().m_autoplayUmaHelper.get())); |
| 51 } |
| 52 |
| 53 std::unique_ptr<DummyPageHolder>& pageHolder() { return m_pageHolder; } |
| 54 |
| 55 MOCK_METHOD0(TestEnded, void()); |
| 56 |
| 57 private: |
| 58 void SetUp() override { |
| 59 m_pageHolder = DummyPageHolder::create(IntSize(800, 600)); |
| 60 document().documentElement()->setInnerHTML("<video id=video></video>", |
| 61 ASSERT_NO_EXCEPTION); |
| 62 HTMLMediaElement& element = mediaElement(); |
| 63 element.m_autoplayUmaHelper = new MockAutoplayUmaHelper(&element); |
| 64 } |
| 65 |
| 66 std::unique_ptr<DummyPageHolder> m_pageHolder; |
| 67 }; |
| 68 |
| 69 TEST_F(AutoplayUmaHelperTest, VisibilityChangeWhenUnload) { |
| 70 // This is to avoid handleContextDestroyed() to be called during TearDown(). |
| 71 EXPECT_CALL(*this, TestEnded()) |
| 72 .After(EXPECT_CALL(umaHelper(), handleContextDestroyed())); |
| 73 |
| 74 mediaElement().setMuted(true); |
| 75 umaHelper().onAutoplayInitiated(AutoplaySource::Attribute); |
| 76 umaHelper().handlePlayingEvent(); |
| 77 pageHolder().reset(); |
| 78 TestEnded(); |
| 79 } |
| 80 |
| 81 } // namespace blink |
| OLD | NEW |