1 // Copyright 2014 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.
7 #include "public/web/WebDocument.h"
9 #include "core/CSSPropertyNames.h"
10 #include "core/dom/NodeRenderStyle.h"
11 #include "core/dom/StyleEngine.h"
12 #include "core/frame/LocalFrame.h"
13 #include "core/html/HTMLElement.h"
14 #include "core/page/Page.h"
15 #include "core/rendering/style/RenderStyle.h"
16 #include "platform/graphics/Color.h"
17 #include "web/tests/FrameTestHelpers.h"
18 #include "web/tests/URLTestHelpers.h"
20 #include <gtest/gtest.h>
22 using namespace blink;
24 using blink::Document;
25 using blink::HTMLElement;
26 using blink::RenderStyle;
27 using blink::FrameTestHelpers::WebViewHelper;
28 using blink::URLTestHelpers::toKURL;
29 using blink::WebDocument;
33 TEST(WebDocumentTest, InsertStyleSheet)
35 WebViewHelper webViewHelper;
36 webViewHelper.initializeAndLoad("about:blank");
38 WebDocument webDoc = webViewHelper.webView()->mainFrame()->document();
39 Document* coreDoc = toLocalFrame(webViewHelper.webViewImpl()->page()->mainFrame())->document();
41 webDoc.insertStyleSheet("body { color: green }");
43 // Check insertStyleSheet did not cause a synchronous style recalc.
44 unsigned accessCount = coreDoc->styleEngine()->resolverAccessCount();
45 ASSERT_EQ(0U, accessCount);
47 HTMLElement* bodyElement = coreDoc->body();
50 RenderStyle* style = bodyElement->renderStyle();
53 // Inserted stylesheet not yet applied.
54 ASSERT_EQ(Color(0, 0, 0), style->visitedDependentColor(blink::CSSPropertyColor));
56 // Apply inserted stylesheet.
57 coreDoc->updateRenderTreeIfNeeded();
59 style = bodyElement->renderStyle();
62 // Inserted stylesheet applied.
63 ASSERT_EQ(Color(0, 128, 0), style->visitedDependentColor(blink::CSSPropertyColor));
66 TEST(WebDocumentTest, BeginExitTransition)
68 std::string baseURL = "http://www.test.com:0/";
69 const char* htmlURL = "transition_exit.html";
70 const char* cssURL = "transition_exit.css";
71 URLTestHelpers::registerMockedURLLoad(toKURL(baseURL + htmlURL), WebString::fromUTF8(htmlURL));
72 URLTestHelpers::registerMockedURLLoad(toKURL(baseURL + cssURL), WebString::fromUTF8(cssURL));
74 WebViewHelper webViewHelper;
75 webViewHelper.initializeAndLoad(baseURL + htmlURL);
77 WebFrame* frame = webViewHelper.webView()->mainFrame();
78 Document* coreDoc = toLocalFrame(webViewHelper.webViewImpl()->page()->mainFrame())->document();
79 Element* transitionElement = coreDoc->getElementById("foo");
80 ASSERT(transitionElement);
82 RenderStyle* transitionStyle = transitionElement->renderStyle();
83 ASSERT(transitionStyle);
85 HTMLElement* bodyElement = coreDoc->body();
88 RenderStyle* bodyStyle = bodyElement->renderStyle();
90 // The transition_exit.css stylesheet should not have been applied at this point.
91 ASSERT_EQ(Color(0, 0, 0), bodyStyle->visitedDependentColor(blink::CSSPropertyColor));
93 frame->document().beginExitTransition("#foo");
95 // Make sure the stylesheet load request gets processed.
96 FrameTestHelpers::pumpPendingRequestsDoNotUse(frame);
97 coreDoc->updateRenderTreeIfNeeded();
99 // The element should now be hidden.
100 transitionStyle = transitionElement->renderStyle();
101 ASSERT_TRUE(transitionStyle);
102 ASSERT_EQ(transitionStyle->opacity(), 0);
104 // The stylesheet should now have been applied.
105 bodyStyle = bodyElement->renderStyle();
107 ASSERT_EQ(Color(0, 128, 0), bodyStyle->visitedDependentColor(blink::CSSPropertyColor));