Back to index

unity  6.0.0
test_model.cpp
Go to the documentation of this file.
00001 #include <gmock/gmock.h>
00002 #include <glib-object.h>
00003 
00004 #include <UnityCore/GLibWrapper.h>
00005 #include <UnityCore/Model.h>
00006 
00007 #include "test_utils.h"
00008 
00009 using namespace std;
00010 using namespace unity::dash;
00011 using namespace testing;
00012 
00013 namespace
00014 {
00015 
00016 static const string swarm_name = "com.canonical.test.model";
00017 static const unsigned int n_rows = 100;
00018 
00019 class TestAdaptor : public RowAdaptorBase
00020 {
00021 public:
00022   TestAdaptor(DeeModel* model, DeeModelIter* iter, DeeModelTag* tag)
00023     : RowAdaptorBase(model, iter, tag)
00024   {
00025   }
00026 
00027   unsigned int index()
00028   {
00029     return GetUIntAt(0);
00030   }
00031 
00032   string name()
00033   {
00034     return GetStringAt(1);
00035   }
00036 };
00037 
00038 static void WaitForSynchronize(Model<TestAdaptor>& model)
00039 {
00040   ::Utils::WaitForModelSynchronize<TestAdaptor>(model, n_rows);
00041 }
00042 
00043 TEST(TestModel, TestConstruction)
00044 {
00045   Model<TestAdaptor> model;
00046   model.swarm_name = swarm_name;
00047   ::Utils::WaitForModelSynchronize<TestAdaptor>(model, n_rows);
00048 }
00049 
00050 TEST(TestModel, TestSynchronization)
00051 {
00052   Model<TestAdaptor> model;
00053   model.swarm_name = swarm_name;
00054 
00055   WaitForSynchronize(model);
00056   EXPECT_EQ(model.count, n_rows);
00057 }
00058 
00059 TEST(TestModel, TestRowsValid)
00060 {
00061   Model<TestAdaptor> model;
00062   model.swarm_name = swarm_name;
00063 
00064   WaitForSynchronize(model);
00065 
00066   for (unsigned int i = 0; i < n_rows; i++)
00067   {
00068     TestAdaptor adaptor = model.RowAtIndex(i);
00069 
00070     EXPECT_EQ(adaptor.index(), i);
00071     unity::glib::String tmp(g_strdup_printf("Test%u", i));
00072     EXPECT_EQ(adaptor.name(), tmp.Str());
00073   }
00074 }
00075 
00076 // We're testing the model's ability to store and retrieve random pointers
00077 TEST(TestModel, TestSetGetRenderer)
00078 {
00079   Model<TestAdaptor> model;
00080   model.swarm_name = swarm_name;
00081 
00082   WaitForSynchronize(model);
00083 
00084   for (unsigned int i = 0; i < n_rows; i++)
00085   {
00086     TestAdaptor adaptor = model.RowAtIndex(i);
00087 
00088     char* value = g_strdup_printf("Renderer%d", i);
00089     adaptor.set_renderer<char*>(value);
00090   }
00091 
00092   for (unsigned int i = 0; i < n_rows; i++)
00093   {
00094     TestAdaptor adaptor = model.RowAtIndex(i);
00095 
00096     unity::glib::String value(adaptor.renderer<char*>());
00097     unity::glib::String renderer(g_strdup_printf("Renderer%d", i));
00098 
00099     EXPECT_EQ(value.Str(), renderer.Str());
00100   }
00101 }
00102 
00103 
00104 
00105 void discard_g_log_calls(const gchar* log_domain,
00106                          GLogLevelFlags log_level,
00107                          const gchar* message,
00108                          gpointer user_data)
00109 {
00110   // do nothing for the messages.
00111 }
00112 
00113 class TestRowAdapterBase : public Test
00114 {
00115 public:
00116   void SetUp() {
00117     logger_ = g_log_set_default_handler(discard_g_log_calls, NULL);
00118   }
00119   void TearDown() {
00120     g_log_set_default_handler(logger_, NULL);
00121   }
00122 private:
00123   GLogFunc logger_;
00124 };
00125 
00126 TEST_F(TestRowAdapterBase, TestGetStringNull)
00127 {
00128   DeeModel* model = dee_sequence_model_new();
00129   dee_model_set_schema(model, "i", NULL);
00130   // Add in zero to an int field
00131   DeeModelIter* iter = dee_model_append(model, 0);
00132 
00133   RowAdaptorBase row(model, iter);
00134 
00135   // Check that the method we call is null.
00136   const gchar* value = dee_model_get_string(model, iter, 0);
00137   ASSERT_THAT(value, IsNull());
00138   ASSERT_THAT(model, NotNull());
00139   ASSERT_THAT(iter, NotNull());
00140   ASSERT_THAT(row.GetStringAt(0), Eq(""));
00141 }
00142 
00143 TEST_F(TestRowAdapterBase, TestGetStringEmpty)
00144 {
00145   DeeModel* model = dee_sequence_model_new();
00146   dee_model_set_schema(model, "s", NULL);
00147   // Add on a empty string.
00148   DeeModelIter* iter = dee_model_append(model, "");
00149 
00150   RowAdaptorBase row(model, iter);
00151 
00152   ASSERT_THAT(model, NotNull());
00153   ASSERT_THAT(iter, NotNull());
00154   ASSERT_THAT(row.GetStringAt(0), Eq(""));
00155 }
00156 
00157 TEST_F(TestRowAdapterBase, TestGetStringValue)
00158 {
00159   DeeModel* model = dee_sequence_model_new();
00160   dee_model_set_schema(model, "s", NULL);
00161   // Add on a real string.
00162   DeeModelIter* iter = dee_model_append(model, "Hello");
00163 
00164   RowAdaptorBase row(model, iter);
00165 
00166   ASSERT_THAT(model, NotNull());
00167   ASSERT_THAT(iter, NotNull());
00168   ASSERT_THAT(row.GetStringAt(0), Eq("Hello"));
00169 }
00170 
00171 }