1 // Copyright (c) 2013 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 "media/webm/webm_webvtt_parser.h" 6 #include "testing/gmock/include/gmock/gmock.h" 7 #include "testing/gtest/include/gtest/gtest.h" 8 9 using ::testing::InSequence; 10 11 namespace media { 12 13 typedef std::vector<uint8> Cue; 14 15 static Cue EncodeCue(const std::string& id, 16 const std::string& settings, 17 const std::string& content) { 18 const std::string result = id + '\n' + settings + '\n' + content; 19 const uint8* const buf = reinterpret_cast<const uint8*>(result.data()); 20 return Cue(buf, buf + result.length()); 21 } 22 23 static void DecodeCue(const Cue& cue, 24 std::string* id, 25 std::string* settings, 26 std::string* content) { 27 WebMWebVTTParser::Parse(&cue[0], static_cast<int>(cue.size()), 28 id, settings, content); 29 } 30 31 class WebMWebVTTParserTest : public testing::Test { 32 public: 33 WebMWebVTTParserTest() {} 34 }; 35 36 TEST_F(WebMWebVTTParserTest, Blank) { 37 InSequence s; 38 39 const Cue cue = EncodeCue("", "", "Subtitle"); 40 std::string id, settings, content; 41 42 DecodeCue(cue, &id, &settings, &content); 43 EXPECT_EQ(id, ""); 44 EXPECT_EQ(settings, ""); 45 EXPECT_EQ(content, "Subtitle"); 46 } 47 48 TEST_F(WebMWebVTTParserTest, Id) { 49 InSequence s; 50 51 for (int i = 1; i <= 9; ++i) { 52 const std::string idsrc(1, '0'+i); 53 const Cue cue = EncodeCue(idsrc, "", "Subtitle"); 54 std::string id, settings, content; 55 56 DecodeCue(cue, &id, &settings, &content); 57 EXPECT_EQ(id, idsrc); 58 EXPECT_EQ(settings, ""); 59 EXPECT_EQ(content, "Subtitle"); 60 } 61 } 62 63 TEST_F(WebMWebVTTParserTest, Settings) { 64 InSequence s; 65 66 enum { kSettingsCount = 4 }; 67 const char* const settings_str[kSettingsCount] = { 68 "vertical:lr", 69 "line:50%", 70 "position:42%", 71 "vertical:rl line:42% position:100%" }; 72 73 for (int i = 0; i < kSettingsCount; ++i) { 74 const Cue cue = EncodeCue("", settings_str[i], "Subtitle"); 75 std::string id, settings, content; 76 77 DecodeCue(cue, &id, &settings, &content); 78 EXPECT_EQ(id, ""); 79 EXPECT_EQ(settings, settings_str[i]); 80 EXPECT_EQ(content, "Subtitle"); 81 } 82 } 83 84 TEST_F(WebMWebVTTParserTest, Content) { 85 InSequence s; 86 87 enum { kContentCount = 4 }; 88 const char* const content_str[kContentCount] = { 89 "Subtitle", 90 "Another Subtitle", 91 "Yet Another Subtitle", 92 "Another Subtitle\nSplit Across Two Lines" }; 93 94 for (int i = 0; i < kContentCount; ++i) { 95 const Cue cue = EncodeCue("", "", content_str[i]); 96 std::string id, settings, content; 97 98 DecodeCue(cue, &id, &settings, &content); 99 EXPECT_EQ(id, ""); 100 EXPECT_EQ(settings, ""); 101 EXPECT_EQ(content, content_str[i]); 102 } 103 } 104 105 } // namespace media 106