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. 4 5 #include "media/base/text_ranges.h" 6 7 #include "base/time/time.h" 8 #include "testing/gtest/include/gtest/gtest.h" 9 10 namespace media { 11 12 class TextRangesTest : public ::testing::Test { 13 protected: 14 bool AddCue(int seconds) { 15 return ranges_.AddCue(base::TimeDelta::FromSeconds(seconds)); 16 } 17 18 void Reset() { 19 ranges_.Reset(); 20 } 21 22 size_t RangeCount() { 23 return ranges_.RangeCountForTesting(); 24 } 25 26 TextRanges ranges_; 27 }; 28 29 TEST_F(TextRangesTest, TestEmptyRanges) { 30 // Create a new active range, with t=5. 31 EXPECT_TRUE(AddCue(5)); 32 33 // Create a new active range, with t=2. 34 Reset(); 35 EXPECT_TRUE(AddCue(2)); 36 37 // Create a new active range, with t=8. 38 Reset(); 39 EXPECT_TRUE(AddCue(8)); 40 41 Reset(); 42 43 // Make range [2, 2] active. 44 EXPECT_FALSE(AddCue(2)); 45 EXPECT_EQ(RangeCount(), 3U); 46 47 // Coalesce first two ranges: [2, 5]. 48 EXPECT_FALSE(AddCue(5)); 49 EXPECT_EQ(RangeCount(), 2U); 50 51 // Coalesce first two ranges: [2, 8]. 52 EXPECT_FALSE(AddCue(8)); 53 EXPECT_EQ(RangeCount(), 1U); 54 55 // Add new cue to end of (only) range. 56 EXPECT_TRUE(AddCue(9)); 57 EXPECT_EQ(RangeCount(), 1U); 58 } 59 60 TEST_F(TextRangesTest, TestOneRange) { 61 // Create a new active range, with t=0. 62 EXPECT_TRUE(AddCue(0)); 63 64 // Add cues to end of existing range. 65 EXPECT_TRUE(AddCue(1)); 66 EXPECT_TRUE(AddCue(4)); 67 68 Reset(); 69 EXPECT_FALSE(AddCue(2)); 70 EXPECT_FALSE(AddCue(3)); 71 EXPECT_FALSE(AddCue(4)); 72 } 73 74 TEST_F(TextRangesTest, TestDuplicateLast) { 75 // Create a new active range, with t=0. 76 EXPECT_TRUE(AddCue(0)); 77 EXPECT_TRUE(AddCue(1)); 78 79 Reset(); 80 EXPECT_FALSE(AddCue(1)); 81 EXPECT_TRUE(AddCue(1)); 82 } 83 84 TEST_F(TextRangesTest, TestTwoRanges) { 85 // Create a new active range, with t=0. 86 EXPECT_TRUE(AddCue(0)); 87 88 // Add cue to end of existing range. 89 EXPECT_TRUE(AddCue(2)); 90 91 Reset(); 92 93 // Create a new active range, with t=4. 94 EXPECT_TRUE(AddCue(4)); 95 96 // Add a new cue to end of last (active) range. 97 EXPECT_TRUE(AddCue(5)); 98 99 Reset(); 100 101 // Make first range active. 102 EXPECT_FALSE(AddCue(0)); 103 EXPECT_FALSE(AddCue(2)); 104 105 // Expand first range. 106 EXPECT_TRUE(AddCue(3)); 107 108 // Coalesce first and second ranges. 109 EXPECT_FALSE(AddCue(4)); 110 EXPECT_EQ(RangeCount(), 1U); 111 } 112 113 TEST_F(TextRangesTest, TestThreeRanges) { 114 // Create a new active range, with t=0. 115 EXPECT_TRUE(AddCue(0)); 116 117 // Add cue to end of existing range. 118 EXPECT_TRUE(AddCue(2)); 119 120 Reset(); 121 122 // Create a new active range, with t=4. 123 EXPECT_TRUE(AddCue(4)); 124 125 // Add a new cue to end of last (active) range. 126 EXPECT_TRUE(AddCue(5)); 127 128 Reset(); 129 130 // Create a new active range, in between the other two. 131 EXPECT_TRUE(AddCue(3)); 132 133 // Coalesce middle and last ranges. 134 EXPECT_FALSE(AddCue(4)); 135 136 Reset(); 137 138 // Make first range active. 139 EXPECT_FALSE(AddCue(0)); 140 EXPECT_FALSE(AddCue(2)); 141 142 // Coalesce first and last ranges. 143 EXPECT_FALSE(AddCue(3)); 144 EXPECT_EQ(RangeCount(), 1U); 145 } 146 147 } // namespace media 148