1 /*
2  * Copyright (C) 2020 Damir Porobic <damir.porobic@gmx.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include "AnnotationTabContextMenuTest.h"
21 
TestCustomActionTriggered_Should_CallInnerActionWithCorrectTabIndexSetInData()22 void AnnotationTabContextMenuTest::TestCustomActionTriggered_Should_CallInnerActionWithCorrectTabIndexSetInData()
23 {
24     auto tabIndex = 22;
25     QAction customAction;
26     QSignalSpy spy(&customAction, &QAction::triggered);
27     QWidget parent;
28     AnnotationTabContextMenu menu(&parent);
29     menu.addCustomActions(QList<QAction*>{&customAction});
30     auto outerAction = menu.actions().last();
31     QTimer::singleShot(300, outerAction, &QAction::trigger);
32     QTimer::singleShot(400, &menu, &QMenu::close);
33 
34     menu.show(tabIndex, {});
35 
36     QCOMPARE(customAction.data().toInt(), tabIndex);
37     QCOMPARE(spy.count(), 1);
38 }
39 
TestAddCustomActions_Should_SetTextIconAndToolTipInOuterAction()40 void AnnotationTabContextMenuTest::TestAddCustomActions_Should_SetTextIconAndToolTipInOuterAction()
41 {
42     QAction customAction;
43     customAction.setText(QLatin1Literal("LaLa"));
44     customAction.setToolTip(QLatin1Literal("Hello Hello"));
45     customAction.setIcon(QIcon());
46     QWidget parent;
47     AnnotationTabContextMenu menu(&parent);
48     menu.addCustomActions(QList<QAction*>{&customAction});
49     auto outerAction = menu.actions().last();
50 
51     QCOMPARE(customAction.text(), outerAction->text());
52     QCOMPARE(customAction.toolTip(), outerAction->toolTip());
53     QCOMPARE(customAction.icon(), outerAction->icon());
54 }
55 
TestCustomActionChanged_Should_UpdateEnabledStateOfOuterAction()56 void AnnotationTabContextMenuTest::TestCustomActionChanged_Should_UpdateEnabledStateOfOuterAction()
57 {
58     QAction customAction;
59     customAction.setEnabled(false);
60     QWidget parent;
61     AnnotationTabContextMenu menu(&parent);
62     menu.addCustomActions(QList<QAction*>{&customAction});
63     auto outerAction = menu.actions().last();
64     QCOMPARE(customAction.isEnabled(), outerAction->isEnabled());
65 
66     customAction.setEnabled(true);
67 
68     QCOMPARE(customAction.isEnabled(), outerAction->isEnabled());
69 }
70 
71 QTEST_MAIN(AnnotationTabContextMenuTest);