Skip to content

Commit

Permalink
Add more popup tests
Browse files Browse the repository at this point in the history
Following up on #2748
  • Loading branch information
ghostwords committed May 17, 2021
1 parent 2a638cd commit b82f462
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 46 deletions.
47 changes: 22 additions & 25 deletions src/js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,31 +200,6 @@ function init() {
// add event listeners for click-to-expand first party protections popup section
$('#firstparty-protections-header').on('click', toggleFirstPartyInfoHandler);

// show sliders when sliders were shown last
// or when there is at least one breakage warning
if (POPUP_DATA.settings.showExpandedTrackingSection || (
POPUP_DATA.cookieblocked && Object.keys(POPUP_DATA.cookieblocked).some(
d => POPUP_DATA.origins[d] == constants.USER_BLOCK)
)) {
$('#expand-blocked-resources').hide();
$('#collapse-blocked-resources').show();
$('#blockedResources').show();

} else {
$('#expand-blocked-resources').show();
$('#collapse-blocked-resources').hide();
// show sliders regardless when the button
// that lets you toggle slider visibility
// isn't shown for whatever reason
// (for ex.: "no trackers blocked" but we need to show
// one or more "don't appear to be tracking you" sliders)
if (!$('#tracker-list-header').is(':visible')) {
$('#blockedResources').show();
} else {
$('#blockedResources').hide();
}
}

// show firstparty protections message if current tab is in our content scripts
if (POPUP_DATA.enabled && POPUP_DATA.isOnFirstParty) {
$("#firstparty-protections-container").show();
Expand Down Expand Up @@ -579,6 +554,22 @@ function refreshPopup() {
$("#error_input").val(POPUP_DATA.errorText);
}

// show sliders when sliders were shown last
// or when there is at least one breakage warning
if (POPUP_DATA.settings.showExpandedTrackingSection || (
POPUP_DATA.cookieblocked && Object.keys(POPUP_DATA.cookieblocked).some(
d => POPUP_DATA.origins[d] == constants.USER_BLOCK)
)) {
$('#expand-blocked-resources').hide();
$('#collapse-blocked-resources').show();
$('#blockedResources').show();

} else {
$('#expand-blocked-resources').show();
$('#collapse-blocked-resources').hide();
$('#blockedResources').hide();
}

let origins = POPUP_DATA.origins;
let originsArr = [];
if (origins) {
Expand Down Expand Up @@ -674,6 +665,12 @@ function refreshPopup() {
// show "no trackers" message
$("#instructions-no-trackers").show();

if (printable.length) {
// make sure to show domain list
// (there is no toggle button when nothing was blocked)
$('#blockedResources').show();
}

} else {
$('#tracker-list-header').show();
$('#instructions-many-trackers').html(chrome.i18n.getMessage(
Expand Down
2 changes: 1 addition & 1 deletion src/skin/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ <h2 id="title-name"><span class="i18n_name"></span></h2>
<span id="no-third-parties" class="i18n_popup_blocked" style="display:none"></span>
</div>
</div>
<div id="blockedResources"></div>
<div id="blockedResources" style="display:none"></div>
</div>

<div id="special-browser-page" style="display:none">
Expand Down
181 changes: 161 additions & 20 deletions tests/selenium/popup_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,45 +39,50 @@ def wait_for_page_to_start_loading(self, url, timeout=20):
def open_popup(self, show_nag=False, origins=None):
"""Open popup and optionally close overlay."""

DUMMY_PAGE_URL = "https://efforg.github.io/privacybadger-test-fixtures/"

# hack to get tabData populated for the popup's tab
# to get the popup shown for regular pages
# as opposed to special (no-tabData) browser pages
self.open_window()
self.load_url(DUMMY_PAGE_URL)

self.open_window()
self.load_url(self.popup_url)
self.wait_for_script("return window.POPUP_INITIALIZED")

# override tab ID (to get regular page popup instead of
# special browser page popup),
# optionally set the domains the popup should report,
# optionally ask for the new user welcome page reminder
# populate tabData for the popup's tab
# to get a regular page popup
# instead of a special browser page popup
#
# optionally set the domains the popup should report
#
# optionally show the new user welcome page reminder
popup_js = (
"(function (origins, show_nag, DUMMY_PAGE_URL) {"
"chrome.tabs.query({ url: DUMMY_PAGE_URL }, (tabs) => {"
"(function (origins, show_nag) {"
""
"let bg = chrome.extension.getBackgroundPage();"
""
"chrome.tabs.getCurrent(tab => {"
" bg.badger.recordFrame(tab.id, 0, tab.url);"
""
" for (let domain of Object.keys(origins)) {"
" bg.badger.logThirdPartyOriginOnTab(tab.id, domain, origins[domain]);"
" }"
""
" chrome.runtime.sendMessage({"
" type: 'getPopupData',"
" tabId: tabs[0].id"
" tabId: tab.id"
" }, (response) => {"
" response.settings.seenComic = !show_nag;"
" response.origins = origins;"
" setPopupData(response);"
" refreshPopup();"
" showNagMaybe();"
" window.DONE_REFRESHING = true;"
" });"
"});"
"}(arguments[0], arguments[1], arguments[2]));"
""
"}(arguments[0], arguments[1]));"
)
self.js(popup_js, origins if origins else {}, show_nag, DUMMY_PAGE_URL)
# wait until the async getTab function is done
self.js(popup_js, origins if origins else {}, show_nag)

# wait until the async functions above are done
self.wait_for_script(
"return typeof window.DONE_REFRESHING != 'undefined'",
timeout=5,
message="Timed out waiting for getTab() to complete."
message="Timed out waiting for popup to finish initializing"
)

# wait for any sliders to finish rendering
Expand Down Expand Up @@ -230,6 +235,11 @@ def test_reverting_control(self):

self.open_popup(origins={DOMAIN:"cookieblock"})

# reveal sliders
self.driver.find_element_by_id('expand-blocked-resources').click()
# TODO retry instead
time.sleep(1)

# set the domain to user control
# click input with JavaScript to avoid "Element ... is not clickable" /
# "Other element would receive the click" Selenium limitation
Expand Down Expand Up @@ -350,6 +360,137 @@ def test_donate_button(self):
self.assertEqual(self.driver.current_url, EFF_URL,
"EFF website should open after clicking donate button on popup")

def test_breakage_warnings(self):
YLIST_DOMAIN = "jquery.com"
self.open_popup(origins={YLIST_DOMAIN: "cookieblock"})

def get_breakage_icon():
return self.driver.find_element_by_css_selector(
'div.clicker[data-origin="{}"] span.breakage-warning'.format(YLIST_DOMAIN))

# reveal sliders
self.driver.find_element_by_id('expand-blocked-resources').click()
# TODO retry instead
time.sleep(1)

# verify there is no breakage warning
breakage_icon = get_breakage_icon()
self.assertFalse(breakage_icon.is_displayed())

# manually block the yellowlisted domain
self.js("$('#block-{}').click()".format(YLIST_DOMAIN.replace(".", "-")))

# verify breakage warning is shown
breakage_icon = get_breakage_icon()
self.assertTrue(breakage_icon.is_displayed())

# verify breakage warning is there when reopened
self.open_popup(origins={YLIST_DOMAIN: "user_block"}) # TODO hack
breakage_icon = get_breakage_icon()
self.assertTrue(breakage_icon.is_displayed())

def test_slider_hiding(self):
YLIST_DOMAIN = "jquery.com"
TEST_DOMAINS = {
"example.com": "block",
YLIST_DOMAIN: "cookieblock"
}

def assert_hidden(sliders):
for slider in sliders:
self.assertFalse(slider.is_displayed(),
"{} is visible but should be hidden".format(
slider.get_attribute('data-origin')))

def assert_visible(sliders):
for slider in sliders:
self.assertTrue(slider.is_displayed(),
"{} is hidden but should be visible".format(
slider.get_attribute('data-origin')))

self.open_popup(origins=TEST_DOMAINS)
sliders = self.driver.find_elements_by_css_selector('div.clicker')

# verify we have the expected number of sliders
self.assertEqual(len(sliders), len(TEST_DOMAINS))

# verify sliders are hidden
assert_hidden(sliders)

# reveal sliders
self.driver.find_element_by_id('expand-blocked-resources').click()
# TODO retry instead
time.sleep(1)

# verify sliders are visible
assert_visible(sliders)

# reopen popup
self.open_popup(origins=TEST_DOMAINS)
sliders = self.driver.find_elements_by_css_selector('div.clicker')

# verify sliders are visible
assert_visible(sliders)

# verify domain is shown second in the list
self.assertEqual(sliders[1].get_attribute('data-origin'), YLIST_DOMAIN)

# manually block the yellowlisted domain
self.js("$('#block-{}').click()".format(YLIST_DOMAIN.replace(".", "-")))

# hide sliders
self.driver.find_element_by_id('collapse-blocked-resources').click()
# TODO retry instead
time.sleep(1)

# verify sliders are hidden
assert_hidden(sliders)

# reopen popup
TEST_DOMAINS[YLIST_DOMAIN] = "user_block" # TODO hack
self.open_popup(origins=TEST_DOMAINS)
sliders = self.driver.find_elements_by_css_selector('div.clicker')

# verify sliders are visible
assert_visible(sliders)

# verify breakage warning slider is at the top
self.assertEqual(sliders[0].get_attribute('data-origin'), YLIST_DOMAIN)

# restore the user-set slider to default action
self.driver.find_element_by_css_selector(
'div[data-origin="{}"] a.honeybadgerPowered'.format(YLIST_DOMAIN)
).click()

# get back to a valid window handle as the window just got closed
self.driver.switch_to.window(self.driver.window_handles[0])

# reopen popup
TEST_DOMAINS[YLIST_DOMAIN] = "cookieblock" # TODO hack
self.open_popup(origins=TEST_DOMAINS)
sliders = self.driver.find_elements_by_css_selector('div.clicker')

# verify sliders are hidden again
assert_hidden(sliders)

def test_nothing_blocked_slider_list(self):
# enable local learning and showing non-tracking domains
self.load_url(self.options_url)
self.wait_for_script("return window.OPTIONS_INITIALIZED")
self.find_el_by_css('#local-learning-checkbox').click()
self.find_el_by_css('#show-nontracking-domains-checkbox').click()

# base case: verify blocked slider is hidden (list is collapsed)
self.open_popup(origins={'example.com': 'block'})
slider = self.driver.find_element_by_css_selector('div.clicker[data-origin="example.com"]')
self.assertFalse(slider.is_displayed())

# reopen popup
self.open_popup(origins={'example.com': 'noaction'})
# verify a non-tracking slider gets shown (no collapsing, just show the list)
slider = self.driver.find_element_by_css_selector('div.clicker[data-origin="example.com"]')
self.assertTrue(slider.is_displayed())


if __name__ == "__main__":
unittest.main()

0 comments on commit b82f462

Please sign in to comment.