Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(496)

Unified Diff: third_party/WebKit/Source/core/html/parser/CSSPreloadScannerTest.cpp

Issue 1976463003: Preload scan external CSS for @import (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: preload @import Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/html/parser/CSSPreloadScannerTest.cpp
diff --git a/third_party/WebKit/Source/core/html/parser/CSSPreloadScannerTest.cpp b/third_party/WebKit/Source/core/html/parser/CSSPreloadScannerTest.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..0eb55251b22f573fc803f9d741a8f6ff1ba38da9
--- /dev/null
+++ b/third_party/WebKit/Source/core/html/parser/CSSPreloadScannerTest.cpp
@@ -0,0 +1,83 @@
+// 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/CSSPreloadScanner.h"
+
+#include "core/fetch/FetchContext.h"
+#include "core/fetch/Resource.h"
+#include "core/fetch/ResourceFetcher.h"
+#include "core/frame/Settings.h"
+#include "core/html/parser/HTMLResourcePreloader.h"
+#include "core/testing/DummyPageHolder.h"
+#include "platform/heap/Heap.h"
+#include "platform/network/ResourceRequest.h"
+#include "platform/weborigin/KURL.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include <memory>
+
+namespace blink {
+
+class PreloadSuppressingCSSPreloaderResourceClient : public CSSPreloaderResourceClient {
hiroshige 2016/07/26 09:57:10 Add |final|.
Charlie Harrison 2016/07/26 16:31:52 Done.
+public:
+ PreloadSuppressingCSSPreloaderResourceClient(Resource* resource, HTMLResourcePreloader* preloader)
+ : CSSPreloaderResourceClient(resource, preloader) {}
+ void fetchPreloads(PreloadRequestStream& preloads) override
+ {
+ PreloadRequestStream movedPreloads;
+ movedPreloads.swap(preloads);
+ for (PreloadRequestStream::iterator it = movedPreloads.begin(); it != movedPreloads.end(); ++it) {
+ m_preloads.append(std::move(*it));
+ }
+ }
+
+ PreloadRequestStream m_preloads;
+};
+
+class CSSPreloadScannerTest : public ::testing::Test {
+};
+
+TEST_F(CSSPreloadScannerTest, ScanFromResourceClient)
+{
+ std::unique_ptr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(IntSize(500, 500));
+ dummyPageHolder->document().settings()->setCSSExternalScannerNoPreload(true);
+
+ HTMLResourcePreloader* preloader = HTMLResourcePreloader::create(dummyPageHolder->document());
+
+ KURL url(ParsedURLString, "http://127.0.0.1/foo.css");
+ CSSStyleSheetResource* resource = CSSStyleSheetResource::createForTest(ResourceRequest(url), "utf-8");
+ resource->setStatus(Resource::Pending);
+
+ PreloadSuppressingCSSPreloaderResourceClient* resourceClient = new PreloadSuppressingCSSPreloaderResourceClient(resource, preloader);
+
+ const char* data = "@import url('http://127.0.0.1/preload.css');";
+ resource->appendData(data, strlen(data));
+
+ EXPECT_EQ(1u, resourceClient->m_preloads.size());
+ EXPECT_EQ("http://127.0.0.1/preload.css", resourceClient->m_preloads.first()->resourceURL());
+}
+
+// Regression test for crbug.com/608310 where the client is destroyed but was
+// not removed from the resource's client list.
+TEST_F(CSSPreloadScannerTest, DestroyClientBeforeDataSent)
+{
+ std::unique_ptr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(IntSize(500, 500));
+ dummyPageHolder->document().settings()->setCSSExternalScannerNoPreload(true);
+
+ Persistent<HTMLResourcePreloader> preloader = HTMLResourcePreloader::create(dummyPageHolder->document());
+
+ KURL url(ParsedURLString, "http://127.0.0.1/foo.css");
+ Persistent<CSSStyleSheetResource> resource = CSSStyleSheetResource::createForTest(ResourceRequest(url), "utf-8");
+ resource->setStatus(Resource::Pending);
+
+ new PreloadSuppressingCSSPreloaderResourceClient(resource, preloader);
+
+ // Destroys the resourceClient.
+ ThreadHeap::collectAllGarbage();
+
+ const char* data = "@import url('http://127.0.0.1/preload.css');";
+ // Should not crash.
+ resource->appendData(data, strlen(data));
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698