File: text_system_test.cc

package info (click to toggle)
rlvm 0.14-5.2
  • links: PTS
  • area: main
  • in suites: sid, trixie
  • size: 17,104 kB
  • sloc: cpp: 91,574; ansic: 39,346; perl: 768; sh: 320; python: 181; makefile: 8
file content (368 lines) | stat: -rw-r--r-- 12,453 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// -*- Mode: C++; tab-width:2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi:tw=80:et:ts=2:sts=2
//
// -----------------------------------------------------------------------
//
// This file is part of RLVM, a RealLive virtual machine clone.
//
// -----------------------------------------------------------------------
//
// Copyright (C) 2007 Elliot Glaysher
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
//
// -----------------------------------------------------------------------

#include "gtest/gtest.h"
#include "gmock/gmock.h"

#include "libreallive/archive.h"
#include "long_operations/textout_long_operation.h"
#include "machine/rlmachine.h"
#include "systems/base/text_page.h"
#include "test_system/mock_surface.h"
#include "test_system/mock_text_window.h"
#include "test_system/test_system.h"

#include "test_utils.h"

#include <string>
#include <memory>

using namespace std;

using ::testing::_;

// Increments everytime you ask for ticks to simulate time going by in the real
// world. Neccessary because textout measures ticks so it knows how fast to
// print out characters.
class IncrementingTickCounter : public EventSystemMockHandler {
 public:
  IncrementingTickCounter() : ticks(0) {}
  virtual unsigned int GetTicks() const { return ticks++; }

 private:
  mutable unsigned int ticks;
};

class TextSystemTest : public FullSystemTest {
 protected:
  TextSystemTest() {
    event_mock.reset(new IncrementingTickCounter);
    dynamic_cast<TestEventSystem&>(system.event()).SetMockHandler(event_mock);

    system.text().set_active_window(0);
  }

  TestTextSystem& GetTextSystem() {
    return dynamic_cast<TestTextSystem&>(system.text());
  }

  TestGraphicsSystem& GetGraphicsSystem() {
    return dynamic_cast<TestGraphicsSystem&>(system.graphics());
  }

  MockTextWindow& GetTextWindow(int twn) {
    return dynamic_cast<MockTextWindow&>(*system.text().GetTextWindow(twn));
  }

  TextPage& GetCurrentPage() { return system.text().GetCurrentPage(); }

  void WriteString(const std::string& text, bool nowait) {
    unique_ptr<TextoutLongOperation> tolo(
        new TextoutLongOperation(rlmachine, text));

    if (nowait)
      tolo->set_no_wait();

    while (!(*tolo)(rlmachine))
      ;
  }

  void SnapshotAndClear() {
    TextSystem& text = rlmachine.system().text();
    text.Snapshot();
    text.GetTextWindow(0)->ClearWin();
    text.NewPageOnWindow(0);
  }

  std::shared_ptr<EventSystemMockHandler> event_mock;
};

TEST_F(TextSystemTest, NormalTextDisplay) {
  WriteString("A text string.", false);
  EXPECT_EQ("A text string.", GetTextWindow(0).current_contents());
}

TEST_F(TextSystemTest, NormalTextWithNoWait) {
  WriteString("A text string.", true);
  EXPECT_EQ("A text string.", GetTextWindow(0).current_contents());
}

TEST_F(TextSystemTest, PrintEmptyString) {
  WriteString("", false);
  EXPECT_EQ("", GetTextWindow(0).current_contents());
}

TEST_F(TextSystemTest, BackLogFunctionality) {
  TextSystem& text = rlmachine.system().text();

  WriteString("Page one.", true);
  SnapshotAndClear();
  WriteString("Page two.", true);
  SnapshotAndClear();
  WriteString("Page three.", true);
  SnapshotAndClear();
  WriteString("Page four.", true);

  EXPECT_EQ("Page four.", GetTextWindow(0).current_contents())
      << "We're on the final page!";
  EXPECT_FALSE(text.IsReadingBacklog()) << "We're not reading the backlog.";

  // Reply our way back to the front
  text.BackPage();
  EXPECT_EQ("Page three.", GetTextWindow(0).current_contents())
      << "We're on the 3rd page!";
  EXPECT_TRUE(text.IsReadingBacklog()) << "We're reading the backlog.";

  text.BackPage();
  EXPECT_EQ("Page two.", GetTextWindow(0).current_contents())
      << "We're on the 2nd page!";
  EXPECT_TRUE(text.IsReadingBacklog()) << "We're reading the backlog.";

  text.BackPage();
  EXPECT_EQ("Page one.", GetTextWindow(0).current_contents())
      << "We're on the 1st page!";
  EXPECT_TRUE(text.IsReadingBacklog()) << "We're reading the backlog.";

  // Trying to go back past the first page doesn't do anything.
  text.BackPage();
  EXPECT_EQ("Page one.", GetTextWindow(0).current_contents())
      << "We're still on the 1st page!";
  EXPECT_TRUE(text.IsReadingBacklog()) << "We're reading the backlog.";

  text.ForwardPage();
  EXPECT_EQ("Page two.", GetTextWindow(0).current_contents())
      << "We're back to the 2nd page!";
  EXPECT_TRUE(text.IsReadingBacklog()) << "We're reading the backlog.";

  text.StopReadingBacklog();
  EXPECT_EQ("Page four.", GetTextWindow(0).current_contents())
      << "We're back to the current page!";
  EXPECT_FALSE(text.IsReadingBacklog())
      << "We're no longer reading the backlog.";
}

// -----------------------------------------------------------------------

// Tests that the TextPage::name construct repeats correctly.
TEST_F(TextSystemTest, RepeatsTextPageName) {
  TestTextSystem& sys = GetTextSystem();
  MockTextWindow& win = GetTextWindow(0);
  EXPECT_CALL(win, SetName("Bob", "")).Times(1);
  GetCurrentPage().Name("Bob", "");
  SnapshotAndClear();
  ASSERT_TRUE(::testing::Mock::VerifyAndClearExpectations(&win));

  // Replay it:
  EXPECT_CALL(win, SetName("Bob", _)).Times(1);
  GetTextSystem().BackPage();
  ASSERT_TRUE(::testing::Mock::VerifyAndClearExpectations(&win));
}

// -----------------------------------------------------------------------

// Tests that the TextPgae::hardBreak construct repeats correctly.
TEST_F(TextSystemTest, TextPageHardBreakRepeats) {
  TestTextSystem& sys = GetTextSystem();
  MockTextWindow& win = GetTextWindow(0);
  EXPECT_CALL(win, HardBrake()).Times(1);
  GetCurrentPage().HardBrake();
  SnapshotAndClear();
  ASSERT_TRUE(::testing::Mock::VerifyAndClearExpectations(&win));

  // Replay it:
  EXPECT_CALL(win, HardBrake()).Times(1);
  GetTextSystem().BackPage();
  ASSERT_TRUE(::testing::Mock::VerifyAndClearExpectations(&win));
}

// -----------------------------------------------------------------------

// Tests that the TextPage::ResetIndentation construct repeats correctly.
TEST_F(TextSystemTest, TextPageResetIndentationRepeats) {
  TestTextSystem& sys = GetTextSystem();
  MockTextWindow& win = GetTextWindow(0);
  WriteString("test", true);

  EXPECT_CALL(win, ResetIndentation()).Times(1);
  GetCurrentPage().ResetIndentation();
  SnapshotAndClear();
  ASSERT_TRUE(::testing::Mock::VerifyAndClearExpectations(&win));

  // Replay it:
  EXPECT_CALL(win, ResetIndentation()).Times(1);
  GetTextSystem().BackPage();
  ASSERT_TRUE(::testing::Mock::VerifyAndClearExpectations(&win));
}

// -----------------------------------------------------------------------

// Tests that the TextPage::fontColor construct repeats correctly.
TEST_F(TextSystemTest, TextPageFontColorRepeats) {
  TestTextSystem& sys = GetTextSystem();
  MockTextWindow& win = GetTextWindow(0);

  EXPECT_CALL(win, SetFontColor(_)).Times(1);
  GetCurrentPage().FontColour(0);
  SnapshotAndClear();
  ASSERT_TRUE(::testing::Mock::VerifyAndClearExpectations(&win));

  // The scrollback shouldn't be colored.
  EXPECT_CALL(win, SetFontColor(_)).Times(0);
  GetTextSystem().BackPage();
  ASSERT_TRUE(::testing::Mock::VerifyAndClearExpectations(&win));
}

// -----------------------------------------------------------------------

// Tests that the ruby constructs repeat correctly.
TEST_F(TextSystemTest, RubyRepeats) {
  TestTextSystem& sys = GetTextSystem();
  MockTextWindow& win = GetTextWindow(0);

  EXPECT_CALL(win, MarkRubyBegin()).Times(1);
  EXPECT_CALL(win, DisplayRubyText("ruby")).Times(1);
  GetCurrentPage().MarkRubyBegin();
  WriteString("With Ruby", true);
  GetCurrentPage().DisplayRubyText("ruby");
  SnapshotAndClear();
  ASSERT_TRUE(::testing::Mock::VerifyAndClearExpectations(&win));

  // Replay it:
  EXPECT_CALL(win, MarkRubyBegin()).Times(1);
  EXPECT_CALL(win, DisplayRubyText("ruby")).Times(1);
  GetTextSystem().BackPage();
  ASSERT_TRUE(::testing::Mock::VerifyAndClearExpectations(&win));
}

// -----------------------------------------------------------------------

TEST_F(TextSystemTest, RenderGlyphOntoOneLine) {
  TestTextSystem& sys = GetTextSystem();
  std::shared_ptr<Surface> text_surface =
      sys.RenderText("One", 20, 0, 0, RGBColour::White(), NULL, 3);
  // Ensure that when the number of characters equals the max number of
  // characters, we only use one line.
  EXPECT_EQ(20, text_surface->GetSize().height());
}

TEST_F(TextSystemTest, RenderGlyphNoRestriction) {
  TestTextSystem& sys = GetTextSystem();
  std::shared_ptr<Surface> text_surface =
      sys.RenderText("A Very Long String That Goes On And On",
                     20,
                     0,
                     0,
                     RGBColour::White(),
                     NULL,
                     0);
  // Ensure that when the number of characters equals the max number of
  // characters, we only use one line.
  EXPECT_EQ(20, text_surface->GetSize().height());
}

TEST_F(TextSystemTest, RenderGlyphOntoTwoLines) {
  TestTextSystem& sys = GetTextSystem();
  std::shared_ptr<Surface> text_surface =
      sys.RenderText("OneTwo", 20, 0, 0, RGBColour::White(), NULL, 3);
  EXPECT_EQ(40, text_surface->GetSize().height());

  // Tests the location of rendered glyphs.
  struct {
    const char* str;
    int xpos;
    int ypos;
  } test_data[] = {{"O", 0, 0},
                   {"n", 20, 0},
                   {"e", 40, 0},
                   {"T", 0, 20},
                   {"w", 20, 20},
                   {"o", 40, 20}};

  std::vector<std::tuple<std::string, int, int>> data = sys.glyphs();
  ASSERT_EQ(6, data.size());
  for (int i = 0; i < data.size(); ++i) {
    EXPECT_EQ(test_data[i].str, get<0>(data[i]));
    EXPECT_EQ(test_data[i].xpos, get<1>(data[i]));
    EXPECT_EQ(test_data[i].ypos, get<2>(data[i]));
  }
}

TEST_F(TextSystemTest, DontCrashWithNoEmojiFile) {
  TestTextSystem& sys = GetTextSystem();
  std::shared_ptr<Surface> text_surface =
      sys.RenderText("One#A00Two", 20, 0, 0, RGBColour::White(), NULL, -1);
  EXPECT_EQ(20, text_surface->GetSize().height());
}

TEST_F(TextSystemTest, TestEmoji) {
  // Set up emoji printing:
  system.gameexe().SetStringAt("E_MOJI.004", "emoji_file");

  // Inject a fake surface of (24*5, 24). We will expect this to blit to the
  // text surface.
  std::shared_ptr<MockSurface> mock(
      MockSurface::Create("emoji_file", Size(24 * 5, 24)));
  GetGraphicsSystem().InjectSurface("emoji_file", mock);

  // Our mock surface should render its icon between the Es.
  EXPECT_CALL(*mock,
              BlitToSurface(_,
                            Rect(24 * 2, 0, Size(24, 24)),
                            Rect(20, 0, Size(24, 24)),
                            255,
                            false));

  TestTextSystem& sys = GetTextSystem();
  std::shared_ptr<Surface> text_surface =
      sys.RenderText("E#A02E", 20, 0, 0, RGBColour::White(), NULL, -1);
  EXPECT_EQ(20, text_surface->GetSize().height());

  // Tests the location of rendered glyphs.
  struct {
    const char* str;
    int xpos;
    int ypos;
  } test_data[] = {{"E", 0, 0}, {"E", 40, 0}, };

  std::vector<std::tuple<std::string, int, int>> data = sys.glyphs();
  ASSERT_EQ(2, data.size());
  for (int i = 0; i < data.size(); ++i) {
    EXPECT_EQ(test_data[i].str, get<0>(data[i]));
    EXPECT_EQ(test_data[i].xpos, get<1>(data[i]));
    EXPECT_EQ(test_data[i].ypos, get<2>(data[i]));
  }
}

// If we return an empty surface, we crash. Make sure passing an empty string
// doesn't return an empty surface.
TEST_F(TextSystemTest, TestEmptyString) {
  TestTextSystem& sys = GetTextSystem();
  std::shared_ptr<Surface> text_surface =
      sys.RenderText("", 20, 0, 0, RGBColour::White(), NULL, -1);
  EXPECT_GT(text_surface->GetSize().width(), 0);
  EXPECT_GT(text_surface->GetSize().height(), 0);
}