summaryrefslogtreecommitdiff
path: root/chromium/media/base/text_ranges_unittest.cc
blob: fd62bca83d9e6d74a7c453870fbf24bfcb94854f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/base/text_ranges.h"

#include <stddef.h>

#include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media {

class TextRangesTest : public ::testing::Test {
 protected:
  bool AddCue(int seconds) { return ranges_.AddCue(base::Seconds(seconds)); }

  void Reset() {
    ranges_.Reset();
  }

  size_t RangeCount() {
    return ranges_.RangeCountForTesting();
  }

  TextRanges ranges_;
};

TEST_F(TextRangesTest, TestEmptyRanges) {
  // Create a new active range, with t=5.
  EXPECT_TRUE(AddCue(5));

  // Create a new active range, with t=2.
  Reset();
  EXPECT_TRUE(AddCue(2));

  // Create a new active range, with t=8.
  Reset();
  EXPECT_TRUE(AddCue(8));

  Reset();

  // Make range [2, 2] active.
  EXPECT_FALSE(AddCue(2));
  EXPECT_EQ(RangeCount(), 3U);

  // Coalesce first two ranges: [2, 5].
  EXPECT_FALSE(AddCue(5));
  EXPECT_EQ(RangeCount(), 2U);

  // Coalesce first two ranges: [2, 8].
  EXPECT_FALSE(AddCue(8));
  EXPECT_EQ(RangeCount(), 1U);

  // Add new cue to end of (only) range.
  EXPECT_TRUE(AddCue(9));
  EXPECT_EQ(RangeCount(), 1U);
}

TEST_F(TextRangesTest, TestOneRange) {
  // Create a new active range, with t=0.
  EXPECT_TRUE(AddCue(0));

  // Add cues to end of existing range.
  EXPECT_TRUE(AddCue(1));
  EXPECT_TRUE(AddCue(4));

  Reset();
  EXPECT_FALSE(AddCue(2));
  EXPECT_FALSE(AddCue(3));
  EXPECT_FALSE(AddCue(4));
}

TEST_F(TextRangesTest, TestDuplicateLast) {
  // Create a new active range, with t=0.
  EXPECT_TRUE(AddCue(0));
  EXPECT_TRUE(AddCue(1));

  Reset();
  EXPECT_FALSE(AddCue(1));
  EXPECT_TRUE(AddCue(1));
}

TEST_F(TextRangesTest, TestTwoRanges) {
  // Create a new active range, with t=0.
  EXPECT_TRUE(AddCue(0));

  // Add cue to end of existing range.
  EXPECT_TRUE(AddCue(2));

  Reset();

  // Create a new active range, with t=4.
  EXPECT_TRUE(AddCue(4));

  // Add a new cue to end of last (active) range.
  EXPECT_TRUE(AddCue(5));

  Reset();

  // Make first range active.
  EXPECT_FALSE(AddCue(0));
  EXPECT_FALSE(AddCue(2));

  // Expand first range.
  EXPECT_TRUE(AddCue(3));

  // Coalesce first and second ranges.
  EXPECT_FALSE(AddCue(4));
  EXPECT_EQ(RangeCount(), 1U);
}

TEST_F(TextRangesTest, TestThreeRanges) {
  // Create a new active range, with t=0.
  EXPECT_TRUE(AddCue(0));

  // Add cue to end of existing range.
  EXPECT_TRUE(AddCue(2));

  Reset();

  // Create a new active range, with t=4.
  EXPECT_TRUE(AddCue(4));

  // Add a new cue to end of last (active) range.
  EXPECT_TRUE(AddCue(5));

  Reset();

  // Create a new active range, in between the other two.
  EXPECT_TRUE(AddCue(3));

  // Coalesce middle and last ranges.
  EXPECT_FALSE(AddCue(4));

  Reset();

  // Make first range active.
  EXPECT_FALSE(AddCue(0));
  EXPECT_FALSE(AddCue(2));

  // Coalesce first and last ranges.
  EXPECT_FALSE(AddCue(3));
  EXPECT_EQ(RangeCount(), 1U);
}

}  // namespace media