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

Side by Side Diff: Source/WebKit/chromium/tests/WebFrameTest.cpp

Issue 18371008: Add a WebDocument::watchCssSelectors(selectors) (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@pinned
Patch Set: Fix most comments; TODO benchmark and use Element::recalcStyle Created 7 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 unified diff | Download patch
« no previous file with comments | « no previous file | Source/core/core.gypi » ('j') | Source/core/css/DocumentRuleSets.cpp » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 14 matching lines...) Expand all
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "config.h" 31 #include "config.h"
32 32
33 #include "WebFrame.h" 33 #include "WebFrame.h"
34 34
35 #include <gmock/gmock.h>
35 #include <gtest/gtest.h> 36 #include <gtest/gtest.h>
36 #include "FrameTestHelpers.h" 37 #include "FrameTestHelpers.h"
37 #include "SkBitmap.h" 38 #include "SkBitmap.h"
38 #include "SkCanvas.h" 39 #include "SkCanvas.h"
39 #include "URLTestHelpers.h" 40 #include "URLTestHelpers.h"
40 #include "WebDataSource.h" 41 #include "WebDataSource.h"
41 #include "WebDocument.h" 42 #include "WebDocument.h"
42 #include "WebFindOptions.h" 43 #include "WebFindOptions.h"
43 #include "WebFormElement.h" 44 #include "WebFormElement.h"
44 #include "WebFrameClient.h" 45 #include "WebFrameClient.h"
(...skipping 23 matching lines...) Expand all
68 #include "core/page/Settings.h" 69 #include "core/page/Settings.h"
69 #include "core/platform/ScrollbarTheme.h" 70 #include "core/platform/ScrollbarTheme.h"
70 #include "core/platform/graphics/FloatRect.h" 71 #include "core/platform/graphics/FloatRect.h"
71 #include "core/platform/network/ResourceError.h" 72 #include "core/platform/network/ResourceError.h"
72 #include "core/rendering/HitTestResult.h" 73 #include "core/rendering/HitTestResult.h"
73 #include "core/rendering/RenderLayerCompositor.h" 74 #include "core/rendering/RenderLayerCompositor.h"
74 #include "core/rendering/RenderView.h" 75 #include "core/rendering/RenderView.h"
75 #include "core/rendering/TextAutosizer.h" 76 #include "core/rendering/TextAutosizer.h"
76 #include "v8.h" 77 #include "v8.h"
77 #include "public/platform/Platform.h" 78 #include "public/platform/Platform.h"
79 #include "public/platform/WebCString.h"
78 #include "public/platform/WebFloatRect.h" 80 #include "public/platform/WebFloatRect.h"
79 #include "public/platform/WebThread.h" 81 #include "public/platform/WebThread.h"
80 #include "public/platform/WebUnitTestSupport.h" 82 #include "public/platform/WebUnitTestSupport.h"
81 #include "public/platform/WebURLResponse.h" 83 #include "public/platform/WebURLResponse.h"
82 #include "wtf/dtoa/utils.h" 84 #include "wtf/dtoa/utils.h"
83 #include "wtf/Forward.h" 85 #include "wtf/Forward.h"
86 #include <map>
84 87
85 using namespace WebKit; 88 using namespace WebKit;
86 using WebCore::Document; 89 using WebCore::Document;
87 using WebCore::DocumentMarker; 90 using WebCore::DocumentMarker;
88 using WebCore::Element; 91 using WebCore::Element;
89 using WebCore::FloatRect; 92 using WebCore::FloatRect;
90 using WebCore::HitTestRequest; 93 using WebCore::HitTestRequest;
91 using WebCore::Range; 94 using WebCore::Range;
92 using WebKit::URLTestHelpers::toKURL; 95 using WebKit::URLTestHelpers::toKURL;
93 using WebKit::FrameTestHelpers::runPendingTasks; 96 using WebKit::FrameTestHelpers::runPendingTasks;
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 FrameTestHelpers::loadFrame(m_webView->mainFrame(), "javascript:document.bod y.appendChild(document.createTextNode('Clobbered'))"); 266 FrameTestHelpers::loadFrame(m_webView->mainFrame(), "javascript:document.bod y.appendChild(document.createTextNode('Clobbered'))");
264 267
265 // Required to see any updates in contentAsText. 268 // Required to see any updates in contentAsText.
266 m_webView->layout(); 269 m_webView->layout();
267 270
268 // Now retrieve the frame's text and ensure it wasn't modified by running ja vascript. 271 // Now retrieve the frame's text and ensure it wasn't modified by running ja vascript.
269 std::string content = std::string(m_webView->mainFrame()->contentAsText(1024 ).utf8().data()); 272 std::string content = std::string(m_webView->mainFrame()->contentAsText(1024 ).utf8().data());
270 EXPECT_EQ(std::string::npos, content.find("Clobbered")); 273 EXPECT_EQ(std::string::npos, content.find("Clobbered"));
271 } 274 }
272 275
276 struct CSSCallbackWebFrameClient : public WebFrameClient {
277 CSSCallbackWebFrameClient() : m_updateCount(0) { }
278 virtual void didMatchCSS(WebFrame* frame, const WebVector<WebString>& newlyM atchingSelectors, const WebVector<WebString>& stoppedMatchingSelectors) OVERRIDE
279 {
280 ++m_updateCount;
281 std::set<std::string>& frameSelectors = m_matchedSelectors[frame];
282 for (size_t i = 0; i < newlyMatchingSelectors.size(); ++i) {
283 std::string selector = newlyMatchingSelectors[i].utf8();
284 EXPECT_EQ(0U, frameSelectors.count(selector)) << selector;
285 frameSelectors.insert(selector);
286 }
287 for (size_t i = 0; i < stoppedMatchingSelectors.size(); ++i) {
288 std::string selector = stoppedMatchingSelectors[i].utf8();
289 EXPECT_EQ(1U, frameSelectors.count(selector)) << selector;
290 frameSelectors.erase(selector);
291 }
292 }
293
294 std::map<WebFrame*, std::set<std::string> > m_matchedSelectors;
295 int m_updateCount;
296 };
297
298 TEST_F(WebFrameTest, CSSCallbackAuthorStyleSheet)
299 {
300 CSSCallbackWebFrameClient client;
301 WebView* webView = FrameTestHelpers::createWebViewAndLoad("about:blank", tru e, &client);
302 WebFrame* frame = webView->mainFrame();
303
304 frame->loadHTMLString(
305 "<style>"
306 // This stylesheet checks that the internal property and value can't be
307 // set by a stylesheet, only WebDocument::watchCSSSelectors().
308 "div.initial_on { -internal-callback: none; }"
309 "div.initial_off { -internal-callback: -internal-presence; }"
310 "</style>"
311 "<div class=\"initial_on\"></div>"
312 "<div class=\"initial_off\"></div>",
313 toKURL("about:blank"));
314 runPendingTasks();
315
316 std::vector<WebString> selectors;
317 selectors.push_back(WebString::fromUTF8("div.initial_on"));
318 frame->document().watchCSSSelectors(WebVector<WebString>(selectors));
319 runPendingTasks();
320 EXPECT_EQ(1, client.m_updateCount);
321 EXPECT_THAT(client.m_matchedSelectors[frame],
322 testing::ElementsAre("div.initial_on"));
323
324 WebDocument doc = frame->document();
325 // Check that adding a watched selector calls back for already-present nodes .
326 selectors.push_back(WebString::fromUTF8("div.initial_off"));
327 frame->document().watchCSSSelectors(WebVector<WebString>(selectors));
328 runPendingTasks();
329 EXPECT_EQ(2, client.m_updateCount);
330 EXPECT_THAT(client.m_matchedSelectors[frame],
331 testing::ElementsAre("div.initial_off", "div.initial_on"));
332
333 // Check that we can turn off callbacks for certain selectors.
334 frame->document().watchCSSSelectors(WebVector<WebString>());
335 runPendingTasks();
336 EXPECT_EQ(3, client.m_updateCount);
337 EXPECT_THAT(client.m_matchedSelectors[frame],
338 testing::ElementsAre());
339
340 EXPECT_EQ(1U, client.m_matchedSelectors.size());
341 webView->close();
342 }
343
344 TEST_F(WebFrameTest, CSSCallbackSharedRenderStyle)
345 {
346 CSSCallbackWebFrameClient client;
347 WebView* webView = FrameTestHelpers::createWebViewAndLoad("about:blank", tru e, &client);
348 WebFrame* frame = webView->mainFrame();
349 WebDocument doc = frame->document();
350
351 // Check that adding an element calls back when it matches an existing rule.
352 std::vector<WebString> selectors;
353 selectors.push_back(WebString::fromUTF8("span"));
354 doc.watchCSSSelectors(WebVector<WebString>(selectors));
355
356 frame->executeScript(WebScriptSource(
357 "i1 = document.createElement('span');"
358 "i1.id = 'first_span';"
359 "document.body.appendChild(i1)"));
360 runPendingTasks();
361 EXPECT_EQ(1, client.m_updateCount);
362 EXPECT_THAT(client.m_matchedSelectors[frame],
363 testing::ElementsAre("span"));
364
365 // Adding a second element that shares a RenderStyle shouldn't call back.
366 // We use <span>s to avoid default style rules that can set
367 // RenderStyle::unique().
368 frame->executeScript(WebScriptSource(
369 "i2 = document.createElement('span');"
370 "i2.id = 'second_span';"
371 "i1 = document.getElementById('first_span');"
372 "i1.parentNode.insertBefore(i2, i1.nextSibling);"));
373 runPendingTasks();
374 EXPECT_EQ(1, client.m_updateCount);
375 EXPECT_THAT(client.m_matchedSelectors[frame],
376 testing::ElementsAre("span"));
377
378 // Removing the first element shouldn't call back.
379 frame->executeScript(WebScriptSource(
380 "i1 = document.getElementById('first_span');"
381 "i1.parentNode.removeChild(i1);"));
382 runPendingTasks();
383 EXPECT_EQ(1, client.m_updateCount);
384 EXPECT_THAT(client.m_matchedSelectors[frame],
385 testing::ElementsAre("span"));
386
387 // But removing the second element *should* call back.
388 frame->executeScript(WebScriptSource(
389 "i2 = document.getElementById('second_span');"
390 "i2.parentNode.removeChild(i2);"));
391 runPendingTasks();
392 EXPECT_EQ(2, client.m_updateCount);
393 EXPECT_THAT(client.m_matchedSelectors[frame],
394 testing::ElementsAre());
395
396 EXPECT_EQ(1U, client.m_matchedSelectors.size());
397 webView->close();
398 }
399
400 TEST_F(WebFrameTest, CSSCallbackMultiSelector)
401 {
402 CSSCallbackWebFrameClient client;
403 WebView* webView = FrameTestHelpers::createWebViewAndLoad("about:blank", tru e, &client);
404 WebFrame* frame = webView->mainFrame();
405 frame->loadHTMLString("<span></span>", toKURL("about:blank"));
406 runPendingTasks();
407 WebDocument doc = frame->document();
408
409 // Check that selector lists match as the whole list, not as each element
410 // independently.
411 std::vector<WebString> selectors;
412 selectors.push_back(WebString::fromUTF8("span"));
413 selectors.push_back(WebString::fromUTF8("span,p"));
414 frame->document().watchCSSSelectors(WebVector<WebString>(selectors));
415
416 runPendingTasks();
417 EXPECT_EQ(1, client.m_updateCount);
418 EXPECT_THAT(client.m_matchedSelectors[frame],
419 testing::ElementsAre("span", "span, p"));
420
421 EXPECT_EQ(1U, client.m_matchedSelectors.size());
422 webView->close();
423 }
424
273 TEST_F(WebFrameTest, DispatchMessageEventWithOriginCheck) 425 TEST_F(WebFrameTest, DispatchMessageEventWithOriginCheck)
274 { 426 {
275 registerMockedHttpURLLoad("postmessage_test.html"); 427 registerMockedHttpURLLoad("postmessage_test.html");
276 428
277 // Pass true to enable JavaScript. 429 // Pass true to enable JavaScript.
278 m_webView = FrameTestHelpers::createWebViewAndLoad(m_baseURL + "postmessage_ test.html", true); 430 m_webView = FrameTestHelpers::createWebViewAndLoad(m_baseURL + "postmessage_ test.html", true);
279 431
280 // Send a message with the correct origin. 432 // Send a message with the correct origin.
281 WebSecurityOrigin correctOrigin(WebSecurityOrigin::create(toKURL(m_baseURL)) ); 433 WebSecurityOrigin correctOrigin(WebSecurityOrigin::create(toKURL(m_baseURL)) );
282 WebDOMEvent event = m_webView->mainFrame()->document().createEvent("MessageE vent"); 434 WebDOMEvent event = m_webView->mainFrame()->document().createEvent("MessageE vent");
(...skipping 3125 matching lines...) Expand 10 before | Expand all | Expand 10 after
3408 frame->reload(); 3560 frame->reload();
3409 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); 3561 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
3410 EXPECT_EQ(WebURLRequest::ReloadIgnoringCacheData, frame->dataSource()->reque st().cachePolicy()); 3562 EXPECT_EQ(WebURLRequest::ReloadIgnoringCacheData, frame->dataSource()->reque st().cachePolicy());
3411 3563
3412 m_webView->close(); 3564 m_webView->close();
3413 m_webView = 0; 3565 m_webView = 0;
3414 } 3566 }
3415 3567
3416 3568
3417 } // namespace 3569 } // namespace
OLDNEW
« no previous file with comments | « no previous file | Source/core/core.gypi » ('j') | Source/core/css/DocumentRuleSets.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698