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 "core/testing/URLTestHelpers.h"
17#include "platform/graphics/Color.h"
18#include "web/tests/FrameTestHelpers.h"
19#include <gtest/gtest.h>
20
21namespace {
22
23using blink::FrameTestHelpers::WebViewHelper;
24using blink::URLTestHelpers::toKURL;
25using namespace blink;
26
27TEST(WebDocumentTest, InsertStyleSheet)
28{
29    WebViewHelper webViewHelper;
30    webViewHelper.initializeAndLoad("about:blank");
31
32    WebDocument webDoc = webViewHelper.webView()->mainFrame()->document();
33    Document* coreDoc = toLocalFrame(webViewHelper.webViewImpl()->page()->mainFrame())->document();
34
35    webDoc.insertStyleSheet("body { color: green }");
36
37    // Check insertStyleSheet did not cause a synchronous style recalc.
38    unsigned accessCount = coreDoc->styleEngine()->resolverAccessCount();
39    ASSERT_EQ(0U, accessCount);
40
41    HTMLElement* bodyElement = coreDoc->body();
42    ASSERT(bodyElement);
43
44    RenderStyle* style = bodyElement->renderStyle();
45    ASSERT(style);
46
47    // Inserted stylesheet not yet applied.
48    ASSERT_EQ(Color(0, 0, 0), style->visitedDependentColor(CSSPropertyColor));
49
50    // Apply inserted stylesheet.
51    coreDoc->updateRenderTreeIfNeeded();
52
53    style = bodyElement->renderStyle();
54    ASSERT(style);
55
56    // Inserted stylesheet applied.
57    ASSERT_EQ(Color(0, 128, 0), style->visitedDependentColor(CSSPropertyColor));
58}
59
60TEST(WebDocumentTest, BeginExitTransition)
61{
62    std::string baseURL = "http://www.test.com:0/";
63    const char* htmlURL = "transition_exit.html";
64    const char* cssURL = "transition_exit.css";
65    URLTestHelpers::registerMockedURLLoad(toKURL(baseURL + htmlURL), WebString::fromUTF8(htmlURL));
66    URLTestHelpers::registerMockedURLLoad(toKURL(baseURL + cssURL), WebString::fromUTF8(cssURL));
67
68    WebViewHelper webViewHelper;
69    webViewHelper.initializeAndLoad(baseURL + htmlURL);
70
71    WebFrame* frame = webViewHelper.webView()->mainFrame();
72    Document* coreDoc = toLocalFrame(webViewHelper.webViewImpl()->page()->mainFrame())->document();
73    Element* transitionElement = coreDoc->getElementById("foo");
74    ASSERT(transitionElement);
75
76    RenderStyle* transitionStyle = transitionElement->renderStyle();
77    ASSERT(transitionStyle);
78
79    HTMLElement* bodyElement = coreDoc->body();
80    ASSERT(bodyElement);
81
82    RenderStyle* bodyStyle = bodyElement->renderStyle();
83    ASSERT(bodyStyle);
84    // The transition_exit.css stylesheet should not have been applied at this point.
85    ASSERT_EQ(Color(0, 0, 0), bodyStyle->visitedDependentColor(CSSPropertyColor));
86
87    frame->document().beginExitTransition("#foo");
88
89    // Make sure the stylesheet load request gets processed.
90    FrameTestHelpers::pumpPendingRequestsDoNotUse(frame);
91    coreDoc->updateRenderTreeIfNeeded();
92
93    // The element should now be hidden.
94    transitionStyle = transitionElement->renderStyle();
95    ASSERT_TRUE(transitionStyle);
96    ASSERT_EQ(transitionStyle->opacity(), 0);
97
98    // The stylesheet should now have been applied.
99    bodyStyle = bodyElement->renderStyle();
100    ASSERT(bodyStyle);
101    ASSERT_EQ(Color(0, 128, 0), bodyStyle->visitedDependentColor(CSSPropertyColor));
102}
103
104}
105