~smspillaz/unity/unity.less-paint-insanity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * Copyright 2012 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the  Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * version 3 along with this program.  If not, see
 * <http://www.gnu.org/licenses/>
 *
 * Authored by: Michal Hruby <michal.hruby@canonical.com>
 */

#include <gmock/gmock.h>

#include "IconLoader.h"

using namespace testing;
using namespace unity;

namespace
{
bool IsValidPixbuf(GdkPixbuf *pixbuf)
{
  return GDK_IS_PIXBUF (pixbuf);
}

gboolean TimeoutReached (gpointer data)
{
  bool *b = static_cast<bool*>(data);

  *b = true;

  return FALSE;
}

struct LoadResult
{
  GdkPixbuf *pixbuf;
  bool got_callback;

  LoadResult() : pixbuf(NULL), got_callback(false) {}
  void IconLoaded(std::string const& icon_name, unsigned size,
                  GdkPixbuf *buf)
  {
    pixbuf = buf;

    got_callback = true;
  }
};

TEST(TestIconLoader, TestGetDefault)
{
  // we need to initialize gtk
  int args_cnt = 0;
  gtk_init (&args_cnt, NULL);

  IconLoader::GetDefault();
}

TEST(TestIconLoader, TestGetOneIcon)
{
  LoadResult load_result;
  IconLoader& icon_loader = IconLoader::GetDefault();
  volatile bool timeout_reached = false;

  icon_loader.LoadFromIconName("gedit-icon", 48, sigc::mem_fun(load_result,
        &LoadResult::IconLoaded));

  guint tid = g_timeout_add (10000, TimeoutReached, (gpointer)(&timeout_reached));
  while (!timeout_reached && !load_result.got_callback)
  {
    g_main_context_iteration (NULL, TRUE);
  }

  EXPECT_TRUE(load_result.got_callback);
  EXPECT_TRUE(IsValidPixbuf(load_result.pixbuf));

  g_source_remove (tid);
}

TEST(TestIconLoader, TestGetOneIconManyTimes)
{
  std::vector<LoadResult> results;
  std::vector<int> handles;
  IconLoader& icon_loader = IconLoader::GetDefault();
  volatile bool timeout_reached = false;
  int i, load_count;

  // 100 times should be good
  load_count = 100;
  results.resize (load_count);
  handles.resize (load_count);

  // careful, don't use the same icon as in previous tests, otherwise it'll
  // be cached already!
  for (int i = 0; i < load_count; i++)
  {
    handles[i] = icon_loader.LoadFromIconName("web-browser", 48,
        sigc::mem_fun(results[i], &LoadResult::IconLoaded));
  }

  // disconnect every other handler (and especially the first one)
  for (i = 0; i < load_count; i += 2)
  {
    icon_loader.DisconnectHandle(handles[i]);
  }

  guint tid = g_timeout_add (10000, TimeoutReached, (gpointer)(&timeout_reached));
  int iterations = 0;
  while (!timeout_reached)
  {
    g_main_context_iteration (NULL, TRUE);
    bool all_loaded = true;
    bool any_loaded = false;
    for (i = 1; i < load_count; i += 2)
    {
      all_loaded &= results[i].got_callback;
      any_loaded |= results[i].got_callback;
      if (!all_loaded) break;
    }
    // count the number of iterations where we got some results
    if (any_loaded) iterations++;
    if (all_loaded) break;
  }

  EXPECT_FALSE(timeout_reached);
  // it's all loading the same icon, the results had to come in the same
  // main loop iteration (that's the desired behaviour)
  EXPECT_EQ(iterations, 1);

  for (i = 0; i < load_count; i++)
  {
    if (i % 2)
    {
      EXPECT_TRUE(results[i].got_callback);
      EXPECT_TRUE(IsValidPixbuf(results[i].pixbuf));
    }
    else
    {
      EXPECT_FALSE(results[i].got_callback);
    }
  }

  g_source_remove (tid);
}

TEST(TestIconLoader, TestGetManyIcons)
{
  std::vector<LoadResult> results;
  IconLoader& icon_loader = IconLoader::GetDefault();
  volatile bool timeout_reached = false;
  int i = 0;
  int icon_count;

  GList *icons = gtk_icon_theme_list_icons (gtk_icon_theme_get_default (),
                                            "Applications");
  // loading 100 icons should suffice
  icon_count = MIN (100, g_list_length (icons));
  results.resize (icon_count);
  for (GList *it = icons; it != NULL; it = it->next)
  {
    const char *icon_name = static_cast<char*>(it->data);
    icon_loader.LoadFromIconName(icon_name, 48, sigc::mem_fun(results[i++],
        &LoadResult::IconLoaded));
    if (i >= icon_count) break;
  }

  guint tid = g_timeout_add (30000, TimeoutReached, (gpointer)(&timeout_reached));
  while (!timeout_reached)
  {
    g_main_context_iteration (NULL, TRUE);
    bool all_loaded = true;
    for (auto loader: results)
    {
      all_loaded &= loader.got_callback;
      if (!all_loaded) break;
    }
    if (all_loaded) break;
  }

  for (auto load_result: results)
  {
    EXPECT_TRUE(load_result.got_callback);
    EXPECT_TRUE(IsValidPixbuf(load_result.pixbuf));
  }

  g_source_remove (tid);
}

TEST(TestIconLoader, TestCancelSome)
{
  std::vector<LoadResult> results;
  std::vector<int> handles;
  IconLoader& icon_loader = IconLoader::GetDefault();
  volatile bool timeout_reached = false;
  int i = 0;
  int icon_count;

  GList *icons = gtk_icon_theme_list_icons (gtk_icon_theme_get_default (),
                                            "Emblems");
  // loading 100 icons should suffice
  icon_count = MIN (100, g_list_length (icons));
  results.resize (icon_count);
  handles.resize (icon_count);
  for (GList *it = icons; it != NULL; it = it->next)
  {
    const char *icon_name = static_cast<char*>(it->data);
    int handle = icon_loader.LoadFromIconName(icon_name, 48, sigc::mem_fun(
          results[i], &LoadResult::IconLoaded));
    handles[i++] = handle;
    if (i >= icon_count) break;
  }

  // disconnect every other handler
  for (i = 0; i < icon_count; i += 2)
  {
    icon_loader.DisconnectHandle(handles[i]);
  }

  guint tid = g_timeout_add (30000, TimeoutReached, (gpointer)(&timeout_reached));
  while (!timeout_reached)
  {
    g_main_context_iteration (NULL, TRUE);
    bool all_loaded = true;
    for (int i = 1; i < icon_count; i += 2)
    {
      all_loaded &= results[i].got_callback;
      if (!all_loaded) break;
    }
    if (all_loaded) break;
  }

  for (i = 0; i < icon_count; i++)
  {
    if (i % 2)
    {
      EXPECT_TRUE(results[i].got_callback);
      EXPECT_TRUE(IsValidPixbuf(results[i].pixbuf));
    }
    else
    {
      EXPECT_FALSE(results[i].got_callback);
    }
  }

  g_source_remove (tid);
}


}