#  -*- 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 channels popup.
"""

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

# Local imports
from anaconda_navigator.widgets.dialogs.channels import DialogChannels


@pytest.fixture()
def botchannels(qtbot):
    widget = DialogChannels(
        None,
        channels=['https://conda.anaconda.org/malev', 'goanpeca'],
        active_channels=['https://conda.anaconda.org/malev'])
    widget.show()
    qtbot.addWidget(widget)
    return qtbot, widget


class TestChannelsDialog:

    def test_simple(self, botchannels):
        qtbot, widget = botchannels
        first_checkbox = widget.list.item(0).checkbox_channel.isChecked()
        second_checkbox = widget.list.item(1).checkbox_channel.isChecked()
        assert widget.list.count() == 2
        assert first_checkbox is True
        assert second_checkbox is False

    def test_add_defaults(self, botchannels):
        qtbot, widget = botchannels
        from qtpy.QtWidgets import QApplication
        app = QApplication.instance()
        qtbot.mouseClick(widget.button_add, Qt.LeftButton)
        qtbot.keyClicks(app.focusWidget(), 'defaults')
        qtbot.keyClick(app.focusWidget(), Qt.Key_Enter)
        app.processEvents()
        count = widget.list.count()
        item = widget.list.item(count - 1)
        qtbot.mouseClick(widget.button_ok, Qt.LeftButton)
        assert item.text_channel.text() == 'defaults'
        assert item.checkbox_channel.isChecked() is True
