1 // Copyright (c) 2012 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 "ui/gfx/break_list.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "third_party/skia/include/core/SkColor.h"
9 #include "ui/gfx/range/range.h"
13 class BreakListTest
: public testing::Test
{};
15 TEST_F(BreakListTest
, SetValue
) {
16 // Check the default values applied to new instances.
17 BreakList
<bool> style_breaks(false);
18 EXPECT_TRUE(style_breaks
.EqualsValueForTesting(false));
19 style_breaks
.SetValue(true);
20 EXPECT_TRUE(style_breaks
.EqualsValueForTesting(true));
22 // Ensure that setting values works correctly.
23 BreakList
<SkColor
> color_breaks(SK_ColorRED
);
24 EXPECT_TRUE(color_breaks
.EqualsValueForTesting(SK_ColorRED
));
25 color_breaks
.SetValue(SK_ColorBLACK
);
26 EXPECT_TRUE(color_breaks
.EqualsValueForTesting(SK_ColorBLACK
));
29 TEST_F(BreakListTest
, ApplyValue
) {
30 BreakList
<bool> breaks(false);
31 const size_t max
= 99;
34 // Ensure ApplyValue is a no-op on invalid and empty ranges.
35 breaks
.ApplyValue(true, Range::InvalidRange());
36 EXPECT_TRUE(breaks
.EqualsValueForTesting(false));
37 for (size_t i
= 0; i
< 3; ++i
) {
38 breaks
.ApplyValue(true, Range(i
, i
));
39 EXPECT_TRUE(breaks
.EqualsValueForTesting(false));
42 // Apply a value to a valid range, check breaks; repeating should be no-op.
43 std::vector
<std::pair
<size_t, bool> > expected
;
44 expected
.push_back(std::pair
<size_t, bool>(0, false));
45 expected
.push_back(std::pair
<size_t, bool>(2, true));
46 expected
.push_back(std::pair
<size_t, bool>(3, false));
47 for (size_t i
= 0; i
< 2; ++i
) {
48 breaks
.ApplyValue(true, Range(2, 3));
49 EXPECT_TRUE(breaks
.EqualsForTesting(expected
));
52 // Ensure setting a value overrides the ranged value.
53 breaks
.SetValue(true);
54 EXPECT_TRUE(breaks
.EqualsValueForTesting(true));
56 // Ensure applying a value over [0, |max|) is the same as setting a value.
57 breaks
.ApplyValue(false, Range(0, max
));
58 EXPECT_TRUE(breaks
.EqualsValueForTesting(false));
60 // Ensure applying a value that is already applied has no effect.
61 breaks
.ApplyValue(false, Range(0, 2));
62 breaks
.ApplyValue(false, Range(3, 6));
63 breaks
.ApplyValue(false, Range(7, max
));
64 EXPECT_TRUE(breaks
.EqualsValueForTesting(false));
66 // Ensure applying an identical neighboring value merges the ranges.
67 breaks
.ApplyValue(true, Range(0, 3));
68 breaks
.ApplyValue(true, Range(3, 6));
69 breaks
.ApplyValue(true, Range(6, max
));
70 EXPECT_TRUE(breaks
.EqualsValueForTesting(true));
72 // Ensure applying a value with the same range overrides the ranged value.
73 breaks
.ApplyValue(false, Range(2, 3));
74 breaks
.ApplyValue(true, Range(2, 3));
75 EXPECT_TRUE(breaks
.EqualsValueForTesting(true));
77 // Ensure applying a value with a containing range overrides contained values.
78 breaks
.ApplyValue(false, Range(0, 1));
79 breaks
.ApplyValue(false, Range(2, 3));
80 breaks
.ApplyValue(true, Range(0, 3));
81 EXPECT_TRUE(breaks
.EqualsValueForTesting(true));
82 breaks
.ApplyValue(false, Range(4, 5));
83 breaks
.ApplyValue(false, Range(6, 7));
84 breaks
.ApplyValue(false, Range(8, 9));
85 breaks
.ApplyValue(true, Range(4, 9));
86 EXPECT_TRUE(breaks
.EqualsValueForTesting(true));
88 // Ensure applying various overlapping values yields the intended results.
89 breaks
.ApplyValue(false, Range(1, 4));
90 breaks
.ApplyValue(false, Range(5, 8));
91 breaks
.ApplyValue(true, Range(0, 2));
92 breaks
.ApplyValue(true, Range(3, 6));
93 breaks
.ApplyValue(true, Range(7, max
));
94 std::vector
<std::pair
<size_t, bool> > overlap
;
95 overlap
.push_back(std::pair
<size_t, bool>(0, true));
96 overlap
.push_back(std::pair
<size_t, bool>(2, false));
97 overlap
.push_back(std::pair
<size_t, bool>(3, true));
98 overlap
.push_back(std::pair
<size_t, bool>(6, false));
99 overlap
.push_back(std::pair
<size_t, bool>(7, true));
100 EXPECT_TRUE(breaks
.EqualsForTesting(overlap
));
103 TEST_F(BreakListTest
, SetMax
) {
104 // Ensure values adjust to accommodate max position changes.
105 BreakList
<bool> breaks(false);
107 breaks
.ApplyValue(true, Range(0, 2));
108 breaks
.ApplyValue(true, Range(3, 6));
109 breaks
.ApplyValue(true, Range(7, 9));
111 std::vector
<std::pair
<size_t, bool> > expected
;
112 expected
.push_back(std::pair
<size_t, bool>(0, true));
113 expected
.push_back(std::pair
<size_t, bool>(2, false));
114 expected
.push_back(std::pair
<size_t, bool>(3, true));
115 expected
.push_back(std::pair
<size_t, bool>(6, false));
116 expected
.push_back(std::pair
<size_t, bool>(7, true));
117 EXPECT_TRUE(breaks
.EqualsForTesting(expected
));
119 // Setting a smaller max should remove any corresponding breaks.
122 EXPECT_TRUE(breaks
.EqualsForTesting(expected
));
125 EXPECT_TRUE(breaks
.EqualsForTesting(expected
));
127 EXPECT_TRUE(breaks
.EqualsForTesting(expected
));
129 // Setting a larger max should not change any breaks.
131 EXPECT_TRUE(breaks
.EqualsForTesting(expected
));
134 TEST_F(BreakListTest
, GetBreakAndRange
) {
135 BreakList
<bool> breaks(false);
137 breaks
.ApplyValue(true, Range(1, 2));
138 breaks
.ApplyValue(true, Range(4, 6));
145 { 0, 0, Range(0, 1) },
146 { 1, 1, Range(1, 2) },
147 { 2, 2, Range(2, 4) },
148 { 3, 2, Range(2, 4) },
149 { 4, 3, Range(4, 6) },
150 { 5, 3, Range(4, 6) },
151 { 6, 4, Range(6, 8) },
152 { 7, 4, Range(6, 8) },
153 // Positions at or beyond the max simply return the last break and range.
154 { 8, 4, Range(6, 8) },
155 { 9, 4, Range(6, 8) },
159 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
160 BreakList
<bool>::const_iterator it
= breaks
.GetBreak(cases
[i
].position
);
161 EXPECT_EQ(breaks
.breaks()[cases
[i
].break_index
], *it
);
162 EXPECT_EQ(breaks
.GetRange(it
), cases
[i
].range
);