2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2017 Google LLC. All rights reserved.
|
2017-09-14 16:15:24 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2020-06-30 21:31:07 +00:00
|
|
|
#include <gmock/gmock.h>
|
2017-09-14 16:15:24 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/file/file.h>
|
|
|
|
#include <packager/media/formats/webvtt/text_readers.h>
|
|
|
|
#include <packager/status/status_test_util.h>
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
namespace shaka {
|
|
|
|
namespace media {
|
|
|
|
|
2020-06-30 21:31:07 +00:00
|
|
|
using testing::ElementsAre;
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
TEST(TextReadersTest, ReadLinesWithNewLine) {
|
2020-06-30 21:31:07 +00:00
|
|
|
const uint8_t text[] = "a\nb\nc";
|
2018-01-03 00:35:38 +00:00
|
|
|
|
2020-06-30 21:31:07 +00:00
|
|
|
LineReader reader;
|
|
|
|
reader.PushData(text, sizeof(text) - 1);
|
|
|
|
reader.Flush();
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
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) {
|
2020-06-30 21:31:07 +00:00
|
|
|
const uint8_t text[] = "a\r\nb\r\nc";
|
2018-01-03 00:35:38 +00:00
|
|
|
|
2020-06-30 21:31:07 +00:00
|
|
|
LineReader reader;
|
|
|
|
reader.PushData(text, sizeof(text) - 1);
|
|
|
|
reader.Flush();
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
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) {
|
2020-06-30 21:31:07 +00:00
|
|
|
const uint8_t text[] = "a\n\rb\n\rc";
|
2018-01-03 00:35:38 +00:00
|
|
|
|
2020-06-30 21:31:07 +00:00
|
|
|
LineReader reader;
|
|
|
|
reader.PushData(text, sizeof(text) - 1);
|
|
|
|
reader.Flush();
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
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) {
|
2020-06-30 21:31:07 +00:00
|
|
|
const uint8_t text[] = "a\r\nb\r\nc\r";
|
2018-01-03 00:35:38 +00:00
|
|
|
|
2020-06-30 21:31:07 +00:00
|
|
|
LineReader reader;
|
|
|
|
reader.PushData(text, sizeof(text) - 1);
|
|
|
|
reader.Flush();
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2020-06-30 21:31:07 +00:00
|
|
|
TEST(TextReadersTest, ReadLinesWithMultiplePushes) {
|
|
|
|
const uint8_t text1[] = "a\nb";
|
|
|
|
const uint8_t text2[] = "c\nd";
|
|
|
|
|
|
|
|
LineReader reader;
|
|
|
|
reader.PushData(text1, sizeof(text1) - 1);
|
|
|
|
|
|
|
|
std::string s;
|
|
|
|
ASSERT_TRUE(reader.Next(&s));
|
|
|
|
ASSERT_EQ(s, "a");
|
|
|
|
ASSERT_FALSE(reader.Next(&s));
|
|
|
|
|
|
|
|
reader.PushData(text2, sizeof(text2) - 1);
|
|
|
|
reader.Flush();
|
|
|
|
ASSERT_TRUE(reader.Next(&s));
|
|
|
|
ASSERT_EQ(s, "bc");
|
|
|
|
ASSERT_TRUE(reader.Next(&s));
|
|
|
|
ASSERT_EQ(s, "d");
|
|
|
|
ASSERT_FALSE(reader.Next(&s));
|
|
|
|
}
|
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
TEST(TextReadersTest, ReadBlocksReadMultilineBlock) {
|
2020-06-30 21:31:07 +00:00
|
|
|
const uint8_t text[] =
|
2017-09-14 16:15:24 +00:00
|
|
|
"block 1 - line 1\n"
|
|
|
|
"block 1 - line 2";
|
|
|
|
|
2020-06-30 21:31:07 +00:00
|
|
|
BlockReader reader;
|
|
|
|
reader.PushData(text, sizeof(text) - 1);
|
|
|
|
reader.Flush();
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
std::vector<std::string> block;
|
|
|
|
ASSERT_TRUE(reader.Next(&block));
|
2020-06-30 21:31:07 +00:00
|
|
|
EXPECT_THAT(block, ElementsAre("block 1 - line 1", "block 1 - line 2"));
|
2017-09-14 16:15:24 +00:00
|
|
|
ASSERT_FALSE(reader.Next(&block));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextReadersTest, ReadBlocksSkipBlankLinesBeforeBlocks) {
|
2020-06-30 21:31:07 +00:00
|
|
|
const uint8_t text[] =
|
2017-09-14 16:15:24 +00:00
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"block 1\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"block 2\n";
|
|
|
|
|
2020-06-30 21:31:07 +00:00
|
|
|
BlockReader reader;
|
|
|
|
reader.PushData(text, sizeof(text) - 1);
|
|
|
|
reader.Flush();
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
std::vector<std::string> block;
|
|
|
|
|
|
|
|
ASSERT_TRUE(reader.Next(&block));
|
2020-06-30 21:31:07 +00:00
|
|
|
EXPECT_THAT(block, ElementsAre("block 1"));
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(reader.Next(&block));
|
2020-06-30 21:31:07 +00:00
|
|
|
EXPECT_THAT(block, ElementsAre("block 2"));
|
2017-09-14 16:15:24 +00:00
|
|
|
ASSERT_FALSE(reader.Next(&block));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TextReadersTest, ReadBlocksWithOnlyBlankLines) {
|
2020-06-30 21:31:07 +00:00
|
|
|
const uint8_t text[] = "\n\n\n\n";
|
2018-01-03 00:35:38 +00:00
|
|
|
|
2020-06-30 21:31:07 +00:00
|
|
|
BlockReader reader;
|
|
|
|
reader.PushData(text, sizeof(text) - 1);
|
|
|
|
reader.Flush();
|
2018-01-03 00:35:38 +00:00
|
|
|
|
2020-06-30 21:31:07 +00:00
|
|
|
std::vector<std::string> block;
|
|
|
|
ASSERT_FALSE(reader.Next(&block));
|
|
|
|
}
|
2017-09-14 16:15:24 +00:00
|
|
|
|
2020-06-30 21:31:07 +00:00
|
|
|
TEST(TextReadersTest, ReadBlocksMultipleReads) {
|
|
|
|
const uint8_t text1[] = "block 1\n";
|
|
|
|
const uint8_t text2[] =
|
|
|
|
"block 2\n"
|
|
|
|
"\n"
|
|
|
|
"\n"
|
|
|
|
"end";
|
|
|
|
|
|
|
|
BlockReader reader;
|
|
|
|
reader.PushData(text1, sizeof(text1) - 1);
|
2017-09-14 16:15:24 +00:00
|
|
|
|
|
|
|
std::vector<std::string> block;
|
|
|
|
ASSERT_FALSE(reader.Next(&block));
|
2020-06-30 21:31:07 +00:00
|
|
|
reader.PushData(text2, sizeof(text2) - 1);
|
|
|
|
reader.Flush();
|
|
|
|
|
|
|
|
ASSERT_TRUE(reader.Next(&block));
|
|
|
|
EXPECT_THAT(block, ElementsAre("block 1", "block 2"));
|
2017-09-14 16:15:24 +00:00
|
|
|
}
|
2020-06-30 21:31:07 +00:00
|
|
|
|
2017-09-14 16:15:24 +00:00
|
|
|
} // namespace media
|
|
|
|
} // namespace shaka
|