1+ import pytest
2+ from matplotlib .figure import Figure
3+
4+ from omc3 .plotting .utils .windows import PlotWidget , TabWidget , SimpleTabWindow , VerticalTabWindow
5+
6+
7+ @pytest .mark .basic
8+ def test_plot_widget (monkeypatch ):
9+ # Preparation ---
10+ monkeypatch .setattr ("omc3.plotting.utils.windows.QVBoxLayout" , MockLayout )
11+ monkeypatch .setattr ("omc3.plotting.utils.windows.FigureCanvas" , MockFigureCanvas )
12+ monkeypatch .setattr ("omc3.plotting.utils.windows.NavigationToolbar" , MockNavigationToolbar )
13+
14+ class MockPlotWidget (PlotWidget , MockQWidget ):
15+ pass
16+
17+ figures = (Figure (), Figure (), Figure ())
18+ my_title = "Hello OMC!"
19+
20+ # Execution ---
21+ widget = MockPlotWidget (* figures , title = my_title )
22+
23+ # Assert ---
24+ assert widget .title == my_title
25+ assert isinstance (widget ._layout , MockLayout )
26+ assert len (widget ._layout .widgets ) == len (figures ) * 2
27+ for idx , w in enumerate (widget ._layout .widgets ):
28+ if idx % 2 :
29+ assert isinstance (w , MockNavigationToolbar )
30+ else :
31+ assert isinstance (w , MockFigureCanvas )
32+ assert w .figure == figures [idx // 2 ]
33+
34+
35+ @pytest .mark .basic
36+ def test_tab_widget ():
37+ # Preparation ---
38+ class MockTabWidget (TabWidget , MockQTabWidget ):
39+ pass
40+
41+ tabs = [MockQWidget ("tab1" ), MockQWidget ("tab2" ), MockQWidget ("tab3" )]
42+ my_title = "Hello OMC!"
43+
44+ # Execution ---
45+ widget = MockTabWidget (title = my_title )
46+ for tab in tabs :
47+ widget .add_tab (tab )
48+
49+ # Assert ---
50+ assert widget .title == my_title
51+ assert len (widget .tabs ) == len (tabs )
52+ assert len (widget .added_tabs ) == len (tabs )
53+ for idx , (title , tab ) in enumerate (widget .added_tabs .items ()):
54+ assert tabs [idx ] == widget .tabs [title ]
55+ assert tabs [idx ] == tab
56+
57+
58+ @pytest .mark .basic
59+ @pytest .mark .parametrize ('WindowClass' , (SimpleTabWindow , VerticalTabWindow ))
60+ def test_tab_window (monkeypatch , WindowClass ):
61+ # Preparation ---
62+ monkeypatch .setattr ("omc3.plotting.utils.windows.QApplication" , MockQApplication )
63+ monkeypatch .setattr ("omc3.plotting.utils.windows.QMainWindow" , MockQMainWindow )
64+ monkeypatch .setattr ("omc3.plotting.utils.windows.QTabWidget" , MockQTabWidget )
65+
66+ class MockTabWidget (TabWidget , MockQTabWidget ):
67+ pass
68+
69+ monkeypatch .setattr ("omc3.plotting.utils.windows.TabWidget" , MockTabWidget )
70+
71+
72+ tabs = [MockQWidget ("tab1" ), MockQWidget ("tab2" ), MockQWidget ("tab3" )]
73+ my_title = "Hello OMC!"
74+ my_size = (800 , 600 )
75+
76+ # Execution ---
77+ window = WindowClass (title = my_title , size = my_size )
78+ for tab in tabs :
79+ window .add_tab (tab )
80+
81+ # Assert Main Window ---
82+ assert window .main_window .title == my_title
83+ assert window .main_window .size == my_size
84+ assert window .main_window .shown
85+
86+ # Assert Tab Widget ---
87+ assert isinstance (window .tabs_widget , TabWidget )
88+ assert len (window .tabs_widget .added_tabs ) == len (tabs )
89+ for tab , tab_added in zip (tabs , window .tabs_widget .added_tabs .values ()):
90+ assert tab == tab_added
91+ assert window .tabs_widget .position == (MockQTabWidget .West if (WindowClass == VerticalTabWindow ) else 0 )
92+
93+ # Assert App ---
94+ assert not window .app .executed
95+ window .show ()
96+ assert window .app .executed
97+
98+
99+ # Mock Classes -----------------------------------------------------------------
100+
101+ class MockQWidget :
102+
103+ def __init__ (self , title = None ):
104+ self .layout = None
105+ self .title = title
106+
107+ def setLayout (self , layout ):
108+ self .layout = layout
109+
110+
111+ class MockQTabWidget :
112+ West = 1
113+ Center = 2
114+ East = 3
115+
116+ def __init__ (self ):
117+ self .added_tabs = {}
118+ self .position = 0
119+
120+ def addTab (self , tab , tab_title ):
121+ self .added_tabs [tab_title ] = tab
122+
123+ def setTabPosition (self , position ):
124+ self .position = position
125+
126+
127+ class MockLayout :
128+
129+ def __init__ (self ) -> None :
130+ self .widgets = []
131+
132+
133+ def addWidget (self , widget ):
134+ self .widgets .append (widget )
135+
136+
137+ class MockFigureCanvas :
138+
139+ def __init__ (self , figure ):
140+ self .figure = figure
141+
142+
143+ class MockNavigationToolbar ():
144+
145+ def __init__ (self , canvas , parent = None ):
146+ self .canvas = canvas
147+ self .parent = parent
148+
149+
150+ class MockQApplication :
151+
152+ def __init__ (self , * args , ** kwargs ):
153+ self .executed = False
154+
155+ def exec (self ):
156+ self .executed = True
157+
158+
159+ class MockQMainWindow :
160+
161+ def __init__ (self ):
162+ self .title = None
163+ self .central_widget = None
164+ self .size = None
165+ self .shown = False
166+
167+ def setWindowTitle (self , title ):
168+ self .title = title
169+
170+ def setCentralWidget (self , widget ):
171+ self .central_widget = widget
172+
173+ def resize (self , width , height ):
174+ self .size = (width , height )
175+
176+ def show (self ):
177+ self .shown = True
0 commit comments