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 369 370 371
|
// -*- 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 <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/scoped_ptr.hpp>
#include <boost/scoped_ptr.hpp>
#include <functional>
#include <iostream>
#include <string>
#include <tuple>
#include <vector>
#include "libreallive/archive.h"
#include "libreallive/intmemref.h"
#include "machine/memory.h"
#include "machine/rlmachine.h"
#include "machine/serialization.h"
#include "modules/module_obj_fg_bg.h"
#include "modules/module_obj_management.h"
#include "modules/module_str.h"
#include "systems/base/colour_filter_object_data.h"
#include "systems/base/graphics_object.h"
#include "systems/base/graphics_object_of_file.h"
#include "systems/base/object_mutator.h"
#include "systems/base/parent_graphics_object_data.h"
#include "test_system/mock_colour_filter.h"
#include "test_system/test_graphics_system.h"
#include "test_system/test_system.h"
#include "utilities/exception.h"
#include "test_utils.h"
#include <iostream>
using namespace boost;
using namespace std;
using namespace libreallive;
using namespace Serialization;
using ::testing::_;
using ::testing::Ref;
using ::testing::Return;
const char* FILE_NAME = "doesntmatter";
class GraphicsObjectTest : public FullSystemTest {};
// Test the serialization of an individual GraphicsObjectOfFile object.
TEST_F(GraphicsObjectTest, SerializeObjectData) {
stringstream ss;
Serialization::g_current_machine = &rlmachine;
{
const scoped_ptr<GraphicsObjectData> inputObjOfFile(
new GraphicsObjectOfFile(system, FILE_NAME));
boost::archive::text_oarchive oa(ss);
oa << inputObjOfFile;
}
{
scoped_ptr<GraphicsObjectData> dst;
boost::archive::text_iarchive ia(ss);
ia >> dst;
GraphicsObjectOfFile& obj = dynamic_cast<GraphicsObjectOfFile&>(*dst);
EXPECT_EQ(FILE_NAME, obj.filename()) << "Preserved file name";
}
Serialization::g_current_machine = NULL;
}
// -----------------------------------------------------------------------
// Try it again, this time wrapped in the GraphicsObject
TEST_F(GraphicsObjectTest, SerializeObject) {
stringstream ss;
Serialization::g_current_machine = &rlmachine;
{
const scoped_ptr<GraphicsObject> obj(new GraphicsObject());
obj->SetObjectData(new GraphicsObjectOfFile(system, FILE_NAME));
boost::archive::text_oarchive oa(ss);
oa << obj;
}
{
scoped_ptr<GraphicsObject> dst;
boost::archive::text_iarchive ia(ss);
ia >> dst;
GraphicsObjectOfFile& obj =
dynamic_cast<GraphicsObjectOfFile&>(dst->GetObjectData());
EXPECT_EQ(FILE_NAME, obj.filename()) << "Preserved file name";
}
Serialization::g_current_machine = NULL;
}
// -----------------------------------------------------------------------
// Automated tests for accessors that take one int.
typedef std::tuple<std::function<void(GraphicsObject&, const int)>,
std::function<int(const GraphicsObject&)>> TupleT;
class AccessorTest : public ::testing::TestWithParam<TupleT> {
// Empty.
};
TEST_P(AccessorTest, TestReferenceCount) {
TupleT accessors = GetParam();
GraphicsObject obj;
GraphicsObject objCopy(obj);
// At this step, it is equal to three because they all share the
// empty object
EXPECT_EQ(3, obj.reference_count())
<< "Both objects have the same internal object";
EXPECT_EQ(3, objCopy.reference_count())
<< "Both objects have the same internal object";
// Call the getter method (ignoring the result). We expect that
// this won't force a copy-on-write.
(get<1>(accessors))(objCopy);
EXPECT_EQ(3, obj.reference_count())
<< "Both objects have the same internal object";
EXPECT_EQ(3, objCopy.reference_count())
<< "Both objects have the same internal object";
// Call this setter function. This should force the copy-on-write
// code to trigger.
(get<0>(accessors))(objCopy, 1);
EXPECT_EQ(2, obj.reference_count())
<< "Untouched object still points to empty";
EXPECT_EQ(1, objCopy.reference_count()) << "Modified object has its own impl";
}
typedef vector<TupleT> SetterVec;
SetterVec graphics_object_setters = {
std::make_tuple(&GraphicsObject::SetVisible, &GraphicsObject::visible),
std::make_tuple(&GraphicsObject::SetX, &GraphicsObject::x),
std::make_tuple(&GraphicsObject::SetY, &GraphicsObject::y),
std::make_tuple(&GraphicsObject::SetVert, &GraphicsObject::vert),
std::make_tuple(&GraphicsObject::SetOriginX, &GraphicsObject::origin_x),
std::make_tuple(&GraphicsObject::SetOriginY, &GraphicsObject::origin_y),
std::make_tuple(&GraphicsObject::SetWidth, &GraphicsObject::width),
std::make_tuple(&GraphicsObject::SetHeight, &GraphicsObject::height),
std::make_tuple(&GraphicsObject::SetHqWidth, &GraphicsObject::hq_width),
std::make_tuple(&GraphicsObject::SetHqHeight, &GraphicsObject::hq_height),
std::make_tuple(&GraphicsObject::SetRotation, &GraphicsObject::rotation),
std::make_tuple(&GraphicsObject::SetPattNo, &GraphicsObject::GetPattNo),
std::make_tuple(&GraphicsObject::SetMono, &GraphicsObject::mono),
std::make_tuple(&GraphicsObject::SetInvert, &GraphicsObject::invert),
std::make_tuple(&GraphicsObject::SetLight, &GraphicsObject::light),
std::make_tuple(&GraphicsObject::SetCompositeMode,
&GraphicsObject::composite_mode),
std::make_tuple(&GraphicsObject::SetScrollRateX,
&GraphicsObject::scroll_rate_x),
std::make_tuple(&GraphicsObject::SetScrollRateY,
&GraphicsObject::scroll_rate_y),
std::make_tuple(&GraphicsObject::SetZOrder, &GraphicsObject::z_order),
std::make_tuple(&GraphicsObject::SetZLayer, &GraphicsObject::z_layer),
std::make_tuple(&GraphicsObject::SetZDepth, &GraphicsObject::z_depth),
std::make_tuple(&GraphicsObject::SetAlpha, &GraphicsObject::raw_alpha),
std::make_tuple(&GraphicsObject::SetWipeCopy, &GraphicsObject::wipe_copy)};
INSTANTIATE_TEST_CASE_P(GraphicsObjectSimple,
AccessorTest,
::testing::ValuesIn(graphics_object_setters));
// -----------------------------------------------------------------------
int getTintR(const GraphicsObject& obj) { return obj.tint().r(); }
int getTintG(const GraphicsObject& obj) { return obj.tint().g(); }
int getTintB(const GraphicsObject& obj) { return obj.tint().b(); }
int getColourR(const GraphicsObject& obj) { return obj.colour().r(); }
int getColourG(const GraphicsObject& obj) { return obj.colour().g(); }
int getColourB(const GraphicsObject& obj) { return obj.colour().b(); }
int getColourLevel(const GraphicsObject& obj) { return obj.colour().a(); }
typedef vector<TupleT> SetterVec;
SetterVec graphics_object_colour_setters = {
std::make_tuple(&GraphicsObject::SetTintRed, getTintR),
std::make_tuple(&GraphicsObject::SetTintGreen, getTintG),
std::make_tuple(&GraphicsObject::SetTintBlue, getTintB),
std::make_tuple(&GraphicsObject::SetColourRed, getColourR),
std::make_tuple(&GraphicsObject::SetColourGreen, getColourG),
std::make_tuple(&GraphicsObject::SetColourBlue, getColourB),
std::make_tuple(&GraphicsObject::SetColourLevel, getColourLevel)};
INSTANTIATE_TEST_CASE_P(GraphicsObjectTintAndColour,
AccessorTest,
::testing::ValuesIn(graphics_object_colour_setters));
// -----------------------------------------------------------------------
class MockGraphicsObjectData : public GraphicsObjectData {
public:
MOCK_METHOD2(Render, void(const GraphicsObject&, std::ostream*));
MOCK_METHOD1(PixelWidth, int(const GraphicsObject&));
MOCK_METHOD1(PixelHeight, int(const GraphicsObject&));
MOCK_CONST_METHOD0(Clone, GraphicsObjectData*());
MOCK_METHOD1(Execute, void(RLMachine&));
MOCK_METHOD0(IsAnimation, bool());
MOCK_METHOD1(PlaySet, void(int));
MOCK_METHOD1(CurrentSurface,
std::shared_ptr<const Surface>(const GraphicsObject&));
MOCK_METHOD1(ObjectInfo, void(std::ostream&));
};
TEST_F(GraphicsObjectTest, TestPixelHeightWidth) {
GraphicsObject obj;
// Check default values when we don't have a GraphicsObjectData.
EXPECT_EQ(0, obj.PixelWidth());
EXPECT_EQ(0, obj.PixelHeight());
MockGraphicsObjectData* data = new MockGraphicsObjectData;
obj.SetObjectData(data);
EXPECT_CALL(*data, PixelWidth(Ref(obj))).Times(1).WillOnce(Return(30));
EXPECT_CALL(*data, PixelHeight(Ref(obj))).Times(1).WillOnce(Return(50));
EXPECT_EQ(30, obj.PixelWidth());
EXPECT_EQ(50, obj.PixelHeight());
::testing::Mock::VerifyAndClearExpectations(data);
}
TEST_F(GraphicsObjectTest, GetObjectData) {
GraphicsObject obj;
// Throws when it doesn't have data.
EXPECT_THROW({ obj.GetObjectData(); }, rlvm::Exception);
MockGraphicsObjectData* data = new MockGraphicsObjectData;
obj.SetObjectData(data);
EXPECT_EQ(data, &obj.GetObjectData());
}
// TODO: Use the above mock to test more of the insides of GraphicsObject...
TEST_F(GraphicsObjectTest, TestColourFilter) {
// In the past, we've had regressions because we ignored updated screen_rects
// in colour filter objects.
GraphicsObject obj;
// Two completely different rects.
const Rect one(10, 12, Size(18, 14));
const Rect two(43, 81, Size(5, 20));
ColourFilterObjectData* data =
new ColourFilterObjectData(system.graphics(), one);
obj.SetObjectData(data);
MockColourFilter* filter =
dynamic_cast<MockColourFilter*>(data->GetColourFilter());
EXPECT_CALL(*filter, Fill(_, one, _));
EXPECT_CALL(*filter, Fill(_, two, _));
// Render as is (for the first call).
data->Render(obj, NULL, NULL);
data->set_rect(two);
// Render with the modified rect (for the second call).
data->Render(obj, NULL, NULL);
}
TEST_F(GraphicsObjectTest, objFgFreeAll) {
GraphicsObject obj;
obj.SetX(50);
obj.SetY(120);
obj.SetObjectData(new ColourFilterObjectData(
system.graphics(), Rect(10, 10, Size(80, 70))));
system.graphics().SetObject(0, 10, obj);
system.graphics().SetObject(0, 46, obj);
system.graphics().SetObject(1, 18, obj);
rlmachine.AttachModule(new ObjFgManagement);
rlmachine.Exe("objFgFreeAll", 0);
EXPECT_FALSE(system.graphics().GetObject(0, 10).has_object_data());
EXPECT_FALSE(system.graphics().GetObject(0, 46).has_object_data());
EXPECT_TRUE(system.graphics().GetObject(1, 18).has_object_data());
}
TEST_F(GraphicsObjectTest, ObjectMutatorCopy) {
GraphicsObject obj;
obj.SetX(50);
obj.SetY(120);
obj.SetObjectData(new ColourFilterObjectData(
system.graphics(), Rect(10, 10, Size(80, 70))));
system.graphics().SetObject(1, 18, obj);
// Set a mutator on the object.
rlmachine.AttachModule(new ObjBgModule);
rlmachine.Exe("objBgEveColLevel", 1, TestMachine::Arg(18, 128, 0, 0, 0));
EXPECT_TRUE(system.graphics().GetObject(1, 18).IsMutatorRunningMatching(
-1, "objEveColLevel"));
system.graphics().ClearAndPromoteObjects();
EXPECT_TRUE(system.graphics().GetObject(0, 18).IsMutatorRunningMatching(
-1, "objEveColLevel"));
}
class MutatorTest : public ObjectMutator {
public:
MutatorTest()
: ObjectMutator(-1, "MutatorTest", 0, 1000, 0, 0),
called_(false) {}
bool called() const { return called_; }
virtual bool operator()(RLMachine& machine, GraphicsObject& object) override {
called_ = true;
return false;
}
virtual void SetToEnd(RLMachine& machine, GraphicsObject& object) override {}
virtual ObjectMutator* Clone() const override { return NULL; }
virtual void PerformSetting(RLMachine& machine, GraphicsObject& object)
override {}
private:
bool called_;
};
TEST_F(GraphicsObjectTest, RunMutatorsOnChildObjects) {
GraphicsObject parent;
ParentGraphicsObjectData* parent_data = new ParentGraphicsObjectData(10);
parent.SetObjectData(parent_data);
MutatorTest* mutator_test = new MutatorTest;
parent_data->GetObject(5).AddObjectMutator(
std::unique_ptr<ObjectMutator>(mutator_test));
EXPECT_FALSE(mutator_test->called());
parent.Execute(rlmachine);
EXPECT_TRUE(mutator_test->called());
}
|