Skip to content

Commit 0d15eb5

Browse files
committed
Unit testing of color-mix()
1 parent 4e18fb0 commit 0d15eb5

File tree

3 files changed

+83
-20
lines changed

3 files changed

+83
-20
lines changed

svgnative/src/SVGStringParser.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,7 @@ static bool ParseColor(CharIt& pos, const CharIt& end, ColorImpl& paint, bool su
11241124
}
11251125
else if (char4string.compare("colo") == 0)
11261126
{
1127-
if (std::distance(pos, end) > 10 && std::string(pos, pos + 10).compare("r-mix(") == 0)
1127+
if (std::distance(pos, end) > 10 && std::string(pos + 4, pos + 10).compare("r-mix(") == 0)
11281128
{
11291129
pos += 10;
11301130
if (!SkipOptWsp(pos, end))
@@ -1152,7 +1152,7 @@ static bool ParseColor(CharIt& pos, const CharIt& end, ColorImpl& paint, bool su
11521152
float blendProgress1{ 50.f };
11531153
bool hasBlendProgress1{};
11541154
auto tempPos = pos;
1155-
if (tempPos != end && isWsp(*tempPos)
1155+
if (tempPos != end && isWsp(*tempPos) && SkipOptWsp(tempPos, end)
11561156
&& ParseFloatingPoint(tempPos, end, blendProgress1)
11571157
&& tempPos != end && *tempPos == '%')
11581158
{
@@ -1164,13 +1164,13 @@ static bool ParseColor(CharIt& pos, const CharIt& end, ColorImpl& paint, bool su
11641164
return false;
11651165

11661166
resultColor = SVGDocumentImpl::Result::kInvalid;
1167-
if (!ParseColor(pos, end, colorMixPtr->color1, supportsCurrentColor, resultColor)
1167+
if (!ParseColor(pos, end, colorMixPtr->color2, supportsCurrentColor, resultColor)
11681168
|| resultColor != SVGDocumentImpl::Result::kSuccess)
11691169
return false;
11701170
float blendProgress2{ 50.f };
11711171
bool hasBlendProgress2{};
11721172
tempPos = pos;
1173-
if (tempPos != end && isWsp(*tempPos)
1173+
if (tempPos != end && isWsp(*tempPos) && SkipOptWsp(tempPos, end)
11741174
&& ParseFloatingPoint(tempPos, end, blendProgress2)
11751175
&& tempPos != end && *tempPos == '%')
11761176
{
@@ -1189,9 +1189,9 @@ static bool ParseColor(CharIt& pos, const CharIt& end, ColorImpl& paint, bool su
11891189
if (!hasBlendProgress1 && hasBlendProgress2)
11901190
blendProgress1 = 1 - blendProgress2;
11911191
else if (hasBlendProgress1 && !hasBlendProgress2)
1192-
hasBlendProgress2 = 1 - blendProgress1;
1192+
blendProgress2 = 1 - blendProgress1;
11931193

1194-
colorMixPtr->blendProgress = blendProgress1 / (blendProgress1 + blendProgress2);
1194+
colorMixPtr->blendProgress = blendProgress2 / (blendProgress1 + blendProgress2);
11951195

11961196
paint = colorMixPtr;
11971197
result = SVGDocumentImpl::Result::kSuccess;

svgnative/tests/CMakeLists.txt

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
set (SOURCE_FILES
22
${CMAKE_CURRENT_SOURCE_DIR}/../src/Interval.cpp
33
${CMAKE_CURRENT_SOURCE_DIR}/../src/Interval.h
4-
)
5-
add_executable(intervalTests interval-tests.cpp ${SOURCE_FILES})
6-
target_include_directories(intervalTests PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../src")
7-
target_link_libraries(intervalTests SVGNativeViewerLib)
8-
target_link_libraries(intervalTests gtest_main)
9-
add_test(NAME interval_tests COMMAND intervalTests)
10-
11-
set (SOURCE_FILES
12-
${CMAKE_CURRENT_SOURCE_DIR}/../src/Interval.cpp
134
${CMAKE_CURRENT_SOURCE_DIR}/../src/Rect.cpp
145
)
15-
add_executable(rectangleTests rectangle-tests.cpp ${SOURCE_FILES})
16-
target_include_directories(rectangleTests PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../src")
17-
target_link_libraries(rectangleTests SVGNativeViewerLib)
18-
target_link_libraries(rectangleTests gtest_main)
19-
add_test(NAME rectangle_tests COMMAND rectangleTests)
206

7+
set (TEST_FILES
8+
interval-tests.cpp
9+
rectangle-tests.cpp
10+
parsing-tests.cpp
11+
)
12+
add_executable(unitTesting ${TEST_FILES} ${SOURCE_FILES})
13+
target_include_directories(unitTesting PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../src")
14+
target_link_libraries(unitTesting SVGNativeViewerLib)
15+
target_link_libraries(unitTesting gtest_main)
16+
add_test(NAME interval_tests COMMAND unitTesting)
17+
add_test(NAME rectangle_tests COMMAND unitTesting)
18+
add_test(NAME parsing_tests COMMAND unitTesting)
2119

2220
# TODO: For now we just use the Skia port, but later on we should
2321
# extend this and generalize this so that all the ports are equally

svgnative/tests/parsing-tests.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2023 Adobe. All rights reserved.
3+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License. You may obtain a copy
5+
of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
Unless required by applicable law or agreed to in writing, software distributed under
8+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
OF ANY KIND, either express or implied. See the License for the specific language
10+
governing permissions and limitations under the License.
11+
*/
12+
13+
#include "gtest/gtest.h"
14+
#include "gtest/gtest-spi.h"
15+
16+
#include <svgnative/SVGRenderer.h>
17+
#include "SVGStringParser.h"
18+
19+
using namespace SVGNative;
20+
21+
using BeforeAfterPath = std::pair<std::string, Color>;
22+
23+
class ParsingTestFixture : public ::testing::TestWithParam<BeforeAfterPath> {
24+
};
25+
26+
TEST_P(ParsingTestFixture, color_mix)
27+
{
28+
const auto& param = GetParam();
29+
30+
static const ColorMap colorMap{
31+
{ "myBlue", Color{0.f, 0.f, 1.f, 1.f}},
32+
{ "myRed", Color{1.f, 0.f, 0.f, 1.f}},
33+
{ "myGreen", Color{0.f, 0.5f, 0.f, 1.f}},
34+
};
35+
36+
ColorImpl colorImpl;
37+
auto parsingResult = SVGStringParser::ParseColor(param.first, colorImpl);
38+
39+
auto wasParsingSuccessful = parsingResult == SVGDocumentImpl::Result::kSuccess;
40+
EXPECT_EQ(wasParsingSuccessful, true);
41+
EXPECT_EQ(colorImpl.type() == typeid(ColorMixPtr), true);
42+
43+
auto blendedColor = boost::get<ColorMixPtr>(colorImpl)->BlendedColor(colorMap);
44+
auto& expectedColor = param.second;
45+
EXPECT_FLOAT_EQ(blendedColor[0], expectedColor[0]);
46+
EXPECT_FLOAT_EQ(blendedColor[1], expectedColor[1]);
47+
EXPECT_FLOAT_EQ(blendedColor[2], expectedColor[2]);
48+
EXPECT_FLOAT_EQ(blendedColor[3], expectedColor[3]);
49+
}
50+
51+
INSTANTIATE_TEST_SUITE_P(ColorMixParsing, ParsingTestFixture,
52+
::testing::ValuesIn(std::vector<BeforeAfterPath>{
53+
{
54+
"color-mix(in srgb, black, white 80%)",
55+
Color{{.8f, .8f, .8f, 1.f}}
56+
},
57+
{
58+
"color-mix(in srgb, black 80%, white)",
59+
Color{{.2f, .2f, .2f, 1.f}}
60+
},
61+
{
62+
"color-mix(in srgb, rgb(255, 127, 0) 80%, rgba(0, 127, 0, 1.0))",
63+
Color{{.2f, .2f, .2f, 1.f}}
64+
}
65+
}));

0 commit comments

Comments
 (0)