Project

General

Profile

Download (4.09 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / test / haketilo_test / unit / test_settings.py @ aec5c9ae

1
# SPDX-License-Identifier: CC0-1.0
2

    
3
"""
4
Haketilo unit tests - entire settings page
5
"""
6

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

    
20
import pytest
21
from .utils import *
22

    
23
from selenium.webdriver.support.ui import WebDriverWait
24
from selenium.webdriver.support import expected_conditions as EC
25
from selenium.webdriver.common.by import By
26

    
27
from ..extension_crafting import ExtraHTML
28
from ..script_loader import load_script
29
from .utils import *
30

    
31
ext_data = {
32
    'background_script': broker_js,
33
    'extra_html': ExtraHTML('html/settings.html', wrap_into_htmldoc=False)
34
}
35

    
36
@pytest.mark.ext_data(ext_data)
37
@pytest.mark.usefixtures('webextension')
38
def test_settings_page_tabs(driver, execute_in_page):
39
    """
40
    Test navigation through the tabs of the settings page.
41
    """
42
    # First, put some sample data in IndexedDB.
43
    execute_in_page(load_script('common/indexeddb.js'))
44
    execute_in_page(
45
        '''
46
        initial_data = arguments[0];
47
        returnval(get_db().then(() => {}));
48
        ''',
49
        make_complete_sample_data())
50

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

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

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

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

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

    
70
        assert 'active_head' in heads[tab_name].get_attribute('class')
71
        assert 'active_tab' in tabs[tab_name].get_attribute('class')
72
        assert tabs[tab_name].is_displayed()
73
        for tab_name_2 in [n for n in names if n != tab_name]:
74
            assert 'active_head' not in heads[tab_name_2].get_attribute('class')
75
            assert 'active_tab' not in tabs[tab_name_2].get_attribute('class')
76
            assert not tabs[tab_name_2].is_displayed()
77

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

    
91
    execute_in_page(
92
        '''{
93
        /*
94
         * Mock an unavailable IndexedDB. Calling onerror() without having set
95
         * "errorCode" on the request is the behavior observed under Mozilla
96
         * when IndexedDB is disabled.
97
         */
98
        indexedDB.open = function() {
99
            const dummy_open_request = {};
100
            const dummy_event = {target: dummy_open_request};
101
            setTimeout(() => dummy_open_request.onerror(dummy_event), 0);
102

    
103
            return dummy_open_request;
104
        }
105

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

    
109
        init_settings_page();
110
        }''',
111
        incognito)
112

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

    
116
    assert driver.find_elements_by_id('loader') == []
117
    assert not driver.find_element_by_id('main_view').is_displayed()
(23-23/26)