Commit e346670f authored by Nate Chapin's avatar Nate Chapin Committed by Commit Bot

Have ScriptStreamerTest go through ClassicPendingScript::Fetch()

This lets us get rid of the last non-trival usage of
ResourceClient::SetResource in ClassicPendingScript

Bug: 793028
Change-Id: I39a4859cb1b941ff6a399fad88d40e353df4d462
Reviewed-on: https://chromium-review.googlesource.com/822176Reviewed-by: default avatarHiroshige Hayashizaki <hiroshige@chromium.org>
Commit-Queue: Nate Chapin <japhet@chromium.org>
Cr-Commit-Position: refs/heads/master@{#523569}
parent 1beaf80f
......@@ -15,12 +15,16 @@
#include "core/dom/ClassicScript.h"
#include "core/dom/MockScriptElementBase.h"
#include "core/frame/Settings.h"
#include "core/testing/DummyPageHolder.h"
#include "platform/exported/WrappedResourceResponse.h"
#include "platform/heap/Handle.h"
#include "platform/loader/fetch/ResourceLoader.h"
#include "platform/loader/fetch/ScriptFetchOptions.h"
#include "platform/scheduler/child/web_scheduler.h"
#include "platform/testing/UnitTestHelpers.h"
#include "platform/wtf/text/TextEncoding.h"
#include "public/platform/Platform.h"
#include "public/platform/WebURLLoaderMockFactory.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "v8/include/v8.h"
......@@ -35,24 +39,26 @@ class ScriptStreamingTest : public ::testing::Test {
->CurrentThread()
->Scheduler()
->LoadingTaskRunner()),
settings_(Settings::Create()),
dummy_document_(Document::CreateForTest()) {
resource_ = ScriptResource::CreateForTest(
KURL("http://www.streaming-test.com/"), UTF8Encoding());
resource_->SetStatus(ResourceStatus::kPending);
dummy_page_holder_(DummyPageHolder::Create(IntSize(800, 600))) {
dummy_page_holder_->GetPage().GetSettings().SetScriptEnabled(true);
MockScriptElementBase* element = MockScriptElementBase::Create();
// Basically we are not interested in ScriptElementBase* calls, just making
// the method(s) to return default values.
EXPECT_CALL(*element, IntegrityAttributeValue())
.WillRepeatedly(::testing::Return(String()));
EXPECT_CALL(*element, GetDocument())
.WillRepeatedly(::testing::ReturnRef(*dummy_document_.Get()));
.WillRepeatedly(
::testing::ReturnRef(dummy_page_holder_->GetDocument()));
EXPECT_CALL(*element, Loader()).WillRepeatedly(::testing::Return(nullptr));
pending_script_ =
ClassicPendingScript::CreateExternalForTest(element, resource_.Get());
KURL url("http://www.streaming-test.com/");
Platform::Current()->GetURLLoaderMockFactory()->RegisterURL(
url, WrappedResourceResponse(ResourceResponse()), "");
pending_script_ = ClassicPendingScript::Fetch(
url, dummy_page_holder_->GetDocument(), ScriptFetchOptions(),
WTF::TextEncoding(), element, FetchParameters::kNoDefer);
ScriptStreamer::SetSmallScriptThresholdForTesting(0);
Platform::Current()->GetURLLoaderMockFactory()->UnregisterURL(url);
}
~ScriptStreamingTest() {
......@@ -64,9 +70,17 @@ class ScriptStreamingTest : public ::testing::Test {
return pending_script_.Get();
}
Settings* GetSettings() const {
return &dummy_page_holder_->GetPage().GetSettings();
}
ScriptResource* GetResource() const {
return ToScriptResource(pending_script_->GetResource());
}
protected:
void AppendData(const char* data) {
resource_->AppendData(data, strlen(data));
GetResource()->AppendData(data, strlen(data));
// Yield control to the background thread, so that V8 gets a chance to
// process the data before the main thread adds more. Note that we
// cannot fully control in what kind of chunks the data is passed to V8
......@@ -85,8 +99,8 @@ class ScriptStreamingTest : public ::testing::Test {
}
void Finish() {
resource_->FinishForTest();
resource_->SetStatus(ResourceStatus::kCached);
GetResource()->Loader()->DidFinishLoading(0.0, 0, 0, 0);
GetResource()->SetStatus(ResourceStatus::kCached);
}
void ProcessTasksUntilStreamingComplete() {
......@@ -99,14 +113,12 @@ class ScriptStreamingTest : public ::testing::Test {
}
scoped_refptr<WebTaskRunner> loading_task_runner_;
std::unique_ptr<Settings> settings_;
// The Resource and PendingScript where we stream from. These don't really
// The PendingScript where we stream from. These don't really
// fetch any data outside the test; the test controls the data by calling
// ScriptResource::appendData.
Persistent<ScriptResource> resource_;
// ScriptResource::AppendData.
Persistent<ClassicPendingScript> pending_script_;
Persistent<Document> dummy_document_;
std::unique_ptr<DummyPageHolder> dummy_page_holder_;
};
class TestPendingScriptClient final
......@@ -127,7 +139,7 @@ TEST_F(ScriptStreamingTest, CompilingStreamedScript) {
// Test that we can successfully compile a streamed script.
V8TestingScope scope;
ScriptStreamer::StartStreaming(
GetPendingScript(), ScriptStreamer::kParsingBlocking, settings_.get(),
GetPendingScript(), ScriptStreamer::kParsingBlocking, GetSettings(),
scope.GetScriptState(), loading_task_runner_);
TestPendingScriptClient* client = new TestPendingScriptClient;
GetPendingScript()->WatchForLoad(client);
......@@ -165,7 +177,7 @@ TEST_F(ScriptStreamingTest, CompilingStreamedScriptWithParseError) {
// handle it gracefully.
V8TestingScope scope;
ScriptStreamer::StartStreaming(
GetPendingScript(), ScriptStreamer::kParsingBlocking, settings_.get(),
GetPendingScript(), ScriptStreamer::kParsingBlocking, GetSettings(),
scope.GetScriptState(), loading_task_runner_);
TestPendingScriptClient* client = new TestPendingScriptClient;
GetPendingScript()->WatchForLoad(client);
......@@ -204,7 +216,7 @@ TEST_F(ScriptStreamingTest, CancellingStreaming) {
// while streaming is ongoing, and ScriptStreamer handles it gracefully.
V8TestingScope scope;
ScriptStreamer::StartStreaming(
GetPendingScript(), ScriptStreamer::kParsingBlocking, settings_.get(),
GetPendingScript(), ScriptStreamer::kParsingBlocking, GetSettings(),
scope.GetScriptState(), loading_task_runner_);
TestPendingScriptClient* client = new TestPendingScriptClient;
GetPendingScript()->WatchForLoad(client);
......@@ -219,7 +231,6 @@ TEST_F(ScriptStreamingTest, CancellingStreaming) {
EXPECT_FALSE(client->Finished());
GetPendingScript()->Dispose();
pending_script_ = nullptr; // This will destroy m_resource.
resource_ = nullptr;
// The V8 side will complete too. This should not crash. We don't receive
// any results from the streaming and the client doesn't get notified.
......@@ -234,14 +245,14 @@ TEST_F(ScriptStreamingTest, SuppressingStreaming) {
// script is loaded.
V8TestingScope scope;
ScriptStreamer::StartStreaming(
GetPendingScript(), ScriptStreamer::kParsingBlocking, settings_.get(),
GetPendingScript(), ScriptStreamer::kParsingBlocking, GetSettings(),
scope.GetScriptState(), loading_task_runner_);
TestPendingScriptClient* client = new TestPendingScriptClient;
GetPendingScript()->WatchForLoad(client);
AppendData("function foo() {");
AppendPadding();
CachedMetadataHandler* cache_handler = resource_->CacheHandler();
CachedMetadataHandler* cache_handler = GetResource()->CacheHandler();
EXPECT_TRUE(cache_handler);
cache_handler->SetCachedMetadata(
V8ScriptRunner::TagForCodeCache(cache_handler), "X", 1,
......@@ -269,7 +280,7 @@ TEST_F(ScriptStreamingTest, EmptyScripts) {
// loaded.
V8TestingScope scope;
ScriptStreamer::StartStreaming(
GetPendingScript(), ScriptStreamer::kParsingBlocking, settings_.get(),
GetPendingScript(), ScriptStreamer::kParsingBlocking, GetSettings(),
scope.GetScriptState(), loading_task_runner_);
TestPendingScriptClient* client = new TestPendingScriptClient;
GetPendingScript()->WatchForLoad(client);
......@@ -294,7 +305,7 @@ TEST_F(ScriptStreamingTest, SmallScripts) {
ScriptStreamer::SetSmallScriptThresholdForTesting(100);
ScriptStreamer::StartStreaming(
GetPendingScript(), ScriptStreamer::kParsingBlocking, settings_.get(),
GetPendingScript(), ScriptStreamer::kParsingBlocking, GetSettings(),
scope.GetScriptState(), loading_task_runner_);
TestPendingScriptClient* client = new TestPendingScriptClient;
GetPendingScript()->WatchForLoad(client);
......@@ -322,7 +333,7 @@ TEST_F(ScriptStreamingTest, ScriptsWithSmallFirstChunk) {
ScriptStreamer::SetSmallScriptThresholdForTesting(100);
ScriptStreamer::StartStreaming(
GetPendingScript(), ScriptStreamer::kParsingBlocking, settings_.get(),
GetPendingScript(), ScriptStreamer::kParsingBlocking, GetSettings(),
scope.GetScriptState(), loading_task_runner_);
TestPendingScriptClient* client = new TestPendingScriptClient;
GetPendingScript()->WatchForLoad(client);
......@@ -356,15 +367,15 @@ TEST_F(ScriptStreamingTest, EncodingChanges) {
// It's possible that the encoding of the Resource changes after we start
// loading it.
V8TestingScope scope;
resource_->SetEncodingForTest("windows-1252");
GetResource()->SetEncodingForTest("windows-1252");
ScriptStreamer::StartStreaming(
GetPendingScript(), ScriptStreamer::kParsingBlocking, settings_.get(),
GetPendingScript(), ScriptStreamer::kParsingBlocking, GetSettings(),
scope.GetScriptState(), loading_task_runner_);
TestPendingScriptClient* client = new TestPendingScriptClient;
GetPendingScript()->WatchForLoad(client);
resource_->SetEncodingForTest("UTF-8");
GetResource()->SetEncodingForTest("UTF-8");
// \xec\x92\x81 are the raw bytes for \uc481.
AppendData(
"function foo() { var foob\xec\x92\x81r = 13; return foob\xec\x92\x81r; "
......@@ -395,10 +406,10 @@ TEST_F(ScriptStreamingTest, EncodingFromBOM) {
V8TestingScope scope;
// This encoding is wrong on purpose.
resource_->SetEncodingForTest("windows-1252");
GetResource()->SetEncodingForTest("windows-1252");
ScriptStreamer::StartStreaming(
GetPendingScript(), ScriptStreamer::kParsingBlocking, settings_.get(),
GetPendingScript(), ScriptStreamer::kParsingBlocking, GetSettings(),
scope.GetScriptState(), loading_task_runner_);
TestPendingScriptClient* client = new TestPendingScriptClient;
GetPendingScript()->WatchForLoad(client);
......@@ -431,7 +442,7 @@ TEST_F(ScriptStreamingTest, EncodingFromBOM) {
TEST_F(ScriptStreamingTest, GarbageCollectDuringStreaming) {
V8TestingScope scope;
ScriptStreamer::StartStreaming(
GetPendingScript(), ScriptStreamer::kParsingBlocking, settings_.get(),
GetPendingScript(), ScriptStreamer::kParsingBlocking, GetSettings(),
scope.GetScriptState(), loading_task_runner_);
TestPendingScriptClient* client = new TestPendingScriptClient;
......
......@@ -54,18 +54,6 @@ ClassicPendingScript* ClassicPendingScript::Fetch(
return pending_script;
}
ClassicPendingScript* ClassicPendingScript::CreateExternalForTest(
ScriptElementBase* element,
ScriptResource* resource) {
DCHECK(resource);
ClassicPendingScript* pending_script = new ClassicPendingScript(
element, TextPosition(), ScriptSourceLocationType::kExternalFile,
ScriptFetchOptions(), true /* is_external */);
pending_script->SetResource(resource);
pending_script->CheckState();
return pending_script;
}
ClassicPendingScript* ClassicPendingScript::CreateInline(
ScriptElementBase* element,
const TextPosition& starting_position,
......@@ -114,7 +102,7 @@ void ClassicPendingScript::Prefinalize() {
void ClassicPendingScript::DisposeInternal() {
MemoryCoordinator::Instance().UnregisterClient(this);
SetResource(nullptr);
ClearResource();
integrity_failure_ = false;
CancelStreaming();
}
......
......@@ -41,10 +41,6 @@ class CORE_EXPORT ClassicPendingScript final : public PendingScript,
ScriptElementBase*,
FetchParameters::DeferOption);
// For a script from an external file, with a supplied ScriptResource.
static ClassicPendingScript* CreateExternalForTest(ScriptElementBase*,
ScriptResource*);
// For an inline script.
static ClassicPendingScript* CreateInline(ScriptElementBase*,
const TextPosition&,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment