1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2020 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "experimental/skrive/src/reader/StreamReader.h"
9cb93a386Sopenharmony_ci#include "tests/Test.h"
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ciusing namespace skrive::internal;
12cb93a386Sopenharmony_ci
13cb93a386Sopenharmony_ciDEF_TEST(SkRive_BinaryReader, reporter) {
14cb93a386Sopenharmony_ci    static constexpr uint8_t bin[] = {
15cb93a386Sopenharmony_ci        0x46, 0x4c, 0x41, 0x52, 0x45,   // 'FLARE'
16cb93a386Sopenharmony_ci        0x12, 0x00, 0x00, 0x00,         // version: 18
17cb93a386Sopenharmony_ci        0x73,                           // block type: kArtboards (115)
18cb93a386Sopenharmony_ci        0x38, 0x00, 0x00, 0x00,         // block size: 56
19cb93a386Sopenharmony_ci        0x01, 0x00,                     // container count: 1
20cb93a386Sopenharmony_ci        0x72,                           // block type: kActorArtboard (114)
21cb93a386Sopenharmony_ci        0x31, 0x00, 0x00, 0x00,         // block size: 49
22cb93a386Sopenharmony_ci        0x04, 0x00, 0x00, 0x00,         // name len: 4
23cb93a386Sopenharmony_ci        0x46, 0x6f, 0x6f, 0x6f,         // name: 'Fooo'
24cb93a386Sopenharmony_ci                                        // translation:
25cb93a386Sopenharmony_ci        0x00, 0x00, 0x00, 0x00,         //   0
26cb93a386Sopenharmony_ci        0x00, 0x00, 0x00, 0x00,         //   0
27cb93a386Sopenharmony_ci        0x00, 0xc0, 0x57, 0x44,         // width:  863.0
28cb93a386Sopenharmony_ci        0x00, 0xc0, 0x60, 0x44,         // height: 899.0
29cb93a386Sopenharmony_ci                                        // origin:
30cb93a386Sopenharmony_ci        0x00, 0x00, 0x00, 0x00,         //   0
31cb93a386Sopenharmony_ci        0x00, 0x00, 0x00, 0x00,         //   0
32cb93a386Sopenharmony_ci        0x01,                           // clipContents: true
33cb93a386Sopenharmony_ci                                        // color:
34cb93a386Sopenharmony_ci        0x00, 0x00, 0x00, 0x3f,         //   0.5
35cb93a386Sopenharmony_ci        0x00, 0x00, 0x00, 0x3f,         //   0.5
36cb93a386Sopenharmony_ci        0x00, 0x00, 0x00, 0x3f,         //   0.5
37cb93a386Sopenharmony_ci        0x00, 0x00, 0x80, 0x3f,         //   1.0
38cb93a386Sopenharmony_ci    };
39cb93a386Sopenharmony_ci
40cb93a386Sopenharmony_ci    auto sr = StreamReader::Make(SkData::MakeWithoutCopy(bin, sizeof(bin)));
41cb93a386Sopenharmony_ci
42cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, sr);
43cb93a386Sopenharmony_ci    REPORTER_ASSERT(reporter, sr->readUInt32("version") == 18);
44cb93a386Sopenharmony_ci    {
45cb93a386Sopenharmony_ci        StreamReader::AutoBlock ab(sr);
46cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, ab.type() == StreamReader::BlockType::kArtboards);
47cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, sr->readLength16() == 1);
48cb93a386Sopenharmony_ci
49cb93a386Sopenharmony_ci        {
50cb93a386Sopenharmony_ci            StreamReader::AutoBlock ab(sr);
51cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, ab.type() == StreamReader::BlockType::kActorArtboard);
52cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, sr->readString("name").equals("Fooo"));
53cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, sr->readV2("translation") == (SkV2{0,0}));
54cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, sr->readFloat("width" ) == 863);
55cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, sr->readFloat("height") == 899);
56cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, sr->readV2("origin") == (SkV2{0,0}));
57cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, sr->readBool("clipContents"));
58cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, sr->readColor("color") == (SkColor4f{0.5f,0.5f,0.5f,1}));
59cb93a386Sopenharmony_ci
60cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, sr->readString("INVALID").equals(""));
61cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, sr->readFloat("INVALID" ) == 0);
62cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, !sr->readBool("INVALID"));
63cb93a386Sopenharmony_ci        }
64cb93a386Sopenharmony_ci        {
65cb93a386Sopenharmony_ci            StreamReader::AutoBlock ab(sr);
66cb93a386Sopenharmony_ci            REPORTER_ASSERT(reporter, ab.type() == StreamReader::BlockType::kEoB);
67cb93a386Sopenharmony_ci        }
68cb93a386Sopenharmony_ci    }
69cb93a386Sopenharmony_ci    {
70cb93a386Sopenharmony_ci        StreamReader::AutoBlock ab(sr);
71cb93a386Sopenharmony_ci        REPORTER_ASSERT(reporter, ab.type() == StreamReader::BlockType::kEoB);
72cb93a386Sopenharmony_ci    }
73cb93a386Sopenharmony_ci}
74