1 /*
2    Vimpc
3    Copyright (C) 2013 Nathan Sweetman
4 
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 
18    screen.cpp - tests for the screen class
19 */
20 
21 #include <cppunit/extensions/HelperMacros.h>
22 
23 #include "screen.hpp"
24 #include "test.hpp"
25 #include "window/debug.hpp"
26 #include "window/scrollwindow.hpp"
27 #include "window/songwindow.hpp"
28 
29 class ScreenTester : public CppUnit::TestFixture
30 {
31    CPPUNIT_TEST_SUITE(ScreenTester);
32    CPPUNIT_TEST(TestChangeWindow);
33 
34    CPPUNIT_TEST(TestAlign);
35    CPPUNIT_TEST(TestAlignTo);
36    CPPUNIT_TEST(TestSelect);
37 
38    CPPUNIT_TEST_SUITE_END();
39 
40 public:
ScreenTester()41    ScreenTester()
42       : screen_(*Main::Tester::Instance().Screen) { }
43 
44 public:
45    void setUp();
46    void tearDown();
47 
48 protected:
49    void TestChangeWindow();
50    void TestAlign();
51    void TestAlignTo();
52    void TestSelect();
53 
54 private:
55    Ui::Screen & screen_;
56    Ui::SongWindow * window_;
57    int32_t windowId_;
58 };
59 
setUp()60 void ScreenTester::setUp()
61 {
62    window_ = screen_.CreateSongWindow("test");
63    Main::Library().ForEachSong([this] (Mpc::Song * song) { window_->Add(song); });
64    windowId_ = screen_.GetWindowFromName(window_->Name());
65    screen_.SetActiveAndVisible(windowId_);
66 }
67 
tearDown()68 void ScreenTester::tearDown()
69 {
70    window_->Clear();
71    screen_.SetVisible(windowId_, false);
72 }
73 
74 // Test that cycles through all the windows
TestChangeWindow()75 void ScreenTester::TestChangeWindow()
76 {
77    int32_t WindowCount = screen_.VisibleWindows();
78    int32_t StartWindow = screen_.GetActiveWindow();
79    int32_t PreviousWindow = screen_.GetActiveWindow();
80 
81    // Cycle through every window using the index in the tab bar
82    for (int i = 0; i < WindowCount; ++i)
83    {
84       PreviousWindow = screen_.GetActiveWindow();
85       screen_.SetActiveWindow(i);
86 
87       // Ensure that it is the correct window
88       CPPUNIT_ASSERT(screen_.GetActiveWindowIndex() == i);
89       CPPUNIT_ASSERT(screen_.IsVisible(screen_.GetActiveWindow()) == true);
90 
91       // Ensure that previous is set correctly
92       CPPUNIT_ASSERT(screen_.GetPreviousWindow() == PreviousWindow);
93    }
94 
95    screen_.SetActiveAndVisible(StartWindow);
96 
97    // Cycle through all windows using :tabnext
98    for (int i = 0; i < WindowCount; ++i)
99    {
100       PreviousWindow = screen_.GetActiveWindow();
101       screen_.SetActiveWindow(Ui::Screen::Next);
102       CPPUNIT_ASSERT(screen_.IsVisible(screen_.GetActiveWindow()) == true);
103 
104       // Ensure that previous is set correctly
105       CPPUNIT_ASSERT(screen_.GetPreviousWindow() == PreviousWindow);
106    }
107 
108    // Test that cycling through all windows returns to start
109    CPPUNIT_ASSERT(screen_.GetActiveWindow() == StartWindow);
110 
111    // Cycle through all windows using :tabprevious
112    for (int i = 0; i < WindowCount; ++i)
113    {
114       PreviousWindow = screen_.GetActiveWindow();
115       screen_.SetActiveWindow(Ui::Screen::Previous);
116       CPPUNIT_ASSERT(screen_.IsVisible(screen_.GetActiveWindow()) == true);
117 
118       // Ensure that previous is set correctly
119       CPPUNIT_ASSERT(screen_.GetPreviousWindow() == PreviousWindow);
120    }
121 
122    // Test that cycling through all windows returns to start
123    CPPUNIT_ASSERT(screen_.GetActiveWindow() == StartWindow);
124 
125    // Check that going to absolute window works
126    screen_.SetActiveAndVisible(StartWindow);
127    CPPUNIT_ASSERT(screen_.GetActiveWindow() == StartWindow);
128 }
129 
TestAlign()130 void ScreenTester::TestAlign() // ^E, ^Y
131 {
132    int32_t rows = screen_.MaxRows();
133    int32_t bufferSize = window_->BufferSize();
134 
135    screen_.ScrollTo(0);
136 
137    CPPUNIT_ASSERT(window_->FirstLine() == 0);
138    CPPUNIT_ASSERT(window_->LastLine() + 1 == rows);
139 
140    screen_.Align(Ui::Screen::Direction::Down, 3);
141 
142    CPPUNIT_ASSERT(window_->FirstLine() == 3);
143    CPPUNIT_ASSERT(window_->LastLine() + 1 == rows + 3);
144 
145    screen_.Align(Ui::Screen::Direction::Up, 2);
146 
147    CPPUNIT_ASSERT(window_->FirstLine() == 1);
148    CPPUNIT_ASSERT(window_->LastLine() + 1== rows + 1);
149 
150    screen_.Align(Ui::Screen::Direction::Up, 2);
151 
152    CPPUNIT_ASSERT(window_->FirstLine() == 0);
153    CPPUNIT_ASSERT(window_->LastLine() + 1 == rows);
154 
155    screen_.Scroll(bufferSize);
156 
157    CPPUNIT_ASSERT(window_->FirstLine() == bufferSize - rows);
158    CPPUNIT_ASSERT(window_->LastLine() + 1 == bufferSize);
159 }
160 
TestAlignTo()161 void ScreenTester::TestAlignTo() // z<CR>, z-, z.
162 {
163    int32_t rows = screen_.MaxRows();
164    int32_t halfRows = (rows + 1) / 2;
165    int32_t bufferSize = window_->BufferSize();
166 
167    screen_.ScrollTo(rows);
168 
169    CPPUNIT_ASSERT(window_->FirstLine() == rows - halfRows);
170    CPPUNIT_ASSERT(window_->LastLine() + 1 == (rows + halfRows) - 1);
171 
172    screen_.AlignTo(Ui::Screen::Location::Top, 0); // zt or z<CR>
173 
174    CPPUNIT_ASSERT(window_->FirstLine() == rows);
175    CPPUNIT_ASSERT(window_->LastLine() + 1 == rows * 2);
176 
177    screen_.AlignTo(Ui::Screen::Location::Bottom, 0); // zb or z-
178 
179    CPPUNIT_ASSERT(window_->FirstLine() == 1);
180    CPPUNIT_ASSERT(window_->LastLine() + 1 == rows + 1);
181 
182    screen_.AlignTo(Ui::Screen::Location::Centre, 0); // zz or z.
183 
184    CPPUNIT_ASSERT(window_->FirstLine() == rows - halfRows);
185    CPPUNIT_ASSERT(window_->LastLine() + 1 == (rows + halfRows) - 1);
186 
187    screen_.AlignTo(Ui::Screen::Location::Specific, 1); // 1G or gg
188 
189    CPPUNIT_ASSERT(window_->FirstLine() == 0);
190    CPPUNIT_ASSERT(window_->LastLine() + 1 == rows);
191 
192    screen_.AlignTo(Ui::Screen::Location::Top, 2); // zt with count
193    CPPUNIT_ASSERT(window_->FirstLine() == 1);
194 
195    screen_.AlignTo(Ui::Screen::Location::Bottom, (rows * 2) + 1); // zb with count
196    CPPUNIT_ASSERT(window_->LastLine() == (rows * 2));
197 
198    screen_.AlignTo(Ui::Screen::Location::Centre, rows + 1); // zz with count
199    CPPUNIT_ASSERT(window_->FirstLine() == rows - halfRows);
200 }
201 
TestSelect()202 void ScreenTester::TestSelect() // H, L, M
203 {
204    int32_t rows = screen_.MaxRows();
205    int32_t halfRows = (rows + 1) / 2;
206    int32_t bufferSize = window_->BufferSize();
207 
208    screen_.ScrollTo(rows);
209 
210    CPPUNIT_ASSERT(window_->FirstLine() == rows - halfRows);
211    CPPUNIT_ASSERT(window_->LastLine() + 1 == (rows + halfRows) - 1);
212 
213    window_->Select(Ui::ScrollWindow::Position::First, 1); // H
214 
215    CPPUNIT_ASSERT(window_->FirstLine() == rows - halfRows);
216    CPPUNIT_ASSERT(window_->LastLine() + 1 == (rows + halfRows) - 1);
217    CPPUNIT_ASSERT(window_->CurrentLine() == screen_.ActiveWindow().FirstLine());
218 
219    window_->Select(Ui::ScrollWindow::Position::Middle, 1); // M
220 
221    CPPUNIT_ASSERT(window_->FirstLine() == rows - halfRows);
222    CPPUNIT_ASSERT(window_->LastLine() + 1 == (rows + halfRows) - 1);
223    CPPUNIT_ASSERT(window_->CurrentLine() == rows - 1);
224 
225    window_->Select(Ui::ScrollWindow::Position::Last, 1); // L
226 
227    CPPUNIT_ASSERT(window_->FirstLine() == rows - halfRows);
228    CPPUNIT_ASSERT(window_->LastLine() + 1 == (rows + halfRows) - 1);
229    CPPUNIT_ASSERT(window_->CurrentLine() == screen_.ActiveWindow().LastLine());
230 
231    // H and L with a count
232 
233    window_->Select(Ui::ScrollWindow::Position::First, 5);
234 
235    CPPUNIT_ASSERT(window_->FirstLine() == rows - halfRows);
236    CPPUNIT_ASSERT(window_->LastLine() + 1 == (rows + halfRows) - 1);
237    CPPUNIT_ASSERT(window_->CurrentLine() == screen_.ActiveWindow().FirstLine() + 4);
238 
239    window_->Select(Ui::ScrollWindow::Position::Last, 5);
240 
241    CPPUNIT_ASSERT(window_->FirstLine() == rows - halfRows);
242    CPPUNIT_ASSERT(window_->LastLine() + 1 == (rows + halfRows) - 1);
243    CPPUNIT_ASSERT(window_->CurrentLine() == screen_.ActiveWindow().LastLine() - 4);
244 
245    // H and L with out of bounds count
246 
247    window_->Select(Ui::ScrollWindow::Position::First, rows);
248 
249    CPPUNIT_ASSERT(window_->FirstLine() == rows - halfRows);
250    CPPUNIT_ASSERT(window_->LastLine() + 1 == (rows + halfRows) - 1);
251    CPPUNIT_ASSERT(window_->CurrentLine() == screen_.ActiveWindow().LastLine());
252 
253    window_->Select(Ui::ScrollWindow::Position::Last, rows);
254 
255    CPPUNIT_ASSERT(window_->FirstLine() == rows - halfRows);
256    CPPUNIT_ASSERT(window_->LastLine() + 1 == (rows + halfRows) - 1);
257    CPPUNIT_ASSERT(window_->CurrentLine() == screen_.ActiveWindow().FirstLine());
258 
259    window_->Select(Ui::ScrollWindow::Position::First, rows + 1);
260 
261    CPPUNIT_ASSERT(window_->FirstLine() == rows - halfRows);
262    CPPUNIT_ASSERT(window_->LastLine() + 1 == (rows + halfRows) - 1);
263    CPPUNIT_ASSERT(window_->CurrentLine() == screen_.ActiveWindow().LastLine());
264 
265    window_->Select(Ui::ScrollWindow::Position::Last, rows + 1);
266 
267    CPPUNIT_ASSERT(window_->FirstLine() == rows - halfRows);
268    CPPUNIT_ASSERT(window_->LastLine() + 1 == (rows + halfRows) - 1);
269    CPPUNIT_ASSERT(window_->CurrentLine() == screen_.ActiveWindow().FirstLine());
270 }
271 
272 CPPUNIT_TEST_SUITE_REGISTRATION(ScreenTester);
273 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ScreenTester, "screen");
274