1 /*
2 * Copyright 2022 Google LLC
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/SkData.h"
9 #include "include/core/SkFont.h"
10 #include "include/core/SkRefCnt.h"
11 #include "src/core/SkReadBuffer.h"
12 #include "src/core/SkStrike.h"
13 #include "src/core/SkStrikeCache.h"
14 #include "src/core/SkStrikeSpec.h"
15 #include "src/core/SkWriteBuffer.h"
16 #include "src/text/StrikeForGPU.h"
17 #include "tests/Test.h"
18 #include "tools/fonts/FontToolUtils.h"
19
20 #include <cstdint>
21 #include <memory>
22 #include <optional>
23 #include <utility>
24
25 using namespace sktext;
26
DEF_TEST(SkStrikePromise_Basic,reporter)27 DEF_TEST(SkStrikePromise_Basic, reporter) {
28 auto strikeCache = std::make_unique<SkStrikeCache>();
29 auto [strikeSpec, _] = SkStrikeSpec::MakeCanonicalized(ToolUtils::DefaultFont());
30
31 class Pinner : public ::SkStrikePinner {
32 public:
33 // Changing canDelete to return true causes this test to expectedly fail.
34 bool canDelete() override { return false; }
35 };
36
37 intptr_t toCompareWith;
38 sk_sp<SkData> data;
39
40 // Ensure that the ref in srcPromise is dropped.
41 {
42 // Make a strike with a Pinner.
43 auto strike = strikeCache->createStrike(strikeSpec, nullptr, std::make_unique<Pinner>());
44 toCompareWith = reinterpret_cast<intptr_t>(strike.get());
45 SkStrikePromise srcPromise(std::move(strike));
46 SkBinaryWriteBuffer writeBuffer({});
47 srcPromise.flatten(writeBuffer);
48 data = writeBuffer.snapshotAsData();
49 }
50
51 // Remove all unpinned strikes.
52 strikeCache->purgeAll();
53
54 SkReadBuffer readBuffer{data->data(), data->size()};
55 std::optional<SkStrikePromise> dstPromise = SkStrikePromise::MakeFromBuffer(
56 readBuffer, nullptr, strikeCache.get());
57
58 REPORTER_ASSERT(reporter, dstPromise.has_value());
59 REPORTER_ASSERT(reporter,
60 reinterpret_cast<intptr_t>(dstPromise->strike()) == toCompareWith);
61 }
62