Skip to content

Commit

Permalink
add browser tests for cosmetic filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
antonok-edm committed Sep 3, 2019
1 parent 0e7c3f5 commit 25d6c39
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const getShieldSettingsForTabData = (tabData?: chrome.tabs.Tab) => {

return Promise.all([
chrome.braveShields.getBraveShieldsEnabledAsync(tabData.url),
chrome.braveShields.getCosmeticFilteredElementsAsync(tabData.url),
chrome.braveShields.getCosmeticFilteringControlTypeAsync(tabData.url),
chrome.braveShields.getAdControlTypeAsync(tabData.url),
chrome.braveShields.getHTTPSEverywhereEnabledAsync(tabData.url),
chrome.braveShields.getNoScriptControlTypeAsync(tabData.url),
Expand Down Expand Up @@ -51,6 +51,7 @@ export const getShieldSettingsForTabData = (tabData?: chrome.tabs.Tab) => {
hostname,
id: tabData.id,
braveShields: 'block',
cosmeticBlocking: 0,
ads: 0,
trackers: 0,
httpUpgradableResources: 0,
Expand Down Expand Up @@ -97,7 +98,7 @@ export const setAllowBraveShields = (origin: string, setting: string) =>
* @return a promise which resolves when the setting is set
*/
export const setAllowCosmeticElements = (origin: string, setting: string) =>
chrome.braveShields.setCosmeticFilteredElementsAsync(setting, origin)
chrome.braveShields.setCosmeticFilteringControlTypeAsync(setting, origin)

/**
* Changes the ads at origin to be allowed or blocked.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BlockTypes, BlockOptions, BlockFPOptions, BlockJSOptions, BlockCookiesO

export interface ShieldDetails {
id: number
cosmeticBlocking: BlockOptions
ads: BlockOptions
trackers: BlockOptions
httpUpgradableResources: BlockOptions
Expand Down
109 changes: 109 additions & 0 deletions components/brave_shields/browser/ad_block_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -744,3 +744,112 @@ IN_PROC_BROWSER_TEST_F(AdBlockServiceTest, CancelRequestOptionTest) {
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"
);

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"
);

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: 0px)"
);

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', '0px')",
&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"
);

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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ describe('cosmeticFilter API', () => {
})
})
})
describe('applySiteFilters', () => {
describe('applyCSSCosmeticFilters', () => {
const filter = '#cssFilter'
const filter2 = '#cssFilter2'

Expand Down Expand Up @@ -206,7 +206,7 @@ describe('cosmeticFilter API', () => {
'brave.com': [filter]
}
})
cosmeticFilterAPI.applySiteFilters(1, 'brave.com')
cosmeticFilterAPI.applyCSSCosmeticFilters(1, 'brave.com')
expect(insertCSSStub.getCall(0).args[0]).toEqual(1)
expect(insertCSSStub.getCall(0).args[1]).toEqual({
code: `${filter} {display: none !important;}`,
Expand All @@ -220,7 +220,7 @@ describe('cosmeticFilter API', () => {
'brave.com': [filter, filter2]
}
})
cosmeticFilterAPI.applySiteFilters(1, 'brave.com')
cosmeticFilterAPI.applyCSSCosmeticFilters(1, 'brave.com')
expect(insertCSSStub.getCall(0).args[0]).toEqual(1)
expect(insertCSSStub.getCall(0).args[1]).toEqual({
code: `${filter } {display: none !important;}`,
Expand All @@ -239,7 +239,7 @@ describe('cosmeticFilter API', () => {
getStorageStub.yields({
cosmeticFilterList: {}
})
cosmeticFilterAPI.applySiteFilters(1, 'brave.com')
cosmeticFilterAPI.applyCSSCosmeticFilters(1, 'brave.com')
expect(insertCSSStub.called).toBe(false)
})
it('doesn\'t apply filters if storage is explicitly undefined', () => {
Expand All @@ -248,7 +248,7 @@ describe('cosmeticFilter API', () => {
'brave.com': undefined
}
})
cosmeticFilterAPI.applySiteFilters(1, 'brave.com')
cosmeticFilterAPI.applyCSSCosmeticFilters(1, 'brave.com')
expect(insertCSSStub.called).toBe(false)
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('Shields API', () => {
origin: 'https://www.brave.com',
hostname: 'www.brave.com',
braveShields: 'block',
cosmeticBlocking: 'block',
ads: 'block',
trackers: 'block',
httpUpgradableResources: 'block',
Expand Down Expand Up @@ -94,6 +95,7 @@ describe('Shields API', () => {
})
it('resolves and calls requestShieldPanelData', (cb) => {
const details: ShieldDetails = {
cosmeticBlocking: 'block',
ads: 'block',
trackers: 'block',
httpUpgradableResources: 'block',
Expand Down
6 changes: 6 additions & 0 deletions components/test/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ export const getMockChrome = () => {
getBraveShieldsEnabledAsync: function (url: string) {
return Promise.resolve(false)
},
getCosmeticFilteringControlTypeAsync: function (url: string) {
return Promise.resolve('block')
},
getAdControlTypeAsync: function (url: string) {
return Promise.resolve('block')
},
Expand All @@ -235,6 +238,9 @@ export const getMockChrome = () => {
setBraveShieldsEnabledAsync: function (url: string, enabled: boolean) {
return new Promise(() => [])
},
setCosmeticFilteringControlTypeAsync: function (url: string, controlType: string) {
return new Promise(() => [])
},
setAdControlTypeAsync: function (url: string, controlType: string) {
return new Promise(() => [])
},
Expand Down
41 changes: 41 additions & 0 deletions test/data/cosmetic_filtering.html
Original file line number Diff line number Diff line change
@@ -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)
}, 1000)
}

</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>

0 comments on commit 25d6c39

Please sign in to comment.