| 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 "content/browser/media/cdm_service_impl.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/base_paths.h" | |
| 12 #include "base/files/file_path.h" | |
| 13 #include "base/strings/string_split.h" | |
| 14 #include "base/strings/string_util.h" | |
| 15 #include "base/strings/utf_string_conversions.h" | |
| 16 #include "base/version.h" | |
| 17 #include "content/public/common/cdm_info.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 namespace content { | |
| 21 | |
| 22 const char kTestKeySystemType[] = "test"; | |
| 23 const char kTestPath[] = "/aa/bb"; | |
| 24 const char kVersion1[] = "1.1.1.1"; | |
| 25 const char kVersion2[] = "1.1.1.2"; | |
| 26 const char kTestCodecs[] = "vp9,mp4"; | |
| 27 const char kCodecDelimiter[] = ","; | |
| 28 | |
| 29 // For simplicity and to make failures easier to diagnose, this test uses | |
| 30 // std::string instead of base::FilePath and std::vector<std::string>. | |
| 31 class CdmServiceImplTest : public testing::Test { | |
| 32 public: | |
| 33 CdmServiceImplTest() {} | |
| 34 ~CdmServiceImplTest() override {} | |
| 35 | |
| 36 protected: | |
| 37 void Register(const std::string& type, | |
| 38 const std::string& version, | |
| 39 const std::string& path, | |
| 40 const std::string& supported_codecs) { | |
| 41 const std::vector<std::string> codecs = | |
| 42 base::SplitString(supported_codecs, kCodecDelimiter, | |
| 43 base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); | |
| 44 cdm_service_.RegisterCdm(CdmInfo(type, base::Version(version), | |
| 45 base::FilePath::FromUTF8Unsafe(path), | |
| 46 codecs)); | |
| 47 } | |
| 48 | |
| 49 bool IsRegistered(const std::string& type, const std::string& version) { | |
| 50 for (const auto& cdm : cdm_service_.GetAllRegisteredCdms()) { | |
| 51 if (cdm.type == type && cdm.version.GetString() == version) | |
| 52 return true; | |
| 53 } | |
| 54 return false; | |
| 55 } | |
| 56 | |
| 57 std::vector<std::string> GetVersions(const std::string& type) { | |
| 58 std::vector<std::string> versions; | |
| 59 for (const auto& cdm : cdm_service_.GetAllRegisteredCdms()) { | |
| 60 if (cdm.type == type) | |
| 61 versions.push_back(cdm.version.GetString()); | |
| 62 } | |
| 63 return versions; | |
| 64 } | |
| 65 | |
| 66 private: | |
| 67 CdmServiceImpl cdm_service_; | |
| 68 }; | |
| 69 | |
| 70 // Note that KeySystemService is a singleton, and thus the actions of | |
| 71 // one test impact other tests. So each test defines a different key system | |
| 72 // name to avoid conflicts. | |
| 73 | |
| 74 TEST_F(CdmServiceImplTest, Register) { | |
| 75 Register(kTestKeySystemType, kVersion1, kTestPath, kTestCodecs); | |
| 76 EXPECT_TRUE(IsRegistered(kTestKeySystemType, kVersion1)); | |
| 77 } | |
| 78 | |
| 79 TEST_F(CdmServiceImplTest, ReRegister) { | |
| 80 Register(kTestKeySystemType, kVersion1, "/bb/cc", "unknown"); | |
| 81 EXPECT_TRUE(IsRegistered(kTestKeySystemType, kVersion1)); | |
| 82 | |
| 83 // Now register same key system with different values. | |
| 84 Register(kTestKeySystemType, kVersion1, kTestPath, kTestCodecs); | |
| 85 EXPECT_TRUE(IsRegistered(kTestKeySystemType, kVersion1)); | |
| 86 } | |
| 87 | |
| 88 TEST_F(CdmServiceImplTest, MultipleVersions) { | |
| 89 Register(kTestKeySystemType, kVersion1, kTestPath, kTestCodecs); | |
| 90 Register(kTestKeySystemType, kVersion2, "/bb/cc", "unknown"); | |
| 91 EXPECT_TRUE(IsRegistered(kTestKeySystemType, kVersion1)); | |
| 92 EXPECT_TRUE(IsRegistered(kTestKeySystemType, kVersion2)); | |
| 93 } | |
| 94 | |
| 95 TEST_F(CdmServiceImplTest, NewVersionInsertedFirst) { | |
| 96 Register(kTestKeySystemType, kVersion1, kTestPath, kTestCodecs); | |
| 97 Register(kTestKeySystemType, kVersion2, "/bb/cc", "unknown"); | |
| 98 | |
| 99 const std::vector<std::string> versions = GetVersions(kTestKeySystemType); | |
| 100 EXPECT_EQ(2u, versions.size()); | |
| 101 EXPECT_EQ(kVersion2, versions[0]); | |
| 102 EXPECT_EQ(kVersion1, versions[1]); | |
| 103 } | |
| 104 | |
| 105 } // namespace content | |
| OLD | NEW |