2
* Copyright 2012 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU General Public License version 3, as published
6
* by the Free Software Foundation.
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 GNU General Public License for more details.
13
* You should have received a copy of the GNU General Public License
14
* version 3 along with this program. If not, see
15
* <http://www.gnu.org/licenses/>
17
* Authored by: Michal Hruby <michal.hruby@canonical.com>
20
#include <gmock/gmock.h>
21
#include <sigc++/sigc++.h>
23
#include "IconLoader.h"
24
#include "test_utils.h"
26
using namespace testing;
27
using namespace unity;
31
bool IsValidPixbuf(GdkPixbuf *pixbuf)
33
return GDK_IS_PIXBUF (pixbuf);
36
gboolean TimeoutReached (gpointer data)
38
bool *b = static_cast<bool*>(data);
47
glib::Object<GdkPixbuf> pixbuf;
51
LoadResult() : pixbuf(NULL), got_callback(false), disconnected(false) {}
52
void IconLoaded(std::string const& icon_name, int max_width, int max_height,
53
glib::Object<GdkPixbuf> const& buf)
61
void CheckResults(std::vector<LoadResult> const& results)
63
Utils::WaitUntilMSec([&results] {
65
for (auto const& result : results)
67
got_all = (result.got_callback == !result.disconnected);
76
for (auto const& result : results)
78
if (!result.disconnected)
80
ASSERT_TRUE(result.got_callback);
81
ASSERT_TRUE(IsValidPixbuf(result.pixbuf));
85
ASSERT_FALSE(result.got_callback);
91
TEST(TestIconLoader, TestGetDefault)
93
// we need to initialize gtk
95
gtk_init (&args_cnt, NULL);
97
IconLoader::GetDefault();
100
TEST(TestIconLoader, TestGetOneIcon)
102
LoadResult load_result;
103
IconLoader& icon_loader = IconLoader::GetDefault();
105
icon_loader.LoadFromIconName("gedit-icon", -1, 48, sigc::mem_fun(load_result,
106
&LoadResult::IconLoaded));
108
Utils::WaitUntilMSec(load_result.got_callback);
109
EXPECT_TRUE(load_result.got_callback);
110
EXPECT_TRUE(IsValidPixbuf(load_result.pixbuf));
113
TEST(TestIconLoader, TestGetAnnotatedIcon)
115
LoadResult load_result;
116
IconLoader& icon_loader = IconLoader::GetDefault();
118
icon_loader.LoadFromGIconString(". UnityProtocolAnnotatedIcon %7B'base-icon':%20%3C'gedit-icon'%3E,%20'ribbon':%20%3C'foo'%3E%7D", -1, 48, sigc::mem_fun(load_result,
119
&LoadResult::IconLoaded));
121
Utils::WaitUntilMSec(load_result.got_callback);
122
EXPECT_TRUE(load_result.got_callback);
123
EXPECT_TRUE(IsValidPixbuf(load_result.pixbuf));
126
TEST(TestIconLoader, TestGetOneIconManyTimes)
128
std::vector<LoadResult> results;
129
std::vector<int> handles;
130
IconLoader& icon_loader = IconLoader::GetDefault();
133
// 100 times should be good
135
results.resize (load_count);
136
handles.resize (load_count);
138
// careful, don't use the same icon as in previous tests, otherwise it'll
139
// be cached already!
140
for (int i = 0; i < load_count; i++)
142
handles[i] = icon_loader.LoadFromIconName("web-browser", -1, 48,
143
sigc::mem_fun(results[i], &LoadResult::IconLoaded));
146
// disconnect every other handler (and especially the first one)
147
for (i = 0; i < load_count; i += 2)
149
icon_loader.DisconnectHandle(handles[i]);
150
results[i].disconnected = true;
153
CheckResults(results);
156
// Disabled until we have the new thread safe lp:fontconfig
157
TEST(TestIconLoader, DISABLED_TestGetManyIcons)
159
std::vector<LoadResult> results;
160
IconLoader& icon_loader = IconLoader::GetDefault();
164
GList *icons = gtk_icon_theme_list_icons (gtk_icon_theme_get_default (),
166
// loading 100 icons should suffice
167
icon_count = MIN (100, g_list_length (icons));
168
results.resize (icon_count);
169
for (GList *it = icons; it != NULL; it = it->next)
171
const char *icon_name = static_cast<char*>(it->data);
172
icon_loader.LoadFromIconName(icon_name, -1, 48, sigc::mem_fun(results[i++],
173
&LoadResult::IconLoaded));
174
if (i >= icon_count) break;
177
CheckResults(results);
180
TEST(TestIconLoader, TestCancelSome)
182
std::vector<LoadResult> results;
183
std::vector<int> handles;
184
IconLoader& icon_loader = IconLoader::GetDefault();
188
GList *icons = gtk_icon_theme_list_icons (gtk_icon_theme_get_default (),
190
// loading 100 icons should suffice
191
icon_count = MIN (100, g_list_length (icons));
192
results.resize (icon_count);
193
handles.resize (icon_count);
194
for (GList *it = icons; it != NULL; it = it->next)
196
const char *icon_name = static_cast<char*>(it->data);
197
int handle = icon_loader.LoadFromIconName(icon_name, -1, 48, sigc::mem_fun(
198
results[i], &LoadResult::IconLoaded));
199
handles[i++] = handle;
200
if (i >= icon_count) break;
203
// disconnect every other handler
204
for (i = 0; i < icon_count; i += 2)
206
icon_loader.DisconnectHandle(handles[i]);
207
results[i].disconnected = true;
210
CheckResults(results);