aboutsummaryrefslogtreecommitdiff
path: root/test/haketilo_test/unit/test_settings.py
blob: 6f669cb4cb9a224565dfb0b48679a1b05135d4cb (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# SPDX-License-Identifier: CC0-1.0

"""
Haketilo unit tests - entire settings page
"""

# This file is part of Haketilo
#
# Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the CC0 1.0 Universal License as published by
# the Creative Commons Corporation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# CC0 1.0 Universal License for more details.

import pytest
from .utils import *

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

from ..extension_crafting import ExtraHTML
from ..script_loader import load_script
from .utils import *

ext_data = {
    'background_script': broker_js,
    'extra_html': ExtraHTML('html/settings.html', wrap_into_htmldoc=False)
}

@pytest.mark.ext_data(ext_data)
@pytest.mark.usefixtures('webextension')
def test_settings_page_tabs(driver, execute_in_page):
    """
    Test navigation through the tabs of the settings page.
    """
    # First, put some sample data in IndexedDB.
    execute_in_page(load_script('common/indexeddb.js'))
    execute_in_page(
        '''
        initial_data = arguments[0];
        returnval(get_db().then(() => {}));
        ''',
        make_complete_sample_data())

    # Now navigate to settings page.
    testpage_url = driver.execute_script('return window.location.href;')
    driver.get(testpage_url.replace('testpage.html', 'html/settings.html'))
    execute_in_page('init_settings_page();')

    WebDriverWait(driver, 10)\
        .until(EC.visibility_of_element_located((By.ID, "main_view")))

    assert driver.find_elements_by_id('loader') == []
    assert not driver.find_element_by_id('indexeddb_error').is_displayed()

    names = ['blocking', 'mappings', 'resources', 'new_payload', 'repos']
    tabs = dict([(n, driver.find_element_by_id(f'{n}_tab')) for n in names])
    heads = dict([(n, driver.find_element_by_id(f'{n}_head')) for n in names])

    for i, tab_name in enumerate(['new_payload', *names]):
        if (i > 0):
            heads[tab_name].click()

        assert 'active_head' in heads[tab_name].get_attribute('class')
        assert 'active_tab' in tabs[tab_name].get_attribute('class')
        assert tabs[tab_name].is_displayed()
        for tab_name_2 in [n for n in names if n != tab_name]:
            assert 'active_head' not in heads[tab_name_2].get_attribute('class')
            assert 'active_tab' not in tabs[tab_name_2].get_attribute('class')
            assert not tabs[tab_name_2].is_displayed()

@pytest.mark.ext_data({
    **ext_data,
    'navigate_to': 'html/settings.html'
})
@pytest.mark.usefixtures('webextension')
@pytest.mark.parametrize('incognito', [True, False])
def test_settings_page_error(driver, execute_in_page, incognito):
    """
    Test whether being in Private Browsing mode and failure to access IndexedDB
    in settings page result in the appropriate messages being shown.
    """
    error_divs = 'indexeddb_error', 'private_mode_error'

    execute_in_page(
        '''{
        /*
         * Mock an unavailable IndexedDB. Calling onerror() without having set
         * "errorCode" on the request is the behavior observed under Mozilla
         * when IndexedDB is disabled.
         */
        indexedDB.open = function() {
            const dummy_open_request = {};
            const dummy_event = {target: dummy_open_request};
            setTimeout(() => dummy_open_request.onerror(dummy_event), 0);

            return dummy_open_request;
        }

        const dummy_tab = {incognito: arguments[0]};
        browser.tabs.getCurrent = () => Promise.resolve(dummy_tab);

        init_settings_page();
        }''',
        incognito)

    WebDriverWait(driver, 10)\
        .until(EC.visibility_of_element_located((By.ID, error_divs[incognito])))

    assert driver.find_elements_by_id('loader') == []
    assert not driver.find_element_by_id('main_view').is_displayed()