Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / renderer / media / render_media_client_unittest.cc
blobf40d0c5aa2dc504c4eb278ba094a8e8c01f5396c
1 // Copyright 2014 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.
5 #include <string>
6 #include <vector>
8 #include "base/test/simple_test_tick_clock.h"
9 #include "content/public/renderer/content_renderer_client.h"
10 #include "content/renderer/media/render_media_client.h"
11 #include "content/test/test_content_client.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 #include "widevine_cdm_version.h" // In SHARED_INTERMEDIATE_DIR.
16 namespace content {
18 namespace {
20 class TestContentRendererClient : public ContentRendererClient {
21 public:
22 TestContentRendererClient() : is_extra_key_system_enabled_(false) {}
24 // ContentRendererClient implementation.
25 void AddKeySystems(
26 std::vector<media::KeySystemInfo>* key_systems_info) override {
27 // TODO(sandersd): Was this supposed to be added to the list?
28 media::KeySystemInfo key_system_info;
29 key_system_info.key_system = "test.keysystem";
30 key_system_info.max_audio_robustness = media::EmeRobustness::EMPTY;
31 key_system_info.max_video_robustness = media::EmeRobustness::EMPTY;
32 key_system_info.persistent_license_support =
33 media::EmeSessionTypeSupport::NOT_SUPPORTED;
34 key_system_info.persistent_release_message_support =
35 media::EmeSessionTypeSupport::NOT_SUPPORTED;
36 key_system_info.persistent_state_support =
37 media::EmeFeatureSupport::NOT_SUPPORTED;
38 key_system_info.distinctive_identifier_support =
39 media::EmeFeatureSupport::NOT_SUPPORTED;
40 key_systems_info->push_back(key_system_info);
41 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
42 if (is_extra_key_system_enabled_) {
43 media::KeySystemInfo wv_key_system_info;
44 wv_key_system_info.key_system = kWidevineKeySystem;
45 wv_key_system_info.max_audio_robustness = media::EmeRobustness::EMPTY;
46 wv_key_system_info.max_video_robustness = media::EmeRobustness::EMPTY;
47 wv_key_system_info.persistent_license_support =
48 media::EmeSessionTypeSupport::NOT_SUPPORTED;
49 wv_key_system_info.persistent_release_message_support =
50 media::EmeSessionTypeSupport::NOT_SUPPORTED;
51 wv_key_system_info.persistent_state_support =
52 media::EmeFeatureSupport::NOT_SUPPORTED;
53 wv_key_system_info.distinctive_identifier_support =
54 media::EmeFeatureSupport::NOT_SUPPORTED;
55 key_systems_info->push_back(wv_key_system_info);
57 #endif
60 void EnableExtraKeySystem() { is_extra_key_system_enabled_ = true; }
62 private:
63 // Whether a platform-specific extra key system is "supported" by |this|.
64 bool is_extra_key_system_enabled_;
67 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
68 bool ContainsWidevine(
69 const std::vector<media::KeySystemInfo>& key_systems_info) {
70 for (const auto& key_system_info: key_systems_info) {
71 if (key_system_info.key_system == kWidevineKeySystem)
72 return true;
74 return false;
76 #endif
78 } // namespace
80 class RenderMediaClientTest : public testing::Test {
81 protected:
82 RenderMediaClientTest()
83 : render_media_client_(GetRenderMediaClientInstanceForTesting()) {
84 SetContentClient(&test_content_client_);
85 SetRendererClientForTesting(&test_content_renderer_client_);
88 void EnableExtraKeySystem() {
89 test_content_renderer_client_.EnableExtraKeySystem();
92 RenderMediaClient* render_media_client_;
94 private:
95 typedef base::hash_map<std::string, std::string> KeySystemNameForUMAMap;
97 TestContentClient test_content_client_;
98 TestContentRendererClient test_content_renderer_client_;
99 KeySystemNameForUMAMap key_system_name_for_uma_map_;
102 TEST_F(RenderMediaClientTest, KeySystemNameForUMA) {
103 std::vector<media::KeySystemInfoForUMA> key_systems_info_for_uma;
104 render_media_client_->AddKeySystemsInfoForUMA(&key_systems_info_for_uma);
106 std::string widevine_uma_name;
107 std::string clearkey_uma_name;
108 for (const media::KeySystemInfoForUMA& info : key_systems_info_for_uma) {
109 if (info.key_system == "com.widevine.alpha")
110 widevine_uma_name = info.key_system_name_for_uma;
111 if (info.key_system == "org.w3.clearkey")
112 clearkey_uma_name = info.key_system_name_for_uma;
115 #if defined(WIDEVINE_CDM_AVAILABLE)
116 EXPECT_EQ("Widevine", widevine_uma_name);
117 #else
118 EXPECT_TRUE(widevine_uma_name.empty());
119 #endif
121 EXPECT_TRUE(clearkey_uma_name.empty()) << "Clear Key is added by media/ and "
122 "should not be added by the "
123 "MediaClient.";
127 TEST_F(RenderMediaClientTest, IsKeySystemsUpdateNeeded) {
128 base::SimpleTestTickClock* tick_clock = new base::SimpleTestTickClock();
129 render_media_client_->SetTickClockForTesting(
130 scoped_ptr<base::TickClock>(tick_clock));
132 // IsKeySystemsUpdateNeeded() always returns true after construction.
133 EXPECT_TRUE(render_media_client_->IsKeySystemsUpdateNeeded());
135 std::vector<media::KeySystemInfo> key_systems_info;
136 render_media_client_->AddSupportedKeySystems(&key_systems_info);
138 // No update needed immediately after AddSupportedKeySystems() call.
139 EXPECT_FALSE(render_media_client_->IsKeySystemsUpdateNeeded());
141 #if defined(WIDEVINE_CDM_AVAILABLE) && defined(WIDEVINE_CDM_IS_COMPONENT)
142 // Widevine not supported because extra key system isn't enabled.
143 EXPECT_FALSE(ContainsWidevine(key_systems_info));
145 // This is timing related. The update interval for Widevine is 1000 ms.
146 EXPECT_FALSE(render_media_client_->IsKeySystemsUpdateNeeded());
147 tick_clock->Advance(base::TimeDelta::FromMilliseconds(990));
148 EXPECT_FALSE(render_media_client_->IsKeySystemsUpdateNeeded());
149 tick_clock->Advance(base::TimeDelta::FromMilliseconds(10));
150 EXPECT_TRUE(render_media_client_->IsKeySystemsUpdateNeeded());
152 EnableExtraKeySystem();
154 key_systems_info.clear();
155 render_media_client_->AddSupportedKeySystems(&key_systems_info);
156 EXPECT_TRUE(ContainsWidevine(key_systems_info));
158 EXPECT_FALSE(render_media_client_->IsKeySystemsUpdateNeeded());
159 tick_clock->Advance(base::TimeDelta::FromMilliseconds(1000));
160 EXPECT_FALSE(render_media_client_->IsKeySystemsUpdateNeeded());
161 tick_clock->Advance(base::TimeDelta::FromMilliseconds(1000));
162 EXPECT_FALSE(render_media_client_->IsKeySystemsUpdateNeeded());
163 #endif
166 } // namespace content