~ubuntu-branches/ubuntu/precise/nux/precise-proposed

« back to all changes in this revision

Viewing changes to tests/test_object.cpp

  • Committer: Package Import Robot
  • Author(s): Didier Roche
  • Date: 2012-03-12 11:45:31 UTC
  • mfrom: (1.1.37)
  • Revision ID: package-import@ubuntu.com-20120312114531-3zctml10fifuiap8
Tags: 2.6.0-0ubuntu1
* New upstream release.
  - Restore OpenGL default line width and point size (LP: #937444)
  - [unity-5.6] can't enter accents (^o->) in the dash since recent updates
    (LP: #944674)
  - SetEnableView / ViewEnable / ViewDisable give inverted results
    (LP: #938823)
* debian/control:
  - build-dep on libibus-1.0-dev for ibus support

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2010 Inalogic Inc.
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify it
5
 
 * under the terms of the GNU Lesser General Public License version 3, as
6
 
 * published by the  Free Software Foundation.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful, but
9
 
 * WITHOUT ANY WARRANTY; without even the implied warranties of
10
 
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
11
 
 * PURPOSE.  See the applicable version of the GNU Lesser General Public
12
 
 * License for more details.
13
 
 *
14
 
 * You should have received a copy of both the GNU Lesser General Public
15
 
 * License version 3 along with this program.  If not, see
16
 
 * <http://www.gnu.org/licenses/>
17
 
 *
18
 
 * Authored by: Jay Taoko <jaytaoko@inalogic.com>
19
 
 *
20
 
 */
21
 
 
22
 
#include <gmock/gmock.h>
23
 
 
24
 
#include "Nux/Nux.h"
25
 
 
26
 
using namespace testing;
27
 
 
28
 
namespace {
29
 
 
30
 
const int ARRAY_SIZE = 1000;
31
 
 
32
 
class OwnedObject: public nux::Object
33
 
{
34
 
public:
35
 
  OwnedObject(NUX_FILE_LINE_PROTO)
36
 
    : nux::Object (true, NUX_FILE_LINE_PARAM)
37
 
  {
38
 
  }
39
 
 
40
 
  ~OwnedObject () {}
41
 
 
42
 
  int array [ARRAY_SIZE];
43
 
};
44
 
 
45
 
class ChildOwnedObject: public OwnedObject
46
 
{
47
 
public:
48
 
  ChildOwnedObject(NUX_FILE_LINE_PROTO)
49
 
    : OwnedObject (NUX_FILE_LINE_PARAM)
50
 
  {
51
 
  }
52
 
 
53
 
  ~ChildOwnedObject () {}
54
 
 
55
 
  int array [ARRAY_SIZE];
56
 
};
57
 
 
58
 
 
59
 
class UnOwnedObject: public nux::Object
60
 
{
61
 
public:
62
 
  UnOwnedObject(NUX_FILE_LINE_PROTO)
63
 
    : nux::Object (false, NUX_FILE_LINE_PARAM)
64
 
  {
65
 
  }
66
 
 
67
 
  ~UnOwnedObject () {}
68
 
 
69
 
  int array [ARRAY_SIZE];
70
 
};
71
 
 
72
 
class ChildUnOwnedObject: public UnOwnedObject
73
 
{
74
 
public:
75
 
  ChildUnOwnedObject(NUX_FILE_LINE_PROTO)
76
 
    : UnOwnedObject (NUX_FILE_LINE_PARAM)
77
 
  {
78
 
  }
79
 
 
80
 
  ~ChildUnOwnedObject () {}
81
 
 
82
 
  int array [ARRAY_SIZE];
83
 
};
84
 
 
85
 
TEST(TestObject, TestObject) {
86
 
 
87
 
  OwnedObject *a = new OwnedObject (NUX_TRACKER_LOCATION);
88
 
  OwnedObject b(NUX_TRACKER_LOCATION);
89
 
 
90
 
  EXPECT_THAT(a, NotNull());
91
 
  EXPECT_TRUE(a->IsHeapAllocated());
92
 
 
93
 
  EXPECT_FALSE(b.IsHeapAllocated());
94
 
 
95
 
  EXPECT_THAT(a->GetObjectSize(), Ge(ARRAY_SIZE));
96
 
 
97
 
  a->UnReference();
98
 
}
99
 
 
100
 
TEST(TestObject, TestObjectReference) {
101
 
 
102
 
  OwnedObject *a = new OwnedObject (NUX_TRACKER_LOCATION);  // ref count = 1, owned
103
 
  UnOwnedObject *b = new UnOwnedObject (NUX_TRACKER_LOCATION); // ref count = 1, unowned
104
 
 
105
 
  EXPECT_THAT(a->GetReferenceCount(), Eq(1));
106
 
  EXPECT_THAT(b->GetReferenceCount(), Eq(1));
107
 
  EXPECT_FALSE(b->OwnsTheReference());
108
 
 
109
 
  a->Reference (); // ref count = 2
110
 
  a->Reference (); // ref count = 3
111
 
  b->Reference (); // ref count = 1, owned
112
 
 
113
 
  EXPECT_THAT(a->GetReferenceCount(), Eq(3));
114
 
  EXPECT_THAT(b->GetReferenceCount(), Eq(1));
115
 
  EXPECT_TRUE(b->OwnsTheReference());
116
 
 
117
 
  EXPECT_FALSE(a->UnReference());
118
 
  EXPECT_THAT(a->GetReferenceCount(), Eq(2));
119
 
  EXPECT_FALSE(a->UnReference());
120
 
  EXPECT_THAT(a->GetReferenceCount(), Eq(1));
121
 
  EXPECT_TRUE(a->UnReference()); // object destroyed
122
 
 
123
 
  EXPECT_TRUE(b->UnReference()); // object destroyed
124
 
}
125
 
 
126
 
TEST(TestObject, TestObjectPtr) {
127
 
 
128
 
  OwnedObject *a = new OwnedObject (NUX_TRACKER_LOCATION);  // ref count = 1, owned
129
 
 
130
 
  nux::ObjectPtr<OwnedObject> object_ptr (a); // ref count = 2
131
 
 
132
 
  EXPECT_THAT(a->GetReferenceCount(), Eq(2));
133
 
  EXPECT_FALSE(a->UnReference()); // ref count = 1
134
 
  EXPECT_THAT(a->GetReferenceCount(), Eq(1));
135
 
 
136
 
  // Calling UnReference repeatedly should not destroy the object when there
137
 
  // are ObjectPtr's hosting it.
138
 
  EXPECT_FALSE(a->UnReference());
139
 
  EXPECT_THAT(a->GetReferenceCount(), Eq(1));
140
 
 
141
 
  object_ptr.Release();
142
 
}
143
 
 
144
 
TEST(TestObject, TestObjectPtrAdopt) {
145
 
 
146
 
  OwnedObject* a = new OwnedObject(NUX_TRACKER_LOCATION);  // ref count = 1, owned
147
 
 
148
 
  nux::ObjectPtr<OwnedObject> object_ptr;
149
 
 
150
 
  object_ptr.Adopt(a);
151
 
 
152
 
  EXPECT_THAT(a->GetReferenceCount(), Eq(1));
153
 
}
154
 
 
155
 
TEST(TestObject, TestObjectPtrGetPointer) {
156
 
 
157
 
  OwnedObject* a = new OwnedObject(NUX_TRACKER_LOCATION);  // ref count = 1, owned
158
 
  nux::ObjectPtr<OwnedObject> object_ptr;
159
 
  object_ptr.Adopt(a);
160
 
 
161
 
  EXPECT_THAT(object_ptr.GetPointer(), Eq(a));
162
 
  // Const too
163
 
  nux::ObjectPtr<OwnedObject> const& object_ptr_ref = object_ptr;
164
 
  EXPECT_THAT(object_ptr_ref.GetPointer(), Eq(a));
165
 
}
166
 
 
167
 
TEST(TestObject, TestObjectPtr1) {
168
 
 
169
 
  ChildOwnedObject *c = new ChildOwnedObject (NUX_TRACKER_LOCATION);  // ref count = 1, owned
170
 
 
171
 
  nux::ObjectPtr<OwnedObject> object_ptr0 (c); // ref count = 2
172
 
 
173
 
  EXPECT_THAT(c->GetReferenceCount(), Eq(2));
174
 
 
175
 
  nux::ObjectPtr<OwnedObject> object_ptr1 (object_ptr0); // ref count = 3
176
 
 
177
 
  EXPECT_THAT(c->GetReferenceCount(), Eq(3));
178
 
 
179
 
  EXPECT_FALSE(c->UnReference()); // ref count = 2
180
 
  EXPECT_FALSE(c->UnReference()); // ref count = 2
181
 
  EXPECT_FALSE(c->UnReference()); // ref count = 2
182
 
 
183
 
  EXPECT_THAT(c->GetReferenceCount(), Eq(2));
184
 
 
185
 
  object_ptr1.Release ();
186
 
 
187
 
  EXPECT_THAT(c->GetReferenceCount(), Eq(1));
188
 
 
189
 
  object_ptr0.Release ();
190
 
}
191
 
 
192
 
TEST(TestObject, TestObjectPtr2) {
193
 
 
194
 
  ChildOwnedObject *c = new ChildOwnedObject(NUX_TRACKER_LOCATION);
195
 
 
196
 
  nux::ObjectPtr<OwnedObject> obj_ptr(c);
197
 
  nux::ObjectWeakPtr<OwnedObject> weak_ptr(obj_ptr);
198
 
 
199
 
  EXPECT_THAT(c->GetReferenceCount(), Eq(2));
200
 
  EXPECT_FALSE(c->UnReference());
201
 
 
202
 
  EXPECT_THAT(c->GetReferenceCount(), Eq(1));
203
 
 
204
 
  // Clearing the smart pointer deletes the object.
205
 
  EXPECT_TRUE(obj_ptr.Release());
206
 
 
207
 
  EXPECT_FALSE(weak_ptr.IsValid());
208
 
  EXPECT_TRUE(weak_ptr.IsNull());
209
 
  EXPECT_FALSE(weak_ptr());
210
 
}
211
 
 
212
 
bool g_signal_called = false;
213
 
 
214
 
void on_destroyed_cb (nux::Object *obj)
215
 
{
216
 
  g_signal_called = true;
217
 
}
218
 
 
219
 
TEST(TestObject, TestObjectSignal) {
220
 
 
221
 
  nux::Object *obj = new nux::Object ();
222
 
  obj->OnDestroyed.connect(sigc::ptr_fun(on_destroyed_cb));
223
 
  EXPECT_FALSE(g_signal_called);
224
 
  obj->UnReference ();
225
 
  EXPECT_TRUE(g_signal_called);
226
 
}
227
 
 
228
 
}