Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "core/html/parser/HTMLDocumentParser.h" | |
| 6 | |
| 7 #include "core/html/HTMLDocument.h" | |
| 8 #include "core/html/parser/TextResourceDecoder.h" | |
| 9 #include "core/loader/PrerendererClient.h" | |
| 10 #include "core/loader/TextResourceDecoderBuilder.h" | |
| 11 #include "core/testing/DummyPageHolder.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 #include <memory> | |
| 14 | |
| 15 namespace blink { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 class TestPrerendererClient : | |
| 20 public GarbageCollected<TestPrerendererClient>, public PrerendererClient { | |
| 21 USING_GARBAGE_COLLECTED_MIXIN(TestPrerendererClient); | |
| 22 | |
| 23 public: | |
| 24 TestPrerendererClient(bool isPrefetchOnly) | |
| 25 : m_isPrefetchOnly(isPrefetchOnly) | |
| 26 { | |
| 27 } | |
| 28 | |
| 29 private: | |
| 30 void willAddPrerender(Prerender*) override {}; | |
| 31 bool isPrefetchOnly() override { return m_isPrefetchOnly; } | |
| 32 | |
| 33 bool m_isPrefetchOnly; | |
| 34 }; | |
| 35 | |
| 36 class HTMLDocumentParserTest : public testing::Test { | |
| 37 protected: | |
| 38 HTMLDocumentParserTest() | |
| 39 : m_dummyPageHolder(DummyPageHolder::create()) | |
| 40 { | |
| 41 } | |
| 42 | |
| 43 HTMLDocumentParser* CreateParser(HTMLDocument& document) | |
|
Yoav Weiss
2016/08/23 20:03:58
nit: s/CreateParser/createParser/ in order to matc
droger
2016/08/24 11:33:45
Done.
| |
| 44 { | |
| 45 HTMLDocumentParser* parser = | |
| 46 HTMLDocumentParser::create(document, ForceSynchronousParsing); | |
| 47 TextResourceDecoderBuilder decoderBuilder("text/html", nullAtom); | |
| 48 std::unique_ptr<TextResourceDecoder> decoder( | |
| 49 decoderBuilder.buildFor(&document)); | |
| 50 parser->setDecoder(std::move(decoder)); | |
| 51 return parser; | |
| 52 } | |
| 53 | |
| 54 std::unique_ptr<DummyPageHolder> m_dummyPageHolder; | |
| 55 }; | |
| 56 | |
| 57 } // namespace | |
| 58 | |
| 59 TEST_F(HTMLDocumentParserTest, AppendPrefetch) | |
| 60 { | |
| 61 HTMLDocument& document = toHTMLDocument(m_dummyPageHolder->document()); | |
| 62 providePrerendererClientTo(*document.page(), | |
| 63 new TestPrerendererClient(true)); | |
| 64 EXPECT_TRUE(document.isPrefetchOnly()); | |
| 65 HTMLDocumentParser* parser = CreateParser(document); | |
| 66 | |
| 67 const char bytes[] = "<ht"; | |
| 68 parser->appendBytes(bytes, sizeof(bytes)); | |
| 69 // The bytes are forwarded to the preload scanner, not to the tokenizer. | |
| 70 HTMLScriptRunnerHost* scriptRunnerHost = | |
| 71 parser->asHTMLScriptRunnerHostForTesting(); | |
| 72 EXPECT_TRUE(scriptRunnerHost->hasPreloadScanner()); | |
| 73 EXPECT_EQ(HTMLTokenizer::DataState, parser->tokenizer()->getState()); | |
| 74 } | |
| 75 | |
| 76 TEST_F(HTMLDocumentParserTest, AppendNoPrefetch) | |
| 77 { | |
| 78 HTMLDocument& document = toHTMLDocument(m_dummyPageHolder->document()); | |
| 79 EXPECT_FALSE(document.isPrefetchOnly()); | |
| 80 // Use ForceSynchronousParsing to allow calling append(). | |
| 81 HTMLDocumentParser* parser = CreateParser(document); | |
| 82 | |
| 83 const char bytes[] = "<ht"; | |
| 84 parser->appendBytes(bytes, sizeof(bytes)); | |
| 85 // The bytes are forwarded to the tokenizer. | |
| 86 HTMLScriptRunnerHost* scriptRunnerHost = | |
| 87 parser->asHTMLScriptRunnerHostForTesting(); | |
| 88 EXPECT_FALSE(scriptRunnerHost->hasPreloadScanner()); | |
| 89 EXPECT_EQ(HTMLTokenizer::TagNameState, parser->tokenizer()->getState()); | |
| 90 } | |
| 91 | |
| 92 } // namespace blink | |
| OLD | NEW |