Upload upstream chromium 73.0.3683.0
[platform/framework/web/chromium-efl.git] / printing / metafile_skia_unittest.cc
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.
4
5 #include "printing/metafile_skia.h"
6
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"
11
12 namespace printing {
13
14 TEST(MetafileSkiaTest, TestFrameContent) {
15   constexpr int kPictureSideLen = 100;
16   constexpr int kPageSideLen = 150;
17
18   // Create a placeholder picture.
19   sk_sp<SkPicture> pic_holder = SkPicture::MakePlaceholder(
20       SkRect::MakeXYWH(0, 0, kPictureSideLen, kPictureSideLen));
21
22   // Create the page with nested content which is the placeholder and will be
23   // replaced later.
24   sk_sp<cc::PaintRecord> record = sk_make_sp<cc::PaintRecord>();
25   cc::PaintFlags flags;
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);
32
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);
40
41   // Draw a 100 by 100 red square which will be the actual content of
42   // the placeholder.
43   SkPictureRecorder recorder;
44   SkCanvas* canvas = recorder.beginRecording(kPictureSideLen, kPictureSideLen);
45   SkPaint paint;
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),
50                    paint);
51   sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
52   EXPECT_TRUE(picture);
53
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);
59   ASSERT_TRUE(pic);
60
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);
64   SkBitmap bitmap;
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),
72             SK_ColorRED);
73   // Check inside of the red square.
74   EXPECT_EQ(bitmap.getColor(kPictureSideLen / 2, kPictureSideLen / 2),
75             SK_ColorRED);
76   // Check outside of the red square.
77   EXPECT_EQ(bitmap.getColor(kPictureSideLen, kPictureSideLen), SK_ColorWHITE);
78 }
79
80 }  // namespace printing