void TouchActionTest::runShadowDOMTest(std::string file) { TouchActionTrackingWebViewClient client; WebView* webView = setupTest(file, client); TrackExceptionState es; // Oilpan: see runTouchActionTest() comment why these are persistent // references. Persistent<Document> document = static_cast<Document*>(webView->mainFrame()->document()); Persistent<StaticElementList> hostNodes = document->querySelectorAll("[shadow-host]", es); ASSERT_FALSE(es.hadException()); ASSERT_GE(hostNodes->length(), 1u); for (unsigned index = 0; index < hostNodes->length(); index++) { ShadowRoot* shadowRoot = hostNodes->item(index)->openShadowRoot(); runTestOnTree(shadowRoot, webView, client); } // Projections show up in the main document. runTestOnTree(document.get(), webView, client); // Explicitly reset to break dependency on locally scoped client. m_webViewHelper.reset(); }
TEST(LinkLoaderTest, Preconnect) { struct { const char* href; CrossOriginAttributeValue crossOrigin; const bool shouldLoad; const bool isHTTPS; const bool isCrossOrigin; } cases[] = { {"http://example.com/", CrossOriginAttributeNotSet, true, false, false}, {"https://example.com/", CrossOriginAttributeNotSet, true, true, false}, {"http://example.com/", CrossOriginAttributeAnonymous, true, false, true}, {"//example.com/", CrossOriginAttributeNotSet, true, false, false}, {"http://example.com/", CrossOriginAttributeNotSet, false, false, false}, }; // Test the cases with a single header for (const auto& testCase : cases) { std::unique_ptr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(IntSize(500, 500)); Persistent<MockLinkLoaderClient> loaderClient = MockLinkLoaderClient::create(testCase.shouldLoad); LinkLoader* loader = LinkLoader::create(loaderClient.get()); KURL hrefURL = KURL(KURL(ParsedURLStringTag(), String("http://example.com")), testCase.href); NetworkHintsMock networkHints; loader->loadLink(LinkRelAttribute("preconnect"), testCase.crossOrigin, String(), String(), String(), ReferrerPolicyDefault, hrefURL, dummyPageHolder->document(), networkHints); EXPECT_EQ(testCase.shouldLoad, networkHints.didPreconnect()); EXPECT_EQ(testCase.isHTTPS, networkHints.isHTTPS()); EXPECT_EQ(testCase.isCrossOrigin, networkHints.isCrossOrigin()); } }
TEST(LinkLoaderTest, DNSPrefetch) { struct { const char* href; const bool shouldLoad; } cases[] = { {"http://example.com/", true}, {"https://example.com/", true}, {"//example.com/", true}, {"//example.com/", false}, }; // Test the cases with a single header for (const auto& testCase : cases) { std::unique_ptr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(IntSize(500, 500)); dummyPageHolder->document().settings()->setDNSPrefetchingEnabled(true); Persistent<MockLinkLoaderClient> loaderClient = MockLinkLoaderClient::create(testCase.shouldLoad); LinkLoader* loader = LinkLoader::create(loaderClient.get()); KURL hrefURL = KURL(KURL(ParsedURLStringTag(), String("http://example.com")), testCase.href); NetworkHintsMock networkHints; loader->loadLink(LinkRelAttribute("dns-prefetch"), CrossOriginAttributeNotSet, String(), String(), String(), ReferrerPolicyDefault, hrefURL, dummyPageHolder->document(), networkHints); EXPECT_FALSE(networkHints.didPreconnect()); EXPECT_EQ(testCase.shouldLoad, networkHints.didDnsPrefetch()); } }
void testMQEvaluator(TestCase* testCases, const MediaQueryEvaluator& mediaQueryEvaluator) { Persistent<MediaQuerySet> querySet = nullptr; for (unsigned i = 0; testCases[i].input; ++i) { querySet = MediaQuerySet::create(testCases[i].input); EXPECT_EQ(testCases[i].output, mediaQueryEvaluator.eval(querySet.get())); } }
TEST(LinkLoaderTest, Prefetch) { struct TestCase { const char* href; // TODO(yoav): Add support for type and media crbug.com/662687 const char* type; const char* media; const ReferrerPolicy referrerPolicy; const bool linkLoaderShouldLoadValue; const bool expectingLoad; const ReferrerPolicy expectedReferrerPolicy; } cases[] = { // Referrer Policy {"http://example.test/cat.jpg", "image/jpg", "", ReferrerPolicyOrigin, true, true, ReferrerPolicyOrigin}, {"http://example.test/cat.jpg", "image/jpg", "", ReferrerPolicyOriginWhenCrossOrigin, true, true, ReferrerPolicyOriginWhenCrossOrigin}, {"http://example.test/cat.jpg", "image/jpg", "", ReferrerPolicyNever, true, true, ReferrerPolicyNever}, }; // Test the cases with a single header for (const auto& testCase : cases) { std::unique_ptr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(IntSize(500, 500)); dummyPageHolder->frame().settings()->setScriptEnabled(true); Persistent<MockLinkLoaderClient> loaderClient = MockLinkLoaderClient::create(testCase.linkLoaderShouldLoadValue); LinkLoader* loader = LinkLoader::create(loaderClient.get()); KURL hrefURL = KURL(KURL(), testCase.href); URLTestHelpers::registerMockedErrorURLLoad(hrefURL); loader->loadLink(LinkRelAttribute("prefetch"), CrossOriginAttributeNotSet, testCase.type, "", testCase.media, testCase.referrerPolicy, hrefURL, dummyPageHolder->document(), NetworkHintsMock()); ASSERT_TRUE(dummyPageHolder->document().fetcher()); Resource* resource = loader->resource(); if (testCase.expectingLoad) { EXPECT_TRUE(resource); } else { EXPECT_FALSE(resource); } if (resource) { if (testCase.expectedReferrerPolicy != ReferrerPolicyDefault) { EXPECT_EQ(testCase.expectedReferrerPolicy, resource->resourceRequest().getReferrerPolicy()); } } Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs(); } }
TEST(RawResourceTest, RemoveClientDuringCallback) { Resource* raw = RawResource::create(ResourceRequest("data:text/html,"), Resource::Raw); // Create a non-null response. ResourceResponse response = raw->response(); response.setURL(KURL(ParsedURLString, "http://600.613/")); raw->setResponse(response); raw->finish(); EXPECT_FALSE(raw->response().isNull()); Persistent<DummyClient> dummyClient = new DummyClient(); Persistent<RemovingClient> removingClient = new RemovingClient(dummyClient.get()); raw->addClient(dummyClient); raw->addClient(removingClient); testing::runPendingTasks(); EXPECT_FALSE(raw->hasClientsOrObservers()); }
void TouchActionTest::runIFrameTest(std::string file) { TouchActionTrackingWebViewClient client; WebView* webView = setupTest(file, client); WebFrame* curFrame = webView->mainFrame()->firstChild(); ASSERT_TRUE(curFrame); for (; curFrame; curFrame = curFrame->nextSibling()) { // Oilpan: see runTouchActionTest() comment why these are persistent // references. Persistent<Document> contentDoc = static_cast<Document*>(curFrame->document()); runTestOnTree(contentDoc.get(), webView, client); } // Explicitly reset to break dependency on locally scoped client. m_webViewHelper.reset(); }
void TouchActionTest::runTouchActionTest(std::string file) { TouchActionTrackingWebViewClient client; // runTouchActionTest() loads a document in a frame, setting up a // nested message loop. Should any Oilpan GC happen while it is in // effect, the implicit assumption that we're outside any event // loop (=> there being no pointers on the stack needing scanning) // when that GC strikes will no longer hold. // // To ensure that the references on the stack are also traced, we // turn them into persistent, stack allocated references. This // workaround is sufficient to handle this artificial test // scenario. WebView* webView = setupTest(file, client); Persistent<Document> document = static_cast<Document*>(webView->mainFrame()->document()); runTestOnTree(document.get(), webView, client); // Explicitly reset to break dependency on locally scoped client. m_webViewHelper.reset(); }
TEST(RawResourceTest, RevalidationSucceededUpdateHeaders) { Resource* resource = RawResource::create(ResourceRequest("data:text/html,"), Resource::Raw); ResourceResponse response; response.setHTTPStatusCode(200); response.addHTTPHeaderField("keep-alive", "keep-alive value"); response.addHTTPHeaderField("expires", "expires value"); response.addHTTPHeaderField("last-modified", "last-modified value"); response.addHTTPHeaderField("proxy-authenticate", "proxy-authenticate value"); response.addHTTPHeaderField("proxy-connection", "proxy-connection value"); response.addHTTPHeaderField("x-custom", "custom value"); resource->responseReceived(response, nullptr); resource->finish(); memoryCache()->add(resource); // Simulate a successful revalidation. resource->setRevalidatingRequest(ResourceRequest("data:text/html,")); // Validate that these headers pre-update. EXPECT_EQ("keep-alive value", resource->response().httpHeaderField("keep-alive")); EXPECT_EQ("expires value", resource->response().httpHeaderField("expires")); EXPECT_EQ("last-modified value", resource->response().httpHeaderField("last-modified")); EXPECT_EQ("proxy-authenticate value", resource->response().httpHeaderField("proxy-authenticate")); EXPECT_EQ("proxy-authenticate value", resource->response().httpHeaderField("proxy-authenticate")); EXPECT_EQ("proxy-connection value", resource->response().httpHeaderField("proxy-connection")); EXPECT_EQ("custom value", resource->response().httpHeaderField("x-custom")); Persistent<DummyClient> client = new DummyClient; resource->addClient(client.get()); // Perform a revalidation step. ResourceResponse revalidatingResponse; revalidatingResponse.setHTTPStatusCode(304); // Headers that aren't copied with an 304 code. revalidatingResponse.addHTTPHeaderField("keep-alive", "garbage"); revalidatingResponse.addHTTPHeaderField("expires", "garbage"); revalidatingResponse.addHTTPHeaderField("last-modified", "garbage"); revalidatingResponse.addHTTPHeaderField("proxy-authenticate", "garbage"); revalidatingResponse.addHTTPHeaderField("proxy-connection", "garbage"); // Header that is updated with 304 code. revalidatingResponse.addHTTPHeaderField("x-custom", "updated"); resource->responseReceived(revalidatingResponse, nullptr); // Validate the original response. EXPECT_EQ(200, resource->response().httpStatusCode()); // Validate that these headers are not updated. EXPECT_EQ("keep-alive value", resource->response().httpHeaderField("keep-alive")); EXPECT_EQ("expires value", resource->response().httpHeaderField("expires")); EXPECT_EQ("last-modified value", resource->response().httpHeaderField("last-modified")); EXPECT_EQ("proxy-authenticate value", resource->response().httpHeaderField("proxy-authenticate")); EXPECT_EQ("proxy-authenticate value", resource->response().httpHeaderField("proxy-authenticate")); EXPECT_EQ("proxy-connection value", resource->response().httpHeaderField("proxy-connection")); EXPECT_EQ("updated", resource->response().httpHeaderField("x-custom")); memoryCache()->remove(resource); resource->removeClient(client); EXPECT_FALSE(resource->hasClientsOrObservers()); EXPECT_FALSE(client->called()); EXPECT_EQ(0u, client->data().size()); }
TEST(LinkLoaderTest, Preload) { struct TestCase { const char* href; const char* as; const char* type; const char* media; const ReferrerPolicy referrerPolicy; const ResourceLoadPriority priority; const WebURLRequest::RequestContext context; const bool linkLoaderShouldLoadValue; const bool expectingLoad; const ReferrerPolicy expectedReferrerPolicy; } cases[] = { {"http://example.test/cat.jpg", "image", "", "", ReferrerPolicyDefault, ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, true, true, ReferrerPolicyDefault}, {"http://example.test/cat.js", "script", "", "", ReferrerPolicyDefault, ResourceLoadPriorityHigh, WebURLRequest::RequestContextScript, true, true, ReferrerPolicyDefault}, {"http://example.test/cat.css", "style", "", "", ReferrerPolicyDefault, ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextStyle, true, true, ReferrerPolicyDefault}, // TODO(yoav): It doesn't seem like the audio context is ever used. That // should probably be fixed (or we can consolidate audio and video). {"http://example.test/cat.wav", "media", "", "", ReferrerPolicyDefault, ResourceLoadPriorityLow, WebURLRequest::RequestContextVideo, true, true, ReferrerPolicyDefault}, {"http://example.test/cat.mp4", "media", "", "", ReferrerPolicyDefault, ResourceLoadPriorityLow, WebURLRequest::RequestContextVideo, true, true, ReferrerPolicyDefault}, {"http://example.test/cat.vtt", "track", "", "", ReferrerPolicyDefault, ResourceLoadPriorityLow, WebURLRequest::RequestContextTrack, true, true, ReferrerPolicyDefault}, {"http://example.test/cat.woff", "font", "", "", ReferrerPolicyDefault, ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextFont, true, true, ReferrerPolicyDefault}, // TODO(yoav): subresource should be *very* low priority (rather than // low). {"http://example.test/cat.empty", "", "", "", ReferrerPolicyDefault, ResourceLoadPriorityHigh, WebURLRequest::RequestContextSubresource, true, true, ReferrerPolicyDefault}, {"http://example.test/cat.blob", "blabla", "", "", ReferrerPolicyDefault, ResourceLoadPriorityLow, WebURLRequest::RequestContextSubresource, false, false, ReferrerPolicyDefault}, {"bla://example.test/cat.gif", "image", "", "", ReferrerPolicyDefault, ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextImage, false, false, ReferrerPolicyDefault}, // MIME type tests {"http://example.test/cat.webp", "image", "image/webp", "", ReferrerPolicyDefault, ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, true, true, ReferrerPolicyDefault}, {"http://example.test/cat.svg", "image", "image/svg+xml", "", ReferrerPolicyDefault, ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, true, true, ReferrerPolicyDefault}, {"http://example.test/cat.jxr", "image", "image/jxr", "", ReferrerPolicyDefault, ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextImage, false, false, ReferrerPolicyDefault}, {"http://example.test/cat.js", "script", "text/javascript", "", ReferrerPolicyDefault, ResourceLoadPriorityHigh, WebURLRequest::RequestContextScript, true, true, ReferrerPolicyDefault}, {"http://example.test/cat.js", "script", "text/coffeescript", "", ReferrerPolicyDefault, ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextScript, false, false, ReferrerPolicyDefault}, {"http://example.test/cat.css", "style", "text/css", "", ReferrerPolicyDefault, ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextStyle, true, true, ReferrerPolicyDefault}, {"http://example.test/cat.css", "style", "text/sass", "", ReferrerPolicyDefault, ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextStyle, false, false, ReferrerPolicyDefault}, {"http://example.test/cat.wav", "media", "audio/wav", "", ReferrerPolicyDefault, ResourceLoadPriorityLow, WebURLRequest::RequestContextVideo, true, true, ReferrerPolicyDefault}, {"http://example.test/cat.wav", "media", "audio/mp57", "", ReferrerPolicyDefault, ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextVideo, false, false, ReferrerPolicyDefault}, {"http://example.test/cat.webm", "media", "video/webm", "", ReferrerPolicyDefault, ResourceLoadPriorityLow, WebURLRequest::RequestContextVideo, true, true, ReferrerPolicyDefault}, {"http://example.test/cat.mp199", "media", "video/mp199", "", ReferrerPolicyDefault, ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextVideo, false, false, ReferrerPolicyDefault}, {"http://example.test/cat.vtt", "track", "text/vtt", "", ReferrerPolicyDefault, ResourceLoadPriorityLow, WebURLRequest::RequestContextTrack, true, true, ReferrerPolicyDefault}, {"http://example.test/cat.vtt", "track", "text/subtitlething", "", ReferrerPolicyDefault, ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextTrack, false, false, ReferrerPolicyDefault}, {"http://example.test/cat.woff", "font", "font/woff2", "", ReferrerPolicyDefault, ResourceLoadPriorityVeryHigh, WebURLRequest::RequestContextFont, true, true, ReferrerPolicyDefault}, {"http://example.test/cat.woff", "font", "font/woff84", "", ReferrerPolicyDefault, ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextFont, false, false, ReferrerPolicyDefault}, {"http://example.test/cat.empty", "", "foo/bar", "", ReferrerPolicyDefault, ResourceLoadPriorityHigh, WebURLRequest::RequestContextSubresource, true, true, ReferrerPolicyDefault}, {"http://example.test/cat.blob", "blabla", "foo/bar", "", ReferrerPolicyDefault, ResourceLoadPriorityLow, WebURLRequest::RequestContextSubresource, false, false, ReferrerPolicyDefault}, // Media tests {"http://example.test/cat.gif", "image", "image/gif", "(max-width: 600px)", ReferrerPolicyDefault, ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, true, true, ReferrerPolicyDefault}, {"http://example.test/cat.gif", "image", "image/gif", "(max-width: 400px)", ReferrerPolicyDefault, ResourceLoadPriorityUnresolved, WebURLRequest::RequestContextImage, true, false, ReferrerPolicyDefault}, {"http://example.test/cat.gif", "image", "image/gif", "(max-width: 600px)", ReferrerPolicyDefault, ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, false, false, ReferrerPolicyDefault}, // Referrer Policy {"http://example.test/cat.gif", "image", "image/gif", "", ReferrerPolicyOrigin, ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, true, true, ReferrerPolicyOrigin}, {"http://example.test/cat.gif", "image", "image/gif", "", ReferrerPolicyOriginWhenCrossOrigin, ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, true, true, ReferrerPolicyOriginWhenCrossOrigin}, {"http://example.test/cat.gif", "image", "image/gif", "", ReferrerPolicyNever, ResourceLoadPriorityLow, WebURLRequest::RequestContextImage, true, true, ReferrerPolicyNever}, }; // Test the cases with a single header for (const auto& testCase : cases) { std::unique_ptr<DummyPageHolder> dummyPageHolder = DummyPageHolder::create(IntSize(500, 500)); dummyPageHolder->frame().settings()->setScriptEnabled(true); Persistent<MockLinkLoaderClient> loaderClient = MockLinkLoaderClient::create(testCase.linkLoaderShouldLoadValue); LinkLoader* loader = LinkLoader::create(loaderClient.get()); KURL hrefURL = KURL(KURL(), testCase.href); URLTestHelpers::registerMockedErrorURLLoad(hrefURL); loader->loadLink(LinkRelAttribute("preload"), CrossOriginAttributeNotSet, testCase.type, testCase.as, testCase.media, testCase.referrerPolicy, hrefURL, dummyPageHolder->document(), NetworkHintsMock()); ASSERT_TRUE(dummyPageHolder->document().fetcher()); HeapListHashSet<Member<Resource>>* preloads = dummyPageHolder->document().fetcher()->preloads(); if (testCase.expectingLoad) { if (!preloads) { fprintf(stderr, "Unexpected result %s %s %s\n", testCase.href, testCase.as, testCase.type); } EXPECT_TRUE(preloads); } else { EXPECT_FALSE(preloads); } if (preloads) { if (testCase.priority == ResourceLoadPriorityUnresolved) { EXPECT_EQ(0u, preloads->size()); } else { EXPECT_EQ(1u, preloads->size()); if (preloads->size() > 0) { Resource* resource = preloads->begin().get()->get(); EXPECT_EQ(testCase.priority, resource->resourceRequest().priority()); EXPECT_EQ(testCase.context, resource->resourceRequest().requestContext()); if (testCase.expectedReferrerPolicy != ReferrerPolicyDefault) { EXPECT_EQ(testCase.expectedReferrerPolicy, resource->resourceRequest().getReferrerPolicy()); } } } dummyPageHolder->document().fetcher()->clearPreloads(); } memoryCache()->evictResources(); Platform::current()->getURLLoaderMockFactory()->unregisterAllURLs(); } }