Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / web / tests / WebDocumentTest.cpp
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.
4
5 #include "config.h"
6
7 #include "public/web/WebDocument.h"
8
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"
19
20 #include <gtest/gtest.h>
21
22 using namespace blink;
23 using blink::Color;
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;
30
31 namespace {
32
33 TEST(WebDocumentTest, InsertStyleSheet)
34 {
35     WebViewHelper webViewHelper;
36     webViewHelper.initializeAndLoad("about:blank");
37
38     WebDocument webDoc = webViewHelper.webView()->mainFrame()->document();
39     Document* coreDoc = toLocalFrame(webViewHelper.webViewImpl()->page()->mainFrame())->document();
40
41     webDoc.insertStyleSheet("body { color: green }");
42
43     // Check insertStyleSheet did not cause a synchronous style recalc.
44     unsigned accessCount = coreDoc->styleEngine()->resolverAccessCount();
45     ASSERT_EQ(0U, accessCount);
46
47     HTMLElement* bodyElement = coreDoc->body();
48     ASSERT(bodyElement);
49
50     RenderStyle* style = bodyElement->renderStyle();
51     ASSERT(style);
52
53     // Inserted stylesheet not yet applied.
54     ASSERT_EQ(Color(0, 0, 0), style->visitedDependentColor(blink::CSSPropertyColor));
55
56     // Apply inserted stylesheet.
57     coreDoc->updateRenderTreeIfNeeded();
58
59     style = bodyElement->renderStyle();
60     ASSERT(style);
61
62     // Inserted stylesheet applied.
63     ASSERT_EQ(Color(0, 128, 0), style->visitedDependentColor(blink::CSSPropertyColor));
64 }
65
66 TEST(WebDocumentTest, BeginExitTransition)
67 {
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));
73
74     WebViewHelper webViewHelper;
75     webViewHelper.initializeAndLoad(baseURL + htmlURL);
76
77     WebFrame* frame = webViewHelper.webView()->mainFrame();
78     Document* coreDoc = toLocalFrame(webViewHelper.webViewImpl()->page()->mainFrame())->document();
79     Element* transitionElement = coreDoc->getElementById("foo");
80     ASSERT(transitionElement);
81
82     RenderStyle* transitionStyle = transitionElement->renderStyle();
83     ASSERT(transitionStyle);
84
85     HTMLElement* bodyElement = coreDoc->body();
86     ASSERT(bodyElement);
87
88     RenderStyle* bodyStyle = bodyElement->renderStyle();
89     ASSERT(bodyStyle);
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));
92
93     frame->document().beginExitTransition("#foo");
94
95     // Make sure the stylesheet load request gets processed.
96     FrameTestHelpers::pumpPendingRequestsDoNotUse(frame);
97     coreDoc->updateRenderTreeIfNeeded();
98
99     // The element should now be hidden.
100     transitionStyle = transitionElement->renderStyle();
101     ASSERT_TRUE(transitionStyle);
102     ASSERT_EQ(transitionStyle->opacity(), 0);
103
104     // The stylesheet should now have been applied.
105     bodyStyle = bodyElement->renderStyle();
106     ASSERT(bodyStyle);
107     ASSERT_EQ(Color(0, 128, 0), bodyStyle->visitedDependentColor(blink::CSSPropertyColor));
108 }
109
110 }