Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame^] | 1 | // Copyright (c) 2012 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 "base/basictypes.h" |
| 6 | #include "base/command_line.h" |
| 7 | #include "base/file_util.h" |
| 8 | #include "base/path_service.h" |
| 9 | #include "base/string16.h" |
| 10 | #include "base/string_util.h" |
| 11 | #include "base/stringprintf.h" |
| 12 | #include "base/utf_string_conversions.h" |
| 13 | #include "base/win/windows_version.h" |
| 14 | #include "content/browser/web_contents/web_contents_impl.h" |
| 15 | #include "content/public/common/content_switches.h" |
| 16 | #include "content/public/test/browser_test_utils.h" |
| 17 | #include "content/shell/shell.h" |
| 18 | #include "content/test/content_browser_test.h" |
| 19 | #include "content/test/content_browser_test_utils.h" |
| 20 | #include "googleurl/src/gurl.h" |
| 21 | #include "media/base/media_switches.h" |
| 22 | #include "webkit/media/crypto/key_systems.h" |
| 23 | |
| 24 | // Platform-specific filename relative to the chrome executable. |
| 25 | #if defined(OS_WIN) |
| 26 | static const wchar_t kLibraryName[] = L"clearkeycdmadapter.dll"; |
| 27 | #elif defined(OS_MACOSX) |
| 28 | static const char kLibraryName[] = "clearkeycdmadapter.plugin"; |
| 29 | #elif defined(OS_POSIX) |
| 30 | static const char kLibraryName[] = "libclearkeycdmadapter.so"; |
| 31 | #endif |
| 32 | |
| 33 | // Available key systems. |
| 34 | static const char kClearKeyKeySystem[] = "webkit-org.w3.clearkey"; |
| 35 | static const char kExternalClearKeyKeySystem[] = |
| 36 | "org.chromium.externalclearkey"; |
| 37 | |
| 38 | static const char kWebMAudioOnly[] = "audio/webm; codecs=\"vorbis\""; |
| 39 | static const char kWebMVideoOnly[] = "video/webm; codecs=\"vp8\""; |
| 40 | static const char kWebMAudioVideo[] = "video/webm; codecs=\"vorbis, vp8\""; |
| 41 | static const char kMP4AudioOnly[] = "audio/mp4; codecs=\"mp4a.40.2\""; |
| 42 | static const char kMP4VideoOnly[] = "video/mp4; codecs=\"avc1.4D4041\""; |
| 43 | |
| 44 | namespace content { |
| 45 | |
| 46 | class EncryptedMediaTest : public testing::WithParamInterface<const char*>, |
| 47 | public ContentBrowserTest { |
| 48 | public: |
| 49 | void TestSimplePlayback(const char* encrypted_media, const char* media_type, |
| 50 | const char* key_system, const string16 expectation) { |
| 51 | PlayEncryptedMedia("encrypted_media_player.html", encrypted_media, |
| 52 | media_type, key_system, expectation); |
| 53 | } |
| 54 | |
| 55 | void TestFrameSizeChange(const char* key_system, const string16 expectation) { |
| 56 | PlayEncryptedMedia("encrypted_frame_size_change.html", |
| 57 | "frame_size_change-av-enc-v.webm", kWebMAudioVideo, |
| 58 | key_system, expectation); |
| 59 | } |
| 60 | |
| 61 | void PlayEncryptedMedia(const char* html_page, const char* media_file, |
| 62 | const char* media_type, const char* key_system, |
| 63 | const string16 expectation) { |
| 64 | // TODO(shadi): Add non-HTTP tests once src is supported for EME. |
| 65 | ASSERT_TRUE(test_server()->Start()); |
| 66 | |
| 67 | const string16 kError = ASCIIToUTF16("ERROR"); |
| 68 | const string16 kFailed = ASCIIToUTF16("FAILED"); |
| 69 | GURL player_gurl = test_server()->GetURL(base::StringPrintf( |
| 70 | "files/media/%s?keysystem=%s&mediafile=%s&mediatype=%s", html_page, |
| 71 | key_system, media_file, media_type)); |
| 72 | TitleWatcher title_watcher(shell()->web_contents(), expectation); |
| 73 | title_watcher.AlsoWaitForTitle(kError); |
| 74 | title_watcher.AlsoWaitForTitle(kFailed); |
| 75 | |
| 76 | NavigateToURL(shell(), player_gurl); |
| 77 | |
| 78 | string16 final_title = title_watcher.WaitAndGetTitle(); |
| 79 | EXPECT_EQ(expectation, final_title); |
| 80 | |
| 81 | if (final_title == kFailed) { |
| 82 | std::string fail_message; |
| 83 | EXPECT_TRUE(ExecuteScriptAndExtractString( |
| 84 | shell()->web_contents(), |
| 85 | "window.domAutomationController.send(failMessage);", |
| 86 | &fail_message)); |
| 87 | LOG(INFO) << "Test failed: " << fail_message; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | protected: |
| 92 | // Registers any CDM plugins not registered by default. |
| 93 | virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { |
| 94 | command_line->AppendSwitch(switches::kDisableAudio); |
| 95 | |
| 96 | // Append the switch to register the Clear Key CDM plugin. |
| 97 | base::FilePath plugin_dir; |
| 98 | EXPECT_TRUE(PathService::Get(base::DIR_MODULE, &plugin_dir)); |
| 99 | base::FilePath plugin_lib = plugin_dir.Append(kLibraryName); |
| 100 | EXPECT_TRUE(file_util::PathExists(plugin_lib)); |
| 101 | base::FilePath::StringType pepper_plugin = plugin_lib.value(); |
| 102 | pepper_plugin.append(FILE_PATH_LITERAL( |
| 103 | "#Clear Key CDM#Clear Key CDM 0.1.0.0#0.1.0.0;")); |
| 104 | #if defined(OS_WIN) |
| 105 | pepper_plugin.append(ASCIIToWide( |
| 106 | webkit_media::GetPluginType(kExternalClearKeyKeySystem))); |
| 107 | #else |
| 108 | pepper_plugin.append( |
| 109 | webkit_media::GetPluginType(kExternalClearKeyKeySystem)); |
| 110 | #endif |
| 111 | command_line->AppendSwitchNative(switches::kRegisterPepperPlugins, |
| 112 | pepper_plugin); |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | INSTANTIATE_TEST_CASE_P(ClearKey, EncryptedMediaTest, |
| 117 | ::testing::Values(kClearKeyKeySystem)); |
| 118 | |
| 119 | INSTANTIATE_TEST_CASE_P(ExternalClearKey, EncryptedMediaTest, |
| 120 | ::testing::Values(kExternalClearKeyKeySystem)); |
| 121 | |
| 122 | IN_PROC_BROWSER_TEST_F(EncryptedMediaTest, InvalidKeySystem) { |
| 123 | const string16 kExpected = ASCIIToUTF16( |
| 124 | StringToUpperASCII(std::string("GenerateKeyRequestException"))); |
| 125 | ASSERT_NO_FATAL_FAILURE( |
| 126 | TestSimplePlayback("bear-320x240-av-enc_av.webm", kWebMAudioVideo, |
| 127 | "com.example.invalid", kExpected)); |
| 128 | } |
| 129 | |
| 130 | IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_AudioOnly_WebM) { |
| 131 | const string16 kExpected = ASCIIToUTF16("ENDED"); |
| 132 | ASSERT_NO_FATAL_FAILURE( |
| 133 | TestSimplePlayback("bear-a-enc_a.webm", kWebMAudioOnly, |
| 134 | GetParam(), kExpected)); |
| 135 | } |
| 136 | |
| 137 | IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_AudioClearVideo_WebM) { |
| 138 | const string16 kExpected = ASCIIToUTF16("ENDED"); |
| 139 | ASSERT_NO_FATAL_FAILURE( |
| 140 | TestSimplePlayback("bear-320x240-av-enc_a.webm", kWebMAudioVideo, |
| 141 | GetParam(), kExpected)); |
| 142 | } |
| 143 | |
| 144 | IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_VideoAudio_WebM) { |
| 145 | const string16 kExpected = ASCIIToUTF16("ENDED"); |
| 146 | ASSERT_NO_FATAL_FAILURE( |
| 147 | TestSimplePlayback("bear-320x240-av-enc_av.webm", kWebMAudioVideo, |
| 148 | GetParam(), kExpected)); |
| 149 | } |
| 150 | |
| 151 | IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_VideoOnly_WebM) { |
| 152 | const string16 kExpected = ASCIIToUTF16("ENDED"); |
| 153 | ASSERT_NO_FATAL_FAILURE( |
| 154 | TestSimplePlayback("bear-320x240-v-enc_v.webm", kWebMVideoOnly, |
| 155 | GetParam(), kExpected)); |
| 156 | } |
| 157 | |
| 158 | IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_VideoClearAudio_WebM) { |
| 159 | const string16 kExpected = ASCIIToUTF16("ENDED"); |
| 160 | ASSERT_NO_FATAL_FAILURE( |
| 161 | TestSimplePlayback("bear-320x240-av-enc_v.webm", kWebMAudioVideo, |
| 162 | GetParam(), kExpected)); |
| 163 | } |
| 164 | |
| 165 | IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, FrameChangeVideo) { |
| 166 | // Times out on Windows XP. http://crbug.com/171937 |
| 167 | #if defined(OS_WIN) |
| 168 | if (base::win::GetVersion() < base::win::VERSION_VISTA) |
| 169 | return; |
| 170 | #endif |
| 171 | const string16 kExpected = ASCIIToUTF16("ENDED"); |
| 172 | ASSERT_NO_FATAL_FAILURE(TestFrameSizeChange(GetParam(), kExpected)); |
| 173 | } |
| 174 | |
| 175 | #if defined(GOOGLE_CHROME_BUILD) || defined(USE_PROPRIETARY_CODECS) |
| 176 | IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_VideoOnly_MP4) { |
| 177 | const string16 kExpected = ASCIIToUTF16("ENDED"); |
| 178 | ASSERT_NO_FATAL_FAILURE( |
| 179 | TestSimplePlayback("bear-640x360-v_frag-cenc.mp4", kMP4VideoOnly, |
| 180 | GetParam(), kExpected)); |
| 181 | } |
| 182 | |
| 183 | IN_PROC_BROWSER_TEST_P(EncryptedMediaTest, Playback_AudioOnly_MP4) { |
| 184 | const string16 kExpected = ASCIIToUTF16("ENDED"); |
| 185 | ASSERT_NO_FATAL_FAILURE( |
| 186 | TestSimplePlayback("bear-640x360-a_frag-cenc.mp4", kMP4AudioOnly, |
| 187 | GetParam(), kExpected)); |
| 188 | } |
| 189 | #endif |
| 190 | |
| 191 | } // namespace content |