Example #1
0
    def test_multiple_items(self, hist_tester):
        items = [
            Item(url=QUrl('http://www.qutebrowser.org/'), title='test 1'),
            Item(url=QUrl('http://www.example.com/'),
                 title='test 2',
                 active=True),
            Item(url=QUrl('http://www.example.org/'), title='test 3'),
        ]

        expected = [
            {
                'url': 'http://www.qutebrowser.org/',
                'title': 'test 1',
            },
            {
                'url': 'http://www.example.com/',
                'title': 'test 2',
                'active': True,
                'scroll-pos': {
                    'x': 0,
                    'y': 0
                },
                'zoom': 1.0
            },
            {
                'url': 'http://www.example.org/',
                'title': 'test 3',
            },
        ]

        hist = hist_tester.get(items)
        assert hist == expected
Example #2
0
def test_two_active_items():
    """Check tabhistory.serialize with two active items."""
    items = [Item(QUrl(), '', active=True),
             Item(QUrl(), ''),
             Item(QUrl(), '', active=True)]
    with pytest.raises(ValueError):
        tabhistory.serialize(items)
Example #3
0
 def test_zoom(self, hist_tester, factor):
     """Test zoom."""
     items = [
         Item(url=QUrl('http://www.example.com/'),
              title='Test title',
              active=True),
         Item(url=QUrl('http://www.example.com/'),
              title='Test title',
              user_data={'zoom': factor}),
     ]
     hist_tester.webview.setZoomFactor(factor)
     hist = hist_tester.get(items)
     assert hist[0]['zoom'] == factor
     assert hist[1]['zoom'] == factor
Example #4
0
    def test_scroll_current(self, hist_tester, pos_x, pos_y):
        """Test scroll position on the current URL."""
        items = [
            Item(url=QUrl('http://www.example.com/'),
                 title='Test title',
                 active=True),
            Item(url=QUrl('http://www.example.com/'),
                 title='Test title',
                 user_data={'scroll-pos': QPoint(pos_x, pos_y)}),
        ]
        frame = hist_tester.webview.page().mainFrame()

        text = '{}\n'.format('x' * 100) * 100
        frame.setHtml('<html><body>{}</body></html>'.format(text))
        frame.setScrollPosition(QPoint(pos_x, pos_y))

        hist = hist_tester.get(items)
        assert hist[0]['scroll-pos'] == {'x': pos_x, 'y': pos_y}
        assert hist[1]['scroll-pos'] == {'x': pos_x, 'y': pos_y}
Example #5
0
    def test_utf_8_valid(self, tmpdir, sess_man, fake_history):
        """Make sure data containing valid UTF8 gets saved correctly."""
        session_path = tmpdir / 'foo.yml'
        fake_history([
            Item(QUrl('http://www.qutebrowser.org/'), 'foo☃bar', active=True)
        ])

        sess_man.save(str(session_path))

        data = session_path.read_text('utf-8')
        assert 'title: foo☃bar' in data
Example #6
0
    def test_original_url(self, hist_tester):
        """Test with an original-url which differs from the URL."""
        item = Item(url=QUrl('http://www.example.com/'),
                    original_url=QUrl('http://www.example.org/'),
                    title='Test title',
                    active=True)

        hist = hist_tester.get_single(item)

        assert hist['url'] == 'http://www.example.com/'
        assert hist['original-url'] == 'http://www.example.org/'
Example #7
0
    def test_utf_8_invalid(self, tmpdir, sess_man, fake_history):
        """Make sure data containing invalid UTF8 raises SessionError."""
        session_path = tmpdir / 'foo.yml'
        fake_history(
            [Item(QUrl('http://www.qutebrowser.org/'), '\ud800', active=True)])

        try:
            sess_man.save(str(session_path))
        except sessions.SessionError:
            # This seems to happen on some systems only?!
            pass
        else:
            data = session_path.read_text('utf-8')
            assert r'title: "\uD800"' in data
Example #8
0
    def test_single_item(self, hist_tester):
        item = Item(url=QUrl('http://www.qutebrowser.org/'),
                    title='Test title',
                    active=True)
        expected = {
            'url': 'http://www.qutebrowser.org/',
            'title': 'Test title',
            'active': True,
            'scroll-pos': {
                'x': 0,
                'y': 0
            },
            'zoom': 1.0
        }

        hist = hist_tester.get_single(item)
        assert hist == expected
Example #9
0
    def test_long_output(self, fake_windows, tmpdir, sess_man):
        session_path = tmpdir / 'foo.yml'

        items1 = [
            Item(QUrl('http://www.qutebrowser.org/'), 'test title 1'),
            Item(QUrl('http://www.example.org/'),
                 'test title 2',
                 original_url=QUrl('http://www.example.com/'),
                 active=True),
        ]
        items2 = [
            Item(QUrl('http://www.example.com/?q=foo+bar'), 'test title 3'),
            Item(QUrl('http://www.example.com/?q=test%20foo'),
                 'test title 4',
                 active=True),
        ]
        items3 = []
        items4 = [
            Item(QUrl('http://www.github.com/The-Compiler/qutebrowser'),
                 'test title 5',
                 active=True),
        ]

        self._set_data(fake_windows[0], 0, items1)
        self._set_data(fake_windows[0], 1, items2)
        self._set_data(fake_windows[1], 0, items3)
        self._set_data(fake_windows[1], 1, items4)

        expected = """
            windows:
            - active: true
              geometry: !!binary |
                ZmFrZS1nZW9tZXRyeS0w
              tabs:
              - history:
                - title: test title 1
                  url: http://www.qutebrowser.org/
                - active: true
                  original-url: http://www.example.com/
                  scroll-pos:
                    x: 0
                    y: 0
                  title: test title 2
                  url: http://www.example.org/
                  zoom: 1.0
              - active: true
                history:
                - title: test title 3
                  url: http://www.example.com/?q=foo+bar
                - active: true
                  scroll-pos:
                    x: 0
                    y: 0
                  title: test title 4
                  url: http://www.example.com/?q=test%20foo
                  zoom: 1.0
            - geometry: !!binary |
                ZmFrZS1nZW9tZXRyeS0x
              tabs:
              - history: []
              - active: true
                history:
                - active: true
                  scroll-pos:
                    x: 0
                    y: 0
                  title: test title 5
                  url: http://www.github.com/The-Compiler/qutebrowser
                  zoom: 1.0
        """

        sess_man.save(str(session_path))
        data = session_path.read_text('utf-8')
        assert data == textwrap.dedent(expected.strip('\n'))
Example #10
0
def test_no_active_item():
    """Check tabhistory.serialize with no active item."""
    items = [Item(QUrl(), '')]
    with pytest.raises(ValueError):
        tabhistory.serialize(items)
Example #11
0
"""Tests for webelement.tabhistory."""

import collections

from PyQt5.QtCore import QUrl, QPoint
import pytest

from qutebrowser.browser import tabhistory
from qutebrowser.browser.tabhistory import TabHistoryItem as Item
from qutebrowser.utils import qtutils

pytestmark = pytest.mark.qt_log_ignore('QIODevice::read.*: device not open',
                                       extend=True)

ITEMS = [
    Item(QUrl('https://www.heise.de/'), 'heise'),
    Item(QUrl('http://example.com/%E2%80%A6'), 'percent', active=True),
    Item(QUrl('http://example.com/?foo=bar'),
         'arg',
         original_url=QUrl('http://original.url.example.com/'),
         user_data={
             'foo': 23,
             'bar': 42
         }),
    # From https://github.com/OtterBrowser/otter-browser/issues/709#issuecomment-74749471
    Item(QUrl('http://github.com/OtterBrowser/24/134/2344/otter-browser/'
              'issues/709/'),
         'Page not found | github',
         user_data={
             'zoom': 149,
             'scroll-pos': QPoint(0, 0)
Example #12
0
class TestSerializeHistory:
    """Tests for serialize()."""

    ITEMS = [
        Item(QUrl('https://www.heise.de/'), QUrl('http://www.heise.de/'),
             'heise'),
        Item(QUrl('http://example.com/%E2%80%A6'),
             QUrl('http://example.com/%E2%80%A6'),
             'percent',
             active=True),
        Item(QUrl('http://example.com/?foo=bar'),
             QUrl('http://original.url.example.com/'),
             'arg',
             user_data={
                 'foo': 23,
                 'bar': 42
             }),
        # From https://github.com/OtterBrowser/otter-browser/issues/709#issuecomment-74749471
        Item(QUrl('http://github.com/OtterBrowser/24/134/2344/otter-browser/'
                  'issues/709/'),
             QUrl('http://github.com/OtterBrowser/24/134/2344/otter-browser/'
                  'issues/709/'),
             'Page not found | github',
             user_data={
                 'zoom': 149,
                 'scroll-pos': QPoint(0, 0)
             }),
        Item(QUrl('https://mail.google.com/mail/u/0/#label/some+label/'
                  '234lkjsd0932lkjf884jqwerdf4'),
             QUrl('https://mail.google.com/mail/u/0/#label/some+label/'
                  '234lkjsd0932lkjf884jqwerdf4'),
             '"some label" - [email protected] - Gmail"',
             user_data={
                 'zoom': 120,
                 'scroll-pos': QPoint(0, 0)
             }),
    ]

    @pytest.fixture(autouse=True)
    def setup(self, webpage):
        self.page = webpage
        self.history = self.page.history()
        assert self.history.count() == 0

        stream, _data, self.user_data = tabhistory.serialize(self.ITEMS)
        qtutils.deserialize_stream(stream, self.history)

    def test_count(self):
        """Check if the history's count was loaded correctly."""
        assert self.history.count() == len(self.ITEMS)

    @pytest.mark.parametrize('i', range(len(ITEMS)))
    def test_valid(self, i):
        """Check if all items are valid."""
        assert self.history.itemAt(i).isValid()

    @pytest.mark.parametrize('i', range(len(ITEMS)))
    def test_no_userdata(self, i):
        """Check if all items have no user data."""
        assert self.history.itemAt(i).userData() is None

    def test_userdata(self):
        """Check if all user data has been restored to self.user_data."""
        userdata_items = [item.user_data for item in self.ITEMS]
        assert userdata_items == self.user_data

    def test_currentitem(self):
        """Check if the current item index was loaded correctly."""
        assert self.history.currentItemIndex() == 1

    @pytest.mark.parametrize('i, item', enumerate(ITEMS))
    def test_urls(self, i, item):
        """Check if the URLs were loaded correctly."""
        assert self.history.itemAt(i).url() == item.url

    @pytest.mark.parametrize('i, item', enumerate(ITEMS))
    def test_original_urls(self, i, item):
        """Check if the original URLs were loaded correctly."""
        assert self.history.itemAt(i).originalUrl() == item.original_url

    @pytest.mark.parametrize('i, item', enumerate(ITEMS))
    def test_titles(self, i, item):
        """Check if the titles were loaded correctly."""
        assert self.history.itemAt(i).title() == item.title