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
11using namespace skrive::internal;
12
13DEF_TEST(SkRive_BinaryReader, reporter) {
14    static constexpr uint8_t bin[] = {
15        0x46, 0x4c, 0x41, 0x52, 0x45,   // 'FLARE'
16        0x12, 0x00, 0x00, 0x00,         // version: 18
17        0x73,                           // block type: kArtboards (115)
18        0x38, 0x00, 0x00, 0x00,         // block size: 56
19        0x01, 0x00,                     // container count: 1
20        0x72,                           // block type: kActorArtboard (114)
21        0x31, 0x00, 0x00, 0x00,         // block size: 49
22        0x04, 0x00, 0x00, 0x00,         // name len: 4
23        0x46, 0x6f, 0x6f, 0x6f,         // name: 'Fooo'
24                                        // translation:
25        0x00, 0x00, 0x00, 0x00,         //   0
26        0x00, 0x00, 0x00, 0x00,         //   0
27        0x00, 0xc0, 0x57, 0x44,         // width:  863.0
28        0x00, 0xc0, 0x60, 0x44,         // height: 899.0
29                                        // origin:
30        0x00, 0x00, 0x00, 0x00,         //   0
31        0x00, 0x00, 0x00, 0x00,         //   0
32        0x01,                           // clipContents: true
33                                        // color:
34        0x00, 0x00, 0x00, 0x3f,         //   0.5
35        0x00, 0x00, 0x00, 0x3f,         //   0.5
36        0x00, 0x00, 0x00, 0x3f,         //   0.5
37        0x00, 0x00, 0x80, 0x3f,         //   1.0
38    };
39
40    auto sr = StreamReader::Make(SkData::MakeWithoutCopy(bin, sizeof(bin)));
41
42    REPORTER_ASSERT(reporter, sr);
43    REPORTER_ASSERT(reporter, sr->readUInt32("version") == 18);
44    {
45        StreamReader::AutoBlock ab(sr);
46        REPORTER_ASSERT(reporter, ab.type() == StreamReader::BlockType::kArtboards);
47        REPORTER_ASSERT(reporter, sr->readLength16() == 1);
48
49        {
50            StreamReader::AutoBlock ab(sr);
51            REPORTER_ASSERT(reporter, ab.type() == StreamReader::BlockType::kActorArtboard);
52            REPORTER_ASSERT(reporter, sr->readString("name").equals("Fooo"));
53            REPORTER_ASSERT(reporter, sr->readV2("translation") == (SkV2{0,0}));
54            REPORTER_ASSERT(reporter, sr->readFloat("width" ) == 863);
55            REPORTER_ASSERT(reporter, sr->readFloat("height") == 899);
56            REPORTER_ASSERT(reporter, sr->readV2("origin") == (SkV2{0,0}));
57            REPORTER_ASSERT(reporter, sr->readBool("clipContents"));
58            REPORTER_ASSERT(reporter, sr->readColor("color") == (SkColor4f{0.5f,0.5f,0.5f,1}));
59
60            REPORTER_ASSERT(reporter, sr->readString("INVALID").equals(""));
61            REPORTER_ASSERT(reporter, sr->readFloat("INVALID" ) == 0);
62            REPORTER_ASSERT(reporter, !sr->readBool("INVALID"));
63        }
64        {
65            StreamReader::AutoBlock ab(sr);
66            REPORTER_ASSERT(reporter, ab.type() == StreamReader::BlockType::kEoB);
67        }
68    }
69    {
70        StreamReader::AutoBlock ab(sr);
71        REPORTER_ASSERT(reporter, ab.type() == StreamReader::BlockType::kEoB);
72    }
73}
74