xref: /aosp_15_r20/external/skia/tests/AsADashTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2014 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkPathEffect.h"
9 #include "include/core/SkRefCnt.h"
10 #include "include/core/SkScalar.h"
11 #include "include/effects/SkCornerPathEffect.h"
12 #include "include/effects/SkDashPathEffect.h"
13 #include "include/private/base/SkTemplates.h"
14 #include "src/core/SkPathEffectBase.h"
15 #include "tests/Test.h"
16 
17 using namespace skia_private;
18 
DEF_TEST(AsADashTest_noneDash,reporter)19 DEF_TEST(AsADashTest_noneDash, reporter) {
20     sk_sp<SkPathEffect> pe(SkCornerPathEffect::Make(1.0));
21     SkPathEffectBase::DashInfo info;
22 
23     SkPathEffectBase::DashType dashType = as_PEB(pe)->asADash(&info);
24     REPORTER_ASSERT(reporter, SkPathEffectBase::DashType::kNone == dashType);
25 }
26 
DEF_TEST(AsADashTest_nullInfo,reporter)27 DEF_TEST(AsADashTest_nullInfo, reporter) {
28     SkScalar inIntervals[] = { 4.0, 2.0, 1.0, 3.0 };
29     const SkScalar phase = 2.0;
30     sk_sp<SkPathEffect> pe(SkDashPathEffect::Make(inIntervals, 4, phase));
31 
32     SkPathEffectBase::DashType dashType = as_PEB(pe)->asADash(nullptr);
33     REPORTER_ASSERT(reporter, SkPathEffectBase::DashType::kDash == dashType);
34 }
35 
DEF_TEST(AsADashTest_usingDash,reporter)36 DEF_TEST(AsADashTest_usingDash, reporter) {
37     SkScalar inIntervals[] = { 4.0, 2.0, 1.0, 3.0 };
38     SkScalar totalIntSum = 10.0;
39     const SkScalar phase = 2.0;
40 
41     sk_sp<SkPathEffect> pe(SkDashPathEffect::Make(inIntervals, 4, phase));
42 
43     SkPathEffectBase::DashInfo info;
44 
45     SkPathEffectBase::DashType dashType = as_PEB(pe)->asADash(&info);
46     REPORTER_ASSERT(reporter, SkPathEffectBase::DashType::kDash == dashType);
47     REPORTER_ASSERT(reporter, 4 == info.fCount);
48     REPORTER_ASSERT(reporter, SkScalarMod(phase, totalIntSum) == info.fPhase);
49 
50     // Since it is a kDash_DashType, allocate space for the intervals and recall asADash
51     AutoTArray<SkScalar> intervals(info.fCount);
52     info.fIntervals = intervals.get();
53     as_PEB(pe)->asADash(&info);
54     REPORTER_ASSERT(reporter, inIntervals[0] == info.fIntervals[0]);
55     REPORTER_ASSERT(reporter, inIntervals[1] == info.fIntervals[1]);
56     REPORTER_ASSERT(reporter, inIntervals[2] == info.fIntervals[2]);
57     REPORTER_ASSERT(reporter, inIntervals[3] == info.fIntervals[3]);
58 
59     // Make sure nothing else has changed on us
60     REPORTER_ASSERT(reporter, 4 == info.fCount);
61     REPORTER_ASSERT(reporter, SkScalarMod(phase, totalIntSum) == info.fPhase);
62 }
63