~3v1n0/unity/light-shortcuts

« back to all changes in this revision

Viewing changes to tests/test_icon_loader.cpp

  • Committer: Marco Trevisan (Treviño)
  • Date: 2013-04-26 12:41:09 UTC
  • Revision ID: mail@3v1n0.net-20130426124109-t3b2shjah2omiqa2
Unity: Remove all the views, but the Shortcuts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2012 Canonical Ltd.
3
 
 *
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.
7
 
 *
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.
12
 
 *
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/>
16
 
 *
17
 
 * Authored by: Michal Hruby <michal.hruby@canonical.com>
18
 
 */
19
 
 
20
 
#include <gmock/gmock.h>
21
 
#include <sigc++/sigc++.h>
22
 
 
23
 
#include "IconLoader.h"
24
 
#include "test_utils.h"
25
 
 
26
 
using namespace testing;
27
 
using namespace unity;
28
 
 
29
 
namespace
30
 
{
31
 
bool IsValidPixbuf(GdkPixbuf *pixbuf)
32
 
{
33
 
  return GDK_IS_PIXBUF (pixbuf);
34
 
}
35
 
 
36
 
gboolean TimeoutReached (gpointer data)
37
 
{
38
 
  bool *b = static_cast<bool*>(data);
39
 
 
40
 
  *b = true;
41
 
 
42
 
  return FALSE;
43
 
}
44
 
 
45
 
struct LoadResult
46
 
{
47
 
  glib::Object<GdkPixbuf> pixbuf;
48
 
  bool got_callback;
49
 
  bool disconnected;
50
 
 
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)
54
 
  {
55
 
    pixbuf = buf;
56
 
 
57
 
    got_callback = true;
58
 
  }
59
 
};
60
 
 
61
 
void CheckResults(std::vector<LoadResult> const& results)
62
 
{
63
 
  Utils::WaitUntilMSec([&results] {
64
 
    bool got_all = true;
65
 
    for (auto const& result : results)
66
 
    {
67
 
      got_all = (result.got_callback == !result.disconnected);
68
 
 
69
 
      if (!got_all)
70
 
        break;
71
 
    }
72
 
 
73
 
    return got_all;
74
 
  });
75
 
 
76
 
  for (auto const& result : results)
77
 
  {
78
 
    if (!result.disconnected)
79
 
    {
80
 
      ASSERT_TRUE(result.got_callback);
81
 
      ASSERT_TRUE(IsValidPixbuf(result.pixbuf));
82
 
    }
83
 
    else
84
 
    {
85
 
      ASSERT_FALSE(result.got_callback);
86
 
    }
87
 
  }
88
 
}
89
 
 
90
 
 
91
 
TEST(TestIconLoader, TestGetDefault)
92
 
{
93
 
  // we need to initialize gtk
94
 
  int args_cnt = 0;
95
 
  gtk_init (&args_cnt, NULL);
96
 
 
97
 
  IconLoader::GetDefault();
98
 
}
99
 
 
100
 
TEST(TestIconLoader, TestGetOneIcon)
101
 
{
102
 
  LoadResult load_result;
103
 
  IconLoader& icon_loader = IconLoader::GetDefault();
104
 
 
105
 
  icon_loader.LoadFromIconName("gedit-icon", -1, 48, sigc::mem_fun(load_result,
106
 
        &LoadResult::IconLoaded));
107
 
 
108
 
  Utils::WaitUntilMSec(load_result.got_callback);
109
 
  EXPECT_TRUE(load_result.got_callback);
110
 
  EXPECT_TRUE(IsValidPixbuf(load_result.pixbuf));
111
 
}
112
 
 
113
 
TEST(TestIconLoader, TestGetAnnotatedIcon)
114
 
{
115
 
  LoadResult load_result;
116
 
  IconLoader& icon_loader = IconLoader::GetDefault();
117
 
 
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));
120
 
 
121
 
  Utils::WaitUntilMSec(load_result.got_callback);
122
 
  EXPECT_TRUE(load_result.got_callback);
123
 
  EXPECT_TRUE(IsValidPixbuf(load_result.pixbuf));
124
 
}
125
 
 
126
 
TEST(TestIconLoader, TestGetOneIconManyTimes)
127
 
{
128
 
  std::vector<LoadResult> results;
129
 
  std::vector<int> handles;
130
 
  IconLoader& icon_loader = IconLoader::GetDefault();
131
 
  int i, load_count;
132
 
 
133
 
  // 100 times should be good
134
 
  load_count = 100;
135
 
  results.resize (load_count);
136
 
  handles.resize (load_count);
137
 
 
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++)
141
 
  {
142
 
    handles[i] = icon_loader.LoadFromIconName("web-browser", -1, 48,
143
 
        sigc::mem_fun(results[i], &LoadResult::IconLoaded));
144
 
  }
145
 
 
146
 
  // disconnect every other handler (and especially the first one)
147
 
  for (i = 0; i < load_count; i += 2)
148
 
  {
149
 
    icon_loader.DisconnectHandle(handles[i]);
150
 
    results[i].disconnected = true;
151
 
  }
152
 
 
153
 
  CheckResults(results);
154
 
}
155
 
 
156
 
// Disabled until we have the new thread safe lp:fontconfig
157
 
TEST(TestIconLoader, DISABLED_TestGetManyIcons)
158
 
{
159
 
  std::vector<LoadResult> results;
160
 
  IconLoader& icon_loader = IconLoader::GetDefault();
161
 
  int i = 0;
162
 
  int icon_count;
163
 
 
164
 
  GList *icons = gtk_icon_theme_list_icons (gtk_icon_theme_get_default (),
165
 
                                            "Applications");
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)
170
 
  {
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;
175
 
  }
176
 
 
177
 
  CheckResults(results);
178
 
}
179
 
 
180
 
TEST(TestIconLoader, TestCancelSome)
181
 
{
182
 
  std::vector<LoadResult> results;
183
 
  std::vector<int> handles;
184
 
  IconLoader& icon_loader = IconLoader::GetDefault();
185
 
  int i = 0;
186
 
  int icon_count;
187
 
 
188
 
  GList *icons = gtk_icon_theme_list_icons (gtk_icon_theme_get_default (),
189
 
                                            "Emblems");
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)
195
 
  {
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;
201
 
  }
202
 
 
203
 
  // disconnect every other handler
204
 
  for (i = 0; i < icon_count; i += 2)
205
 
  {
206
 
    icon_loader.DisconnectHandle(handles[i]);
207
 
    results[i].disconnected = true;
208
 
  }
209
 
 
210
 
  CheckResults(results);
211
 
}
212
 
 
213
 
 
214
 
}