summaryrefslogtreecommitdiff
path: root/chromium/content/browser/media/key_system_support_impl_unittest.cc
blob: 719dcf29eecb9fb7096d9adbd9a2afad04fb3c80 (plain)
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
// Copyright 2017 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 "content/browser/media/key_system_support_impl.h"

#include <string>
#include <vector>

#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/browser/cdm_registry.h"
#include "content/public/browser/plugin_service.h"
#include "content/public/common/cdm_info.h"
#include "content/public/common/webplugininfo.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "media/base/video_codecs.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {

namespace {

const char kTestCdmGuid[] = "62FE9C4B-384E-48FD-B28A-9F6F248BC8CC";
const char kVersion[] = "1.1.1.1";
const char kTestPath[] = "/aa/bb";
const char kTestFileSystemId[] = "file_system_id";

}  // namespace

class KeySystemSupportTest : public testing::Test {
 protected:
  void SetUp() final {
    DVLOG(1) << __func__;
    KeySystemSupportImpl::Create(mojo::MakeRequest(&key_system_support_));
  }

  // Registers |key_system| with |supported_video_codecs| and
  // |supports_persistent_license|. All other values for CdmInfo have some
  // default value as they're not returned by IsKeySystemSupported().
  void Register(const std::string& key_system,
                const std::vector<media::VideoCodec> supported_video_codecs,
                bool supports_persistent_license) {
    DVLOG(1) << __func__;

    CdmRegistry::GetInstance()->RegisterCdm(
        CdmInfo(key_system, kTestCdmGuid, base::Version(kVersion),
                base::FilePath::FromUTF8Unsafe(kTestPath), kTestFileSystemId,
                supported_video_codecs, supports_persistent_license, key_system,
                false));

    // As IsKeySystemSupported() checks for the matching pepper plugin,
    // register a dummy one.
    // TODO(crbug.com/772160) Remove this when pepper CDM support removed.
    PluginService::GetInstance()->RegisterInternalPlugin(
        WebPluginInfo(base::ASCIIToUTF16(key_system),
                      base::FilePath::FromUTF8Unsafe(kTestPath),
                      base::ASCIIToUTF16(kVersion), base::string16()),
        true);
  }

  // Determines if |key_system| is registered. If it is, updates |codecs_|
  // and |persistent_|.
  bool IsSupported(const std::string& key_system) {
    DVLOG(1) << __func__;
    bool is_available = false;
    key_system_support_->IsKeySystemSupported(key_system, &is_available,
                                              &codecs_, &persistent_);
    return is_available;
  }

  media::mojom::KeySystemSupportPtr key_system_support_;
  TestBrowserThreadBundle test_browser_thread_bundle_;

  // Updated by IsSupported().
  std::vector<media::VideoCodec> codecs_;
  bool persistent_;
};

// Note that as CdmRegistry::GetInstance() is a static, it is shared between
// tests. So use unique key system names in each test below to avoid
// interactions between the tests.

TEST_F(KeySystemSupportTest, NoKeySystems) {
  EXPECT_FALSE(IsSupported("KeySystem1"));
}

TEST_F(KeySystemSupportTest, OneKeySystem) {
  Register("KeySystem2", {media::VideoCodec::kCodecVP8}, true);
  EXPECT_TRUE(IsSupported("KeySystem2"));
  EXPECT_EQ(1u, codecs_.size());
  EXPECT_EQ(media::VideoCodec::kCodecVP8, codecs_[0]);
  EXPECT_TRUE(persistent_);
}

TEST_F(KeySystemSupportTest, MultipleKeySystems) {
  Register("KeySystem3",
           {media::VideoCodec::kCodecVP8, media::VideoCodec::kCodecVP9}, true);
  Register("KeySystem4", {media::VideoCodec::kCodecVP9}, false);
  EXPECT_TRUE(IsSupported("KeySystem3"));
  EXPECT_EQ(2u, codecs_.size());
  EXPECT_EQ(media::VideoCodec::kCodecVP8, codecs_[0]);
  EXPECT_EQ(media::VideoCodec::kCodecVP9, codecs_[1]);
  EXPECT_TRUE(persistent_);
  EXPECT_TRUE(IsSupported("KeySystem4"));
  EXPECT_EQ(1u, codecs_.size());
  EXPECT_EQ(media::VideoCodec::kCodecVP9, codecs_[0]);
  EXPECT_FALSE(persistent_);
}

TEST_F(KeySystemSupportTest, MissingKeySystem) {
  Register("KeySystem5", {media::VideoCodec::kCodecVP8}, true);
  EXPECT_FALSE(IsSupported("KeySystem6"));
}

}  // namespace content