1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "printing/metafile_skia.h"
7 #include "cc/paint/paint_record.h"
8 #include "printing/common/metafile_utils.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/skia/include/core/SkPictureRecorder.h"
14 TEST(MetafileSkiaTest, TestFrameContent) {
15 constexpr int kPictureSideLen = 100;
16 constexpr int kPageSideLen = 150;
18 // Create a placeholder picture.
19 sk_sp<SkPicture> pic_holder = SkPicture::MakePlaceholder(
20 SkRect::MakeXYWH(0, 0, kPictureSideLen, kPictureSideLen));
22 // Create the page with nested content which is the placeholder and will be
24 sk_sp<cc::PaintRecord> record = sk_make_sp<cc::PaintRecord>();
26 flags.setColor(SK_ColorWHITE);
27 const SkRect page_rect = SkRect::MakeXYWH(0, 0, kPageSideLen, kPageSideLen);
28 record->push<cc::DrawRectOp>(page_rect, flags);
29 const uint32_t content_id = pic_holder->uniqueID();
30 record->push<cc::CustomDataOp>(content_id);
31 SkSize page_size = SkSize::Make(kPageSideLen, kPageSideLen);
33 // Finish creating the entire metafile.
34 MetafileSkia metafile(SkiaDocumentType::MSKP, 1);
35 metafile.AppendPage(page_size, std::move(record));
36 metafile.AppendSubframeInfo(content_id, 2, std::move(pic_holder));
37 metafile.FinishFrameContent();
38 SkStreamAsset* metafile_stream = metafile.GetPdfData();
39 ASSERT_TRUE(metafile_stream);
41 // Draw a 100 by 100 red square which will be the actual content of
43 SkPictureRecorder recorder;
44 SkCanvas* canvas = recorder.beginRecording(kPictureSideLen, kPictureSideLen);
46 paint.setStyle(SkPaint::kStrokeAndFill_Style);
47 paint.setColor(SK_ColorRED);
48 paint.setAlpha(SK_AlphaOPAQUE);
49 canvas->drawRect(SkRect::MakeXYWH(0, 0, kPictureSideLen, kPictureSideLen),
51 sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
54 // Get the complete picture by replacing the placeholder.
55 DeserializationContext subframes;
56 subframes[content_id] = picture;
57 SkDeserialProcs procs = DeserializationProcs(&subframes);
58 sk_sp<SkPicture> pic = SkPicture::MakeFromStream(metafile_stream, &procs);
61 // Verify the resultant picture is as expected by comparing the sizes and
62 // detecting the color inside and outside of the square area.
63 EXPECT_TRUE(pic->cullRect() == page_rect);
65 bitmap.allocN32Pixels(kPageSideLen, kPageSideLen);
66 SkCanvas bitmap_canvas(bitmap);
67 pic->playback(&bitmap_canvas);
68 // Check top left pixel color of the red square.
69 EXPECT_EQ(bitmap.getColor(0, 0), SK_ColorRED);
70 // Check bottom right pixel of the red square.
71 EXPECT_EQ(bitmap.getColor(kPictureSideLen - 1, kPictureSideLen - 1),
73 // Check inside of the red square.
74 EXPECT_EQ(bitmap.getColor(kPictureSideLen / 2, kPictureSideLen / 2),
76 // Check outside of the red square.
77 EXPECT_EQ(bitmap.getColor(kPictureSideLen, kPictureSideLen), SK_ColorWHITE);
80 } // namespace printing