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

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: Initial 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
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 21 matching lines...) Expand all
66 #include "core/page/Settings.h" 67 #include "core/page/Settings.h"
67 #include "core/platform/ScrollbarTheme.h" 68 #include "core/platform/ScrollbarTheme.h"
68 #include "core/platform/graphics/FloatRect.h" 69 #include "core/platform/graphics/FloatRect.h"
69 #include "core/platform/network/ResourceError.h" 70 #include "core/platform/network/ResourceError.h"
70 #include "core/rendering/HitTestResult.h" 71 #include "core/rendering/HitTestResult.h"
71 #include "core/rendering/RenderLayerCompositor.h" 72 #include "core/rendering/RenderLayerCompositor.h"
72 #include "core/rendering/RenderView.h" 73 #include "core/rendering/RenderView.h"
73 #include "core/rendering/TextAutosizer.h" 74 #include "core/rendering/TextAutosizer.h"
74 #include "v8.h" 75 #include "v8.h"
75 #include "public/platform/Platform.h" 76 #include "public/platform/Platform.h"
77 #include "public/platform/WebCString.h"
76 #include "public/platform/WebFloatRect.h" 78 #include "public/platform/WebFloatRect.h"
77 #include "public/platform/WebThread.h" 79 #include "public/platform/WebThread.h"
78 #include "public/platform/WebUnitTestSupport.h" 80 #include "public/platform/WebUnitTestSupport.h"
79 #include "public/platform/WebURLResponse.h" 81 #include "public/platform/WebURLResponse.h"
80 #include "wtf/dtoa/utils.h" 82 #include "wtf/dtoa/utils.h"
81 #include "wtf/Forward.h" 83 #include "wtf/Forward.h"
84 #include <map>
82 85
83 using namespace WebKit; 86 using namespace WebKit;
84 using WebCore::Document; 87 using WebCore::Document;
85 using WebCore::DocumentMarker; 88 using WebCore::DocumentMarker;
86 using WebCore::Element; 89 using WebCore::Element;
87 using WebCore::FloatRect; 90 using WebCore::FloatRect;
88 using WebCore::HitTestRequest; 91 using WebCore::HitTestRequest;
89 using WebCore::Range; 92 using WebCore::Range;
90 using WebKit::URLTestHelpers::toKURL; 93 using WebKit::URLTestHelpers::toKURL;
91 using WebKit::FrameTestHelpers::runPendingTasks; 94 using WebKit::FrameTestHelpers::runPendingTasks;
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 FrameTestHelpers::loadFrame(m_webView->mainFrame(), "javascript:document.bod y.appendChild(document.createTextNode('Clobbered'))"); 264 FrameTestHelpers::loadFrame(m_webView->mainFrame(), "javascript:document.bod y.appendChild(document.createTextNode('Clobbered'))");
262 265
263 // Required to see any updates in contentAsText. 266 // Required to see any updates in contentAsText.
264 m_webView->layout(); 267 m_webView->layout();
265 268
266 // Now retrieve the frame's text and ensure it wasn't modified by running ja vascript. 269 // Now retrieve the frame's text and ensure it wasn't modified by running ja vascript.
267 std::string content = std::string(m_webView->mainFrame()->contentAsText(1024 ).utf8().data()); 270 std::string content = std::string(m_webView->mainFrame()->contentAsText(1024 ).utf8().data());
268 EXPECT_EQ(std::string::npos, content.find("Clobbered")); 271 EXPECT_EQ(std::string::npos, content.find("Clobbered"));
269 } 272 }
270 273
274 #if ENABLE(CSS_CALLBACKS)
275 struct CssCallbackWebFrameClient : public WebFrameClient {
276 CssCallbackWebFrameClient() : m_updateCount(0) { }
277 virtual void didMatchCss(WebFrame* frame, const WebVector<WebString>& newlyM atchingSelectors, const WebVector<WebString>& stoppedMatchingSelectors) OVERRIDE
278 {
279 ++m_updateCount;
280 std::set<std::string>& frameSelectors = m_matchedSelectors[frame];
281 for (size_t i = 0; i < newlyMatchingSelectors.size(); ++i) {
282 std::string selector = newlyMatchingSelectors[i].utf8();
283 EXPECT_EQ(0U, frameSelectors.count(selector)) << selector;
284 frameSelectors.insert(selector);
285 }
286 for (size_t i = 0; i < stoppedMatchingSelectors.size(); ++i) {
287 std::string selector = stoppedMatchingSelectors[i].utf8();
288 EXPECT_EQ(1U, frameSelectors.count(selector)) << selector;
289 frameSelectors.erase(selector);
290 }
291 }
292
293 std::map<WebFrame*, std::set<std::string> > m_matchedSelectors;
294 int m_updateCount;
295 };
296
297 TEST_F(WebFrameTest, CssCallbackAuthorStyleSheet)
298 {
299 CssCallbackWebFrameClient client;
300 WebView* webView = FrameTestHelpers::createWebViewAndLoad("about:blank", tru e, &client);
301 WebFrame* frame = webView->mainFrame();
302
303 frame->loadHTMLString(
304 "<style>"
305 // This stylesheet checks that the internal property and value can't be
306 // set by a stylesheet, only WebDocument::watchCssSelectors().
307 "div.initial_on { -internal-callback: none; }"
308 "div.initial_off { -internal-callback: -internal-presence; }"
309 "</style>"
310 "<div class=\"initial_on\"></div>"
311 "<div class=\"initial_off\"></div>",
312 toKURL("about:blank"));
313 runPendingTasks();
314
315 std::vector<WebString> selectors;
316 selectors.push_back(WebString::fromUTF8("div.initial_on"));
317 frame->document().watchCssSelectors(WebVector<WebString>(selectors));
318 runPendingTasks();
319 EXPECT_EQ(1, client.m_updateCount);
320 EXPECT_THAT(client.m_matchedSelectors[frame],
321 testing::ElementsAre("div.initial_on"));
322
323 WebDocument doc = frame->document();
324 // Check that adding a watched selector calls back for already-present nodes .
325 selectors.push_back(WebString::fromUTF8("div.initial_off"));
326 frame->document().watchCssSelectors(WebVector<WebString>(selectors));
327 runPendingTasks();
328 EXPECT_EQ(2, client.m_updateCount);
329 EXPECT_THAT(client.m_matchedSelectors[frame],
330 testing::ElementsAre("div.initial_off", "div.initial_on"));
331
332 // Check that we can turn off callbacks for certain selectors.
333 frame->document().watchCssSelectors(WebVector<WebString>());
334 runPendingTasks();
335 EXPECT_EQ(3, client.m_updateCount);
336 EXPECT_THAT(client.m_matchedSelectors[frame],
337 testing::ElementsAre());
338
339 EXPECT_EQ(1U, client.m_matchedSelectors.size());
340 webView->close();
341 }
342
343 TEST_F(WebFrameTest, CssCallbackSharedRenderStyle)
344 {
345 CssCallbackWebFrameClient client;
346 WebView* webView = FrameTestHelpers::createWebViewAndLoad("about:blank", tru e, &client);
347 WebFrame* frame = webView->mainFrame();
348 WebDocument doc = frame->document();
349
350 // Check that adding an element calls back when it matches an existing rule.
351 std::vector<WebString> selectors;
352 selectors.push_back(WebString::fromUTF8("span"));
353 doc.watchCssSelectors(WebVector<WebString>(selectors));
354
355 frame->executeScript(WebScriptSource(
356 "i1 = document.createElement('span');"
357 "i1.id = 'first_span';"
358 "document.body.appendChild(i1)"));
359 runPendingTasks();
360 EXPECT_EQ(1, client.m_updateCount);
361 EXPECT_THAT(client.m_matchedSelectors[frame],
362 testing::ElementsAre("span"));
363
364 // Adding a second element that shares a RenderStyle shouldn't call back.
365 // We use <span>s to avoid default style rules that can set
366 // RenderStyle::unique().
367 frame->executeScript(WebScriptSource(
368 "i2 = document.createElement('span');"
369 "i2.id = 'second_span';"
370 "i1 = document.getElementById('first_span');"
371 "i1.parentNode.insertBefore(i2, i1.nextSibling);"));
372 runPendingTasks();
373 EXPECT_EQ(1, client.m_updateCount);
374 EXPECT_THAT(client.m_matchedSelectors[frame],
375 testing::ElementsAre("span"));
376
377 // Removing the first element shouldn't call back.
378 frame->executeScript(WebScriptSource(
379 "i1 = document.getElementById('first_span');"
380 "i1.parentNode.removeChild(i1);"));
381 runPendingTasks();
382 EXPECT_EQ(1, client.m_updateCount);
383 EXPECT_THAT(client.m_matchedSelectors[frame],
384 testing::ElementsAre("span"));
385
386 // But removing the second element *should* call back.
387 frame->executeScript(WebScriptSource(
388 "i2 = document.getElementById('second_span');"
389 "i2.parentNode.removeChild(i2);"));
390 runPendingTasks();
391 EXPECT_EQ(2, client.m_updateCount);
392 EXPECT_THAT(client.m_matchedSelectors[frame],
393 testing::ElementsAre());
394
395 EXPECT_EQ(1U, client.m_matchedSelectors.size());
396 webView->close();
397 }
398
399 TEST_F(WebFrameTest, CssCallbackMultiSelector)
400 {
401 CssCallbackWebFrameClient client;
402 WebView* webView = FrameTestHelpers::createWebViewAndLoad("about:blank", tru e, &client);
403 WebFrame* frame = webView->mainFrame();
404 frame->loadHTMLString("<span></span>", toKURL("about:blank"));
405 runPendingTasks();
406 WebDocument doc = frame->document();
407
408 // Check that selector lists match as the whole list, not as each element
409 // independently.
410 std::vector<WebString> selectors;
411 selectors.push_back(WebString::fromUTF8("span"));
412 selectors.push_back(WebString::fromUTF8("span,p"));
413 frame->document().watchCssSelectors(WebVector<WebString>(selectors));
414
415 runPendingTasks();
416 EXPECT_EQ(1, client.m_updateCount);
417 EXPECT_THAT(client.m_matchedSelectors[frame],
418 testing::ElementsAre("span", "span, p"));
419
420 EXPECT_EQ(1U, client.m_matchedSelectors.size());
421 webView->close();
422 }
423 #endif
424
271 TEST_F(WebFrameTest, DispatchMessageEventWithOriginCheck) 425 TEST_F(WebFrameTest, DispatchMessageEventWithOriginCheck)
272 { 426 {
273 registerMockedHttpURLLoad("postmessage_test.html"); 427 registerMockedHttpURLLoad("postmessage_test.html");
274 428
275 // Pass true to enable JavaScript. 429 // Pass true to enable JavaScript.
276 m_webView = FrameTestHelpers::createWebViewAndLoad(m_baseURL + "postmessage_ test.html", true); 430 m_webView = FrameTestHelpers::createWebViewAndLoad(m_baseURL + "postmessage_ test.html", true);
277 431
278 // Send a message with the correct origin. 432 // Send a message with the correct origin.
279 WebSecurityOrigin correctOrigin(WebSecurityOrigin::create(toKURL(m_baseURL)) ); 433 WebSecurityOrigin correctOrigin(WebSecurityOrigin::create(toKURL(m_baseURL)) );
280 WebDOMEvent event = m_webView->mainFrame()->document().createEvent("MessageE vent"); 434 WebDOMEvent event = m_webView->mainFrame()->document().createEvent("MessageE vent");
(...skipping 3123 matching lines...) Expand 10 before | Expand all | Expand 10 after
3404 frame->reload(); 3558 frame->reload();
3405 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests(); 3559 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
3406 EXPECT_EQ(WebURLRequest::ReloadIgnoringCacheData, frame->dataSource()->reque st().cachePolicy()); 3560 EXPECT_EQ(WebURLRequest::ReloadIgnoringCacheData, frame->dataSource()->reque st().cachePolicy());
3407 3561
3408 m_webView->close(); 3562 m_webView->close();
3409 m_webView = 0; 3563 m_webView = 0;
3410 } 3564 }
3411 3565
3412 3566
3413 } // namespace 3567 } // namespace
OLDNEW
« no previous file with comments | « Source/WebKit/chromium/src/WebScriptBindings.cpp ('k') | Source/core/css/CSSComputedStyleDeclaration.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698