Back to index

unity  6.0.0
test_glib_object.cpp
Go to the documentation of this file.
00001 // -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
00002 /*
00003  * Copyright (C) 2011 Canonical Ltd
00004  *
00005  * This program is free software: you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 3 as
00007  * published by the Free Software Foundation.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  *
00017  * Authored by: Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
00018  */
00019 
00020 #include <list>
00021 #include <algorithm>
00022 #include <gmock/gmock.h>
00023 #include <UnityCore/GLibWrapper.h>
00024 
00025 #include "test_glib_object_utils.h"
00026 
00027 using namespace std;
00028 using namespace testing;
00029 using namespace unity;
00030 using namespace unity::glib;
00031 
00032 namespace
00033 {
00034 typedef glib::Object<TestGObject> TestObjectWrapper;
00035 
00036 bool IsGOBject(TestGObject* t_obj)
00037 {
00038   return G_IS_OBJECT(t_obj);
00039 }
00040 
00041 bool IsGOBject(TestObjectWrapper const& g_obj)
00042 {
00043   return IsGOBject(g_obj.RawPtr());
00044 }
00045 
00046 bool IsNotGOBject(TestGObject* t_obj)
00047 {
00048   return !IsGOBject(t_obj);
00049 }
00050 
00051 bool IsNotGOBject(TestObjectWrapper const& g_obj)
00052 {
00053   return !IsGOBject(g_obj);
00054 }
00055 
00056 unsigned int RefCount(TestGObject* t_obj)
00057 {
00058   return G_OBJECT(t_obj)->ref_count;
00059 }
00060 
00061 unsigned int RefCount(TestObjectWrapper const& g_obj)
00062 {
00063   return RefCount(g_obj.RawPtr());
00064 }
00065 
00066 bool RefCountIs(TestObjectWrapper const& g_obj, unsigned int expected_ref)
00067 {
00068   return (RefCount(g_obj) == expected_ref);
00069 }
00070 
00071 bool RefCountIs(TestGObject* t_obj, unsigned int expected_ref)
00072 {
00073   return (RefCount(t_obj) == expected_ref);
00074 }
00075 
00076 TEST(TestGLibObject, ConstructEmpty)
00077 {
00078   TestObjectWrapper empty_obj;
00079   ASSERT_THAT(empty_obj.RawPtr(), IsNull());
00080   EXPECT_TRUE(IsNotGOBject(empty_obj));
00081 }
00082 
00083 TEST(TestGLibObject, ConstructValidObject)
00084 {
00085   TestGObject *t_obj = test_gobject_new();
00086   TestObjectWrapper g_obj(t_obj);
00087   ASSERT_THAT(t_obj, NotNull());
00088 
00089   EXPECT_EQ(g_obj.RawPtr(), t_obj);
00090 }
00091 
00092 TEST(TestGLibObject, ConstructDoubleRef)
00093 {
00094   TestGObject *t_obj = test_gobject_new();
00095   {
00096     TestObjectWrapper g_obj_double_ref(t_obj, AddRef());
00097     EXPECT_EQ(g_obj_double_ref.RawPtr(), t_obj);
00098     EXPECT_TRUE(RefCountIs(g_obj_double_ref, 2));
00099   }
00100   EXPECT_TRUE(RefCountIs(t_obj, 1));
00101   g_object_unref(G_OBJECT(t_obj));
00102 }
00103 
00104 TEST(TestGLibObject, ConstructInitialize)
00105 {
00106   TestGObject *t_obj = test_gobject_new();
00107   ASSERT_THAT(t_obj, NotNull());
00108 
00109   TestObjectWrapper g_obj1(t_obj);
00110   EXPECT_EQ(g_obj1.RawPtr(), t_obj);
00111 }
00112 
00113 TEST(TestGLibObject, ConstructCopy)
00114 {
00115   TestGObject *t_obj = test_gobject_new();
00116   TestObjectWrapper g_obj1(t_obj);
00117   EXPECT_TRUE(RefCountIs(t_obj, 1));
00118 
00119   TestObjectWrapper g_obj2(g_obj1);
00120   EXPECT_TRUE(RefCountIs(t_obj, 2));
00121 
00122   EXPECT_EQ(g_obj1.RawPtr(), g_obj2.RawPtr());
00123 }
00124 
00125 TEST(TestGLibObject, AssignmentOperators)
00126 {
00127   TestGObject *t_obj = test_gobject_new();
00128 
00129   {
00130     TestObjectWrapper g_obj1;
00131     TestObjectWrapper g_obj2;
00132 
00133     g_obj1 = t_obj;
00134     EXPECT_EQ(g_obj1.RawPtr(), t_obj);
00135     EXPECT_TRUE(RefCountIs(t_obj, 1));
00136 
00137     g_obj1 = g_obj1;
00138     EXPECT_TRUE(RefCountIs(t_obj, 1));
00139 
00140     g_obj2 = g_obj1;
00141     EXPECT_EQ(g_obj1.RawPtr(), g_obj2.RawPtr());
00142     EXPECT_TRUE(RefCountIs(t_obj, 2));
00143   }
00144 
00145   EXPECT_FALSE(IsGOBject(t_obj));
00146 }
00147 
00148 TEST(TestGLibObject, AssignmentOperatorOnEqualObject)
00149 {
00150   TestGObject *t_obj = test_gobject_new();
00151   TestObjectWrapper g_obj1(t_obj);
00152   TestObjectWrapper g_obj2(t_obj, AddRef());
00153   EXPECT_EQ(g_obj1.RawPtr(), g_obj2.RawPtr());
00154 
00155   g_obj1 = g_obj2;
00156   EXPECT_EQ(g_obj1.RawPtr(), g_obj2.RawPtr());
00157   EXPECT_TRUE(RefCountIs(g_obj1, 2));
00158 }
00159 
00160 TEST(TestGLibObject, EqualityOperators)
00161 {
00162   TestGObject *t_obj = test_gobject_new();
00163   TestObjectWrapper g_obj1;
00164   TestObjectWrapper g_obj2;
00165 
00166   // self equality checks
00167   EXPECT_TRUE(g_obj1 == g_obj1);
00168   EXPECT_FALSE(g_obj1 != g_obj1);
00169   EXPECT_TRUE(g_obj1 == g_obj2);
00170   EXPECT_FALSE(g_obj1 != g_obj2);
00171 
00172   g_obj1 = t_obj;
00173   EXPECT_TRUE(RefCountIs(g_obj1, 1));
00174 
00175   // Ref is needed here, since the t_obj reference is already owned by g_obj1
00176   g_obj2 = TestObjectWrapper(t_obj, AddRef());
00177 
00178   // other object equality checks
00179   EXPECT_TRUE(g_obj1 == t_obj);
00180   EXPECT_TRUE(t_obj == g_obj1);
00181   EXPECT_TRUE(g_obj1 == g_obj1);
00182   EXPECT_TRUE(g_obj1 == g_obj2);
00183   EXPECT_FALSE(g_obj1 != t_obj);
00184   EXPECT_FALSE(t_obj != g_obj1);
00185   EXPECT_FALSE(g_obj1 != g_obj1);
00186   EXPECT_FALSE(g_obj1 != g_obj2);
00187 
00188   g_obj2 = test_gobject_new();
00189   EXPECT_FALSE(g_obj1 == g_obj2);
00190   EXPECT_TRUE(g_obj1 != g_obj2);
00191 }
00192 
00193 TEST(TestGLibObject, CastOperator)
00194 {
00195   TestGObject *t_obj = test_gobject_new();
00196   EXPECT_TRUE(t_obj == (TestGObject*) TestObjectWrapper(t_obj));
00197 }
00198 
00199 TEST(TestGLibObject, CastObject)
00200 {
00201   TestObjectWrapper gt_obj(test_gobject_new());
00202 
00203   TestGObject* cast_copy = glib::object_cast<TestGObject>(gt_obj);
00204   EXPECT_EQ(cast_copy, gt_obj.RawPtr());
00205 
00206   Object<GObject> g_obj = glib::object_cast<GObject>(gt_obj);
00207   EXPECT_EQ(g_obj->ref_count, 2);
00208 
00209   g_object_set_data(g_obj, "TestData", GINT_TO_POINTER(55));
00210   EXPECT_EQ(GPOINTER_TO_INT(g_object_get_data(g_obj, "TestData")), 55);
00211 }
00212 
00213 TEST(TestGLibObject, BoolOperator)
00214 {
00215   TestObjectWrapper g_obj;
00216   EXPECT_FALSE(g_obj);
00217 
00218   g_obj = test_gobject_new();
00219   EXPECT_TRUE(g_obj);
00220 
00221   g_obj = nullptr;
00222   EXPECT_FALSE(g_obj);
00223 }
00224 
00225 TEST(TestGLibObject, AccessToMembers)
00226 {
00227   TestObjectWrapper g_obj(test_gobject_new());
00228   g_obj->public_value = 1234567890;
00229   EXPECT_EQ(g_obj->public_value, 1234567890);
00230   EXPECT_EQ(test_gobject_get_public_value(g_obj), 1234567890);
00231 
00232   test_gobject_set_public_value(g_obj, 987654321);
00233   EXPECT_EQ(test_gobject_get_public_value(g_obj), 987654321);
00234 }
00235 
00236 TEST(TestGLibObject, UseFunction)
00237 {
00238   TestObjectWrapper g_obj(test_gobject_new());
00239 
00240   EXPECT_NE(test_gobject_get_private_value(g_obj), 0);
00241 
00242   test_gobject_set_private_value(g_obj, 1234567890);
00243   EXPECT_EQ(test_gobject_get_private_value(g_obj), 1234567890);
00244 }
00245 
00246 TEST(TestGLibObject, ReleaseObject)
00247 {
00248   TestGObject *t_obj = test_gobject_new();
00249   TestObjectWrapper g_obj(t_obj);
00250   ASSERT_THAT(t_obj, NotNull());
00251 
00252   // Release() doesn't unref the object.
00253   g_obj.Release();
00254   EXPECT_EQ(g_obj, 0);
00255   EXPECT_EQ(RefCount(t_obj), 1);
00256 
00257   g_object_unref(t_obj);
00258 }
00259 
00260 TEST(TestGLibObject, SwapObjects)
00261 {
00262   TestGObject *t_obj1 = test_gobject_new();
00263   TestGObject *t_obj2 = test_gobject_new();
00264 
00265   {
00266     TestObjectWrapper g_obj1(t_obj1);
00267     EXPECT_EQ(g_obj1, t_obj1);
00268 
00269     TestObjectWrapper g_obj2(t_obj2);
00270     EXPECT_EQ(g_obj2, t_obj2);
00271 
00272     std::swap(g_obj1, g_obj2);
00273 
00274     EXPECT_EQ(g_obj1, t_obj2);
00275     EXPECT_EQ(g_obj2, t_obj1);
00276 
00277     g_obj1.swap(g_obj2);
00278 
00279     EXPECT_EQ(g_obj1, t_obj1);
00280     EXPECT_EQ(g_obj2, t_obj2);
00281 
00282     EXPECT_EQ(RefCount(g_obj1), 1);
00283     EXPECT_EQ(RefCount(g_obj2), 1);
00284   }
00285 
00286   EXPECT_FALSE(G_IS_OBJECT(t_obj1));
00287   EXPECT_FALSE(G_IS_OBJECT(t_obj2));
00288 }
00289 
00290 TEST(TestGLibObject, ListOperations)
00291 {
00292   list<TestObjectWrapper> obj_list;
00293   TestGObject *t_obj1 = test_gobject_new();
00294 
00295   TestObjectWrapper g_obj1(t_obj1);
00296   TestObjectWrapper g_obj2(test_gobject_new());
00297   TestObjectWrapper g_obj3(test_gobject_new());
00298   TestObjectWrapper g_obj4;
00299   TestObjectWrapper g_obj5;
00300 
00301   EXPECT_EQ(RefCount(g_obj1), 1);
00302 
00303   obj_list.push_back(g_obj1);
00304   obj_list.push_back(g_obj2);
00305   obj_list.push_back(g_obj3);
00306   obj_list.push_back(g_obj4);
00307   obj_list.push_back(g_obj5);
00308   EXPECT_EQ(obj_list.size(), 5);
00309 
00310   EXPECT_EQ(RefCount(g_obj1), 2);
00311 
00312   obj_list.remove(g_obj2);
00313   EXPECT_EQ(obj_list.size(), 4);
00314 
00315   EXPECT_TRUE(std::find(obj_list.begin(), obj_list.end(), g_obj3) != obj_list.end());
00316   EXPECT_TRUE(std::find(obj_list.begin(), obj_list.end(), g_obj3.RawPtr()) != obj_list.end());
00317 
00318   obj_list.remove(TestObjectWrapper(t_obj1, AddRef()));
00319   EXPECT_EQ(obj_list.size(), 3);
00320 
00321   EXPECT_TRUE(std::find(obj_list.begin(), obj_list.end(), g_obj4) != obj_list.end());
00322   obj_list.remove(g_obj5);
00323   EXPECT_EQ(obj_list.size(), 1);
00324 }
00325 
00326 } // Namespace