Back to index

unity  6.0.0
test_desktop_utilities.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2012 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: Marco Trevisan (TreviƱo) <3v1n0@ubuntu.com>
00018  *              Tim Penhey <tim.penhey@canonical.com>
00019  *              Andrea Azzarone <azzarone@gmail.com>
00020  */
00021 
00022 #include <config.h>
00023 
00024 #include <UnityCore/DesktopUtilities.h>
00025 
00026 #include <glib.h>
00027 #include <gmock/gmock.h>
00028 
00029 using namespace unity;
00030 using testing::Eq;
00031 
00032 namespace {
00033   
00034 TEST(TestDesktopUtilitiesDesktopID, TestEmptyValues)
00035 {
00036   std::vector<std::string> empty_list;
00037 
00038   EXPECT_THAT(DesktopUtilities::GetDesktopID(empty_list, "/some/path/to.desktop"),
00039               Eq("/some/path/to.desktop"));
00040   EXPECT_THAT(DesktopUtilities::GetDesktopID(empty_list, "/some/path/to.desktop"),
00041               Eq("/some/path/to.desktop"));
00042 
00043   std::vector<std::string> empty_path_list;
00044   empty_path_list.push_back("");
00045 
00046   EXPECT_THAT(DesktopUtilities::GetDesktopID(empty_path_list, "/some/path/to.desktop"),
00047               Eq("/some/path/to.desktop"));
00048 }
00049 
00050 TEST(TestDesktopUtilitiesDesktopID, TestPathNeedsApplications)
00051 {
00052   std::vector<std::string> dirs;
00053 
00054   dirs.push_back("/this/path");
00055   dirs.push_back("/that/path/");
00056 
00057   EXPECT_THAT(DesktopUtilities::GetDesktopID(dirs, "/this/path/to.desktop"),
00058               Eq("/this/path/to.desktop"));
00059   EXPECT_THAT(DesktopUtilities::GetDesktopID(dirs, "/that/path/to.desktop"),
00060               Eq("/that/path/to.desktop"));
00061 }
00062 
00063 TEST(TestDesktopUtilitiesDesktopID, TestStripsPath)
00064 {
00065   std::vector<std::string> dirs;
00066 
00067   dirs.push_back("/this/path");
00068   dirs.push_back("/that/path/");
00069 
00070   EXPECT_THAT(DesktopUtilities::GetDesktopID(dirs, "/this/path/applications/to.desktop"),
00071               Eq("to.desktop"));
00072   EXPECT_THAT(DesktopUtilities::GetDesktopID(dirs, "/that/path/applications/to.desktop"),
00073               Eq("to.desktop"));
00074   EXPECT_THAT(DesktopUtilities::GetDesktopID(dirs, "/some/path/applications/to.desktop"),
00075               Eq("/some/path/applications/to.desktop"));
00076 }
00077 
00078 TEST(TestDesktopUtilitiesDesktopID, TestSubdirectory)
00079 {
00080   std::vector<std::string> dirs;
00081 
00082   dirs.push_back("/this/path");
00083   dirs.push_back("/that/path/");
00084 
00085   EXPECT_THAT(DesktopUtilities::GetDesktopID(dirs, "/this/path/applications/subdir/to.desktop"),
00086               Eq("subdir-to.desktop"));
00087   EXPECT_THAT(DesktopUtilities::GetDesktopID(dirs, "/that/path/applications/subdir/to.desktop"),
00088               Eq("subdir-to.desktop"));
00089   EXPECT_THAT(DesktopUtilities::GetDesktopID(dirs, "/this/path/applications/subdir1/subdir2/to.desktop"),
00090               Eq("subdir1-subdir2-to.desktop"));
00091 }
00092 
00093 TEST(TestDesktopUtilitiesDataDirectories, TestGetUserDataDirectory)
00094 {
00095   const gchar* old_home = g_getenv("XDG_DATA_HOME");
00096   g_setenv("XDG_DATA_HOME", "UnityUserConfig", TRUE);
00097 
00098   std::string const& user_data_dir = DesktopUtilities::GetUserDataDirectory();
00099 
00100   g_setenv("XDG_DATA_HOME", old_home, TRUE);
00101 
00102   EXPECT_THAT(user_data_dir, Eq("UnityUserConfig"));
00103 }
00104 
00105 TEST(TestDesktopUtilitiesDataDirectories, TestGetSystemDataDirectory)
00106 {
00107   const gchar* old_dirs = g_getenv("XDG_DATA_DIRS");
00108   g_setenv("XDG_DATA_DIRS", "dir1:dir2::dir3:dir4", TRUE);
00109 
00110   std::vector<std::string> const& system_dirs = DesktopUtilities::GetSystemDataDirectories();
00111 
00112   g_setenv("XDG_DATA_DIRS", old_dirs, TRUE);
00113 
00114   ASSERT_THAT(system_dirs.size(), Eq(4));
00115   EXPECT_THAT(system_dirs[0], Eq("dir1"));
00116   EXPECT_THAT(system_dirs[1], Eq("dir2"));
00117   EXPECT_THAT(system_dirs[2], Eq("dir3"));
00118   EXPECT_THAT(system_dirs[3], Eq("dir4"));
00119 }
00120 
00121 TEST(TestDesktopUtilitiesDataDirectories, TestGetDataDirectory)
00122 {
00123   const gchar* old_dirs = g_getenv("XDG_DATA_DIRS");
00124   g_setenv("XDG_DATA_DIRS", "dir1:dir2::dir3:dir4", TRUE);
00125   const gchar* old_home = g_getenv("XDG_DATA_HOME");
00126   g_setenv("XDG_DATA_HOME", "UnityUserConfig", TRUE);
00127 
00128   std::vector<std::string> const& data_dirs = DesktopUtilities::GetDataDirectories();
00129 
00130   g_setenv("XDG_DATA_DIRS", old_dirs, TRUE);
00131   g_setenv("XDG_DATA_HOME", old_home, TRUE);
00132 
00133   ASSERT_THAT(data_dirs.size(), Eq(5));
00134   EXPECT_THAT(data_dirs[0], Eq("dir1"));
00135   EXPECT_THAT(data_dirs[1], Eq("dir2"));
00136   EXPECT_THAT(data_dirs[2], Eq("dir3"));
00137   EXPECT_THAT(data_dirs[3], Eq("dir4"));
00138   EXPECT_THAT(data_dirs[4], Eq("UnityUserConfig"));
00139 }
00140 
00141 TEST(TestDesktopUtilitiesDataDirectories, TestGetBackgroundColor)
00142 {
00143   std::string const& color = DesktopUtilities::GetBackgroundColor(BUILDDIR"/tests/data/ubuntu-software-center.desktop");
00144 
00145   EXPECT_EQ(color, "#aabbcc");
00146 }
00147 
00148 TEST(TestDesktopUtilitiesDataDirectories, TestGetBackgroundColorNoKey)
00149 {
00150   std::string const& color = DesktopUtilities::GetBackgroundColor(BUILDDIR"/tests/data/update-manager.desktop");
00151 
00152   EXPECT_TRUE(color.empty());
00153 }
00154 
00155 TEST(TestDesktopUtilitiesDataDirectories, TestGetBackgroundColorNoFile)
00156 {
00157   std::string const& color = DesktopUtilities::GetBackgroundColor("hello-world.desktop");
00158 
00159   EXPECT_TRUE(color.empty());
00160 }
00161 
00162 } // anonymous namespace