blob: a638da9351ebc44f8db8ba6fc1c939089e3dd861 [file] [log] [blame]
commit-bot@chromium.org97f81672013-09-26 15:16:12 +00001// SkPaints only have an SkPaintOptionsAndroid if SK_BUILD_FOR_ANDROID is true.
2#ifdef SK_BUILD_FOR_ANDROID
3
4#include "SkPaintOptionsAndroid.h"
5#include "SkOrderedReadBuffer.h"
6#include "SkOrderedWriteBuffer.h"
7#include "SkPaint.h"
8#include "Test.h"
9#include "TestClassDef.h"
10
11static size_t Reconstruct(const SkPaint& src, SkPaint* dst) {
12 SkOrderedWriteBuffer writer(64 /*arbitrary*/);
13 src.flatten(writer);
14
15 const size_t size = writer.bytesWritten();
16 SkAutoMalloc bytes(size);
17 writer.writeToMemory(bytes.get());
18
19 SkOrderedReadBuffer reader(bytes.get(), size);
20 dst->unflatten(reader);
21
22 return size;
23}
24
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +000025DEF_TEST(AndroidOptionsSerialization, reporter) {
commit-bot@chromium.org97f81672013-09-26 15:16:12 +000026 // We want to make sure that Android's paint options survive a flatten/unflatten round trip.
27 // These are all non-default options.
28 SkPaintOptionsAndroid options;
29 options.setLanguage("ja-JP");
30 options.setFontVariant(SkPaintOptionsAndroid::kElegant_Variant);
31 options.setUseFontFallbacks(true);
32
33 SkPaint paint;
34 paint.setPaintOptionsAndroid(options);
35
36 SkPaint reconstructed;
37 Reconstruct(paint, &reconstructed);
38
39 REPORTER_ASSERT(reporter, options == reconstructed.getPaintOptionsAndroid());
40}
commit-bot@chromium.org97f81672013-09-26 15:16:12 +000041
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +000042DEF_TEST(AndroidOptionsSerializationReverse, reporter) {
commit-bot@chromium.org97f81672013-09-26 15:16:12 +000043 // Opposite test of above: make sure the serialized default values of a paint overwrite
44 // non-default values on the paint we're unflattening into.
45 const SkPaint defaultOptions;
46
47 SkPaintOptionsAndroid options;
48 options.setLanguage("ja-JP");
49 options.setFontVariant(SkPaintOptionsAndroid::kElegant_Variant);
50 options.setUseFontFallbacks(true);
51 SkPaint nonDefaultOptions;
52 nonDefaultOptions.setPaintOptionsAndroid(options);
53
54 Reconstruct(defaultOptions, &nonDefaultOptions);
55
56 REPORTER_ASSERT(reporter,
57 defaultOptions.getPaintOptionsAndroid() ==
58 nonDefaultOptions.getPaintOptionsAndroid());
59}
commit-bot@chromium.org97f81672013-09-26 15:16:12 +000060
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +000061DEF_TEST(AndroidOptionsSize, reporter) {
commit-bot@chromium.org97f81672013-09-26 15:16:12 +000062 // A paint with default android options should serialize to something smaller than
63 // a paint with non-default android options.
64
65 SkPaint defaultOptions;
66
67 SkPaintOptionsAndroid options;
68 options.setUseFontFallbacks(true);
69 SkPaint nonDefaultOptions;
70 nonDefaultOptions.setPaintOptionsAndroid(options);
71
72 SkPaint dummy;
73
74 REPORTER_ASSERT(reporter,
75 Reconstruct(defaultOptions, &dummy) < Reconstruct(nonDefaultOptions, &dummy));
76}
commit-bot@chromium.org97f81672013-09-26 15:16:12 +000077
78#endif // SK_BUILD_FOR_ANDROID