/ test / tst_qsettings.cpp
tst_qsettings.cpp
  1  #include <gui/mainwindow.h>
  2  #include <msgpackrequest.h>
  3  #include <QtTest/QtTest>
  4  
  5  #include "common.h"
  6  #include "common_gui.h"
  7  #include "mock_qsettings.h"
  8  #include "tst_shell.h"
  9  
 10  namespace NeovimQt {
 11  
 12  class TestQSettings : public QObject
 13  {
 14  	Q_OBJECT
 15  
 16  private slots:
 17  	void initTestCase() noexcept;
 18  	void cleanup() noexcept;
 19  
 20  	void OptionLineGrid() noexcept;
 21  	void OptionPopupMenu() noexcept;
 22  	void OptionTabline() noexcept;
 23  	void GuiFont() noexcept;
 24  	void GuiScrollBar() noexcept;
 25  	void GuiTreeView() noexcept;
 26  };
 27  
 28  static void SendNeovimCommand(NeovimConnector* connector, const QString& command) noexcept
 29  {
 30  	QSignalSpy spyCommand{ connector->api0()->vim_command_output(connector->encode(command)),
 31  		&MsgpackRequest::finished };
 32  
 33  	QVERIFY(spyCommand.isValid());
 34  	QVERIFY(SPYWAIT(spyCommand));
 35  
 36  
 37  	// Hypothesis: sometimes we do not wait long enough for the effects of
 38  	// a command to manifest because it requires
 39  	// 1. msg from gui to nvim
 40  	// 2. msg from nvim to gui
 41  	// The later are usually asynchronous notifications
 42  	//
 43  	// Attempt to ensure the previous command had the inteded effect
 44  	QTest::qSleep(1000);
 45  }
 46  
 47  void TestQSettings::initTestCase() noexcept
 48  {
 49  	NeovimQt::MockQSettings::EnableByDefault();
 50  }
 51  
 52  void TestQSettings::cleanup() noexcept
 53  {
 54  	NeovimQt::MockQSettings::ClearAllContents();
 55  }
 56  
 57  void TestQSettings::OptionLineGrid() noexcept
 58  {
 59  	QSettings settings;
 60  
 61  	settings.setValue("ext_linegrid", true);
 62  	auto sWithLineGrid = CreateShellWidget();
 63  	ShellOptions shellOptionsWithLineGrid{ sWithLineGrid->GetShellOptions() };
 64  
 65  	QCOMPARE(shellOptionsWithLineGrid.IsLineGridEnabled(), true);
 66  
 67  	settings.setValue("ext_linegrid", false);
 68  	auto sLegacy = CreateShellWidget();
 69  	ShellOptions shellOptionsLegacy{ sLegacy->GetShellOptions() };
 70  
 71  	QCOMPARE(shellOptionsLegacy.IsLineGridEnabled(), false);
 72  }
 73  
 74  void TestQSettings::OptionPopupMenu() noexcept
 75  {
 76  	auto w = CreateMainWindowWithRuntime();
 77  	NeovimConnector* connector = w->shell()->nvim();
 78  
 79  	QSettings settings;
 80  	QSignalSpy spy_fontchange(w->shell(), &ShellWidget::shellFontChanged);
 81  
 82  	SendNeovimCommand(connector, "GuiPopupmenu 1");
 83  	SPYWAIT(spy_fontchange, 2500 /*msec*/);
 84  	QCOMPARE(settings.value("ext_popupmenu").toBool(), true);
 85  
 86  	SendNeovimCommand(connector, "GuiPopupmenu 0");
 87  	SPYWAIT(spy_fontchange, 2500 /*msec*/);
 88  	QCOMPARE(settings.value("ext_popupmenu").toBool(), false);
 89  }
 90  
 91  void TestQSettings::OptionTabline() noexcept
 92  {
 93  	auto w = CreateMainWindowWithRuntime();
 94  	NeovimConnector* connector = w->shell()->nvim();
 95  
 96  	QSettings settings;
 97  
 98  	SendNeovimCommand(connector, "GuiTabline 1");
 99  	QCOMPARE(settings.value("ext_tabline").toBool(), true);
100  
101  	SendNeovimCommand(connector, "GuiTabline 0");
102  	QCOMPARE(settings.value("ext_tabline").toBool(), false);
103  }
104  
105  void TestQSettings::GuiFont() noexcept
106  {
107  	auto w = CreateMainWindowWithRuntime();
108  	NeovimConnector* connector = w->shell()->nvim();
109  
110  	QSettings settings;
111  
112  	const QString fontDesc{ QStringLiteral("%1:h20").arg(GetPlatformTestFont()) };
113  	const QString fontCommand{ QStringLiteral("GuiFont! %1").arg(fontDesc) };
114  
115  	SendNeovimCommand(connector, fontCommand);
116  	QCOMPARE(w->shell()->fontDesc(), fontDesc);
117  	QCOMPARE(settings.value("Gui/Font").toString(), fontDesc);
118  }
119  
120  void TestQSettings::GuiScrollBar() noexcept
121  {
122  	auto w = CreateMainWindowWithRuntime();
123  	NeovimConnector* connector = w->shell()->nvim();
124  
125  	QSettings settings;
126  
127  	SendNeovimCommand(connector, "GuiScrollBar 1");
128  	QCOMPARE(settings.value("Gui/ScrollBar").toBool(), true);
129  
130  	SendNeovimCommand(connector, "GuiScrollBar 0");
131  	QCOMPARE(settings.value("Gui/ScrollBar").toBool(), false);
132  }
133  void TestQSettings::GuiTreeView() noexcept
134  {
135  	auto w = CreateMainWindowWithRuntime();
136  	NeovimConnector* connector = w->shell()->nvim();
137  
138  	QSettings settings;
139  
140  	SendNeovimCommand(connector, "GuiTreeviewShow");
141  	QCOMPARE(settings.value("Gui/TreeView").toBool(), true);
142  
143  	SendNeovimCommand(connector, "GuiTreeviewHide");
144  	QCOMPARE(settings.value("Gui/TreeView").toBool(), false);
145  }
146  
147  } // Namespace NeovimQt
148  
149  QTEST_MAIN(NeovimQt::TestQSettings)
150  #include "tst_qsettings.moc"