hydrus/hydrus/test/TestClientListBoxes.py

206 lines
7.5 KiB
Python
Raw Normal View History

2017-03-22 22:38:15 +00:00
import random
import unittest
2020-07-15 20:52:09 +00:00
2019-11-14 03:56:30 +00:00
from qtpy import QtCore as QC
from qtpy import QtWidgets as QW
from qtpy import QtGui as QG
2020-07-15 20:52:09 +00:00
2020-07-29 20:52:44 +00:00
from hydrus.core import HydrusGlobals as HG
2020-04-22 21:00:35 +00:00
from hydrus.client.gui import QtPorting as QP
2020-07-15 20:52:09 +00:00
from hydrus.client.gui.lists import ClientGUIListBoxes
2020-07-29 20:52:44 +00:00
2020-07-15 20:52:09 +00:00
from hydrus.test import TestController
2017-03-22 22:38:15 +00:00
2018-02-28 22:30:36 +00:00
def DoClick( click, panel, do_delayed_ok_afterwards = False ):
2017-03-22 22:38:15 +00:00
QW.QApplication.instance().postEvent( panel.widget(), click )
2017-03-22 22:38:15 +00:00
2018-02-28 22:30:36 +00:00
if do_delayed_ok_afterwards:
2021-06-23 21:11:38 +00:00
HG.test_controller.CallLaterQtSafe( panel, 1, 'test click', PressKeyOnFocusedWindow, QC.Qt.Key_Return )
2018-02-28 22:30:36 +00:00
2019-11-14 03:56:30 +00:00
QW.QApplication.processEvents()
2017-03-22 22:38:15 +00:00
2019-11-14 03:56:30 +00:00
def GenerateClick( window, pos, click_type, click_button, modifier ):
2017-03-22 22:38:15 +00:00
2019-11-14 03:56:30 +00:00
screen_pos = QC.QPointF( window.mapToGlobal( pos.toPoint() ) )
click = QG.QMouseEvent( click_type, pos, screen_pos, click_button, click_button, modifier )
2017-03-22 22:38:15 +00:00
2019-11-14 03:56:30 +00:00
return click
def GetAllClickableIndices( panel ):
2017-03-22 22:38:15 +00:00
current_y = 5
2019-11-14 03:56:30 +00:00
click = GenerateClick( panel, QC.QPointF( 10, current_y ), QC.QEvent.MouseButtonPress, QC.Qt.LeftButton, QC.Qt.NoModifier )
2017-03-22 22:38:15 +00:00
all_clickable_indices = {}
2021-02-11 01:59:52 +00:00
while panel._GetLogicalIndexUnderMouse( click ) is not None:
2017-03-22 22:38:15 +00:00
2021-02-11 01:59:52 +00:00
index = panel._GetLogicalIndexUnderMouse( click )
2017-03-22 22:38:15 +00:00
if index not in all_clickable_indices:
all_clickable_indices[ index ] = current_y
current_y += 5
2019-11-14 03:56:30 +00:00
click = GenerateClick( panel, QC.QPointF( 10, current_y ), QC.QEvent.MouseButtonPress, QC.Qt.LeftButton, QC.Qt.NoModifier )
2017-03-22 22:38:15 +00:00
return all_clickable_indices
2018-02-28 22:30:36 +00:00
def PressKey( window, key ):
2019-11-14 03:56:30 +00:00
window.setFocus( QC.Qt.OtherFocusReason )
2018-02-28 22:30:36 +00:00
2019-11-14 03:56:30 +00:00
uias = QP.UIActionSimulator()
2018-02-28 22:30:36 +00:00
2020-04-29 21:44:12 +00:00
uias.Char( window, key )
2018-02-28 22:30:36 +00:00
def PressKeyOnFocusedWindow( key ):
2020-04-29 21:44:12 +00:00
window = QW.QApplication.focusWidget()
2019-11-14 03:56:30 +00:00
uias = QP.UIActionSimulator()
2018-02-28 22:30:36 +00:00
2020-04-29 21:44:12 +00:00
uias.Char( window, key )
2018-02-28 22:30:36 +00:00
2017-03-22 22:38:15 +00:00
class TestListBoxes( unittest.TestCase ):
def test_listbox_colour_options( self ):
2019-11-14 03:56:30 +00:00
def qt_code():
2017-03-22 22:38:15 +00:00
2019-03-13 21:04:21 +00:00
frame = TestController.TestFrame()
2017-03-22 22:38:15 +00:00
2019-03-13 21:04:21 +00:00
try:
2017-03-22 22:38:15 +00:00
2019-03-13 21:04:21 +00:00
initial_namespace_colours = { 'series' : ( 153, 101, 21 ), '' : ( 0, 111, 250 ), None : ( 114, 160, 193 ), 'creator' : ( 170, 0, 0 ) }
2018-02-21 21:59:37 +00:00
2019-03-13 21:04:21 +00:00
panel = ClientGUIListBoxes.ListBoxTagsColourOptions( frame, initial_namespace_colours )
2017-03-22 22:38:15 +00:00
2019-03-13 21:04:21 +00:00
frame.SetPanel( panel )
2017-03-22 22:38:15 +00:00
2019-03-13 21:04:21 +00:00
self.assertEqual( panel.GetNamespaceColours(), initial_namespace_colours )
2017-03-22 22:38:15 +00:00
2019-03-13 21:04:21 +00:00
#
2017-03-22 22:38:15 +00:00
2019-03-13 21:04:21 +00:00
new_namespace_colours = dict( initial_namespace_colours )
new_namespace_colours[ 'character' ] = ( 0, 170, 0 )
2017-03-22 22:38:15 +00:00
2019-11-14 03:56:30 +00:00
colour = QG.QColor( 0, 170, 0 )
2017-03-22 22:38:15 +00:00
2019-03-13 21:04:21 +00:00
panel.SetNamespaceColour( 'character', colour )
self.assertEqual( panel.GetNamespaceColours(), new_namespace_colours )
#
2021-02-11 01:59:52 +00:00
terms = set( panel._terms_to_logical_indices.keys() )
2019-03-13 21:04:21 +00:00
ordered_terms = list( panel._ordered_terms )
self.assertEqual( len( terms ), len( ordered_terms ) )
2017-03-22 22:38:15 +00:00
2019-03-13 21:04:21 +00:00
#
2017-03-22 22:38:15 +00:00
2019-03-13 21:04:21 +00:00
all_clickable_indices = GetAllClickableIndices( panel )
self.assertEqual( len( list(all_clickable_indices.keys()) ), len( terms ) )
self.assertEqual( set( all_clickable_indices.keys() ), set( range( len( list(all_clickable_indices.keys()) ) ) ) )
#
2019-11-14 03:56:30 +00:00
for ( index, y ) in list( all_clickable_indices.items() ):
2017-03-22 22:38:15 +00:00
2019-11-14 03:56:30 +00:00
click = GenerateClick( panel, QC.QPointF( 10, y ), QC.QEvent.MouseButtonPress, QC.Qt.LeftButton, QC.Qt.NoModifier )
2017-03-22 22:38:15 +00:00
DoClick( click, panel )
2021-02-11 01:59:52 +00:00
self.assertEqual( panel.GetSelectedNamespaceColours(), dict( [ ordered_terms[ index ].GetNamespaceAndColour() ] ) )
2019-03-13 21:04:21 +00:00
2017-03-22 22:38:15 +00:00
2019-03-13 21:04:21 +00:00
#
2017-03-22 22:38:15 +00:00
2019-03-13 21:04:21 +00:00
current_y = 5
2017-03-22 22:38:15 +00:00
2019-11-14 03:56:30 +00:00
click = QG.QMouseEvent( QC.QEvent.MouseButtonPress, QC.QPointF( 10, current_y ), QC.Qt.LeftButton, QC.Qt.LeftButton, QC.Qt.NoModifier )
2019-03-13 21:04:21 +00:00
2021-02-11 01:59:52 +00:00
while panel._GetLogicalIndexUnderMouse( click ) is not None:
2019-03-13 21:04:21 +00:00
current_y += 5
2019-11-14 03:56:30 +00:00
click = GenerateClick( panel, QC.QPointF( 10, current_y ), QC.QEvent.MouseButtonPress, QC.Qt.LeftButton, QC.Qt.NoModifier )
2019-03-13 21:04:21 +00:00
DoClick( click, panel )
self.assertEqual( panel.GetSelectedNamespaceColours(), {} )
#
if len( list(all_clickable_indices.keys()) ) > 2:
2023-03-08 21:52:17 +00:00
indices = random.sample( list( all_clickable_indices.keys() ), len( list( all_clickable_indices.keys() ) ) - 1 )
2019-03-13 21:04:21 +00:00
for index in indices:
2019-11-14 03:56:30 +00:00
click = GenerateClick( panel, QC.QPointF( 10, all_clickable_indices[ index ] ), QC.QEvent.MouseButtonPress, QC.Qt.LeftButton, QC.Qt.ControlModifier )
2019-03-13 21:04:21 +00:00
DoClick( click, panel )
2021-02-11 01:59:52 +00:00
expected_selected_namespaces_and_colours = [ ordered_terms[ index ].GetNamespaceAndColour() for index in indices ]
2019-03-13 21:04:21 +00:00
2021-02-11 01:59:52 +00:00
self.assertEqual( panel.GetSelectedNamespaceColours(), dict( expected_selected_namespaces_and_colours ) )
2019-03-13 21:04:21 +00:00
#
2017-03-29 19:39:34 +00:00
2019-01-09 22:59:03 +00:00
random_index = random.choice( list(all_clickable_indices.keys()) )
2017-03-29 19:39:34 +00:00
2021-02-11 01:59:52 +00:00
while ordered_terms[ random_index ].GetNamespaceAndColour()[0] in panel.PROTECTED_TERMS:
2019-03-13 21:04:21 +00:00
random_index = random.choice( list(all_clickable_indices.keys()) )
2021-02-11 01:59:52 +00:00
del new_namespace_colours[ ordered_terms[ random_index ].GetNamespaceAndColour()[0] ]
2017-03-22 22:38:15 +00:00
2019-03-13 21:04:21 +00:00
# select nothing
current_y = 5
2019-11-14 03:56:30 +00:00
click = GenerateClick( panel, QC.QPointF( 10, current_y ), QC.QEvent.MouseButtonPress, QC.Qt.LeftButton, QC.Qt.NoModifier )
2017-03-22 22:38:15 +00:00
2021-02-11 01:59:52 +00:00
while panel._GetLogicalIndexUnderMouse( click ) is not None:
2019-03-13 21:04:21 +00:00
current_y += 5
2019-11-14 03:56:30 +00:00
click = GenerateClick( panel, QC.QPointF( 10, current_y ), QC.QEvent.MouseButtonPress, QC.Qt.LeftButton, QC.Qt.NoModifier )
2019-03-13 21:04:21 +00:00
DoClick( click, panel )
# select the random index
2019-11-14 03:56:30 +00:00
click = GenerateClick( panel, QC.QPointF( 10, all_clickable_indices[ random_index ] ), QC.QEvent.MouseButtonPress, QC.Qt.LeftButton, QC.Qt.NoModifier )
2019-03-13 21:04:21 +00:00
DoClick( click, panel )
finally:
2019-11-14 03:56:30 +00:00
frame.deleteLater()
2019-03-13 21:04:21 +00:00
2017-03-22 22:38:15 +00:00
2019-03-13 21:04:21 +00:00
2019-11-14 03:56:30 +00:00
HG.test_controller.CallBlockingToQt( HG.test_controller.win, qt_code )
2017-03-22 22:38:15 +00:00