| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/gfx/break_list.h" | 5 #include "ui/gfx/break_list.h" |
| 6 | 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "third_party/skia/include/core/SkColor.h" | 8 #include "third_party/skia/include/core/SkColor.h" |
| 9 #include "ui/base/range/range.h" | 9 #include "ui/gfx/range/range.h" |
| 10 | 10 |
| 11 namespace gfx { | 11 namespace gfx { |
| 12 | 12 |
| 13 class BreakListTest : public testing::Test {}; | 13 class BreakListTest : public testing::Test {}; |
| 14 | 14 |
| 15 TEST_F(BreakListTest, SetValue) { | 15 TEST_F(BreakListTest, SetValue) { |
| 16 // Check the default values applied to new instances. | 16 // Check the default values applied to new instances. |
| 17 BreakList<bool> style_breaks(false); | 17 BreakList<bool> style_breaks(false); |
| 18 EXPECT_TRUE(style_breaks.EqualsValueForTesting(false)); | 18 EXPECT_TRUE(style_breaks.EqualsValueForTesting(false)); |
| 19 style_breaks.SetValue(true); | 19 style_breaks.SetValue(true); |
| 20 EXPECT_TRUE(style_breaks.EqualsValueForTesting(true)); | 20 EXPECT_TRUE(style_breaks.EqualsValueForTesting(true)); |
| 21 | 21 |
| 22 // Ensure that setting values works correctly. | 22 // Ensure that setting values works correctly. |
| 23 BreakList<SkColor> color_breaks(SK_ColorRED); | 23 BreakList<SkColor> color_breaks(SK_ColorRED); |
| 24 EXPECT_TRUE(color_breaks.EqualsValueForTesting(SK_ColorRED)); | 24 EXPECT_TRUE(color_breaks.EqualsValueForTesting(SK_ColorRED)); |
| 25 color_breaks.SetValue(SK_ColorBLACK); | 25 color_breaks.SetValue(SK_ColorBLACK); |
| 26 EXPECT_TRUE(color_breaks.EqualsValueForTesting(SK_ColorBLACK)); | 26 EXPECT_TRUE(color_breaks.EqualsValueForTesting(SK_ColorBLACK)); |
| 27 } | 27 } |
| 28 | 28 |
| 29 TEST_F(BreakListTest, ApplyValue) { | 29 TEST_F(BreakListTest, ApplyValue) { |
| 30 BreakList<bool> breaks(false); | 30 BreakList<bool> breaks(false); |
| 31 const size_t max = 99; | 31 const size_t max = 99; |
| 32 breaks.SetMax(max); | 32 breaks.SetMax(max); |
| 33 | 33 |
| 34 // Ensure ApplyValue is a no-op on invalid and empty ranges. | 34 // Ensure ApplyValue is a no-op on invalid and empty ranges. |
| 35 breaks.ApplyValue(true, ui::Range::InvalidRange()); | 35 breaks.ApplyValue(true, gfx::Range::InvalidRange()); |
| 36 EXPECT_TRUE(breaks.EqualsValueForTesting(false)); | 36 EXPECT_TRUE(breaks.EqualsValueForTesting(false)); |
| 37 for (size_t i = 0; i < 3; ++i) { | 37 for (size_t i = 0; i < 3; ++i) { |
| 38 breaks.ApplyValue(true, ui::Range(i, i)); | 38 breaks.ApplyValue(true, gfx::Range(i, i)); |
| 39 EXPECT_TRUE(breaks.EqualsValueForTesting(false)); | 39 EXPECT_TRUE(breaks.EqualsValueForTesting(false)); |
| 40 } | 40 } |
| 41 | 41 |
| 42 // Apply a value to a valid range, check breaks; repeating should be no-op. | 42 // Apply a value to a valid range, check breaks; repeating should be no-op. |
| 43 std::vector<std::pair<size_t, bool> > expected; | 43 std::vector<std::pair<size_t, bool> > expected; |
| 44 expected.push_back(std::pair<size_t, bool>(0, false)); | 44 expected.push_back(std::pair<size_t, bool>(0, false)); |
| 45 expected.push_back(std::pair<size_t, bool>(2, true)); | 45 expected.push_back(std::pair<size_t, bool>(2, true)); |
| 46 expected.push_back(std::pair<size_t, bool>(3, false)); | 46 expected.push_back(std::pair<size_t, bool>(3, false)); |
| 47 for (size_t i = 0; i < 2; ++i) { | 47 for (size_t i = 0; i < 2; ++i) { |
| 48 breaks.ApplyValue(true, ui::Range(2, 3)); | 48 breaks.ApplyValue(true, gfx::Range(2, 3)); |
| 49 EXPECT_TRUE(breaks.EqualsForTesting(expected)); | 49 EXPECT_TRUE(breaks.EqualsForTesting(expected)); |
| 50 } | 50 } |
| 51 | 51 |
| 52 // Ensure setting a value overrides the ranged value. | 52 // Ensure setting a value overrides the ranged value. |
| 53 breaks.SetValue(true); | 53 breaks.SetValue(true); |
| 54 EXPECT_TRUE(breaks.EqualsValueForTesting(true)); | 54 EXPECT_TRUE(breaks.EqualsValueForTesting(true)); |
| 55 | 55 |
| 56 // Ensure applying a value over [0, |max|) is the same as setting a value. | 56 // Ensure applying a value over [0, |max|) is the same as setting a value. |
| 57 breaks.ApplyValue(false, ui::Range(0, max)); | 57 breaks.ApplyValue(false, gfx::Range(0, max)); |
| 58 EXPECT_TRUE(breaks.EqualsValueForTesting(false)); | 58 EXPECT_TRUE(breaks.EqualsValueForTesting(false)); |
| 59 | 59 |
| 60 // Ensure applying a value that is already applied has no effect. | 60 // Ensure applying a value that is already applied has no effect. |
| 61 breaks.ApplyValue(false, ui::Range(0, 2)); | 61 breaks.ApplyValue(false, gfx::Range(0, 2)); |
| 62 breaks.ApplyValue(false, ui::Range(3, 6)); | 62 breaks.ApplyValue(false, gfx::Range(3, 6)); |
| 63 breaks.ApplyValue(false, ui::Range(7, max)); | 63 breaks.ApplyValue(false, gfx::Range(7, max)); |
| 64 EXPECT_TRUE(breaks.EqualsValueForTesting(false)); | 64 EXPECT_TRUE(breaks.EqualsValueForTesting(false)); |
| 65 | 65 |
| 66 // Ensure applying an identical neighboring value merges the ranges. | 66 // Ensure applying an identical neighboring value merges the ranges. |
| 67 breaks.ApplyValue(true, ui::Range(0, 3)); | 67 breaks.ApplyValue(true, gfx::Range(0, 3)); |
| 68 breaks.ApplyValue(true, ui::Range(3, 6)); | 68 breaks.ApplyValue(true, gfx::Range(3, 6)); |
| 69 breaks.ApplyValue(true, ui::Range(6, max)); | 69 breaks.ApplyValue(true, gfx::Range(6, max)); |
| 70 EXPECT_TRUE(breaks.EqualsValueForTesting(true)); | 70 EXPECT_TRUE(breaks.EqualsValueForTesting(true)); |
| 71 | 71 |
| 72 // Ensure applying a value with the same range overrides the ranged value. | 72 // Ensure applying a value with the same range overrides the ranged value. |
| 73 breaks.ApplyValue(false, ui::Range(2, 3)); | 73 breaks.ApplyValue(false, gfx::Range(2, 3)); |
| 74 breaks.ApplyValue(true, ui::Range(2, 3)); | 74 breaks.ApplyValue(true, gfx::Range(2, 3)); |
| 75 EXPECT_TRUE(breaks.EqualsValueForTesting(true)); | 75 EXPECT_TRUE(breaks.EqualsValueForTesting(true)); |
| 76 | 76 |
| 77 // Ensure applying a value with a containing range overrides contained values. | 77 // Ensure applying a value with a containing range overrides contained values. |
| 78 breaks.ApplyValue(false, ui::Range(0, 1)); | 78 breaks.ApplyValue(false, gfx::Range(0, 1)); |
| 79 breaks.ApplyValue(false, ui::Range(2, 3)); | 79 breaks.ApplyValue(false, gfx::Range(2, 3)); |
| 80 breaks.ApplyValue(true, ui::Range(0, 3)); | 80 breaks.ApplyValue(true, gfx::Range(0, 3)); |
| 81 EXPECT_TRUE(breaks.EqualsValueForTesting(true)); | 81 EXPECT_TRUE(breaks.EqualsValueForTesting(true)); |
| 82 breaks.ApplyValue(false, ui::Range(4, 5)); | 82 breaks.ApplyValue(false, gfx::Range(4, 5)); |
| 83 breaks.ApplyValue(false, ui::Range(6, 7)); | 83 breaks.ApplyValue(false, gfx::Range(6, 7)); |
| 84 breaks.ApplyValue(false, ui::Range(8, 9)); | 84 breaks.ApplyValue(false, gfx::Range(8, 9)); |
| 85 breaks.ApplyValue(true, ui::Range(4, 9)); | 85 breaks.ApplyValue(true, gfx::Range(4, 9)); |
| 86 EXPECT_TRUE(breaks.EqualsValueForTesting(true)); | 86 EXPECT_TRUE(breaks.EqualsValueForTesting(true)); |
| 87 | 87 |
| 88 // Ensure applying various overlapping values yields the intended results. | 88 // Ensure applying various overlapping values yields the intended results. |
| 89 breaks.ApplyValue(false, ui::Range(1, 4)); | 89 breaks.ApplyValue(false, gfx::Range(1, 4)); |
| 90 breaks.ApplyValue(false, ui::Range(5, 8)); | 90 breaks.ApplyValue(false, gfx::Range(5, 8)); |
| 91 breaks.ApplyValue(true, ui::Range(0, 2)); | 91 breaks.ApplyValue(true, gfx::Range(0, 2)); |
| 92 breaks.ApplyValue(true, ui::Range(3, 6)); | 92 breaks.ApplyValue(true, gfx::Range(3, 6)); |
| 93 breaks.ApplyValue(true, ui::Range(7, max)); | 93 breaks.ApplyValue(true, gfx::Range(7, max)); |
| 94 std::vector<std::pair<size_t, bool> > overlap; | 94 std::vector<std::pair<size_t, bool> > overlap; |
| 95 overlap.push_back(std::pair<size_t, bool>(0, true)); | 95 overlap.push_back(std::pair<size_t, bool>(0, true)); |
| 96 overlap.push_back(std::pair<size_t, bool>(2, false)); | 96 overlap.push_back(std::pair<size_t, bool>(2, false)); |
| 97 overlap.push_back(std::pair<size_t, bool>(3, true)); | 97 overlap.push_back(std::pair<size_t, bool>(3, true)); |
| 98 overlap.push_back(std::pair<size_t, bool>(6, false)); | 98 overlap.push_back(std::pair<size_t, bool>(6, false)); |
| 99 overlap.push_back(std::pair<size_t, bool>(7, true)); | 99 overlap.push_back(std::pair<size_t, bool>(7, true)); |
| 100 EXPECT_TRUE(breaks.EqualsForTesting(overlap)); | 100 EXPECT_TRUE(breaks.EqualsForTesting(overlap)); |
| 101 } | 101 } |
| 102 | 102 |
| 103 TEST_F(BreakListTest, SetMax) { | 103 TEST_F(BreakListTest, SetMax) { |
| 104 // Ensure values adjust to accommodate max position changes. | 104 // Ensure values adjust to accommodate max position changes. |
| 105 BreakList<bool> breaks(false); | 105 BreakList<bool> breaks(false); |
| 106 breaks.SetMax(9); | 106 breaks.SetMax(9); |
| 107 breaks.ApplyValue(true, ui::Range(0, 2)); | 107 breaks.ApplyValue(true, gfx::Range(0, 2)); |
| 108 breaks.ApplyValue(true, ui::Range(3, 6)); | 108 breaks.ApplyValue(true, gfx::Range(3, 6)); |
| 109 breaks.ApplyValue(true, ui::Range(7, 9)); | 109 breaks.ApplyValue(true, gfx::Range(7, 9)); |
| 110 | 110 |
| 111 std::vector<std::pair<size_t, bool> > expected; | 111 std::vector<std::pair<size_t, bool> > expected; |
| 112 expected.push_back(std::pair<size_t, bool>(0, true)); | 112 expected.push_back(std::pair<size_t, bool>(0, true)); |
| 113 expected.push_back(std::pair<size_t, bool>(2, false)); | 113 expected.push_back(std::pair<size_t, bool>(2, false)); |
| 114 expected.push_back(std::pair<size_t, bool>(3, true)); | 114 expected.push_back(std::pair<size_t, bool>(3, true)); |
| 115 expected.push_back(std::pair<size_t, bool>(6, false)); | 115 expected.push_back(std::pair<size_t, bool>(6, false)); |
| 116 expected.push_back(std::pair<size_t, bool>(7, true)); | 116 expected.push_back(std::pair<size_t, bool>(7, true)); |
| 117 EXPECT_TRUE(breaks.EqualsForTesting(expected)); | 117 EXPECT_TRUE(breaks.EqualsForTesting(expected)); |
| 118 | 118 |
| 119 // Setting a smaller max should remove any corresponding breaks. | 119 // Setting a smaller max should remove any corresponding breaks. |
| 120 breaks.SetMax(7); | 120 breaks.SetMax(7); |
| 121 expected.resize(4); | 121 expected.resize(4); |
| 122 EXPECT_TRUE(breaks.EqualsForTesting(expected)); | 122 EXPECT_TRUE(breaks.EqualsForTesting(expected)); |
| 123 breaks.SetMax(4); | 123 breaks.SetMax(4); |
| 124 expected.resize(3); | 124 expected.resize(3); |
| 125 EXPECT_TRUE(breaks.EqualsForTesting(expected)); | 125 EXPECT_TRUE(breaks.EqualsForTesting(expected)); |
| 126 breaks.SetMax(4); | 126 breaks.SetMax(4); |
| 127 EXPECT_TRUE(breaks.EqualsForTesting(expected)); | 127 EXPECT_TRUE(breaks.EqualsForTesting(expected)); |
| 128 | 128 |
| 129 // Setting a larger max should not change any breaks. | 129 // Setting a larger max should not change any breaks. |
| 130 breaks.SetMax(50); | 130 breaks.SetMax(50); |
| 131 EXPECT_TRUE(breaks.EqualsForTesting(expected)); | 131 EXPECT_TRUE(breaks.EqualsForTesting(expected)); |
| 132 } | 132 } |
| 133 | 133 |
| 134 TEST_F(BreakListTest, GetBreakAndRange) { | 134 TEST_F(BreakListTest, GetBreakAndRange) { |
| 135 BreakList<bool> breaks(false); | 135 BreakList<bool> breaks(false); |
| 136 breaks.SetMax(8); | 136 breaks.SetMax(8); |
| 137 breaks.ApplyValue(true, ui::Range(1, 2)); | 137 breaks.ApplyValue(true, gfx::Range(1, 2)); |
| 138 breaks.ApplyValue(true, ui::Range(4, 6)); | 138 breaks.ApplyValue(true, gfx::Range(4, 6)); |
| 139 | 139 |
| 140 struct { | 140 struct { |
| 141 size_t position; | 141 size_t position; |
| 142 size_t break_index; | 142 size_t break_index; |
| 143 ui::Range range; | 143 gfx::Range range; |
| 144 } cases[] = { | 144 } cases[] = { |
| 145 { 0, 0, ui::Range(0, 1) }, | 145 { 0, 0, gfx::Range(0, 1) }, |
| 146 { 1, 1, ui::Range(1, 2) }, | 146 { 1, 1, gfx::Range(1, 2) }, |
| 147 { 2, 2, ui::Range(2, 4) }, | 147 { 2, 2, gfx::Range(2, 4) }, |
| 148 { 3, 2, ui::Range(2, 4) }, | 148 { 3, 2, gfx::Range(2, 4) }, |
| 149 { 4, 3, ui::Range(4, 6) }, | 149 { 4, 3, gfx::Range(4, 6) }, |
| 150 { 5, 3, ui::Range(4, 6) }, | 150 { 5, 3, gfx::Range(4, 6) }, |
| 151 { 6, 4, ui::Range(6, 8) }, | 151 { 6, 4, gfx::Range(6, 8) }, |
| 152 { 7, 4, ui::Range(6, 8) }, | 152 { 7, 4, gfx::Range(6, 8) }, |
| 153 // Positions at or beyond the max simply return the last break and range. | 153 // Positions at or beyond the max simply return the last break and range. |
| 154 { 8, 4, ui::Range(6, 8) }, | 154 { 8, 4, gfx::Range(6, 8) }, |
| 155 { 9, 4, ui::Range(6, 8) }, | 155 { 9, 4, gfx::Range(6, 8) }, |
| 156 }; | 156 }; |
| 157 | 157 |
| 158 | 158 |
| 159 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { | 159 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 160 BreakList<bool>::const_iterator it = breaks.GetBreak(cases[i].position); | 160 BreakList<bool>::const_iterator it = breaks.GetBreak(cases[i].position); |
| 161 EXPECT_EQ(breaks.breaks()[cases[i].break_index], *it); | 161 EXPECT_EQ(breaks.breaks()[cases[i].break_index], *it); |
| 162 EXPECT_EQ(breaks.GetRange(it), cases[i].range); | 162 EXPECT_EQ(breaks.GetRange(it), cases[i].range); |
| 163 } | 163 } |
| 164 } | 164 } |
| 165 | 165 |
| 166 } // namespace gfx | 166 } // namespace gfx |
| OLD | NEW |