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 | |
|
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.
| |
| 19 class TestPrerendererClient : | |
| 20 public GarbageCollected<TestPrerendererClient>, | |
| 21 public PrerendererClient { | |
| 22 USING_GARBAGE_COLLECTED_MIXIN(TestPrerendererClient); | |
| 23 | |
| 24 public: | |
| 25 TestPrerendererClient(bool isPrefetchOnly) | |
| 26 : m_isPrefetchOnly(isPrefetchOnly) | |
| 27 { | |
| 28 } | |
| 29 | |
| 30 private: | |
| 31 void willAddPrerender(Prerender*) override {}; | |
| 32 bool isPrefetchOnly() override { return m_isPrefetchOnly; } | |
| 33 | |
| 34 bool m_isPrefetchOnly; | |
| 35 }; | |
| 36 | |
| 37 class HTMLDocumentParserTest : public testing::Test { | |
| 38 protected: | |
| 39 HTMLDocumentParserTest() | |
| 40 : m_dummyPageHolder(DummyPageHolder::create()) | |
| 41 { | |
| 42 } | |
| 43 | |
| 44 void SetupDecoder(HTMLDocumentParser* parser, HTMLDocument& document) | |
| 45 { | |
| 46 TextResourceDecoderBuilder decoderBuilder("text/html", nullAtom); | |
| 47 std::unique_ptr<TextResourceDecoder> decoder( | |
| 48 decoderBuilder.buildFor(&document)); | |
| 49 parser->setDecoder(std::move(decoder)); | |
| 50 } | |
| 51 | |
| 52 std::unique_ptr<DummyPageHolder> m_dummyPageHolder; | |
| 53 }; | |
| 54 | |
| 55 } // namespace | |
| 56 | |
| 57 TEST_F(HTMLDocumentParserTest, ShouldUseThreadingDefault) | |
| 58 { | |
| 59 // Allow multi-threading by default. | |
| 60 HTMLDocument& document = toHTMLDocument(m_dummyPageHolder->document()); | |
| 61 EXPECT_FALSE(document.isPrefetchOnly()); | |
| 62 EXPECT_TRUE(HTMLDocumentParser::threadingAllowedForDocument(document)); | |
| 63 } | |
| 64 | |
| 65 TEST_F(HTMLDocumentParserTest, ShouldUseThreadingPrefetch) | |
| 66 { | |
| 67 // Prerenderer client in prefetch mode forces single thread mode. | |
| 68 HTMLDocument& document = toHTMLDocument(m_dummyPageHolder->document()); | |
| 69 providePrerendererClientTo(*document.page(), | |
| 70 new TestPrerendererClient(true)); | |
| 71 EXPECT_TRUE(document.isPrefetchOnly()); | |
| 72 EXPECT_FALSE(HTMLDocumentParser::threadingAllowedForDocument(document)); | |
| 73 } | |
| 74 | |
| 75 TEST_F(HTMLDocumentParserTest, ShouldUseThreadingPrerender) | |
| 76 { | |
| 77 // Prerenderer client in prerender mode allows multi threading. | |
| 78 HTMLDocument& document = toHTMLDocument(m_dummyPageHolder->document()); | |
| 79 providePrerendererClientTo(*document.page(), | |
| 80 new TestPrerendererClient(false)); | |
| 81 EXPECT_FALSE(document.isPrefetchOnly()); | |
| 82 EXPECT_TRUE(HTMLDocumentParser::threadingAllowedForDocument(document)); | |
| 83 } | |
| 84 | |
| 85 TEST_F(HTMLDocumentParserTest, AppendPrefetch) | |
| 86 { | |
| 87 HTMLDocument& document = toHTMLDocument(m_dummyPageHolder->document()); | |
| 88 providePrerendererClientTo(*document.page(), | |
| 89 new TestPrerendererClient(true)); | |
| 90 EXPECT_TRUE(document.isPrefetchOnly()); | |
| 91 HTMLDocumentParser* parser = | |
| 92 HTMLDocumentParser::create(document, AllowAsynchronousParsing); | |
| 93 SetupDecoder(parser, document); | |
| 94 | |
| 95 const char bytes[] = "<ht"; | |
| 96 parser->appendBytes(bytes, sizeof(bytes)); | |
| 97 // The bytes are forwarded to the preload scanner, not to the tokenizer. | |
| 98 HTMLScriptRunnerHost* scriptRunnerHost = parser->asHTMLScriptRunnerHost(); | |
| 99 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.
| |
| 100 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.
| |
| 101 } | |
| 102 | |
| 103 TEST_F(HTMLDocumentParserTest, AppendNoPrefetch) | |
| 104 { | |
| 105 HTMLDocument& document = toHTMLDocument(m_dummyPageHolder->document()); | |
| 106 EXPECT_FALSE(document.isPrefetchOnly()); | |
| 107 // Use ForceSynchronousParsing to allow calling append(). | |
| 108 HTMLDocumentParser* parser = | |
| 109 HTMLDocumentParser::create(document, ForceSynchronousParsing); | |
| 110 SetupDecoder(parser, document); | |
| 111 | |
| 112 const char bytes[] = "<ht"; | |
| 113 parser->appendBytes(bytes, sizeof(bytes)); | |
| 114 // The bytes are forwarded to the tokenizer. | |
| 115 HTMLScriptRunnerHost* scriptRunnerHost = parser->asHTMLScriptRunnerHost(); | |
| 116 EXPECT_FALSE(scriptRunnerHost->hasPreloadScanner()); | |
| 117 EXPECT_EQ(HTMLTokenizer::TagNameState, parser->tokenizer()->getState()); | |
| 118 } | |
| 119 | |
| 120 } // namespace blink | |
| OLD | NEW |