Back to index

unity  6.0.0
test_favorite_store_gsettings.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Canonical Ltd.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU General Public License version 3, as published
00006  * by the  Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranties of
00010  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00011  * PURPOSE.  See the GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * version 3 along with this program.  If not, see
00015  * <http://www.gnu.org/licenses/>
00016  *
00017  * Authored by: Neil Jagdish Patel <neil.patel@canonical.com>
00018  *              Andrea Azzarone <azzaronea@gmail.com>
00019  *
00020  */
00021 
00022 #include <config.h>
00023 
00024 #include <algorithm>
00025 #include <memory>
00026 #include <string>
00027 
00028 #define G_SETTINGS_ENABLE_BACKEND
00029 #include <gio/gsettingsbackend.h>
00030 #include <gmock/gmock.h>
00031 #include <glib.h>
00032 
00033 #include "FavoriteStore.h"
00034 #include "FavoriteStoreGSettings.h"
00035 #include "FavoriteStorePrivate.h"
00036 #include <UnityCore/GLibWrapper.h>
00037 
00038 using namespace unity;
00039 using testing::Eq;
00040 
00041 namespace {
00042 
00043 // Constant
00044 const gchar* SCHEMA_DIRECTORY = BUILDDIR"/settings";
00045 const gchar* BASE_STORE_FILE = BUILDDIR"/settings/test-favorite-store-gsettings.store";
00046 const gchar* BASE_STORE_CONTENTS = "[desktop/unity/launcher]\n" \
00047                                    "favorites=['%s', '%s', '%s']";
00048 
00049 const char* base_store_favs[] = { BUILDDIR"/tests/data/ubuntuone-installer.desktop",
00050                                   BUILDDIR"/tests/data/ubuntu-software-center.desktop",
00051                                   BUILDDIR"/tests/data/update-manager.desktop",
00052                                   NULL
00053                                 };
00054 const int n_base_store_favs = G_N_ELEMENTS(base_store_favs) - 1; /* NULL */
00055 
00056 const std::string other_desktop = BUILDDIR"/tests/data/bzr-handle-patch.desktop";
00057 
00058 // Utilities
00059 std::string const& at(FavoriteList const& favs, int index)
00060 {
00061   FavoriteList::const_iterator iter = favs.begin();
00062   std::advance(iter, index);
00063   return *iter;
00064 }
00065 
00066 bool ends_with(std::string const& value, std::string const& suffix)
00067 {
00068   std::string::size_type pos = value.rfind(suffix);
00069   if (pos == std::string::npos)
00070     return false;
00071   else
00072     return (pos + suffix.size()) == value.size();
00073 }
00074 
00075 // A new one of these is created for each test
00076 class TestFavoriteStoreGSettings : public testing::Test
00077 {
00078 public:
00079   glib::Object<GSettingsBackend> backend;
00080   std::unique_ptr<internal::FavoriteStoreGSettings> setting_singleton_instance;
00081 
00082   virtual void SetUp()
00083   {
00084     // set the data directory so gsettings can find the schema
00085     g_setenv("GSETTINGS_SCHEMA_DIR", SCHEMA_DIRECTORY, false);
00086 
00087     glib::Error error;
00088     glib::String contents(g_strdup_printf(BASE_STORE_CONTENTS,
00089                                           base_store_favs[0],
00090                                           base_store_favs[1],
00091                                           base_store_favs[2]
00092                                           ));
00093 
00094     g_file_set_contents(BASE_STORE_FILE,
00095                         contents.Value(),
00096                         -1,
00097                         error.AsOutParam());
00098 
00099     ASSERT_FALSE(error);
00100 
00101     backend = g_keyfile_settings_backend_new(BASE_STORE_FILE, "/", "root");
00102     setting_singleton_instance.reset(new internal::FavoriteStoreGSettings(backend.RawPtr()));
00103   }
00104 
00105   virtual void TearDown()
00106   {
00107   }
00108 
00109 };
00110 
00111 TEST_F(TestFavoriteStoreGSettings, TestAllocation)
00112 {
00113   EXPECT_TRUE(G_IS_SETTINGS_BACKEND(backend.RawPtr()));
00114 }
00115 
00116 TEST_F(TestFavoriteStoreGSettings, TestGetFavorites)
00117 {
00118   FavoriteStore &settings = FavoriteStore::Instance();
00119   FavoriteList const& favs = settings.GetFavorites();
00120 
00121   ASSERT_EQ(favs.size(), n_base_store_favs);
00122   EXPECT_TRUE(ends_with(at(favs, 0), base_store_favs[0]));
00123   EXPECT_TRUE(ends_with(at(favs, 1), base_store_favs[1]));
00124   EXPECT_TRUE(at(favs, 2) == base_store_favs[2]);
00125 }
00126 
00127 TEST_F(TestFavoriteStoreGSettings, TestAddFavorite)
00128 {
00129   FavoriteStore &settings = FavoriteStore::Instance();
00130 
00131   settings.AddFavorite(other_desktop, 0);
00132   FavoriteList const& favs = settings.GetFavorites();
00133   ASSERT_EQ(favs.size(), n_base_store_favs + 1);
00134   EXPECT_TRUE(other_desktop == at(favs, 0));
00135 }
00136 
00137 TEST_F(TestFavoriteStoreGSettings, TestAddFavoritePosition)
00138 {
00139   FavoriteStore &settings = FavoriteStore::Instance();
00140 
00141   settings.AddFavorite(other_desktop, 2);
00142   FavoriteList const& favs = settings.GetFavorites();
00143   ASSERT_EQ(favs.size(), n_base_store_favs + 1);
00144   EXPECT_TRUE(other_desktop == at(favs, 2));
00145 }
00146 
00147 TEST_F(TestFavoriteStoreGSettings,TestAddFavoriteLast)
00148 {
00149   FavoriteStore &settings = FavoriteStore::Instance();
00150 
00151   settings.AddFavorite(other_desktop, -1);
00152   FavoriteList const& favs = settings.GetFavorites();
00153   ASSERT_EQ(favs.size(), n_base_store_favs + 1);
00154   EXPECT_TRUE(other_desktop == favs.back());
00155 }
00156 
00157 TEST_F(TestFavoriteStoreGSettings,TestAddFavoriteOutOfRange)
00158 {
00159   FavoriteStore &settings = FavoriteStore::Instance();
00160 
00161   FavoriteList const& favs = settings.GetFavorites();
00162   settings.AddFavorite(other_desktop, n_base_store_favs + 1);
00163   // size didn't change
00164   ASSERT_EQ(favs.size(), n_base_store_favs);
00165   // didn't get inserted
00166   FavoriteList::const_iterator iter = std::find(favs.begin(), favs.end(), other_desktop);
00167   EXPECT_EQ(iter, favs.end());
00168 }
00169 
00170 TEST_F(TestFavoriteStoreGSettings, TestRemoveFavorite)
00171 {
00172   FavoriteStore &settings = FavoriteStore::Instance();
00173 
00174   FavoriteList const& favs = settings.GetFavorites();
00175   settings.RemoveFavorite(at(favs, 0));
00176 
00177   ASSERT_EQ(favs.size(), n_base_store_favs - 1);
00178   EXPECT_TRUE(ends_with(at(favs, 0), base_store_favs[1]));
00179 
00180   settings.RemoveFavorite(at(favs, 1));
00181   ASSERT_EQ(favs.size(), n_base_store_favs - 2);
00182   EXPECT_TRUE(ends_with(at(favs, 0), base_store_favs[1]));
00183 }
00184 
00185 TEST_F(TestFavoriteStoreGSettings, TestRemoveFavoriteBad)
00186 {
00187   FavoriteStore &settings = FavoriteStore::Instance();
00188 
00189   FavoriteList const& favs = settings.GetFavorites();
00190   settings.RemoveFavorite("");
00191   EXPECT_EQ(favs.size(), n_base_store_favs);
00192 
00193   settings.RemoveFavorite("foo.desktop");
00194   EXPECT_EQ(favs.size(), n_base_store_favs);
00195 
00196   settings.RemoveFavorite("/this/desktop/doesnt/exist/hopefully.desktop");
00197   EXPECT_EQ(favs.size(), n_base_store_favs);
00198 }
00199 
00200 TEST_F(TestFavoriteStoreGSettings, TestMoveFavorite)
00201 {
00202   FavoriteStore &settings = FavoriteStore::Instance();
00203 
00204   FavoriteList const& favs = settings.GetFavorites();
00205 
00206   settings.MoveFavorite(base_store_favs[2], 0);
00207 
00208   ASSERT_EQ(favs.size(), n_base_store_favs);
00209   EXPECT_EQ(at(favs, 0), base_store_favs[2]);
00210   EXPECT_TRUE(ends_with(at(favs, 1), base_store_favs[0]));
00211   EXPECT_TRUE(ends_with(at(favs, 2), base_store_favs[1]));
00212 }
00213 
00214 TEST_F(TestFavoriteStoreGSettings, TestMoveFavoriteBad)
00215 {
00216   FavoriteStore &settings = FavoriteStore::Instance();
00217 
00218   FavoriteList const& favs = settings.GetFavorites();
00219 
00220   settings.MoveFavorite("", 0);
00221   settings.MoveFavorite(at(favs, 0), 100);
00222 
00223   ASSERT_EQ(favs.size(), n_base_store_favs);
00224   EXPECT_TRUE(ends_with(at(favs, 0), base_store_favs[0]));
00225   EXPECT_TRUE(ends_with(at(favs, 1), base_store_favs[1]));
00226   EXPECT_EQ(at(favs, 2), base_store_favs[2]);
00227 }
00228 
00229 TEST_F(TestFavoriteStoreGSettings, TestFavoriteAddedSignalFirst)
00230 {
00231   FavoriteStore &settings = FavoriteStore::Instance();
00232   bool signal_received = false;
00233   std::string position;
00234   bool before = false;
00235 
00236   settings.favorite_added.connect([&](std::string const& path, std::string const& pos, bool bef)
00237   {
00238     signal_received = true;
00239     position = pos;
00240     before = bef;
00241   });
00242 
00243   FavoriteList favs;
00244   favs.push_back(other_desktop);
00245   favs.push_back(base_store_favs[0]);
00246   favs.push_back(base_store_favs[1]);
00247   favs.push_back(base_store_favs[2]);
00248   setting_singleton_instance->SaveFavorites(favs, false);
00249 
00250   sleep(1);
00251 
00252   ASSERT_TRUE(signal_received);
00253   EXPECT_EQ(position, base_store_favs[0]);
00254   EXPECT_TRUE(before);
00255 }
00256 
00257 TEST_F(TestFavoriteStoreGSettings, TestFavoriteAddedSignalMiddle)
00258 {
00259   FavoriteStore &settings = FavoriteStore::Instance();
00260   bool signal_received = false;
00261   std::string position;
00262   bool before = true;
00263 
00264   settings.favorite_added.connect([&](std::string const& path, std::string const& pos, bool bef)
00265   {
00266     signal_received = true;
00267     position = pos;
00268     before = bef;
00269   });
00270 
00271   FavoriteList favs;
00272   favs.push_back(base_store_favs[0]);
00273   favs.push_back(base_store_favs[1]);
00274   favs.push_back(other_desktop);
00275   favs.push_back(base_store_favs[2]);
00276   setting_singleton_instance->SaveFavorites(favs, false);
00277 
00278   sleep(1);
00279 
00280   ASSERT_TRUE(signal_received);
00281   EXPECT_EQ(position, base_store_favs[1]);
00282   EXPECT_FALSE(before);
00283 }
00284 
00285 TEST_F(TestFavoriteStoreGSettings, TestFavoriteAddedSignalEnd)
00286 {
00287   FavoriteStore &settings = FavoriteStore::Instance();
00288   bool signal_received = false;
00289   std::string position;
00290   bool before = true;
00291 
00292   settings.favorite_added.connect([&](std::string const& path, std::string const& pos, bool bef)
00293   {
00294     signal_received = true;
00295     position = pos;
00296     before = bef;
00297   });
00298 
00299   FavoriteList favs;
00300   favs.push_back(base_store_favs[0]);
00301   favs.push_back(base_store_favs[1]);
00302   favs.push_back(base_store_favs[2]);
00303   favs.push_back(other_desktop);
00304   setting_singleton_instance->SaveFavorites(favs, false);
00305 
00306   sleep(1);
00307 
00308   ASSERT_TRUE(signal_received);
00309   EXPECT_EQ(position, base_store_favs[2]);
00310   EXPECT_FALSE(before);
00311 }
00312 
00313 TEST_F(TestFavoriteStoreGSettings, TestFavoriteAddedSignalEmpty)
00314 {
00315   FavoriteStore &settings = FavoriteStore::Instance();
00316   bool signal_received = false;
00317   std::string position;
00318   bool before = false;
00319 
00320   settings.favorite_added.connect([&](std::string const& path, std::string const& pos, bool bef)
00321   {
00322     signal_received = true;
00323     position = pos;
00324     before = bef;
00325   });
00326 
00327   FavoriteList favs;
00328   favs.push_back(other_desktop);
00329   setting_singleton_instance->SaveFavorites(favs, false);
00330 
00331   sleep(1);
00332 
00333   ASSERT_TRUE(signal_received);
00334   EXPECT_EQ(position, "");
00335   EXPECT_TRUE(before);
00336 }
00337 
00338 TEST_F(TestFavoriteStoreGSettings, TestFavoriteRemoved)
00339 {
00340   FavoriteStore &settings = FavoriteStore::Instance();
00341   bool signal_received = false;
00342   std::string path_removed;
00343 
00344   settings.favorite_removed.connect([&](std::string const& path)
00345   {
00346     signal_received = true;
00347     path_removed = path;
00348   });
00349 
00350   FavoriteList favs;
00351   favs.push_back(base_store_favs[0]);
00352   favs.push_back(base_store_favs[2]);
00353   setting_singleton_instance->SaveFavorites(favs, false);
00354 
00355   sleep(1);
00356 
00357   ASSERT_TRUE(signal_received);
00358   EXPECT_EQ(path_removed, base_store_favs[1]);
00359 }
00360 
00361 TEST_F(TestFavoriteStoreGSettings, TestFavoriteReordered)
00362 {
00363   FavoriteStore &settings = FavoriteStore::Instance();
00364   bool signal_received = false;
00365 
00366   settings.reordered.connect([&]()
00367   {
00368     signal_received = true;
00369   });
00370 
00371   FavoriteList favs;
00372   favs.push_back(base_store_favs[0]);
00373   favs.push_back(base_store_favs[2]);
00374   favs.push_back(base_store_favs[1]);
00375   setting_singleton_instance->SaveFavorites(favs, false);
00376 
00377   sleep(1);
00378 
00379   ASSERT_TRUE(signal_received);
00380 
00381   signal_received = false;
00382   favs.push_back(base_store_favs[0]);
00383   favs.push_back(base_store_favs[2]);
00384   favs.push_back(base_store_favs[1]);
00385   setting_singleton_instance->SaveFavorites(favs, false);
00386 
00387   sleep(1);
00388 
00389   ASSERT_FALSE(signal_received);
00390 }
00391 
00392 TEST_F(TestFavoriteStoreGSettings, TestFavoriteSignalsMixed1)
00393 {
00394   FavoriteStore &settings = FavoriteStore::Instance();
00395   bool added_received = false;
00396   bool removed_received = false;
00397   bool reordered_received = false;
00398 
00399   settings.favorite_added.connect([&](std::string const& path, std::string const& pos, bool bef)
00400   {
00401     added_received = true;
00402   });
00403 
00404   settings.favorite_removed.connect([&](std::string const& path)
00405   {
00406     removed_received = true;
00407   });
00408 
00409   settings.reordered.connect([&]()
00410   {
00411     reordered_received = true;
00412   });
00413 
00414   FavoriteList favs;
00415   favs.push_back(base_store_favs[0]);
00416   favs.push_back(base_store_favs[1]);
00417   favs.push_back(other_desktop);
00418   setting_singleton_instance->SaveFavorites(favs, false);
00419 
00420   sleep(1);
00421 
00422   EXPECT_TRUE(added_received);
00423   EXPECT_TRUE(removed_received);
00424   EXPECT_FALSE(reordered_received);
00425 }
00426 
00427 TEST_F(TestFavoriteStoreGSettings, TestFavoriteSignalsMixed2)
00428 {
00429   FavoriteStore &settings = FavoriteStore::Instance();
00430   bool added_received = false;
00431   bool removed_received = false;
00432   bool reordered_received = false;
00433 
00434   settings.favorite_added.connect([&](std::string const& path, std::string const& pos, bool bef)
00435   {
00436     added_received = true;
00437   });
00438 
00439   settings.favorite_removed.connect([&](std::string const& path)
00440   {
00441     removed_received = true;
00442   });
00443 
00444   settings.reordered.connect([&]()
00445   {
00446     reordered_received = true;
00447   });
00448 
00449   FavoriteList favs;
00450   favs.push_back(base_store_favs[1]);
00451   favs.push_back(other_desktop);
00452   favs.push_back(base_store_favs[0]);
00453   setting_singleton_instance->SaveFavorites(favs, false);
00454 
00455   sleep(1);
00456 
00457   EXPECT_TRUE(added_received);
00458   EXPECT_TRUE(removed_received);
00459   EXPECT_TRUE(reordered_received);
00460 }
00461 
00462 TEST_F(TestFavoriteStoreGSettings, TestFavoriteSignalsMixed3)
00463 {
00464   FavoriteStore &settings = FavoriteStore::Instance();
00465   bool added_received = false;
00466   bool removed_received = false;
00467   bool reordered_received = false;
00468 
00469   settings.favorite_added.connect([&](std::string const& path, std::string const& pos, bool bef)
00470   {
00471     added_received = true;
00472   });
00473 
00474   settings.favorite_removed.connect([&](std::string const& path)
00475   {
00476     removed_received = true;
00477   });
00478 
00479   settings.reordered.connect([&]()
00480   {
00481     reordered_received = true;
00482   });
00483 
00484   FavoriteList favs;
00485   favs.push_back(base_store_favs[1]);
00486   favs.push_back(base_store_favs[0]);
00487   setting_singleton_instance->SaveFavorites(favs, false);
00488 
00489   sleep(1);
00490 
00491   EXPECT_FALSE(added_received);
00492   EXPECT_TRUE(removed_received);
00493   EXPECT_TRUE(reordered_received);
00494 }
00495 
00496 } // anonymous namespace