import os
import sys

import pytest

from msl.qt import convert, Button, QtWidgets, QtCore, Qt


def test_text():
    b = Button(text='hello')
    assert b.text() == 'hello'
    assert b.icon().isNull()
    assert b.toolButtonStyle() == Qt.ToolButtonTextOnly


def test_icon():
    path = os.path.dirname(__file__) + '/gamma.png'
    gamma_size = QtCore.QSize(191, 291)

    int_val = QtWidgets.QStyle.SP_DriveNetIcon
    icon = convert.to_qicon(int_val)
    sizes = icon.availableSizes()
    if sys.platform == 'win32':
        assert len(sizes) > 1

    b = Button(icon=int_val)
    assert b.text() == ''
    assert not b.icon().isNull()
    assert b.iconSize() == sizes[0]
    assert b.toolButtonStyle() == Qt.ToolButtonIconOnly

    b = Button(icon=path)
    assert b.text() == ''
    assert not b.icon().isNull()
    assert b.iconSize() == gamma_size
    assert b.toolButtonStyle() == Qt.ToolButtonIconOnly

    b = Button(icon=convert.icon_to_base64(convert.to_qicon(path)))
    assert b.text() == ''
    assert not b.icon().isNull()
    assert b.iconSize() == gamma_size
    assert b.toolButtonStyle() == Qt.ToolButtonIconOnly


def test_icon_size():
    int_val = QtWidgets.QStyle.SP_DriveNetIcon
    icon = convert.to_qicon(int_val)
    sizes = icon.availableSizes()
    if sys.platform == 'win32':
        assert len(sizes) > 1

    #
    # specify the size to the get_icon function
    #

    b = Button(icon=convert.to_qicon(int_val))
    assert b.text() == ''
    assert b.toolButtonStyle() == Qt.ToolButtonIconOnly
    assert b.iconSize() == sizes[0]

    b = Button(icon=convert.to_qicon(int_val, size=789))
    assert b.iconSize() == QtCore.QSize(789, 789)

    b = Button(icon=convert.to_qicon(int_val, size=3.0))
    # specifying a scale factor will use the largest available size
    assert b.iconSize() == QtCore.QSize(3*sizes[-1].width(), 3*sizes[-1].height())

    b = Button(icon=convert.to_qicon(int_val, size=QtCore.QSize(50, 50)))
    assert b.iconSize() == QtCore.QSize(50, 50)

    for size in [(256,), (256, 256, 256)]:
        with pytest.raises(ValueError, match='(width, height)'):
            Button(icon=convert.to_qicon(int_val, size=size))

    #
    # use the icon_size kwarg
    #

    b = Button(icon=convert.to_qicon(int_val), icon_size=1234)
    assert b.iconSize() == QtCore.QSize(1234, 1234)

    b = Button(icon=convert.to_qicon(int_val), icon_size=3.0)
    # specifying a scale factor will use the largest available size
    assert b.iconSize() == QtCore.QSize(3*sizes[-1].width(), 3*sizes[-1].height())

    b = Button(icon=convert.to_qicon(int_val), icon_size=(312, 312))
    assert b.iconSize() == QtCore.QSize(312, 312)

    b = Button(icon=convert.to_qicon(int_val), icon_size=QtCore.QSize(500, 500))
    assert b.iconSize() == QtCore.QSize(500, 500)

    for size in [(256,), (256, 256, 256)]:
        with pytest.raises(ValueError, match='(width, height)'):
            Button(icon=convert.to_qicon(int_val), icon_size=size)


def test_text_and_icon():
    b = Button(text='hello', icon=QtWidgets.QStyle.SP_DriveNetIcon)
    assert b.text() == 'hello'
    assert not b.icon().isNull()
    assert b.toolButtonStyle() == Qt.ToolButtonTextUnderIcon

    b = Button(text='world', icon=QtWidgets.QStyle.SP_DriveNetIcon, is_text_under_icon=False)
    assert b.text() == 'world'
    assert not b.icon().isNull()
    assert b.toolButtonStyle() == Qt.ToolButtonTextBesideIcon


def test_tooltip():
    b = Button(tooltip='hello')
    assert b.text() == ''
    assert b.icon().isNull()
    assert b.toolTip() == 'hello'
    assert b.toolButtonStyle() == Qt.ToolButtonIconOnly

[((122, 142), 'msl.qt.Button', 'Button', ([], {'text': '"""hello"""'}), "(text='hello')\n", (128, 142), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((347, 369), 'msl.qt.QtCore.QSize', 'QtCore.QSize', (['(191)', '(291)'], {}), '(191, 291)\n', (359, 369), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((429, 454), 'msl.qt.convert.to_qicon', 'convert.to_qicon', (['int_val'], {}), '(int_val)\n', (445, 454), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((560, 580), 'msl.qt.Button', 'Button', ([], {'icon': 'int_val'}), '(icon=int_val)\n', (566, 580), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((741, 758), 'msl.qt.Button', 'Button', ([], {'icon': 'path'}), '(icon=path)\n', (747, 758), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((1216, 1241), 'msl.qt.convert.to_qicon', 'convert.to_qicon', (['int_val'], {}), '(int_val)\n', (1232, 1241), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((3013, 3072), 'msl.qt.Button', 'Button', ([], {'text': '"""hello"""', 'icon': 'QtWidgets.QStyle.SP_DriveNetIcon'}), "(text='hello', icon=QtWidgets.QStyle.SP_DriveNetIcon)\n", (3019, 3072), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((3207, 3296), 'msl.qt.Button', 'Button', ([], {'text': '"""world"""', 'icon': 'QtWidgets.QStyle.SP_DriveNetIcon', 'is_text_under_icon': '(False)'}), "(text='world', icon=QtWidgets.QStyle.SP_DriveNetIcon,\n    is_text_under_icon=False)\n", (3213, 3296), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((3449, 3472), 'msl.qt.Button', 'Button', ([], {'tooltip': '"""hello"""'}), "(tooltip='hello')\n", (3455, 3472), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((289, 314), 'os.path.dirname', 'os.path.dirname', (['__file__'], {}), '(__file__)\n', (304, 314), False, 'import os\n'), ((1650, 1672), 'msl.qt.QtCore.QSize', 'QtCore.QSize', (['(789)', '(789)'], {}), '(789, 789)\n', (1662, 1672), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((1984, 2004), 'msl.qt.QtCore.QSize', 'QtCore.QSize', (['(50)', '(50)'], {}), '(50, 50)\n', (1996, 2004), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((2310, 2334), 'msl.qt.QtCore.QSize', 'QtCore.QSize', (['(1234)', '(1234)'], {}), '(1234, 1234)\n', (2322, 2334), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((2646, 2668), 'msl.qt.QtCore.QSize', 'QtCore.QSize', (['(312)', '(312)'], {}), '(312, 312)\n', (2658, 2668), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((2778, 2800), 'msl.qt.QtCore.QSize', 'QtCore.QSize', (['(500)', '(500)'], {}), '(500, 500)\n', (2790, 2800), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((1420, 1445), 'msl.qt.convert.to_qicon', 'convert.to_qicon', (['int_val'], {}), '(int_val)\n', (1436, 1445), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((1586, 1621), 'msl.qt.convert.to_qicon', 'convert.to_qicon', (['int_val'], {'size': '(789)'}), '(int_val, size=789)\n', (1602, 1621), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((1694, 1729), 'msl.qt.convert.to_qicon', 'convert.to_qicon', (['int_val'], {'size': '(3.0)'}), '(int_val, size=3.0)\n', (1710, 1729), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((2062, 2112), 'pytest.raises', 'pytest.raises', (['ValueError'], {'match': '"""(width, height)"""'}), "(ValueError, match='(width, height)')\n", (2075, 2112), False, 'import pytest\n'), ((2240, 2265), 'msl.qt.convert.to_qicon', 'convert.to_qicon', (['int_val'], {}), '(int_val)\n', (2256, 2265), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((2356, 2381), 'msl.qt.convert.to_qicon', 'convert.to_qicon', (['int_val'], {}), '(int_val)\n', (2372, 2381), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((2570, 2595), 'msl.qt.convert.to_qicon', 'convert.to_qicon', (['int_val'], {}), '(int_val)\n', (2586, 2595), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((2690, 2715), 'msl.qt.convert.to_qicon', 'convert.to_qicon', (['int_val'], {}), '(int_val)\n', (2706, 2715), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((2727, 2749), 'msl.qt.QtCore.QSize', 'QtCore.QSize', (['(500)', '(500)'], {}), '(500, 500)\n', (2739, 2749), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((2858, 2908), 'pytest.raises', 'pytest.raises', (['ValueError'], {'match': '"""(width, height)"""'}), "(ValueError, match='(width, height)')\n", (2871, 2908), False, 'import pytest\n'), ((956, 978), 'msl.qt.convert.to_qicon', 'convert.to_qicon', (['path'], {}), '(path)\n', (972, 978), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((1934, 1954), 'msl.qt.QtCore.QSize', 'QtCore.QSize', (['(50)', '(50)'], {}), '(50, 50)\n', (1946, 1954), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((2138, 2174), 'msl.qt.convert.to_qicon', 'convert.to_qicon', (['int_val'], {'size': 'size'}), '(int_val, size=size)\n', (2154, 2174), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n'), ((2934, 2959), 'msl.qt.convert.to_qicon', 'convert.to_qicon', (['int_val'], {}), '(int_val)\n', (2950, 2959), False, 'from msl.qt import convert, Button, QtWidgets, QtCore, Qt\n')]