2017-09-14 16:15:24 +00:00
|
|
|
// Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2018-01-03 00:35:38 +00:00
|
|
|
#include "packager/file/file.h"
|
2017-09-14 16:15:24 +00:00
|
|
|
#include "packager/media/formats/webvtt/text_readers.h"
|
2018-01-03 00:35:38 +00:00
|
|
|
#include "packager/status_test_util.h"
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
namespace shaka {
|
|
|
|
namespace media {
|
2018-01-03 00:35:38 +00:00
|
|
|
namespace {
|
|
|
|
const char* kFilename = "memory://test-file";
|
|
|
|
} // namespace
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
TEST(TextReadersTest, ReadWholeStream) {
|
2018-01-03 00:35:38 +00:00
|
|
|
const char* text = "abcd";
|
|
|
|
|
|
|
|
ASSERT_TRUE(File::WriteStringToFile(kFilename, text));
|
|
|
|
|
|
|
|
std::unique_ptr<FileReader> source;
|
|
|
|
ASSERT_OK(FileReader::Open(kFilename, &source));
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
char c;
|
2018-01-03 00:35:38 +00:00
|
|
|
ASSERT_TRUE(source->Next(&c));
|
2017-09-14 16:15:24 +00:00
|
|
|
ASSERT_EQ(c, 'a');
|
2018-01-03 00:35:38 +00:00
|
|
|
ASSERT_TRUE(source->Next(&c));
|
2017-09-14 16:15:24 +00:00
|
|
|
ASSERT_EQ(c, 'b');
|
2018-01-03 00:35:38 +00:00
|
|
|
ASSERT_TRUE(source->Next(&c));
|
2017-09-14 16:15:24 +00:00
|
|
|
ASSERT_EQ(c, 'c');
|
2018-01-03 00:35:38 +00:00
|
|
|
ASSERT_TRUE(source->Next(&c));
|
2017-09-14 16:15:24 +00:00
|
|
|
ASSERT_EQ(c, 'd');
|
2018-01-03 00:35:38 +00:00
|
|
|
ASSERT_FALSE(source->Next(&c));
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextReadersTest, Peeking) {
|
2018-01-03 00:35:38 +00:00
|
|
|
const char* text = "abc";
|
|
|
|
|
|
|
|
ASSERT_TRUE(File::WriteStringToFile(kFilename, text));
|
|
|
|
|
|
|
|
std::unique_ptr<FileReader> source;
|
|
|
|
ASSERT_OK(FileReader::Open(kFilename, &source));
|
|
|
|
|
|
|
|
PeekingReader reader(std::move(source));
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
char c;
|
|
|
|
ASSERT_TRUE(reader.Peek(&c));
|
|
|
|
ASSERT_EQ(c, 'a');
|
|
|
|
ASSERT_TRUE(reader.Next(&c));
|
|
|
|
ASSERT_EQ(c, 'a');
|
|
|
|
ASSERT_TRUE(reader.Peek(&c));
|
|
|
|
ASSERT_EQ(c, 'b');
|
|
|
|
ASSERT_TRUE(reader.Next(&c));
|
|
|
|
ASSERT_EQ(c, 'b');
|
|
|
|
ASSERT_TRUE(reader.Peek(&c));
|
|
|
|
ASSERT_EQ(c, 'c');
|
|
|
|
ASSERT_TRUE(reader.Next(&c));
|
|
|
|
ASSERT_EQ(c, 'c');
|
|
|
|
ASSERT_FALSE(reader.Peek(&c));
|
|
|
|
ASSERT_FALSE(reader.Next(&c));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextReadersTest, ReadLinesWithNewLine) {
|
2018-01-03 00:35:38 +00:00
|
|
|
const char* text = "a\nb\nc";
|
|
|
|
|
|
|
|
ASSERT_TRUE(File::WriteStringToFile(kFilename, text));
|
|
|
|
|
|
|
|
std::unique_ptr<FileReader> source;
|
|
|
|
ASSERT_OK(FileReader::Open(kFilename, &source));
|
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
LineReader reader(std::move(source));
|
|
|
|
|
|
|
|
std::string s;
|
|
|
|
ASSERT_TRUE(reader.Next(&s));
|
|
|
|
ASSERT_EQ(s, "a");
|
|
|
|
ASSERT_TRUE(reader.Next(&s));
|
|
|
|
ASSERT_EQ(s, "b");
|
|
|
|
ASSERT_TRUE(reader.Next(&s));
|
|
|
|
ASSERT_EQ(s, "c");
|
|
|
|
ASSERT_FALSE(reader.Next(&s));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextReadersTest, ReadLinesWithReturnsAndNewLine) {
|
2018-01-03 00:35:38 +00:00
|
|
|
const char* text = "a\r\nb\r\nc";
|
|
|
|
|
|
|
|
ASSERT_TRUE(File::WriteStringToFile(kFilename, text));
|
|
|
|
|
|
|
|
std::unique_ptr<FileReader> source;
|
|
|
|
ASSERT_OK(FileReader::Open(kFilename, &source));
|
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
LineReader reader(std::move(source));
|
|
|
|
|
|
|
|
std::string s;
|
|
|
|
ASSERT_TRUE(reader.Next(&s));
|
|
|
|
ASSERT_EQ(s, "a");
|
|
|
|
ASSERT_TRUE(reader.Next(&s));
|
|
|
|
ASSERT_EQ(s, "b");
|
|
|
|
ASSERT_TRUE(reader.Next(&s));
|
|
|
|
ASSERT_EQ(s, "c");
|
|
|
|
ASSERT_FALSE(reader.Next(&s));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextReadersTest, ReadLinesWithNewLineAndReturns) {
|
2018-01-03 00:35:38 +00:00
|
|
|
const char* text = "a\n\rb\n\rc";
|
|
|
|
|
|
|
|
ASSERT_TRUE(File::WriteStringToFile(kFilename, text));
|
|
|
|
|
|
|
|
std::unique_ptr<FileReader> source;
|
|
|
|
ASSERT_OK(FileReader::Open(kFilename, &source));
|
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
LineReader reader(std::move(source));
|
|
|
|
|
|
|
|
std::string s;
|
|
|
|
ASSERT_TRUE(reader.Next(&s));
|
|
|
|
ASSERT_EQ(s, "a");
|
|
|
|
ASSERT_TRUE(reader.Next(&s));
|
|
|
|
ASSERT_EQ(s, "");
|
|
|
|
ASSERT_TRUE(reader.Next(&s));
|
|
|
|
ASSERT_EQ(s, "b");
|
|
|
|
ASSERT_TRUE(reader.Next(&s));
|
|
|
|
ASSERT_EQ(s, "");
|
|
|
|
ASSERT_TRUE(reader.Next(&s));
|
|
|
|
ASSERT_EQ(s, "c");
|
|
|
|
ASSERT_FALSE(reader.Next(&s));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextReadersTest, ReadLinesWithReturnAtEnd) {
|
2018-01-03 00:35:38 +00:00
|
|
|
const char* text = "a\r\nb\r\nc\r";
|
|
|
|
|
|
|
|
ASSERT_TRUE(File::WriteStringToFile(kFilename, text));
|
|
|
|
|
|
|
|
std::unique_ptr<FileReader> source;
|
|
|
|
ASSERT_OK(FileReader::Open(kFilename, &source));
|
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
LineReader reader(std::move(source));
|
|
|
|
|
|
|
|
std::string s;
|
|
|
|
ASSERT_TRUE(reader.Next(&s));
|
|
|
|
ASSERT_EQ(s, "a");
|
|
|
|
ASSERT_TRUE(reader.Next(&s));
|
|
|
|
ASSERT_EQ(s, "b");
|
|
|
|
ASSERT_TRUE(reader.Next(&s));
|
|
|
|
ASSERT_EQ(s, "c");
|
|
|
|
ASSERT_FALSE(reader.Next(&s));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextReadersTest, ReadBlocksReadMultilineBlock) {
|
2018-01-03 00:35:38 +00:00
|
|
|
const char* text =
|
2017-09-14 16:15:24 +00:00
|
|
|
"block 1 - line 1\n"
|
|
|
|
"block 1 - line 2";
|
|
|
|
|
2018-01-03 00:35:38 +00:00
|
|
|
ASSERT_TRUE(File::WriteStringToFile(kFilename, text));
|
|
|
|
|
|
|
|
std::unique_ptr<FileReader> source;
|
|
|
|
ASSERT_OK(FileReader::Open(kFilename, &source));
|
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
BlockReader reader(std::move(source));
|
|
|
|
|
|
|
|
std::vector<std::string> block;
|
|
|
|
|
|
|
|
ASSERT_TRUE(reader.Next(&block));
|
|
|
|
ASSERT_EQ(2u, block.size());
|
|
|
|
ASSERT_EQ("block 1 - line 1", block[0]);
|
|
|
|
ASSERT_EQ("block 1 - line 2", block[1]);
|
|
|
|
|
|
|
|
ASSERT_FALSE(reader.Next(&block));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextReadersTest, ReadBlocksSkipBlankLinesBeforeBlocks) {
|
2018-01-03 00:35:38 +00:00
|
|
|
const char* text =
|
2017-09-14 16:15:24 +00:00
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"block 1\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"block 2\n";
|
|
|
|
|
2018-01-03 00:35:38 +00:00
|
|
|
ASSERT_TRUE(File::WriteStringToFile(kFilename, text));
|
|
|
|
|
|
|
|
std::unique_ptr<FileReader> source;
|
|
|
|
ASSERT_OK(FileReader::Open(kFilename, &source));
|
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
BlockReader reader(std::move(source));
|
|
|
|
|
|
|
|
std::vector<std::string> block;
|
|
|
|
|
|
|
|
ASSERT_TRUE(reader.Next(&block));
|
|
|
|
ASSERT_EQ(1u, block.size());
|
|
|
|
ASSERT_EQ("block 1", block[0]);
|
|
|
|
|
|
|
|
ASSERT_TRUE(reader.Next(&block));
|
|
|
|
ASSERT_EQ(1u, block.size());
|
|
|
|
ASSERT_EQ("block 2", block[0]);
|
|
|
|
|
|
|
|
ASSERT_FALSE(reader.Next(&block));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextReadersTest, ReadBlocksWithOnlyBlankLines) {
|
2018-01-03 00:35:38 +00:00
|
|
|
const char* text = "\n\n\n\n";
|
|
|
|
|
|
|
|
ASSERT_TRUE(File::WriteStringToFile(kFilename, text));
|
|
|
|
|
|
|
|
std::unique_ptr<FileReader> source;
|
|
|
|
ASSERT_OK(FileReader::Open(kFilename, &source));
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
BlockReader reader(std::move(source));
|
|
|
|
|
|
|
|
std::vector<std::string> block;
|
|
|
|
ASSERT_FALSE(reader.Next(&block));
|
|
|
|
}
|
|
|
|
} // namespace media
|
|
|
|
} // namespace shaka
|