| 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 "config.h" | 5 #include "config.h" |
| 6 #include "core/page/PrintContext.h" | 6 #include "core/page/PrintContext.h" |
| 7 | 7 |
| 8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 9 #include "core/frame/FrameHost.h" |
| 10 #include "core/frame/FrameView.h" |
| 9 #include "core/html/HTMLElement.h" | 11 #include "core/html/HTMLElement.h" |
| 12 #include "core/html/HTMLIFrameElement.h" |
| 13 #include "core/loader/EmptyClients.h" |
| 10 #include "core/testing/DummyPageHolder.h" | 14 #include "core/testing/DummyPageHolder.h" |
| 11 #include "platform/graphics/GraphicsContext.h" | 15 #include "platform/graphics/GraphicsContext.h" |
| 12 #include "platform/testing/SkiaForCoreTesting.h" | 16 #include "platform/testing/SkiaForCoreTesting.h" |
| 13 #include "platform/text/TextStream.h" | 17 #include "platform/text/TextStream.h" |
| 14 #include <gtest/gtest.h> | 18 #include <gtest/gtest.h> |
| 15 | 19 |
| 16 namespace blink { | 20 namespace blink { |
| 17 | 21 |
| 18 const int kPageWidth = 800; | 22 const int kPageWidth = 800; |
| 19 const int kPageHeight = 600; | 23 const int kPageHeight = 600; |
| 20 | 24 |
| 21 class MockPrintContext : public PrintContext { | 25 class MockPrintContext : public PrintContext { |
| 22 public: | 26 public: |
| 23 MockPrintContext(LocalFrame* frame) : PrintContext(frame) { } | 27 MockPrintContext(LocalFrame* frame) : PrintContext(frame) { } |
| 24 | 28 |
| 25 void outputLinkAndLinkedDestinations(GraphicsContext& context, Node* node, c
onst IntRect& pageRect) | 29 void outputLinkAndLinkedDestinations(GraphicsContext& context, const IntRect
& pageRect) |
| 26 { | 30 { |
| 27 PrintContext::outputLinkAndLinkedDestinations(context, node, pageRect); | 31 PrintContext::outputLinkAndLinkedDestinations(context, pageRect); |
| 28 } | 32 } |
| 29 }; | 33 }; |
| 30 | 34 |
| 31 class MockCanvas : public SkCanvas { | 35 class MockCanvas : public SkCanvas { |
| 32 public: | 36 public: |
| 33 enum OperationType { | 37 enum OperationType { |
| 34 DrawRect, | 38 DrawRect, |
| 35 DrawPoint | 39 DrawPoint |
| 36 }; | 40 }; |
| 37 | 41 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 58 } | 62 } |
| 59 | 63 |
| 60 const Vector<Operation>& recordedOperations() const { return m_recordedOpera
tions; } | 64 const Vector<Operation>& recordedOperations() const { return m_recordedOpera
tions; } |
| 61 | 65 |
| 62 private: | 66 private: |
| 63 Vector<Operation> m_recordedOperations; | 67 Vector<Operation> m_recordedOperations; |
| 64 }; | 68 }; |
| 65 | 69 |
| 66 class PrintContextTest : public testing::Test { | 70 class PrintContextTest : public testing::Test { |
| 67 protected: | 71 protected: |
| 68 PrintContextTest() | 72 PrintContextTest(PassOwnPtr<FrameLoaderClient> frameLoaderClient = PassOwnPt
r<FrameLoaderClient>()) |
| 69 : m_pageHolder(DummyPageHolder::create(IntSize(kPageWidth, kPageHeight))
) | 73 : m_pageHolder(DummyPageHolder::create(IntSize(kPageWidth, kPageHeight),
nullptr, frameLoaderClient)) |
| 70 , m_printContext(adoptPtrWillBeNoop(new MockPrintContext(document().fram
e()))) { } | 74 , m_printContext(adoptPtrWillBeNoop(new MockPrintContext(document().fram
e()))) { } |
| 71 | 75 |
| 72 Document& document() const { return m_pageHolder->document(); } | 76 Document& document() const { return m_pageHolder->document(); } |
| 73 MockPrintContext& printContext() { return *m_printContext.get(); } | 77 MockPrintContext& printContext() { return *m_printContext.get(); } |
| 74 | 78 |
| 75 void setBodyInnerHTML(String bodyContent) | 79 void setBodyInnerHTML(String bodyContent) |
| 76 { | 80 { |
| 77 document().body()->setInnerHTML(bodyContent, ASSERT_NO_EXCEPTION); | 81 document().body()->setInnerHTML(bodyContent, ASSERT_NO_EXCEPTION); |
| 78 } | 82 } |
| 79 | 83 |
| 80 void printSinglePage(SkCanvas& canvas) | 84 void printSinglePage(SkCanvas& canvas) |
| 81 { | 85 { |
| 82 IntRect pageRect(0, 0, kPageWidth, kPageHeight); | 86 IntRect pageRect(0, 0, kPageWidth, kPageHeight); |
| 83 GraphicsContext context(&canvas, nullptr); | 87 GraphicsContext context(&canvas, nullptr); |
| 84 printContext().begin(kPageWidth, kPageHeight); | 88 printContext().begin(kPageWidth, kPageHeight); |
| 85 printContext().outputLinkAndLinkedDestinations(context, &document(), pag
eRect); | 89 printContext().outputLinkAndLinkedDestinations(context, pageRect); |
| 86 printContext().end(); | 90 printContext().end(); |
| 87 } | 91 } |
| 88 | 92 |
| 89 static String htmlForLink(int x, int y, int width, int height, const char* u
rl) | 93 static String htmlForLink(int x, int y, int width, int height, const char* u
rl) |
| 90 { | 94 { |
| 91 TextStream ts; | 95 TextStream ts; |
| 92 ts << "<a style='position: absolute; left: " << x << "px; top: " << y <<
"px; width: " << width << "px; height: " << height | 96 ts << "<a style='position: absolute; left: " << x << "px; top: " << y <<
"px; width: " << width << "px; height: " << height |
| 93 << "px' href='" << url << "'>" << url << "</a>"; | 97 << "px' href='" << url << "'>" << url << "</a>"; |
| 94 return ts.release(); | 98 return ts.release(); |
| 95 } | 99 } |
| 96 | 100 |
| 97 static String htmlForAnchor(int x, int y, const char* name) | 101 static String htmlForAnchor(int x, int y, const char* name) |
| 98 { | 102 { |
| 99 TextStream ts; | 103 TextStream ts; |
| 100 ts << "<a name='" << name << "' style='position: absolute; left: " << x
<< "px; top: " << y << "px'>" << name << "</a>"; | 104 ts << "<a name='" << name << "' style='position: absolute; left: " << x
<< "px; top: " << y << "px'>" << name << "</a>"; |
| 101 return ts.release(); | 105 return ts.release(); |
| 102 } | 106 } |
| 103 | 107 |
| 104 private: | 108 private: |
| 105 OwnPtr<DummyPageHolder> m_pageHolder; | 109 OwnPtr<DummyPageHolder> m_pageHolder; |
| 106 OwnPtrWillBePersistent<MockPrintContext> m_printContext; | 110 OwnPtrWillBePersistent<MockPrintContext> m_printContext; |
| 107 }; | 111 }; |
| 108 | 112 |
| 113 class SingleChildFrameLoaderClient : public EmptyFrameLoaderClient { |
| 114 public: |
| 115 SingleChildFrameLoaderClient() : m_child(nullptr) { } |
| 116 |
| 117 virtual Frame* firstChild() const override { return m_child; } |
| 118 virtual Frame* lastChild() const override { return m_child; } |
| 119 |
| 120 void setChild(Frame* child) { m_child = child; } |
| 121 |
| 122 private: |
| 123 Frame* m_child; |
| 124 }; |
| 125 |
| 126 class FrameLoaderClientWithParent : public EmptyFrameLoaderClient { |
| 127 public: |
| 128 FrameLoaderClientWithParent(Frame* parent) : m_parent(parent) { } |
| 129 |
| 130 virtual Frame* parent() const override { return m_parent; } |
| 131 |
| 132 private: |
| 133 Frame* m_parent; |
| 134 }; |
| 135 |
| 136 class PrintContextFrameTest : public PrintContextTest { |
| 137 public: |
| 138 PrintContextFrameTest() : PrintContextTest(adoptPtr(new SingleChildFrameLoad
erClient())) { } |
| 139 }; |
| 140 |
| 109 #define EXPECT_SKRECT_EQ(expectedX, expectedY, expectedWidth, expectedHeight, ac
tualRect) \ | 141 #define EXPECT_SKRECT_EQ(expectedX, expectedY, expectedWidth, expectedHeight, ac
tualRect) \ |
| 110 EXPECT_EQ(expectedX, actualRect.x()); \ | 142 EXPECT_EQ(expectedX, actualRect.x()); \ |
| 111 EXPECT_EQ(expectedY, actualRect.y()); \ | 143 EXPECT_EQ(expectedY, actualRect.y()); \ |
| 112 EXPECT_EQ(expectedWidth, actualRect.width()); \ | 144 EXPECT_EQ(expectedWidth, actualRect.width()); \ |
| 113 EXPECT_EQ(expectedHeight, actualRect.height()); | 145 EXPECT_EQ(expectedHeight, actualRect.height()); |
| 114 | 146 |
| 115 TEST_F(PrintContextTest, LinkTarget) | 147 TEST_F(PrintContextTest, LinkTarget) |
| 116 { | 148 { |
| 117 MockCanvas canvas; | 149 MockCanvas canvas; |
| 118 setBodyInnerHTML(htmlForLink(50, 60, 70, 80, "http://www.google.com") | 150 setBodyInnerHTML(htmlForLink(50, 60, 70, 80, "http://www.google.com") |
| (...skipping 30 matching lines...) Expand all Loading... |
| 149 | 181 |
| 150 size_t firstIndex = operations[0].rect.x() == 50 ? 0 : 1; | 182 size_t firstIndex = operations[0].rect.x() == 50 ? 0 : 1; |
| 151 EXPECT_EQ(MockCanvas::DrawRect, operations[firstIndex].type); | 183 EXPECT_EQ(MockCanvas::DrawRect, operations[firstIndex].type); |
| 152 EXPECT_SKRECT_EQ(50, 60, 70, 80, operations[firstIndex].rect); | 184 EXPECT_SKRECT_EQ(50, 60, 70, 80, operations[firstIndex].rect); |
| 153 | 185 |
| 154 size_t secondIndex = firstIndex == 0 ? 1 : 0; | 186 size_t secondIndex = firstIndex == 0 ? 1 : 0; |
| 155 EXPECT_EQ(MockCanvas::DrawPoint, operations[secondIndex].type); | 187 EXPECT_EQ(MockCanvas::DrawPoint, operations[secondIndex].type); |
| 156 EXPECT_SKRECT_EQ(250, 260, 0, 0, operations[secondIndex].rect); | 188 EXPECT_SKRECT_EQ(250, 260, 0, 0, operations[secondIndex].rect); |
| 157 } | 189 } |
| 158 | 190 |
| 191 TEST_F(PrintContextFrameTest, WithSubframe) |
| 192 { |
| 193 MockCanvas canvas; |
| 194 document().setBaseURLOverride(KURL(ParsedURLString, "http://a.com/")); |
| 195 setBodyInnerHTML("<iframe id='frame' src='http://b.com/' width='500' height=
'500'></iframe>"); |
| 196 |
| 197 HTMLIFrameElement& iframe = *toHTMLIFrameElement(document().getElementById("
frame")); |
| 198 OwnPtr<FrameLoaderClient> frameLoaderClient = adoptPtr(new FrameLoaderClient
WithParent(document().frame())); |
| 199 RefPtrWillBePersistent<LocalFrame> subframe = LocalFrame::create(frameLoader
Client.get(), document().frame()->host(), &iframe); |
| 200 subframe->setView(FrameView::create(subframe.get(), IntSize(500, 500))); |
| 201 subframe->init(); |
| 202 static_cast<SingleChildFrameLoaderClient*>(document().frame()->client())->se
tChild(subframe.get()); |
| 203 document().frame()->host()->incrementSubframeCount(); |
| 204 |
| 205 Document& frameDocument = *iframe.contentDocument(); |
| 206 frameDocument.setBaseURLOverride(KURL(ParsedURLString, "http://b.com/")); |
| 207 frameDocument.body()->setInnerHTML(htmlForLink(50, 60, 70, 80, "#fragment") |
| 208 + htmlForLink(150, 160, 170, 180, "http://www.google.com") |
| 209 + htmlForLink(250, 260, 270, 280, "http://www.google.com#fragment"), |
| 210 ASSERT_NO_EXCEPTION); |
| 211 printSinglePage(canvas); |
| 212 |
| 213 const Vector<MockCanvas::Operation>& operations = canvas.recordedOperations(
); |
| 214 ASSERT_EQ(2u, operations.size()); |
| 215 |
| 216 size_t firstIndex = operations[0].rect.x() == 150 ? 0 : 1; |
| 217 EXPECT_EQ(MockCanvas::DrawRect, operations[firstIndex].type); |
| 218 EXPECT_SKRECT_EQ(150, 160, 170, 180, operations[firstIndex].rect); |
| 219 |
| 220 size_t secondIndex = firstIndex == 0 ? 1 : 0; |
| 221 EXPECT_EQ(MockCanvas::DrawRect, operations[secondIndex].type); |
| 222 EXPECT_SKRECT_EQ(250, 260, 270, 280, operations[secondIndex].rect); |
| 223 |
| 224 subframe->detach(); |
| 225 static_cast<SingleChildFrameLoaderClient*>(document().frame()->client())->se
tChild(nullptr); |
| 226 document().frame()->host()->decrementSubframeCount(); |
| 227 } |
| 228 |
| 159 } // namespace blink | 229 } // namespace blink |
| OLD | NEW |