Index: third_party/WebKit/Source/core/html/parser/HTMLDocumentParserTest.cpp |
diff --git a/third_party/WebKit/Source/core/html/parser/HTMLDocumentParserTest.cpp b/third_party/WebKit/Source/core/html/parser/HTMLDocumentParserTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..04f3039c9ff883f3826a01f219a847bf5f884aa9 |
--- /dev/null |
+++ b/third_party/WebKit/Source/core/html/parser/HTMLDocumentParserTest.cpp |
@@ -0,0 +1,120 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "core/html/parser/HTMLDocumentParser.h" |
+ |
+#include "core/html/HTMLDocument.h" |
+#include "core/html/parser/TextResourceDecoder.h" |
+#include "core/loader/PrerendererClient.h" |
+#include "core/loader/TextResourceDecoderBuilder.h" |
+#include "core/testing/DummyPageHolder.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include <memory> |
+ |
+namespace blink { |
+ |
+namespace { |
+ |
Charlie Harrison
2016/08/03 14:40:04
So, it is against Blink style to indent in a neste
droger
2016/08/03 16:45:50
Yes, `git cl format` did this.
Done.
|
+ class TestPrerendererClient : |
+ public GarbageCollected<TestPrerendererClient>, |
+ public PrerendererClient { |
+ USING_GARBAGE_COLLECTED_MIXIN(TestPrerendererClient); |
+ |
+ public: |
+ TestPrerendererClient(bool isPrefetchOnly) |
+ : m_isPrefetchOnly(isPrefetchOnly) |
+ { |
+ } |
+ |
+ private: |
+ void willAddPrerender(Prerender*) override {}; |
+ bool isPrefetchOnly() override { return m_isPrefetchOnly; } |
+ |
+ bool m_isPrefetchOnly; |
+ }; |
+ |
+ class HTMLDocumentParserTest : public testing::Test { |
+ protected: |
+ HTMLDocumentParserTest() |
+ : m_dummyPageHolder(DummyPageHolder::create()) |
+ { |
+ } |
+ |
+ void SetupDecoder(HTMLDocumentParser* parser, HTMLDocument& document) |
+ { |
+ TextResourceDecoderBuilder decoderBuilder("text/html", nullAtom); |
+ std::unique_ptr<TextResourceDecoder> decoder( |
+ decoderBuilder.buildFor(&document)); |
+ parser->setDecoder(std::move(decoder)); |
+ } |
+ |
+ std::unique_ptr<DummyPageHolder> m_dummyPageHolder; |
+ }; |
+ |
+} // namespace |
+ |
+TEST_F(HTMLDocumentParserTest, ShouldUseThreadingDefault) |
+{ |
+ // Allow multi-threading by default. |
+ HTMLDocument& document = toHTMLDocument(m_dummyPageHolder->document()); |
+ EXPECT_FALSE(document.isPrefetchOnly()); |
+ EXPECT_TRUE(HTMLDocumentParser::threadingAllowedForDocument(document)); |
+} |
+ |
+TEST_F(HTMLDocumentParserTest, ShouldUseThreadingPrefetch) |
+{ |
+ // Prerenderer client in prefetch mode forces single thread mode. |
+ HTMLDocument& document = toHTMLDocument(m_dummyPageHolder->document()); |
+ providePrerendererClientTo(*document.page(), |
+ new TestPrerendererClient(true)); |
+ EXPECT_TRUE(document.isPrefetchOnly()); |
+ EXPECT_FALSE(HTMLDocumentParser::threadingAllowedForDocument(document)); |
+} |
+ |
+TEST_F(HTMLDocumentParserTest, ShouldUseThreadingPrerender) |
+{ |
+ // Prerenderer client in prerender mode allows multi threading. |
+ HTMLDocument& document = toHTMLDocument(m_dummyPageHolder->document()); |
+ providePrerendererClientTo(*document.page(), |
+ new TestPrerendererClient(false)); |
+ EXPECT_FALSE(document.isPrefetchOnly()); |
+ EXPECT_TRUE(HTMLDocumentParser::threadingAllowedForDocument(document)); |
+} |
+ |
+TEST_F(HTMLDocumentParserTest, AppendPrefetch) |
+{ |
+ HTMLDocument& document = toHTMLDocument(m_dummyPageHolder->document()); |
+ providePrerendererClientTo(*document.page(), |
+ new TestPrerendererClient(true)); |
+ EXPECT_TRUE(document.isPrefetchOnly()); |
+ HTMLDocumentParser* parser = |
+ HTMLDocumentParser::create(document, AllowAsynchronousParsing); |
+ SetupDecoder(parser, document); |
+ |
+ const char bytes[] = "<ht"; |
+ parser->appendBytes(bytes, sizeof(bytes)); |
+ // The bytes are forwarded to the preload scanner, not to the tokenizer. |
+ HTMLScriptRunnerHost* scriptRunnerHost = parser->asHTMLScriptRunnerHost(); |
+ EXPECT_TRUE(scriptRunnerHost->hasPreloadScanner()); |
Charlie Harrison
2016/08/03 14:40:04
might make more sense to be checking m_haveBackgro
droger
2016/08/03 16:45:50
Why checking the background parser? Since we are i
Charlie Harrison
2016/08/03 17:15:14
Ah nevermind, makes sense.
|
+ EXPECT_EQ(HTMLTokenizer::DataState, parser->tokenizer()->getState()); |
droger
2016/08/03 16:45:50
This is intended to check that the page has is not
Charlie Harrison
2016/08/03 17:15:15
Nice! Yeah that works.
|
+} |
+ |
+TEST_F(HTMLDocumentParserTest, AppendNoPrefetch) |
+{ |
+ HTMLDocument& document = toHTMLDocument(m_dummyPageHolder->document()); |
+ EXPECT_FALSE(document.isPrefetchOnly()); |
+ // Use ForceSynchronousParsing to allow calling append(). |
+ HTMLDocumentParser* parser = |
+ HTMLDocumentParser::create(document, ForceSynchronousParsing); |
+ SetupDecoder(parser, document); |
+ |
+ const char bytes[] = "<ht"; |
+ parser->appendBytes(bytes, sizeof(bytes)); |
+ // The bytes are forwarded to the tokenizer. |
+ HTMLScriptRunnerHost* scriptRunnerHost = parser->asHTMLScriptRunnerHost(); |
+ EXPECT_FALSE(scriptRunnerHost->hasPreloadScanner()); |
+ EXPECT_EQ(HTMLTokenizer::TagNameState, parser->tokenizer()->getState()); |
+} |
+ |
+} // namespace blink |