Example #1
0
def test_no_name_with_fallback():
    """Ensure that script's name can fallback to the provided filename."""
    script = greasemonkey.GreasemonkeyScript([("something", "else")],
                                             "",
                                             filename=r"C:\COM1")
    assert script
    assert script.name == r"C:\COM1"
Example #2
0
    def test_greasemonkey_good_worlds_are_passed(self, worldid, scripts_helper,
                                                 caplog):
        """Make sure scripts with valid worlds have it set."""
        scripts = [
            greasemonkey.GreasemonkeyScript([('name', 'foo'),
                                             ('qute-js-world', worldid)], None)
        ]

        with caplog.at_level(logging.ERROR, 'greasemonkey'):
            scripts_helper.inject(scripts)

        assert scripts_helper.get_script().worldId() == worldid
Example #3
0
    def test_greasemonkey_good_worlds_are_passed(self, worldid,
                                                 webengine_scripts, caplog):
        """Make sure scripts with valid worlds have it set."""
        scripts = [
            greasemonkey.GreasemonkeyScript([('name', 'foo'),
                                             ('qute-js-world', worldid)], None)
        ]

        with caplog.at_level(logging.ERROR, 'greasemonkey'):
            webengine_scripts._inject_greasemonkey_scripts(scripts)

        collection = webengine_scripts._widget.page().scripts()
        assert collection.toList()[-1].worldId() == worldid
Example #4
0
    def test_greasemonkey_undefined_world(self, scripts_helper, caplog):
        """Make sure scripts with non-existent worlds are rejected."""
        scripts = [
            greasemonkey.GreasemonkeyScript([('qute-js-world', 'Mars'),
                                             ('name', 'test')], None)
        ]

        with caplog.at_level(logging.ERROR, 'greasemonkey'):
            injected = scripts_helper.inject(scripts)

        assert len(caplog.records) == 1
        msg = caplog.messages[0]
        assert "has invalid value for '@qute-js-world': Mars" in msg

        assert not injected
Example #5
0
    def test_greasemonkey_undefined_world(self, webengine_scripts, caplog):
        """Make sure scripts with non-existent worlds are rejected."""
        scripts = [
            greasemonkey.GreasemonkeyScript([('qute-js-world', 'Mars'),
                                             ('name', 'test')], None)
        ]

        with caplog.at_level(logging.ERROR, 'greasemonkey'):
            webengine_scripts._inject_greasemonkey_scripts(scripts)

        assert len(caplog.records) == 1
        msg = caplog.records[0].message
        assert "has invalid value for '@qute-js-world': Mars" in msg
        collection = webengine_scripts._widget.page().scripts().toList()
        assert not any(script.name().startswith('GM-')
                       for script in collection)
Example #6
0
    def test_greasemonkey_out_of_range_world(self, worldid, scripts_helper, caplog):
        """Make sure scripts with out-of-range worlds are rejected."""
        scripts = [
            greasemonkey.GreasemonkeyScript(
                [('qute-js-world', worldid), ('name', 'test')], None)
        ]

        with caplog.at_level(logging.ERROR, 'greasemonkey'):
            injected = scripts_helper.inject(scripts)

        assert len(caplog.records) == 1
        msg = caplog.messages[0]
        assert "has invalid value for '@qute-js-world': " in msg
        assert "should be between 0 and" in msg

        assert not injected
Example #7
0
    def test_greasemonkey_document_end_workaround(self, monkeypatch, scripts_helper):
        """Make sure document-end is forced when needed."""
        monkeypatch.setattr(greasemonkey.objects, 'backend',
                            usertypes.Backend.QtWebEngine)

        scripts = [
            greasemonkey.GreasemonkeyScript([
                ('name', 'Iridium'),
                ('namespace', 'https://github.com/ParticleCore'),
                ('run-at', 'document-start'),
            ], None)
        ]
        scripts_helper.inject(scripts)

        script = scripts_helper.get_script()
        assert script.injectionPoint() == QWebEngineScript.DocumentReady
Example #8
0
    def test_greasemonkey_out_of_range_world(self, worldid, webengine_scripts,
                                             caplog):
        """Make sure scripts with out-of-range worlds are rejected."""
        scripts = [
            greasemonkey.GreasemonkeyScript([('qute-js-world', worldid),
                                             ('name', 'test')], None)
        ]

        with caplog.at_level(logging.ERROR, 'greasemonkey'):
            webengine_scripts._inject_greasemonkey_scripts(scripts)

        assert len(caplog.records) == 1
        msg = caplog.records[0].message
        assert "has invalid value for '@qute-js-world': " in msg
        assert "should be between 0 and" in msg
        collection = webengine_scripts._widget.page().scripts().toList()
        assert not any(script.name().startswith('GM-')
                       for script in collection)
Example #9
0
    def test_greasemonkey_document_end_workaround(self, monkeypatch,
                                                  webengine_scripts):
        """Make sure document-end is forced when needed."""
        monkeypatch.setattr(greasemonkey.objects, 'backend',
                            usertypes.Backend.QtWebEngine)

        scripts = [
            greasemonkey.GreasemonkeyScript([
                ('name', 'Iridium'),
                ('namespace', 'https://github.com/ParticleCore'),
                ('run-at', 'document-start'),
            ], None)
        ]

        webengine_scripts._inject_greasemonkey_scripts(scripts)

        collection = webengine_scripts._widget.page().scripts()
        script = collection.toList()[-1]
        assert script.injectionPoint() == QWebEngineScript.DocumentReady
Example #10
0
def test_no_name():
    """Ensure that GreaseMonkeyScripts must have a name."""
    msg = "@name key required or pass filename to init."
    with pytest.raises(ValueError, match=msg):
        greasemonkey.GreasemonkeyScript([("something", "else")], "")
def test_full_name(properties, inc_counter, expected):
    script = greasemonkey.GreasemonkeyScript(properties, code="")
    if inc_counter:
        script.dedup_suffix += 1
    assert script.full_name() == expected
Example #12
0
            "Script run-at-tester has invalid run-at defined, defaulting to "
            "document-end")
        assert caplog.messages == [msg]
    else:
        gm_manager.add_script(script)

    assert gm_manager._run_start == ([script] if start else [])
    assert gm_manager._run_end == ([script] if end else [])
    assert gm_manager._run_idle == ([script] if idle else [])


@pytest.mark.parametrize("scripts, expected", [
    ([], "No Greasemonkey scripts loaded"),
    (
        [
            greasemonkey.GreasemonkeyScript(
                properties={}, code="", filename="test")
        ],
        "Loaded Greasemonkey scripts:\n\ntest",
    ),
    (
        [
            greasemonkey.GreasemonkeyScript(
                properties={}, code="", filename="test1"),
            greasemonkey.GreasemonkeyScript(
                properties={}, code="", filename="test2"),
        ],
        "Loaded Greasemonkey scripts:\n\ntest1\ntest2",
    ),
])
def test_load_results_successful(scripts, expected):
    results = greasemonkey.LoadResults()