File: HTMLMediaElementTest.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (141 lines) | stat: -rw-r--r-- 4,627 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2016 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 "core/html/HTMLMediaElement.h"

#include "core/frame/Settings.h"
#include "core/html/HTMLAudioElement.h"
#include "core/html/HTMLVideoElement.h"
#include "core/page/NetworkStateNotifier.h"
#include "core/testing/DummyPageHolder.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace blink {

enum class TestParam { Audio, Video };

class HTMLMediaElementTest : public ::testing::TestWithParam<TestParam> {
 protected:
  void SetUp() {
    m_dummyPageHolder = DummyPageHolder::create();

    if (GetParam() == TestParam::Audio)
      m_media = HTMLAudioElement::create(m_dummyPageHolder->document());
    else
      m_media = HTMLVideoElement::create(m_dummyPageHolder->document());
  }

  HTMLMediaElement* media() { return m_media.get(); }
  void setCurrentSrc(const String& src) {
    KURL url(ParsedURLString, src);
    media()->m_currentSrc = url;
  }

 private:
  std::unique_ptr<DummyPageHolder> m_dummyPageHolder;
  Persistent<HTMLMediaElement> m_media;
};

INSTANTIATE_TEST_CASE_P(Audio,
                        HTMLMediaElementTest,
                        ::testing::Values(TestParam::Audio));
INSTANTIATE_TEST_CASE_P(Video,
                        HTMLMediaElementTest,
                        ::testing::Values(TestParam::Video));

TEST_P(HTMLMediaElementTest, effectiveMediaVolume) {
  struct TestData {
    double volume;
    bool muted;
    double effectiveVolume;
  } test_data[] = {
      {0.0, false, 0.0}, {0.5, false, 0.5}, {1.0, false, 1.0},
      {0.0, true, 0.0},  {0.5, true, 0.0},  {1.0, true, 0.0},
  };

  for (const auto& data : test_data) {
    media()->setVolume(data.volume);
    media()->setMuted(data.muted);
    EXPECT_EQ(data.effectiveVolume, media()->effectiveMediaVolume());
  }
}

enum class TestURLScheme {
  kHttp,
  kHttps,
  kFtp,
  kFile,
  kData,
  kBlob,
};

String srcSchemeToURL(TestURLScheme scheme) {
  switch (scheme) {
    case TestURLScheme::kHttp:
      return "http://example.com/foo.mp4";
    case TestURLScheme::kHttps:
      return "https://example.com/foo.mp4";
    case TestURLScheme::kFtp:
      return "ftp://example.com/foo.mp4";
    case TestURLScheme::kFile:
      return "file:///foo/bar.mp4";
    case TestURLScheme::kData:
      return "data:video/mp4;base64,XXXXXXX";
    case TestURLScheme::kBlob:
      return "blob:http://example.com/00000000-0000-0000-0000-000000000000";
    default:
      NOTREACHED();
  }
  return emptyString();
}

TEST_P(HTMLMediaElementTest, preloadType) {
  struct TestData {
    bool dataSaverEnabled;
    bool forcePreloadNoneForMediaElements;
    bool isCellular;
    TestURLScheme srcScheme;
    AtomicString preloadToSet;
    AtomicString preloadExpected;
  } testData[] = {
      // Tests for conditions in which preload type should be overriden to
      // "none".
      {false, true, false, TestURLScheme::kHttp, "auto", "none"},
      {true, true, false, TestURLScheme::kHttps, "auto", "none"},
      {true, true, false, TestURLScheme::kFtp, "metadata", "none"},
      {false, false, false, TestURLScheme::kHttps, "auto", "auto"},
      {false, true, false, TestURLScheme::kFile, "auto", "auto"},
      {false, true, false, TestURLScheme::kData, "metadata", "metadata"},
      {false, true, false, TestURLScheme::kBlob, "auto", "auto"},
      {false, true, false, TestURLScheme::kFile, "none", "none"},
      // Tests for conditions in which preload type should be overriden to
      // "metadata".
      {false, false, true, TestURLScheme::kHttp, "auto", "metadata"},
      {false, false, true, TestURLScheme::kHttp, "scheme", "metadata"},
      {false, false, true, TestURLScheme::kHttp, "none", "none"},
      // Tests that the preload is overriden to "auto"
      {false, false, false, TestURLScheme::kHttp, "foo", "auto"},
  };

  int index = 0;
  for (const auto& data : testData) {
    media()->document().settings()->setDataSaverEnabled(data.dataSaverEnabled);
    media()->document().settings()->setForcePreloadNoneForMediaElements(
        data.forcePreloadNoneForMediaElements);
    if (data.isCellular) {
      networkStateNotifier().setOverride(
          true, WebConnectionType::WebConnectionTypeCellular3G, 2.0);
    } else {
      networkStateNotifier().clearOverride();
    }
    setCurrentSrc(srcSchemeToURL(data.srcScheme));
    media()->setPreload(data.preloadToSet);

    EXPECT_EQ(data.preloadExpected, media()->preload())
        << "preload type differs at index" << index;
    ++index;
  }
}

}  // namespace blink