blob: e84d80ef1c5481673292ffd049b6acb82e508bd8 [file] [log] [blame]
[email protected]ccfa43f02013-02-01 04:42:171// 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.
4
5#include "ui/gfx/break_list.h"
6
avic89eb8d42015-12-23 08:08:187#include <stddef.h>
8
9#include "base/macros.h"
[email protected]ccfa43f02013-02-01 04:42:1710#include "testing/gtest/include/gtest/gtest.h"
11#include "third_party/skia/include/core/SkColor.h"
[email protected]db4fc1e22013-09-06 20:01:5112#include "ui/gfx/range/range.h"
[email protected]ccfa43f02013-02-01 04:42:1713
14namespace gfx {
15
16class BreakListTest : public testing::Test {};
17
18TEST_F(BreakListTest, SetValue) {
19 // Check the default values applied to new instances.
20 BreakList<bool> style_breaks(false);
21 EXPECT_TRUE(style_breaks.EqualsValueForTesting(false));
22 style_breaks.SetValue(true);
23 EXPECT_TRUE(style_breaks.EqualsValueForTesting(true));
24
25 // Ensure that setting values works correctly.
26 BreakList<SkColor> color_breaks(SK_ColorRED);
27 EXPECT_TRUE(color_breaks.EqualsValueForTesting(SK_ColorRED));
28 color_breaks.SetValue(SK_ColorBLACK);
29 EXPECT_TRUE(color_breaks.EqualsValueForTesting(SK_ColorBLACK));
30}
31
32TEST_F(BreakListTest, ApplyValue) {
33 BreakList<bool> breaks(false);
34 const size_t max = 99;
35 breaks.SetMax(max);
36
37 // Ensure ApplyValue is a no-op on invalid and empty ranges.
[email protected]3dfe5c52013-09-18 22:01:2238 breaks.ApplyValue(true, Range::InvalidRange());
[email protected]ccfa43f02013-02-01 04:42:1739 EXPECT_TRUE(breaks.EqualsValueForTesting(false));
40 for (size_t i = 0; i < 3; ++i) {
[email protected]3dfe5c52013-09-18 22:01:2241 breaks.ApplyValue(true, Range(i, i));
[email protected]ccfa43f02013-02-01 04:42:1742 EXPECT_TRUE(breaks.EqualsValueForTesting(false));
43 }
44
45 // Apply a value to a valid range, check breaks; repeating should be no-op.
46 std::vector<std::pair<size_t, bool> > expected;
47 expected.push_back(std::pair<size_t, bool>(0, false));
48 expected.push_back(std::pair<size_t, bool>(2, true));
49 expected.push_back(std::pair<size_t, bool>(3, false));
50 for (size_t i = 0; i < 2; ++i) {
[email protected]3dfe5c52013-09-18 22:01:2251 breaks.ApplyValue(true, Range(2, 3));
[email protected]ccfa43f02013-02-01 04:42:1752 EXPECT_TRUE(breaks.EqualsForTesting(expected));
53 }
54
55 // Ensure setting a value overrides the ranged value.
56 breaks.SetValue(true);
57 EXPECT_TRUE(breaks.EqualsValueForTesting(true));
58
59 // Ensure applying a value over [0, |max|) is the same as setting a value.
[email protected]3dfe5c52013-09-18 22:01:2260 breaks.ApplyValue(false, Range(0, max));
[email protected]ccfa43f02013-02-01 04:42:1761 EXPECT_TRUE(breaks.EqualsValueForTesting(false));
62
63 // Ensure applying a value that is already applied has no effect.
[email protected]3dfe5c52013-09-18 22:01:2264 breaks.ApplyValue(false, Range(0, 2));
65 breaks.ApplyValue(false, Range(3, 6));
66 breaks.ApplyValue(false, Range(7, max));
[email protected]ccfa43f02013-02-01 04:42:1767 EXPECT_TRUE(breaks.EqualsValueForTesting(false));
68
69 // Ensure applying an identical neighboring value merges the ranges.
[email protected]3dfe5c52013-09-18 22:01:2270 breaks.ApplyValue(true, Range(0, 3));
71 breaks.ApplyValue(true, Range(3, 6));
72 breaks.ApplyValue(true, Range(6, max));
[email protected]ccfa43f02013-02-01 04:42:1773 EXPECT_TRUE(breaks.EqualsValueForTesting(true));
74
75 // Ensure applying a value with the same range overrides the ranged value.
[email protected]3dfe5c52013-09-18 22:01:2276 breaks.ApplyValue(false, Range(2, 3));
77 breaks.ApplyValue(true, Range(2, 3));
[email protected]ccfa43f02013-02-01 04:42:1778 EXPECT_TRUE(breaks.EqualsValueForTesting(true));
79
80 // Ensure applying a value with a containing range overrides contained values.
[email protected]3dfe5c52013-09-18 22:01:2281 breaks.ApplyValue(false, Range(0, 1));
82 breaks.ApplyValue(false, Range(2, 3));
83 breaks.ApplyValue(true, Range(0, 3));
[email protected]ccfa43f02013-02-01 04:42:1784 EXPECT_TRUE(breaks.EqualsValueForTesting(true));
[email protected]3dfe5c52013-09-18 22:01:2285 breaks.ApplyValue(false, Range(4, 5));
86 breaks.ApplyValue(false, Range(6, 7));
87 breaks.ApplyValue(false, Range(8, 9));
88 breaks.ApplyValue(true, Range(4, 9));
[email protected]ccfa43f02013-02-01 04:42:1789 EXPECT_TRUE(breaks.EqualsValueForTesting(true));
90
91 // Ensure applying various overlapping values yields the intended results.
[email protected]3dfe5c52013-09-18 22:01:2292 breaks.ApplyValue(false, Range(1, 4));
93 breaks.ApplyValue(false, Range(5, 8));
94 breaks.ApplyValue(true, Range(0, 2));
95 breaks.ApplyValue(true, Range(3, 6));
96 breaks.ApplyValue(true, Range(7, max));
[email protected]ccfa43f02013-02-01 04:42:1797 std::vector<std::pair<size_t, bool> > overlap;
98 overlap.push_back(std::pair<size_t, bool>(0, true));
99 overlap.push_back(std::pair<size_t, bool>(2, false));
100 overlap.push_back(std::pair<size_t, bool>(3, true));
101 overlap.push_back(std::pair<size_t, bool>(6, false));
102 overlap.push_back(std::pair<size_t, bool>(7, true));
103 EXPECT_TRUE(breaks.EqualsForTesting(overlap));
104}
105
106TEST_F(BreakListTest, SetMax) {
107 // Ensure values adjust to accommodate max position changes.
108 BreakList<bool> breaks(false);
109 breaks.SetMax(9);
[email protected]3dfe5c52013-09-18 22:01:22110 breaks.ApplyValue(true, Range(0, 2));
111 breaks.ApplyValue(true, Range(3, 6));
112 breaks.ApplyValue(true, Range(7, 9));
[email protected]ccfa43f02013-02-01 04:42:17113
114 std::vector<std::pair<size_t, bool> > expected;
115 expected.push_back(std::pair<size_t, bool>(0, true));
116 expected.push_back(std::pair<size_t, bool>(2, false));
117 expected.push_back(std::pair<size_t, bool>(3, true));
118 expected.push_back(std::pair<size_t, bool>(6, false));
119 expected.push_back(std::pair<size_t, bool>(7, true));
120 EXPECT_TRUE(breaks.EqualsForTesting(expected));
121
122 // Setting a smaller max should remove any corresponding breaks.
123 breaks.SetMax(7);
124 expected.resize(4);
125 EXPECT_TRUE(breaks.EqualsForTesting(expected));
126 breaks.SetMax(4);
127 expected.resize(3);
128 EXPECT_TRUE(breaks.EqualsForTesting(expected));
129 breaks.SetMax(4);
130 EXPECT_TRUE(breaks.EqualsForTesting(expected));
131
132 // Setting a larger max should not change any breaks.
133 breaks.SetMax(50);
134 EXPECT_TRUE(breaks.EqualsForTesting(expected));
135}
136
137TEST_F(BreakListTest, GetBreakAndRange) {
138 BreakList<bool> breaks(false);
139 breaks.SetMax(8);
[email protected]3dfe5c52013-09-18 22:01:22140 breaks.ApplyValue(true, Range(1, 2));
141 breaks.ApplyValue(true, Range(4, 6));
[email protected]ccfa43f02013-02-01 04:42:17142
143 struct {
144 size_t position;
145 size_t break_index;
[email protected]3dfe5c52013-09-18 22:01:22146 Range range;
[email protected]ccfa43f02013-02-01 04:42:17147 } cases[] = {
[email protected]3dfe5c52013-09-18 22:01:22148 { 0, 0, Range(0, 1) },
149 { 1, 1, Range(1, 2) },
150 { 2, 2, Range(2, 4) },
151 { 3, 2, Range(2, 4) },
152 { 4, 3, Range(4, 6) },
153 { 5, 3, Range(4, 6) },
154 { 6, 4, Range(6, 8) },
155 { 7, 4, Range(6, 8) },
[email protected]ccfa43f02013-02-01 04:42:17156 // Positions at or beyond the max simply return the last break and range.
[email protected]3dfe5c52013-09-18 22:01:22157 { 8, 4, Range(6, 8) },
158 { 9, 4, Range(6, 8) },
[email protected]ccfa43f02013-02-01 04:42:17159 };
160
161
viettrungluua5ca99b2014-10-16 16:28:48162 for (size_t i = 0; i < arraysize(cases); ++i) {
[email protected]ccfa43f02013-02-01 04:42:17163 BreakList<bool>::const_iterator it = breaks.GetBreak(cases[i].position);
164 EXPECT_EQ(breaks.breaks()[cases[i].break_index], *it);
165 EXPECT_EQ(breaks.GetRange(it), cases[i].range);
166 }
167}
168
169} // namespace gfx