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 "media/base/text_ranges.h"
7 #include "base/time/time.h"
8 #include "testing/gtest/include/gtest/gtest.h"
12 class TextRangesTest
: public ::testing::Test
{
14 bool AddCue(int seconds
) {
15 return ranges_
.AddCue(base::TimeDelta::FromSeconds(seconds
));
23 return ranges_
.RangeCountForTesting();
29 TEST_F(TextRangesTest
, TestEmptyRanges
) {
30 // Create a new active range, with t=5.
31 EXPECT_TRUE(AddCue(5));
33 // Create a new active range, with t=2.
35 EXPECT_TRUE(AddCue(2));
37 // Create a new active range, with t=8.
39 EXPECT_TRUE(AddCue(8));
43 // Make range [2, 2] active.
44 EXPECT_FALSE(AddCue(2));
45 EXPECT_EQ(RangeCount(), 3U);
47 // Coalesce first two ranges: [2, 5].
48 EXPECT_FALSE(AddCue(5));
49 EXPECT_EQ(RangeCount(), 2U);
51 // Coalesce first two ranges: [2, 8].
52 EXPECT_FALSE(AddCue(8));
53 EXPECT_EQ(RangeCount(), 1U);
55 // Add new cue to end of (only) range.
56 EXPECT_TRUE(AddCue(9));
57 EXPECT_EQ(RangeCount(), 1U);
60 TEST_F(TextRangesTest
, TestOneRange
) {
61 // Create a new active range, with t=0.
62 EXPECT_TRUE(AddCue(0));
64 // Add cues to end of existing range.
65 EXPECT_TRUE(AddCue(1));
66 EXPECT_TRUE(AddCue(4));
69 EXPECT_FALSE(AddCue(2));
70 EXPECT_FALSE(AddCue(3));
71 EXPECT_FALSE(AddCue(4));
74 TEST_F(TextRangesTest
, TestDuplicateLast
) {
75 // Create a new active range, with t=0.
76 EXPECT_TRUE(AddCue(0));
77 EXPECT_TRUE(AddCue(1));
80 EXPECT_FALSE(AddCue(1));
81 EXPECT_TRUE(AddCue(1));
84 TEST_F(TextRangesTest
, TestTwoRanges
) {
85 // Create a new active range, with t=0.
86 EXPECT_TRUE(AddCue(0));
88 // Add cue to end of existing range.
89 EXPECT_TRUE(AddCue(2));
93 // Create a new active range, with t=4.
94 EXPECT_TRUE(AddCue(4));
96 // Add a new cue to end of last (active) range.
97 EXPECT_TRUE(AddCue(5));
101 // Make first range active.
102 EXPECT_FALSE(AddCue(0));
103 EXPECT_FALSE(AddCue(2));
105 // Expand first range.
106 EXPECT_TRUE(AddCue(3));
108 // Coalesce first and second ranges.
109 EXPECT_FALSE(AddCue(4));
110 EXPECT_EQ(RangeCount(), 1U);
113 TEST_F(TextRangesTest
, TestThreeRanges
) {
114 // Create a new active range, with t=0.
115 EXPECT_TRUE(AddCue(0));
117 // Add cue to end of existing range.
118 EXPECT_TRUE(AddCue(2));
122 // Create a new active range, with t=4.
123 EXPECT_TRUE(AddCue(4));
125 // Add a new cue to end of last (active) range.
126 EXPECT_TRUE(AddCue(5));
130 // Create a new active range, in between the other two.
131 EXPECT_TRUE(AddCue(3));
133 // Coalesce middle and last ranges.
134 EXPECT_FALSE(AddCue(4));
138 // Make first range active.
139 EXPECT_FALSE(AddCue(0));
140 EXPECT_FALSE(AddCue(2));
142 // Coalesce first and last ranges.
143 EXPECT_FALSE(AddCue(3));
144 EXPECT_EQ(RangeCount(), 1U);