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
|
// -*- 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 "MachineBase/RLMachine.hpp"
#include "LongOperations/TextoutLongOperation.hpp"
#include "TestSystem/TestSystem.hpp"
#include "TestSystem/MockTextWindow.hpp"
#include "Systems/Base/TextPage.hpp"
#include "libReallive/archive.h"
#include "testUtils.hpp"
#include <string>
#include <memory>
#include <boost/assign/list_of.hpp>
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().setActiveWindow(0);
}
TestTextSystem& getTextSystem() {
return dynamic_cast<TestTextSystem&>(system.text());
}
MockTextWindow& getTextWindow(int twn) {
return dynamic_cast<MockTextWindow&>(*system.text().textWindow(twn));
}
TextPage& currentPage() {
return system.text().currentPage();
}
void writeString(const std::string& text, bool nowait) {
auto_ptr<TextoutLongOperation> tolo(
new TextoutLongOperation(rlmachine, text));
if (nowait)
tolo->setNoWait();
while (!(*tolo)(rlmachine));
}
void snapshotAndClear() {
TextSystem& text = rlmachine.system().text();
text.snapshot();
text.textWindow(0)->clearWin();
text.newPageOnWindow(0);
}
boost::shared_ptr<EventSystemMockHandler> event_mock;
};
TEST_F(TextSystemTest, NormalTextDisplay) {
writeString("A text string.", false);
EXPECT_EQ("A text string.", getTextWindow(0).currentContents());
}
TEST_F(TextSystemTest, NormalTextWithNoWait) {
writeString("A text string.", true);
EXPECT_EQ("A text string.", getTextWindow(0).currentContents());
}
TEST_F(TextSystemTest, PrintEmptyString) {
writeString("", false);
EXPECT_EQ("", getTextWindow(0).currentContents());
}
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).currentContents())
<< "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).currentContents())
<< "We're on the 3rd page!";
EXPECT_TRUE(text.isReadingBacklog()) << "We're reading the backlog.";
text.backPage();
EXPECT_EQ("Page two.", getTextWindow(0).currentContents())
<< "We're on the 2nd page!";
EXPECT_TRUE(text.isReadingBacklog()) << "We're reading the backlog.";
text.backPage();
EXPECT_EQ("Page one.", getTextWindow(0).currentContents())
<< "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).currentContents())
<< "We're still on the 1st page!";
EXPECT_TRUE(text.isReadingBacklog()) << "We're reading the backlog.";
text.forwardPage();
EXPECT_EQ("Page two.", getTextWindow(0).currentContents())
<< "We're back to the 2nd page!";
EXPECT_TRUE(text.isReadingBacklog()) << "We're reading the backlog.";
text.stopReadingBacklog();
EXPECT_EQ("Page four.", getTextWindow(0).currentContents())
<< "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);
currentPage().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);
currentPage().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);
currentPage().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);
currentPage().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);
currentPage().markRubyBegin();
writeString("With Ruby", true);
currentPage().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));
}
|