OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/page/PrintContext.h" | 5 #include "core/page/PrintContext.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
10 #include "core/frame/FrameView.h" | 10 #include "core/frame/FrameView.h" |
11 #include "core/html/HTMLElement.h" | 11 #include "core/html/HTMLElement.h" |
12 #include "core/layout/LayoutTestHelper.h" | 12 #include "core/layout/LayoutTestHelper.h" |
13 #include "core/layout/LayoutView.h" | 13 #include "core/layout/LayoutView.h" |
14 #include "core/paint/PaintLayer.h" | 14 #include "core/paint/PaintLayer.h" |
15 #include "core/paint/PaintLayerPainter.h" | 15 #include "core/paint/PaintLayerPainter.h" |
16 #include "core/testing/DummyPageHolder.h" | 16 #include "core/testing/DummyPageHolder.h" |
17 #include "platform/graphics/GraphicsContext.h" | 17 #include "platform/graphics/GraphicsContext.h" |
18 #include "platform/graphics/paint/DrawingRecorder.h" | 18 #include "platform/graphics/paint/DrawingRecorder.h" |
19 #include "platform/graphics/paint/PaintRecordBuilder.h" | 19 #include "platform/graphics/paint/PaintRecordBuilder.h" |
20 #include "platform/scroll/ScrollbarTheme.h" | 20 #include "platform/scroll/ScrollbarTheme.h" |
21 #include "platform/text/TextStream.h" | 21 #include "platform/text/TextStream.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 #include "third_party/skia/include/core/SkCanvas.h" | 23 #include "third_party/skia/include/core/SkCanvas.h" |
24 | 24 |
25 namespace blink { | 25 namespace blink { |
26 | 26 |
27 const int kPageWidth = 800; | 27 const int kPageWidth = 800; |
28 const int kPageHeight = 600; | 28 const int kPageHeight = 600; |
29 | 29 |
30 class MockPrintContext : public PrintContext { | |
31 public: | |
32 MockPrintContext(LocalFrame* frame) : PrintContext(frame) {} | |
33 | |
34 void OutputLinkedDestinations(GraphicsContext& context, | |
35 const IntRect& page_rect) { | |
36 PrintContext::OutputLinkedDestinations(context, page_rect); | |
37 } | |
38 }; | |
39 | |
40 class MockCanvas : public SkCanvas { | 30 class MockCanvas : public SkCanvas { |
41 public: | 31 public: |
42 enum OperationType { kDrawRect, kDrawPoint }; | 32 enum OperationType { kDrawRect, kDrawPoint }; |
43 | 33 |
44 struct Operation { | 34 struct Operation { |
45 OperationType type; | 35 OperationType type; |
46 SkRect rect; | 36 SkRect rect; |
47 }; | 37 }; |
48 | 38 |
49 MockCanvas() : SkCanvas(kPageWidth, kPageHeight) {} | 39 MockCanvas() : SkCanvas(kPageWidth, kPageHeight) {} |
| 40 ~MockCanvas() override {} |
50 | 41 |
51 void onDrawAnnotation(const SkRect& rect, | 42 void onDrawAnnotation(const SkRect& rect, |
52 const char key[], | 43 const char key[], |
53 SkData* value) override { | 44 SkData* value) override { |
54 if (rect.width() == 0 && rect.height() == 0) { | 45 if (rect.width() == 0 && rect.height() == 0) { |
55 SkPoint point = getTotalMatrix().mapXY(rect.x(), rect.y()); | 46 SkPoint point = getTotalMatrix().mapXY(rect.x(), rect.y()); |
56 Operation operation = {kDrawPoint, | 47 Operation operation = {kDrawPoint, |
57 SkRect::MakeXYWH(point.x(), point.y(), 0, 0)}; | 48 SkRect::MakeXYWH(point.x(), point.y(), 0, 0)}; |
58 recorded_operations_.push_back(operation); | 49 recorded_operations_.push_back(operation); |
59 } else { | 50 } else { |
60 Operation operation = {kDrawRect, rect}; | 51 Operation operation = {kDrawRect, rect}; |
61 getTotalMatrix().mapRect(&operation.rect); | 52 getTotalMatrix().mapRect(&operation.rect); |
62 recorded_operations_.push_back(operation); | 53 recorded_operations_.push_back(operation); |
63 } | 54 } |
64 } | 55 } |
65 | 56 |
66 const Vector<Operation>& RecordedOperations() const { | 57 const Vector<Operation>& RecordedOperations() const { |
67 return recorded_operations_; | 58 return recorded_operations_; |
68 } | 59 } |
69 | 60 |
70 private: | 61 private: |
71 Vector<Operation> recorded_operations_; | 62 Vector<Operation> recorded_operations_; |
72 }; | 63 }; |
73 | 64 |
74 class PrintContextTest : public RenderingTest { | 65 class PrintContextTest : public RenderingTest { |
75 protected: | 66 protected: |
76 explicit PrintContextTest(LocalFrameClient* local_frame_client = nullptr) | 67 explicit PrintContextTest(LocalFrameClient* local_frame_client = nullptr) |
77 : RenderingTest(local_frame_client) {} | 68 : RenderingTest(local_frame_client) {} |
| 69 ~PrintContextTest() override {} |
78 | 70 |
79 void SetUp() override { | 71 void SetUp() override { |
80 RenderingTest::SetUp(); | 72 RenderingTest::SetUp(); |
81 print_context_ = new MockPrintContext(GetDocument().GetFrame()); | 73 print_context_ = new PrintContext(GetDocument().GetFrame()); |
82 } | 74 } |
83 | 75 |
84 MockPrintContext& PrintContext() { return *print_context_.Get(); } | 76 PrintContext& GetPrintContext() { return *print_context_.Get(); } |
85 | 77 |
86 void SetBodyInnerHTML(String body_content) { | 78 void SetBodyInnerHTML(String body_content) { |
87 GetDocument().body()->setAttribute(HTMLNames::styleAttr, "margin: 0"); | 79 GetDocument().body()->setAttribute(HTMLNames::styleAttr, "margin: 0"); |
88 GetDocument().body()->setInnerHTML(body_content); | 80 GetDocument().body()->setInnerHTML(body_content); |
89 } | 81 } |
90 | 82 |
91 void PrintSinglePage(MockCanvas& canvas) { | 83 void PrintSinglePage(MockCanvas& canvas) { |
92 IntRect page_rect(0, 0, kPageWidth, kPageHeight); | 84 IntRect page_rect(0, 0, kPageWidth, kPageHeight); |
93 PrintContext().begin(page_rect.Width(), page_rect.Height()); | 85 GetPrintContext().begin(page_rect.Width(), page_rect.Height()); |
94 GetDocument().View()->UpdateAllLifecyclePhases(); | 86 GetDocument().View()->UpdateAllLifecyclePhases(); |
95 PaintRecordBuilder builder(page_rect); | 87 PaintRecordBuilder builder(page_rect); |
96 GraphicsContext& context = builder.Context(); | 88 GraphicsContext& context = builder.Context(); |
97 context.SetPrinting(true); | 89 context.SetPrinting(true); |
98 GetDocument().View()->PaintContents(context, kGlobalPaintPrinting, | 90 GetDocument().View()->PaintContents(context, kGlobalPaintPrinting, |
99 page_rect); | 91 page_rect); |
100 { | 92 { |
101 DrawingRecorder recorder(context, *GetDocument().GetLayoutView(), | 93 DrawingRecorder recorder(context, *GetDocument().GetLayoutView(), |
102 DisplayItem::kPrintedContentDestinationLocations, | 94 DisplayItem::kPrintedContentDestinationLocations, |
103 page_rect); | 95 page_rect); |
104 PrintContext().OutputLinkedDestinations(context, page_rect); | 96 GetPrintContext().OutputLinkedDestinations(context, page_rect); |
105 } | 97 } |
106 builder.EndRecording()->playback(&canvas); | 98 builder.EndRecording()->playback(&canvas); |
107 PrintContext().end(); | 99 GetPrintContext().end(); |
108 } | 100 } |
109 | 101 |
110 static String AbsoluteBlockHtmlForLink(int x, | 102 static String AbsoluteBlockHtmlForLink(int x, |
111 int y, | 103 int y, |
112 int width, | 104 int width, |
113 int height, | 105 int height, |
114 const char* url, | 106 const char* url, |
115 const char* children = nullptr) { | 107 const char* children = nullptr) { |
116 TextStream ts; | 108 TextStream ts; |
117 ts << "<a style='position: absolute; left: " << x << "px; top: " << y | 109 ts << "<a style='position: absolute; left: " << x << "px; top: " << y |
(...skipping 14 matching lines...) Expand all Loading... |
132 const char* name, | 124 const char* name, |
133 const char* text_content) { | 125 const char* text_content) { |
134 TextStream ts; | 126 TextStream ts; |
135 ts << "<a name='" << name << "' style='position: absolute; left: " << x | 127 ts << "<a name='" << name << "' style='position: absolute; left: " << x |
136 << "px; top: " << y << "px'>" << text_content << "</a>"; | 128 << "px; top: " << y << "px'>" << text_content << "</a>"; |
137 return ts.Release(); | 129 return ts.Release(); |
138 } | 130 } |
139 | 131 |
140 private: | 132 private: |
141 std::unique_ptr<DummyPageHolder> page_holder_; | 133 std::unique_ptr<DummyPageHolder> page_holder_; |
142 Persistent<MockPrintContext> print_context_; | 134 Persistent<PrintContext> print_context_; |
143 }; | 135 }; |
144 | 136 |
145 class PrintContextFrameTest : public PrintContextTest { | 137 class PrintContextFrameTest : public PrintContextTest { |
146 public: | 138 public: |
147 PrintContextFrameTest() | 139 PrintContextFrameTest() |
148 : PrintContextTest(SingleChildLocalFrameClient::Create()) {} | 140 : PrintContextTest(SingleChildLocalFrameClient::Create()) {} |
149 }; | 141 }; |
150 | 142 |
151 #define EXPECT_SKRECT_EQ(expectedX, expectedY, expectedWidth, expectedHeight, \ | 143 #define EXPECT_SKRECT_EQ(expectedX, expectedY, expectedWidth, expectedHeight, \ |
152 actualRect) \ | 144 actualRect) \ |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
352 EXPECT_EQ(MockCanvas::kDrawRect, operations[0].type); | 344 EXPECT_EQ(MockCanvas::kDrawRect, operations[0].type); |
353 EXPECT_SKRECT_EQ(50, 60, 70, 80, | 345 EXPECT_SKRECT_EQ(50, 60, 70, 80, |
354 operations[0].rect); // FIXME: the rect should be clipped. | 346 operations[0].rect); // FIXME: the rect should be clipped. |
355 EXPECT_EQ(MockCanvas::kDrawRect, operations[1].type); | 347 EXPECT_EQ(MockCanvas::kDrawRect, operations[1].type); |
356 EXPECT_SKRECT_EQ(150, 160, 170, 180, operations[1].rect); | 348 EXPECT_SKRECT_EQ(150, 160, 170, 180, operations[1].rect); |
357 EXPECT_EQ(MockCanvas::kDrawRect, operations[2].type); | 349 EXPECT_EQ(MockCanvas::kDrawRect, operations[2].type); |
358 EXPECT_SKRECT_EQ(250, 260, 270, 280, operations[2].rect); | 350 EXPECT_SKRECT_EQ(250, 260, 270, 280, operations[2].rect); |
359 } | 351 } |
360 | 352 |
361 } // namespace blink | 353 } // namespace blink |
OLD | NEW |