New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement cosmetic filtering #3303
Changes from 1 commit
1715046
0b93419
7512da5
c45be3c
3aca80f
5640c42
6a9fa30
e7af497
ec80ce7
File filter...
Jump to…
add browser tests for cosmetic filtering
- Loading branch information
Verified
| @@ -24,6 +24,7 @@ | ||
| #include "content/public/browser/browser_task_traits.h" | ||
| #include "content/public/browser/browser_thread.h" | ||
| #include "content/public/test/browser_test_utils.h" | ||
| #include "extensions/test/extension_test_message_listener.h" | ||
| #include "net/dns/mock_host_resolver.h" | ||
|
|
||
| using content::BrowserThread; | ||
| @@ -218,6 +219,15 @@ class AdBlockServiceTest : public ExtensionBrowserTest { | ||
| base::CreateSingleThreadTaskRunner({BrowserThread::IO}).get())); | ||
| ASSERT_TRUE(io_helper->Run()); | ||
| } | ||
|
|
||
| void WaitForBraveExtensionShieldsDataReady() { | ||
| // Sometimes, the page can start loading before the Shields panel has | ||
| // received information about the window and tab it's loaded in. | ||
| ExtensionTestMessageListener extension_listener( | ||
| "brave-extension-shields-data-ready", | ||
| false); | ||
| ASSERT_TRUE(extension_listener.WaitUntilSatisfied()); | ||
| } | ||
| }; | ||
|
|
||
| // Load a page with an ad image, and make sure it is blocked. | ||
| @@ -774,3 +784,122 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, RedirectRulesAreRespected) { | ||
| EXPECT_TRUE(as_expected); | ||
| EXPECT_EQ(browser()->profile()->GetPrefs()->GetUint64(kAdsBlocked), 1ULL); | ||
| } | ||
|
|
||
| // Test simple cosmetic filtering | ||
| IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringSimple) { | ||
| UpdateAdBlockInstanceWithRules( | ||
| "b.com###ad-banner\n" | ||
| "##.ad"); | ||
|
|
||
| WaitForBraveExtensionShieldsDataReady(); | ||
|
|
||
| GURL tab_url = embedded_test_server()->GetURL("b.com", | ||
| "/cosmetic_filtering.html"); | ||
| ui_test_utils::NavigateToURL(browser(), tab_url); | ||
|
|
||
| content::WebContents* contents = | ||
| browser()->tab_strip_model()->GetActiveWebContents(); | ||
|
|
||
| bool as_expected = false; | ||
| ASSERT_TRUE(ExecuteScriptAndExtractBool( | ||
| contents, | ||
| "checkSelector('#ad-banner', 'display', 'none')", | ||
| &as_expected)); | ||
| EXPECT_TRUE(as_expected); | ||
|
|
||
| as_expected = false; | ||
| ASSERT_TRUE(ExecuteScriptAndExtractBool( | ||
| contents, | ||
| "checkSelector('.ad-banner', 'display', 'block')", | ||
| &as_expected)); | ||
| EXPECT_TRUE(as_expected); | ||
|
|
||
| as_expected = false; | ||
| ASSERT_TRUE(ExecuteScriptAndExtractBool( | ||
| contents, | ||
| "checkSelector('.ad', 'display', 'none')", | ||
| &as_expected)); | ||
| EXPECT_TRUE(as_expected); | ||
| } | ||
|
|
||
| // Test cosmetic filtering on elements added dynamically | ||
| IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringDynamic) { | ||
| UpdateAdBlockInstanceWithRules("##.blockme"); | ||
|
|
||
| WaitForBraveExtensionShieldsDataReady(); | ||
|
|
||
| GURL tab_url = embedded_test_server()->GetURL("b.com", | ||
| "/cosmetic_filtering.html"); | ||
| ui_test_utils::NavigateToURL(browser(), tab_url); | ||
|
|
||
| content::WebContents* contents = | ||
| browser()->tab_strip_model()->GetActiveWebContents(); | ||
|
|
||
| bool as_expected = false; | ||
| ASSERT_TRUE(ExecuteScriptAndExtractBool( | ||
| contents, | ||
| "addElementsDynamically();\n" | ||
| "checkSelector('.blockme', 'display', 'none')", | ||
| &as_expected)); | ||
| EXPECT_TRUE(as_expected); | ||
|
|
||
| as_expected = false; | ||
| ASSERT_TRUE(ExecuteScriptAndExtractBool( | ||
| contents, | ||
| "checkSelector('.dontblockme', 'display', 'block')", | ||
| &as_expected)); | ||
| EXPECT_TRUE(as_expected); | ||
| } | ||
|
|
||
| // Test custom style rules | ||
| IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringCustomStyle) { | ||
| UpdateAdBlockInstanceWithRules("b.com##.ad:style(padding-bottom: 10px)"); | ||
|
|
||
| WaitForBraveExtensionShieldsDataReady(); | ||
|
|
||
| GURL tab_url = embedded_test_server()->GetURL("b.com", | ||
| "/cosmetic_filtering.html"); | ||
| ui_test_utils::NavigateToURL(browser(), tab_url); | ||
|
|
||
| content::WebContents* contents = | ||
| browser()->tab_strip_model()->GetActiveWebContents(); | ||
|
|
||
| bool as_expected = false; | ||
| ASSERT_TRUE(ExecuteScriptAndExtractBool( | ||
| contents, | ||
| "checkSelector('.ad', 'padding-bottom', '10px')", | ||
| &as_expected)); | ||
| EXPECT_TRUE(as_expected); | ||
| } | ||
|
|
||
| // Test rules overridden by hostname-specific exception rules | ||
| IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CosmeticFilteringUnhide) { | ||
| UpdateAdBlockInstanceWithRules( | ||
| "##.ad\n" | ||
| "b.com#@#.ad\n" | ||
| "###ad-banner\n" | ||
| "a.com#@##ad-banner"); | ||
|
|
||
| WaitForBraveExtensionShieldsDataReady(); | ||
|
|
||
| GURL tab_url = embedded_test_server()->GetURL("b.com", | ||
| "/cosmetic_filtering.html"); | ||
| ui_test_utils::NavigateToURL(browser(), tab_url); | ||
|
|
||
| content::WebContents* contents = | ||
| browser()->tab_strip_model()->GetActiveWebContents(); | ||
|
|
||
| bool as_expected = false; | ||
| ASSERT_TRUE(ExecuteScriptAndExtractBool( | ||
| contents, | ||
| "checkSelector('.ad', 'display', 'block')", | ||
| &as_expected)); | ||
| EXPECT_TRUE(as_expected); | ||
|
|
||
| as_expected = false; | ||
| ASSERT_TRUE(ExecuteScriptAndExtractBool( | ||
| contents, | ||
| "checkSelector('#ad-banner', 'display', 'none')", | ||
| &as_expected)); | ||
| EXPECT_TRUE(as_expected); | ||
|
This conversation was marked as resolved
by bbondy
bbondy
Member
|
||
| } | ||
| @@ -0,0 +1,41 @@ | ||
| <html> | ||
| <head> | ||
| <script> | ||
|
|
||
| function addElementsDynamically() { | ||
| let root = document.documentElement; | ||
| for (let i = 0; i < 10; i++) { | ||
| const e = document.createElement('div') | ||
| e.className = 'blockme' | ||
| root.appendChild(e); | ||
| } | ||
|
|
||
| for (let i = 0; i < 10; i++) { | ||
| const e = document.createElement('div') | ||
| e.className = 'dontblockme' | ||
| root.appendChild(e); | ||
| } | ||
| } | ||
|
|
||
| function checkSelector(selector, property, expected) { | ||
| setTimeout(() => { | ||
| let elements = [].slice.call(document.querySelectorAll(selector)); | ||
| let result = elements.every(e => { | ||
| let style = window.getComputedStyle(e); | ||
| return style[property] === expected; | ||
| }) | ||
| window.domAutomationController.send(result) | ||
| }, 1500) | ||
| } | ||
|
|
||
| </script> | ||
| </head> | ||
| <body> | ||
| <div id="ad-banner"></div> | ||
| <div class="ad-banner"> | ||
| <div class="ad"></div> | ||
| </div> | ||
| <div class="ad"></div> | ||
| <div class="ad"></div> | ||
| </body> | ||
| </html> |
Good job on these tests👍