aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/AndroidPaintTest.cpp
blob: 0fc2f9b03a07035b237bbb121a55883cb418dd88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// SkPaints only have an SkPaintOptionsAndroid if SK_BUILD_FOR_ANDROID is true.
#ifdef SK_BUILD_FOR_ANDROID

#include "SkReadBuffer.h"
#include "SkWriteBuffer.h"
#include "SkPaint.h"
#include "SkPaintOptionsAndroid.h"
#include "Test.h"

static size_t Reconstruct(const SkPaint& src, SkPaint* dst) {
    SkWriteBuffer writer;
    src.flatten(writer);

    const size_t size = writer.bytesWritten();
    SkAutoMalloc bytes(size);
    writer.writeToMemory(bytes.get());

    SkReadBuffer reader(bytes.get(), size);
    dst->unflatten(reader);

    return size;
}

DEF_TEST(AndroidOptionsSerialization, reporter) {
    // We want to make sure that Android's paint options survive a flatten/unflatten round trip.
    // These are all non-default options.
    SkPaintOptionsAndroid options;
    options.setLanguage("ja-JP");
    options.setFontVariant(SkPaintOptionsAndroid::kElegant_Variant);
    options.setUseFontFallbacks(true);

    SkPaint paint;
    paint.setPaintOptionsAndroid(options);

    SkPaint reconstructed;
    Reconstruct(paint, &reconstructed);

    REPORTER_ASSERT(reporter, options == reconstructed.getPaintOptionsAndroid());
}

DEF_TEST(AndroidOptionsSerializationReverse, reporter) {
    // Opposite test of above: make sure the serialized default values of a paint overwrite
    // non-default values on the paint we're unflattening into.
    const SkPaint defaultOptions;

    SkPaintOptionsAndroid options;
    options.setLanguage("ja-JP");
    options.setFontVariant(SkPaintOptionsAndroid::kElegant_Variant);
    options.setUseFontFallbacks(true);
    SkPaint nonDefaultOptions;
    nonDefaultOptions.setPaintOptionsAndroid(options);

    Reconstruct(defaultOptions, &nonDefaultOptions);

    REPORTER_ASSERT(reporter,
            defaultOptions.getPaintOptionsAndroid() ==
            nonDefaultOptions.getPaintOptionsAndroid());
}

DEF_TEST(AndroidOptionsSize, reporter) {
    // A paint with default android options should serialize to something smaller than
    // a paint with non-default android options.

    SkPaint defaultOptions;

    SkPaintOptionsAndroid options;
    options.setUseFontFallbacks(true);
    SkPaint nonDefaultOptions;
    nonDefaultOptions.setPaintOptionsAndroid(options);

    SkPaint dummy;

    REPORTER_ASSERT(reporter,
                    Reconstruct(defaultOptions, &dummy) < Reconstruct(nonDefaultOptions, &dummy));
}

#endif  // SK_BUILD_FOR_ANDROID