1 /*
2  * Copyright 2020 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 "experimental/skrive/src/reader/StreamReader.h"
9 #include "tests/Test.h"
10 
11 using namespace skrive::internal;
12 
DEF_TEST(SkRive_JsonReader,reporter)13 DEF_TEST(SkRive_JsonReader, reporter) {
14     static constexpr char json[] = R"({
15                                          "version": 24,
16                                          "artboards": [
17                                            {
18                                              "name"        : "artboard 1",
19                                              "translation" : [ 24, 42 ],
20                                              "width"       : 500,
21                                              "height"      : 250,
22                                              "origin"      : [ 100, 100 ],
23                                              "clipContents": true,
24                                              "color"       : [ 1, 1, 0, 1],
25                                              "type"        : "artboard"
26                                            }
27                                          ]
28                                      })";
29 
30     auto sr = StreamReader::Make(SkData::MakeWithoutCopy(json, strlen(json)));
31 
32     REPORTER_ASSERT(reporter, sr);
33     REPORTER_ASSERT(reporter, sr->readUInt32("version") == 24);
34     {
35         StreamReader::AutoBlock ab(sr);
36         REPORTER_ASSERT(reporter, ab.type() == StreamReader::BlockType::kArtboards);
37         REPORTER_ASSERT(reporter, sr->readLength16() == 1);
38 
39         {
40             StreamReader::AutoBlock ab(sr);
41             REPORTER_ASSERT(reporter, ab.type() == StreamReader::BlockType::kActorArtboard);
42             REPORTER_ASSERT(reporter, sr->readString("name").equals("artboard 1"));
43             REPORTER_ASSERT(reporter, sr->readV2("translation") == (SkV2{24,42}));
44             REPORTER_ASSERT(reporter, sr->readFloat("width" ) == 500);
45             REPORTER_ASSERT(reporter, sr->readFloat("height") == 250);
46             REPORTER_ASSERT(reporter, sr->readV2("origin") == (SkV2{100,100}));
47             REPORTER_ASSERT(reporter, sr->readBool("clipContents"));
48             REPORTER_ASSERT(reporter, sr->readColor("color") == (SkColor4f{1,1,0,1}));
49 
50             REPORTER_ASSERT(reporter, sr->readString("INVALID").equals(""));
51             REPORTER_ASSERT(reporter, sr->readFloat("INVALID" ) == 0);
52             REPORTER_ASSERT(reporter, !sr->readBool("INVALID"));
53         }
54         {
55             StreamReader::AutoBlock ab(sr);
56             REPORTER_ASSERT(reporter, ab.type() == StreamReader::BlockType::kEoB);
57         }
58     }
59     {
60         StreamReader::AutoBlock ab(sr);
61         REPORTER_ASSERT(reporter, ab.type() == StreamReader::BlockType::kEoB);
62     }
63 }
64