File: graphics_object_test.cpp

package info (click to toggle)
rlvm 0.12-4
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 16,348 kB
  • sloc: cpp: 92,030; ansic: 39,532; perl: 768; python: 181; sh: 171; makefile: 7
file content (253 lines) | stat: -rw-r--r-- 9,216 bytes parent folder | download
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
// -*- 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/assign/list_of.hpp>
#include <boost/tuple/tuple.hpp>

#include <string>
#include <vector>

#include "MachineBase/Memory.hpp"
#include "MachineBase/RLMachine.hpp"
#include "MachineBase/Serialization.hpp"
#include "Modules/Module_Str.hpp"
#include "TestSystem/TestGraphicsSystem.hpp"
#include "TestSystem/TestSystem.hpp"
#include "Systems/Base/GraphicsObject.hpp"
#include "Systems/Base/GraphicsObjectOfFile.hpp"
#include "Utilities/Exception.hpp"
#include "libReallive/archive.h"
#include "libReallive/intmemref.h"

#include "testUtils.hpp"
#include <boost/scoped_ptr.hpp>
#include <boost/function.hpp>
#include <iostream>

using namespace boost;
using namespace boost::assign;
using namespace std;
using namespace libReallive;
using namespace Serialization;

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->objectData());

    EXPECT_EQ(FILE_NAME, obj.filename()) << "Preserved file name";
  }

  Serialization::g_current_machine = NULL;
}

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

// Automated tests for accessors that take one int.
typedef boost::tuple<
  boost::function<void(GraphicsObject&, const int)>,
  boost::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.referenceCount())
      << "Both objects have the same internal object";
  EXPECT_EQ(3, objCopy.referenceCount())
      << "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.
  (accessors.get<1>())(objCopy);

  EXPECT_EQ(3, obj.referenceCount())
      << "Both objects have the same internal object";
  EXPECT_EQ(3, objCopy.referenceCount())
      << "Both objects have the same internal object";

  // Call this setter function. This should force the copy-on-write
  // code to trigger.
  (accessors.get<0>())(objCopy, 1);

  EXPECT_EQ(2, obj.referenceCount())
      << "Untouched object still points to empty";
  EXPECT_EQ(1, objCopy.referenceCount())
      << "Modified object has its own impl";
}

typedef vector<TupleT> SetterVec;
SetterVec graphics_object_setters =
    tuple_list_of
         (&GraphicsObject::setVisible, &GraphicsObject::visible)
         (&GraphicsObject::setX, &GraphicsObject::x)
         (&GraphicsObject::setY, &GraphicsObject::y)
         (&GraphicsObject::setVert, &GraphicsObject::vert)
         (&GraphicsObject::setXOrigin, &GraphicsObject::xOrigin)
         (&GraphicsObject::setYOrigin, &GraphicsObject::yOrigin)
         (&GraphicsObject::setWidth, &GraphicsObject::width)
         (&GraphicsObject::setHeight, &GraphicsObject::height)
         (&GraphicsObject::setRotation, &GraphicsObject::rotation)
         (&GraphicsObject::setPattNo, &GraphicsObject::pattNo)
         (&GraphicsObject::setMono, &GraphicsObject::mono)
         (&GraphicsObject::setInvert, &GraphicsObject::invert)
         (&GraphicsObject::setLight, &GraphicsObject::light)
         (&GraphicsObject::setCompositeMode, &GraphicsObject::compositeMode)
         (&GraphicsObject::setScrollRateX, &GraphicsObject::scrollRateX)
         (&GraphicsObject::setScrollRateY, &GraphicsObject::scrollRateY)
         (&GraphicsObject::setAlpha, &GraphicsObject::alpha)
         (&GraphicsObject::setWipeCopy, &GraphicsObject::wipeCopy);

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 =
    tuple_list_of
         (&GraphicsObject::setTintR, getTintR)
         (&GraphicsObject::setTintG, getTintG)
         (&GraphicsObject::setTintB, getTintB)
         (&GraphicsObject::setColourR, getColourR)
         (&GraphicsObject::setColourG, getColourG)
         (&GraphicsObject::setColourB, getColourB)
         (&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_METHOD0(execute, void());
  MOCK_METHOD0(isAnimation, bool());
  MOCK_METHOD1(playSet, void(int));
  MOCK_METHOD1(currentSurface,
               boost::shared_ptr<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.objectData();}, rlvm::Exception);

  MockGraphicsObjectData* data = new MockGraphicsObjectData;
  obj.setObjectData(data);
  EXPECT_EQ(data, &obj.objectData());
}

// TODO: Use the above mock to test more of the insides of GraphicsObject...