|
| 1 | +// Copyright 2013 The Flutter 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 "flutter/display_list/display_list_attributes_testing.h" |
| 6 | +#include "flutter/display_list/display_list_builder.h" |
| 7 | +#include "flutter/display_list/display_list_comparable.h" |
| 8 | +#include "flutter/display_list/types.h" |
| 9 | +#include "gtest/gtest.h" |
| 10 | +#include "include/core/SkPath.h" |
| 11 | + |
| 12 | +namespace flutter { |
| 13 | +namespace testing { |
| 14 | + |
| 15 | +const SkScalar TestDashes1[] = {4.0, 2.0}; |
| 16 | +const SkScalar TestDashes2[] = {1.0, 1.5}; |
| 17 | + |
| 18 | +TEST(DisplayListPathEffect, BuilderSetGet) { |
| 19 | + DlDashPathEffect dash_path_effect(TestDashes1, 2, 0.0); |
| 20 | + DisplayListBuilder builder; |
| 21 | + ASSERT_EQ(builder.getPathEffect(), nullptr); |
| 22 | + builder.setPathEffect(&dash_path_effect); |
| 23 | + ASSERT_NE(builder.getPathEffect(), nullptr); |
| 24 | + ASSERT_TRUE(Equals(builder.getPathEffect(), |
| 25 | + static_cast<DlPathEffect*>(&dash_path_effect))); |
| 26 | + builder.setPathEffect(nullptr); |
| 27 | + ASSERT_EQ(builder.getPathEffect(), nullptr); |
| 28 | +} |
| 29 | + |
| 30 | +TEST(DisplayListPathEffect, FromSkiaNullPathEffect) { |
| 31 | + std::shared_ptr<DlPathEffect> path_effect = DlPathEffect::From(nullptr); |
| 32 | + ASSERT_EQ(path_effect, nullptr); |
| 33 | + ASSERT_EQ(path_effect.get(), nullptr); |
| 34 | +} |
| 35 | + |
| 36 | +TEST(DisplayListPathEffect, FromSkiaPathEffect) { |
| 37 | + sk_sp<SkPathEffect> sk_path_effect = |
| 38 | + SkDashPathEffect::Make(TestDashes2, 2, 0.0); |
| 39 | + std::shared_ptr<DlPathEffect> dl_path_effect = |
| 40 | + DlPathEffect::From(sk_path_effect); |
| 41 | + |
| 42 | + ASSERT_EQ(dl_path_effect->type(), DlPathEffectType::kUnknown); |
| 43 | + // We cannot recapture the dash parameters from an SkDashPathEffect |
| 44 | + ASSERT_EQ(dl_path_effect->asDash(), nullptr); |
| 45 | + ASSERT_EQ(dl_path_effect->skia_object(), sk_path_effect); |
| 46 | +} |
| 47 | + |
| 48 | +TEST(DisplayListPathEffect, EffectShared) { |
| 49 | + DlDashPathEffect effect(TestDashes2, 2, 0.0); |
| 50 | + ASSERT_NE(effect.shared().get(), &effect); |
| 51 | + ASSERT_EQ(*effect.shared(), effect); |
| 52 | +} |
| 53 | + |
| 54 | +TEST(DisplayListPathEffect, DashEffectAsDash) { |
| 55 | + DlDashPathEffect effect(TestDashes2, 2, 0.0); |
| 56 | + ASSERT_NE(effect.asDash(), nullptr); |
| 57 | + ASSERT_EQ(effect.asDash(), &effect); |
| 58 | +} |
| 59 | + |
| 60 | +TEST(DisplayListPathEffect, DashEffectEquals) { |
| 61 | + DlDashPathEffect effect1(TestDashes2, 2, 0.0); |
| 62 | + DlDashPathEffect effect2(TestDashes2, 2, 0.0); |
| 63 | + TestEquals(effect1, effect2); |
| 64 | +} |
| 65 | + |
| 66 | +TEST(DisplayListPathEffect, BlurNotEquals) { |
| 67 | + DlDashPathEffect effect1(TestDashes1, 2, 0.0); |
| 68 | + DlDashPathEffect effect2(TestDashes2, 2, 0.0); |
| 69 | + DlDashPathEffect effect3(TestDashes2, 3, 0.0); |
| 70 | + TestNotEquals(effect1, effect2, "intervals differs"); |
| 71 | + TestNotEquals(effect2, effect3, "phase differs"); |
| 72 | +} |
| 73 | + |
| 74 | +TEST(DisplayListPathEffect, UnknownConstructor) { |
| 75 | + DlUnknownPathEffect path_effect(SkDashPathEffect::Make(TestDashes1, 2, 0.0)); |
| 76 | +} |
| 77 | + |
| 78 | +TEST(DisplayListPathEffect, UnknownShared) { |
| 79 | + DlUnknownPathEffect path_effect(SkDashPathEffect::Make(TestDashes1, 2, 0.0)); |
| 80 | + ASSERT_NE(path_effect.shared().get(), &path_effect); |
| 81 | + ASSERT_EQ(*path_effect.shared(), path_effect); |
| 82 | +} |
| 83 | + |
| 84 | +TEST(DisplayListPathEffect, UnknownContents) { |
| 85 | + sk_sp<SkPathEffect> sk_effect = SkDashPathEffect::Make(TestDashes1, 2, 0.0); |
| 86 | + DlUnknownPathEffect effect(sk_effect); |
| 87 | + ASSERT_EQ(effect.skia_object(), sk_effect); |
| 88 | + ASSERT_EQ(effect.skia_object().get(), sk_effect.get()); |
| 89 | +} |
| 90 | + |
| 91 | +TEST(DisplayListPathEffect, UnknownEquals) { |
| 92 | + sk_sp<SkPathEffect> sk_effect = SkDashPathEffect::Make(TestDashes1, 2, 0.0); |
| 93 | + DlUnknownPathEffect effect1(sk_effect); |
| 94 | + DlUnknownPathEffect effect2(sk_effect); |
| 95 | + TestEquals(effect1, effect1); |
| 96 | +} |
| 97 | + |
| 98 | +TEST(DisplayListPathEffect, UnknownNotEquals) { |
| 99 | + // Even though the effect is the same, it is a different instance |
| 100 | + // and we cannot currently tell them apart because the Skia |
| 101 | + // DashEffect::Make objects do not implement == |
| 102 | + DlUnknownPathEffect path_effect1(SkDashPathEffect::Make(TestDashes1, 2, 0.0)); |
| 103 | + DlUnknownPathEffect path_effect2(SkDashPathEffect::Make(TestDashes1, 2, 0.0)); |
| 104 | + TestNotEquals(path_effect1, path_effect2, |
| 105 | + "SkDashPathEffect instance differs"); |
| 106 | +} |
| 107 | + |
| 108 | +void testEquals(DlPathEffect* a, DlPathEffect* b) { |
| 109 | + // a and b have the same nullness or values |
| 110 | + ASSERT_TRUE(Equals(a, b)); |
| 111 | + ASSERT_FALSE(NotEquals(a, b)); |
| 112 | + ASSERT_TRUE(Equals(b, a)); |
| 113 | + ASSERT_FALSE(NotEquals(b, a)); |
| 114 | +} |
| 115 | + |
| 116 | +void testNotEquals(DlPathEffect* a, DlPathEffect* b) { |
| 117 | + // a and b do not have the same nullness or values |
| 118 | + ASSERT_FALSE(Equals(a, b)); |
| 119 | + ASSERT_TRUE(NotEquals(a, b)); |
| 120 | + ASSERT_FALSE(Equals(b, a)); |
| 121 | + ASSERT_TRUE(NotEquals(b, a)); |
| 122 | +} |
| 123 | + |
| 124 | +void testEquals(std::shared_ptr<const DlPathEffect> a, DlPathEffect* b) { |
| 125 | + // a and b have the same nullness or values |
| 126 | + ASSERT_TRUE(Equals(a, b)); |
| 127 | + ASSERT_FALSE(NotEquals(a, b)); |
| 128 | + ASSERT_TRUE(Equals(b, a)); |
| 129 | + ASSERT_FALSE(NotEquals(b, a)); |
| 130 | +} |
| 131 | + |
| 132 | +void testNotEquals(std::shared_ptr<const DlPathEffect> a, DlPathEffect* b) { |
| 133 | + // a and b do not have the same nullness or values |
| 134 | + ASSERT_FALSE(Equals(a, b)); |
| 135 | + ASSERT_TRUE(NotEquals(a, b)); |
| 136 | + ASSERT_FALSE(Equals(b, a)); |
| 137 | + ASSERT_TRUE(NotEquals(b, a)); |
| 138 | +} |
| 139 | + |
| 140 | +void testEquals(std::shared_ptr<const DlPathEffect> a, |
| 141 | + std::shared_ptr<const DlPathEffect> b) { |
| 142 | + // a and b have the same nullness or values |
| 143 | + ASSERT_TRUE(Equals(a, b)); |
| 144 | + ASSERT_FALSE(NotEquals(a, b)); |
| 145 | + ASSERT_TRUE(Equals(b, a)); |
| 146 | + ASSERT_FALSE(NotEquals(b, a)); |
| 147 | +} |
| 148 | + |
| 149 | +void testNotEquals(std::shared_ptr<const DlPathEffect> a, |
| 150 | + std::shared_ptr<const DlPathEffect> b) { |
| 151 | + // a and b do not have the same nullness or values |
| 152 | + ASSERT_FALSE(Equals(a, b)); |
| 153 | + ASSERT_TRUE(NotEquals(a, b)); |
| 154 | + ASSERT_FALSE(Equals(b, a)); |
| 155 | + ASSERT_TRUE(NotEquals(b, a)); |
| 156 | +} |
| 157 | + |
| 158 | +TEST(DisplayListPathEffect, ComparableTemplates) { |
| 159 | + DlDashPathEffect effect1(TestDashes1, 2, 0.0); |
| 160 | + DlDashPathEffect effect2(TestDashes1, 2, 0.0); |
| 161 | + DlDashPathEffect effect3(TestDashes2, 3, 0.0); |
| 162 | + std::shared_ptr<DlPathEffect> shared_null; |
| 163 | + |
| 164 | + // null to null |
| 165 | + testEquals(nullptr, nullptr); |
| 166 | + testEquals(shared_null, nullptr); |
| 167 | + testEquals(shared_null, shared_null); |
| 168 | + |
| 169 | + // ptr to null |
| 170 | + testNotEquals(&effect1, nullptr); |
| 171 | + testNotEquals(&effect2, nullptr); |
| 172 | + testNotEquals(&effect3, nullptr); |
| 173 | + |
| 174 | + // shared_ptr to null and shared_null to ptr |
| 175 | + testNotEquals(effect1.shared(), nullptr); |
| 176 | + testNotEquals(effect2.shared(), nullptr); |
| 177 | + testNotEquals(effect3.shared(), nullptr); |
| 178 | + testNotEquals(shared_null, &effect1); |
| 179 | + testNotEquals(shared_null, &effect2); |
| 180 | + testNotEquals(shared_null, &effect3); |
| 181 | + |
| 182 | + // ptr to ptr |
| 183 | + testEquals(&effect1, &effect1); |
| 184 | + testEquals(&effect1, &effect2); |
| 185 | + testEquals(&effect3, &effect3); |
| 186 | + testEquals(&effect2, &effect2); |
| 187 | + |
| 188 | + // shared_ptr to ptr |
| 189 | + testEquals(effect1.shared(), &effect1); |
| 190 | + testEquals(effect1.shared(), &effect2); |
| 191 | + testEquals(effect2.shared(), &effect2); |
| 192 | + testEquals(effect3.shared(), &effect3); |
| 193 | + testNotEquals(effect1.shared(), &effect3); |
| 194 | + testNotEquals(effect2.shared(), &effect3); |
| 195 | + testNotEquals(effect3.shared(), &effect1); |
| 196 | + testNotEquals(effect3.shared(), &effect2); |
| 197 | + |
| 198 | + // shared_ptr to shared_ptr |
| 199 | + testEquals(effect1.shared(), effect1.shared()); |
| 200 | + testEquals(effect1.shared(), effect2.shared()); |
| 201 | + testEquals(effect2.shared(), effect2.shared()); |
| 202 | + testEquals(effect3.shared(), effect3.shared()); |
| 203 | + testNotEquals(effect1.shared(), effect3.shared()); |
| 204 | + testNotEquals(effect2.shared(), effect3.shared()); |
| 205 | + testNotEquals(effect3.shared(), effect1.shared()); |
| 206 | + testNotEquals(effect3.shared(), effect2.shared()); |
| 207 | +} |
| 208 | + |
| 209 | +} // namespace testing |
| 210 | +} // namespace flutter |
0 commit comments