| 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 <algorithm> |
| 6 #include <string> |
| 7 #include <vector> |
| 8 |
| 9 #include "base/base_paths.h" |
| 10 #include "base/files/file_path.h" |
| 11 #include "base/strings/string16.h" |
| 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "content/public/common/key_system_service.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 const char kTestPath[] = "/aa/bb"; |
| 19 const char kTestCodecs[] = "webm,mp4"; |
| 20 |
| 21 class KeySystemServiceTest : public testing::Test { |
| 22 public: |
| 23 KeySystemServiceTest() {} |
| 24 ~KeySystemServiceTest() override {} |
| 25 |
| 26 protected: |
| 27 void Register(const std::string& key_system, |
| 28 const std::string& path, |
| 29 const std::string& codecs) { |
| 30 KeySystemService::GetInstance()->Register( |
| 31 key_system, base::FilePath::FromUTF8Unsafe(path), |
| 32 base::ASCIIToUTF16(codecs)); |
| 33 } |
| 34 |
| 35 bool PathDefined(const std::string& key_system) { |
| 36 base::FilePath path; |
| 37 return KeySystemService::GetInstance()->Path(key_system, &path); |
| 38 } |
| 39 |
| 40 std::string Path(const std::string& key_system) { |
| 41 base::FilePath path; |
| 42 EXPECT_TRUE(KeySystemService::GetInstance()->Path(key_system, &path)); |
| 43 return path.AsUTF8Unsafe(); |
| 44 } |
| 45 |
| 46 bool CodecsDefined(const std::string& key_system) { |
| 47 base::string16 codecs; |
| 48 return KeySystemService::GetInstance()->Codecs(key_system, &codecs); |
| 49 } |
| 50 |
| 51 std::string Codecs(const std::string& key_system) { |
| 52 base::string16 codecs; |
| 53 EXPECT_TRUE(KeySystemService::GetInstance()->Codecs(key_system, &codecs)); |
| 54 return base::UTF16ToASCII(codecs); |
| 55 } |
| 56 |
| 57 void GetAllRegisteredKeySystems(std::vector<std::string>* key_systems, |
| 58 std::vector<std::string>* paths) { |
| 59 std::vector<base::FilePath> file_paths; |
| 60 KeySystemService::GetInstance()->GetAllRegisteredKeySystems(key_systems, |
| 61 &file_paths); |
| 62 for (const auto& path : file_paths) |
| 63 paths->push_back(path.AsUTF8Unsafe()); |
| 64 } |
| 65 }; |
| 66 |
| 67 // Note that KeySystemService is a singleton, and thus the actions of |
| 68 // one test impact other tests. So each test defines a different key system |
| 69 // name to avoid conflicts. |
| 70 |
| 71 TEST_F(KeySystemServiceTest, Register) { |
| 72 const char kTestKeySystem[] = "test1"; |
| 73 Register(kTestKeySystem, kTestPath, kTestCodecs); |
| 74 EXPECT_TRUE(PathDefined(kTestKeySystem)); |
| 75 EXPECT_TRUE(CodecsDefined(kTestKeySystem)); |
| 76 } |
| 77 |
| 78 TEST_F(KeySystemServiceTest, Path) { |
| 79 const char kTestKeySystem[] = "test2"; |
| 80 Register(kTestKeySystem, kTestPath, kTestCodecs); |
| 81 EXPECT_TRUE(PathDefined(kTestKeySystem)); |
| 82 EXPECT_EQ(Path(kTestKeySystem), kTestPath); |
| 83 } |
| 84 |
| 85 TEST_F(KeySystemServiceTest, Codecs) { |
| 86 const char kTestKeySystem[] = "test3"; |
| 87 Register(kTestKeySystem, kTestPath, kTestCodecs); |
| 88 EXPECT_TRUE(CodecsDefined(kTestKeySystem)); |
| 89 EXPECT_EQ(Codecs(kTestKeySystem), kTestCodecs); |
| 90 } |
| 91 |
| 92 TEST_F(KeySystemServiceTest, ReRegister) { |
| 93 const char kTestKeySystem[] = "test4"; |
| 94 Register(kTestKeySystem, "/bb/cc", "unknown"); |
| 95 EXPECT_TRUE(PathDefined(kTestKeySystem)); |
| 96 EXPECT_EQ(Path(kTestKeySystem), "/bb/cc"); |
| 97 EXPECT_TRUE(CodecsDefined(kTestKeySystem)); |
| 98 EXPECT_EQ(Codecs(kTestKeySystem), "unknown"); |
| 99 |
| 100 // Now register same key system with different values. |
| 101 Register(kTestKeySystem, kTestPath, kTestCodecs); |
| 102 EXPECT_TRUE(PathDefined(kTestKeySystem)); |
| 103 EXPECT_EQ(Path(kTestKeySystem), kTestPath); |
| 104 EXPECT_TRUE(CodecsDefined(kTestKeySystem)); |
| 105 EXPECT_EQ(Codecs(kTestKeySystem), kTestCodecs); |
| 106 } |
| 107 |
| 108 TEST_F(KeySystemServiceTest, RegisteredKeySystems) { |
| 109 const char kTestKeySystem[] = "test5"; |
| 110 const char kDifferentTestPath[] = "/cc/dd"; |
| 111 std::vector<std::string> key_systems; |
| 112 std::vector<std::string> paths; |
| 113 |
| 114 Register(kTestKeySystem, kDifferentTestPath, kTestCodecs); |
| 115 GetAllRegisteredKeySystems(&key_systems, &paths); |
| 116 EXPECT_GT(key_systems.size(), 0u); |
| 117 EXPECT_GT(paths.size(), 0u); |
| 118 EXPECT_EQ(key_systems.size(), paths.size()); |
| 119 |
| 120 // Verify |kTestKeySystem| and |kDifferentTestPath| is in the returned data. |
| 121 EXPECT_NE(std::find(key_systems.begin(), key_systems.end(), kTestKeySystem), |
| 122 key_systems.end()); |
| 123 EXPECT_NE(std::find(paths.begin(), paths.end(), kDifferentTestPath), |
| 124 paths.end()); |
| 125 |
| 126 // Locate |kTestKeySystem| in |key_systems| and verify that the matching |
| 127 // |kDifferentTestPath| is in the corresponding location in |paths|. |
| 128 for (size_t i = 0; i < key_systems.size(); i++) { |
| 129 if (key_systems[i] == kTestKeySystem) { |
| 130 EXPECT_EQ(paths[i], kDifferentTestPath); |
| 131 } |
| 132 } |
| 133 } |
| 134 |
| 135 } // namespace content |
| OLD | NEW |