#  -*- coding: utf-8 -*-
#
# Copyright 2016 Continuum Analytics, Inc.
# May be copied and distributed freely only as part of an Anaconda or
# Miniconda installation.
#

"""
Tests for logger window.
"""

# Third party imports
from qtpy.QtCore import Qt  # analysis:ignore
import pytest
import pytestqt.qtbot as qtbot  # analysis:ignore

# Local imports
from anaconda_navigator.utils.logs import logger
from anaconda_navigator.widgets.dialogs.logger import LogViewerDialog


@pytest.fixture()
def loggerwidget(qtbot):
    logger.info('TEST')
    widget = LogViewerDialog()
    widget.show()
    qtbot.addWidget(widget)
    return qtbot, widget


class TestLogViewer:

    def test_simple(self, loggerwidget):
        qtbot, widget = loggerwidget
        assert widget.list_logs.count() > 0
        assert 'TEST' in widget.list_logs.currentItem().text()

    def test_search(self, loggerwidget):
        qtbot, widget = loggerwidget
        qtbot.keyClicks(widget.text_search, 'TEST')
        item = widget.list_logs.currentItem()
        assert not item.isHidden()
        qtbot.keyClicks(widget.text_search, 'blah!')
        assert item.isHidden()

    def test_copy(self, loggerwidget):
        from qtpy.QtWidgets import QApplication
        qtbot, widget = loggerwidget
        qtbot.mouseClick(widget.button_copy, Qt.LeftButton)
        app = QApplication.instance()
        clipped = app.clipboard().text()
        assert 'TEST' in clipped
