~jbicha/hud/build-depend-on-valac-not-gir

« back to all changes in this revision

Viewing changes to tests/test-application-list.c

  • Committer: Tarmac
  • Author(s): Ted Gould, Pete Woods, Antti Kaijanmäki, Ted Gould, Albert Astals, Ryan Lortie, Łukasz 'sil2100' Zemczak, Albert Astals Cid, Mathieu Trudel-Lapierre, Kaleo, Tarmac, Ricardo Salveti de Araujo, Michael Terry, Automatic PS uploader
  • Date: 2013-04-10 16:04:51 UTC
  • mfrom: (227.3.148 phablet)
  • Revision ID: tarmac-20130410160451-o3owpv3zaxulm5of
HUD 2.0 Merge.

Approved by PS Jenkins bot, Mathieu Trudel-Lapierre.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 * Copyright © 2012 Canonical Ltd.
 
4
 *
 
5
 * This program is free software: you can redistribute it and/or modify it
 
6
 * under the terms of the GNU General Public License version 3, as
 
7
 * published by the Free Software Foundation.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful, but
 
10
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
11
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
12
 * PURPOSE.  See the GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License along
 
15
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 */
 
17
 
 
18
#define G_LOG_DOMAIN "test-hudappindicatorsource"
 
19
 
 
20
#include "hudsettings.h"
 
21
#include "hudsource.h"
 
22
#include "hudtoken.h"
 
23
#include "application-list.h"
 
24
#include "hudtestutils.h"
 
25
 
 
26
#include <glib-object.h>
 
27
#include <gio/gio.h>
 
28
#include <libdbustest/dbus-test.h>
 
29
 
 
30
/* hardcode some parameters for reasons of determinism.
 
31
 */
 
32
HudSettings hud_settings = {
 
33
  .indicator_penalty = 50,
 
34
  .add_penalty = 10,
 
35
  .drop_penalty = 10,
 
36
  .end_drop_penalty = 1,
 
37
  .swap_penalty = 15,
 
38
  .max_distance = 30
 
39
};
 
40
 
 
41
static const gchar *BAMF_BUS_NAME = "org.ayatana.bamf";
 
42
static const gchar *MATCHER_OBJECT_PATH = "/org/ayatana/bamf/matcher";
 
43
static const gchar *APPLICATION_INTERFACE_NAME = "org.ayatana.bamf.application";
 
44
static const gchar *MATCHER_INTERFACE_NAME = "org.ayatana.bamf.matcher";
 
45
static const gchar *VIEW_INTERFACE_NAME = "org.ayatana.bamf.view";
 
46
static const gchar *WINDOW_INTERFACE_NAME = "org.ayatana.bamf.window";
 
47
static const gchar* REGISTRAR_BUS_NAME = "com.canonical.AppMenu.Registrar";
 
48
static const gchar* REGISTRAR_OBJECT_PATH = "/com/canonical/AppMenu/Registrar";
 
49
static const gchar* REGISTRAR_INTERFACE_NAME = "com.canonical.AppMenu.Registrar";
 
50
 
 
51
static void
 
52
test_window_source_add_view_methods (GDBusConnection* connection,
 
53
    const gchar *object_path)
 
54
{
 
55
  dbus_mock_add_method (connection, BAMF_BUS_NAME, object_path,
 
56
      VIEW_INTERFACE_NAME, "Name", "", "s", "ret = 'name'");
 
57
  dbus_mock_add_method (connection, BAMF_BUS_NAME, object_path,
 
58
      VIEW_INTERFACE_NAME, "Children", "", "as", "ret = []");
 
59
  dbus_mock_add_method (connection, BAMF_BUS_NAME, object_path,
 
60
      VIEW_INTERFACE_NAME, "Icon", "", "s", "ret = 'icon.png'");
 
61
}
 
62
 
 
63
static void
 
64
test_window_source_menu_model ()
 
65
{
 
66
  const gchar *app_dbus_name = "app.dbus.name";
 
67
  const gchar *app_dbus_menu_path = "/app/dbus/menu/path";
 
68
 
 
69
  DbusTestService *service = dbus_test_service_new (NULL);
 
70
  hud_test_utils_dbus_mock_start (service, BAMF_BUS_NAME,
 
71
      MATCHER_OBJECT_PATH, MATCHER_INTERFACE_NAME);
 
72
  hud_test_utils_dbus_mock_start (service, REGISTRAR_BUS_NAME,
 
73
      REGISTRAR_OBJECT_PATH, REGISTRAR_INTERFACE_NAME);
 
74
  hud_test_utils_start_menu_model_full (service,
 
75
      MODEL_SIMPLE, app_dbus_name, app_dbus_menu_path,
 
76
      TRUE);
 
77
  hud_test_utils_json_loader_start_full(service, "app.dbus.name.two", "/menu", JSON_SHORTCUTS);
 
78
  GDBusConnection *connection = hud_test_utils_mock_dbus_connection_new (service,
 
79
      BAMF_BUS_NAME, REGISTRAR_BUS_NAME, app_dbus_name, "app.dbus.name.two", NULL );
 
80
  hud_test_utils_process_mainloop (300);
 
81
 
 
82
  /* Define the mock window */
 
83
  {
 
84
    DBusMockProperties* properties = dbus_mock_new_properties ();
 
85
    DBusMockMethods* methods = dbus_mock_new_methods ();
 
86
    dbus_mock_methods_append (methods, "GetXid", "", "u", "ret = 1");
 
87
    dbus_mock_methods_append (methods, "Monitor", "", "i", "ret = 0");
 
88
    dbus_mock_methods_append (methods, "Maximized", "", "i", "ret = 1");
 
89
    dbus_mock_methods_append (methods, "Xprop", "s", "s", ""
 
90
        "dict = {'_GTK_UNIQUE_BUS_NAME': 'app.dbus.name',\n"
 
91
        "       '_GTK_APP_MENU_OBJECT_PATH': '/app/dbus/menu/path',\n"
 
92
        "       '_GTK_MENUBAR_OBJECT_PATH': '',\n"
 
93
        "       '_GTK_APPLICATION_OBJECT_PATH': '/app/dbus/menu/path',\n"
 
94
        "       '_GTK_WINDOW_OBJECT_PATH': ''\n"
 
95
        "       }\n"
 
96
        "ret = dict[args[0]]");
 
97
    dbus_mock_add_object (connection, BAMF_BUS_NAME, MATCHER_OBJECT_PATH,
 
98
        "/org/ayatana/bamf/window00000001", WINDOW_INTERFACE_NAME, properties,
 
99
        methods);
 
100
    test_window_source_add_view_methods (connection, "/org/ayatana/bamf/window00000001");
 
101
  }
 
102
 
 
103
  /* Define the mock application */
 
104
  {
 
105
      DBusMockProperties* properties = dbus_mock_new_properties ();
 
106
      DBusMockMethods* methods = dbus_mock_new_methods ();
 
107
      dbus_mock_methods_append (methods, "DesktopFile", "", "s",
 
108
          "ret = '/usr/share/applications/name.desktop'");
 
109
      dbus_mock_add_object (connection, BAMF_BUS_NAME, MATCHER_OBJECT_PATH,
 
110
          "/org/ayatana/bamf/application00000001", APPLICATION_INTERFACE_NAME,
 
111
          properties, methods);
 
112
      test_window_source_add_view_methods (connection, "/org/ayatana/bamf/application00000001");
 
113
    }
 
114
 
 
115
  /* Set up the BAMF matcher */
 
116
  {
 
117
    dbus_mock_add_method (connection,
 
118
        BAMF_BUS_NAME, MATCHER_OBJECT_PATH,
 
119
        MATCHER_INTERFACE_NAME, "ActiveWindow", "", "s",
 
120
          "ret = '/org/ayatana/bamf/window00000001'");
 
121
    dbus_mock_add_method (connection,
 
122
        BAMF_BUS_NAME, MATCHER_OBJECT_PATH,
 
123
        MATCHER_INTERFACE_NAME, "ApplicationForXid", "u", "s",
 
124
          "if args[0] == 1:\n"
 
125
          "    ret = '/org/ayatana/bamf/application00000001'\n"
 
126
          "else:\n"
 
127
          "    ret = None");
 
128
    dbus_mock_add_method (connection,
 
129
        BAMF_BUS_NAME, MATCHER_OBJECT_PATH,
 
130
        MATCHER_INTERFACE_NAME, "ApplicationPaths", "", "as",
 
131
          "ret = ['/org/ayatana/bamf/application00000001']");
 
132
    dbus_mock_add_method (connection,
 
133
        BAMF_BUS_NAME, MATCHER_OBJECT_PATH,
 
134
        MATCHER_INTERFACE_NAME, "WindowPaths", "", "as",
 
135
          "ret = ['/org/ayatana/bamf/window00000001']");
 
136
    dbus_mock_add_method (connection,
 
137
        BAMF_BUS_NAME, MATCHER_OBJECT_PATH,
 
138
        MATCHER_INTERFACE_NAME, "ActiveApplication", "", "s",
 
139
          "ret = '/org/ayatana/bamf/application00000001'");
 
140
  }
 
141
 
 
142
  /* Set up the app registrar */
 
143
  {
 
144
    dbus_mock_add_method (connection,
 
145
        REGISTRAR_BUS_NAME, REGISTRAR_OBJECT_PATH,
 
146
        REGISTRAR_INTERFACE_NAME, "GetMenus", "", "a(uso)",
 
147
          "ret = [(1, 'app.dbus.name.two', '/menu')]");
 
148
  }
 
149
 
 
150
  hud_test_utils_process_mainloop (100);
 
151
 
 
152
  HudApplicationList* source = hud_application_list_new();
 
153
  g_assert(source != NULL);
 
154
  g_assert(HUD_IS_APPLICATION_LIST(source));
 
155
 
 
156
  hud_test_utils_process_mainloop (100);
 
157
 
 
158
  {
 
159
    /* old, new */
 
160
    DBusMockSignalArgs* args = dbus_mock_new_signal_args ();
 
161
    dbus_mock_signal_args_append(args, g_variant_new("s", ""));
 
162
    dbus_mock_signal_args_append(args, g_variant_new("s", "/org/ayatana/bamf/window00000001"));
 
163
    dbus_mock_emit_signal (connection, BAMF_BUS_NAME, MATCHER_OBJECT_PATH,
 
164
        MATCHER_INTERFACE_NAME, "ActiveWindowChanged", "ss", args);
 
165
  }
 
166
 
 
167
  hud_test_utils_process_mainloop (100);
 
168
 
 
169
  hud_source_use(HUD_SOURCE(source));
 
170
 
 
171
  HudTokenList *search = hud_token_list_new_from_string ("simple");
 
172
  {
 
173
    GPtrArray *results = g_ptr_array_new_with_free_func(g_object_unref);
 
174
    hud_source_search(HUD_SOURCE(source), search, hud_test_utils_results_append_func, results);
 
175
    g_assert_cmpuint(results->len, ==, 1);
 
176
    hud_test_utils_source_assert_result (results, 0, "Simple");
 
177
    g_ptr_array_free(results, TRUE);
 
178
  }
 
179
 
 
180
  HudTokenList *search_two = hud_token_list_new_from_string ("save");
 
181
  {
 
182
    GPtrArray *results = g_ptr_array_new_with_free_func(g_object_unref);
 
183
    hud_source_search(HUD_SOURCE(source), search_two, hud_test_utils_results_append_func, results);
 
184
    g_assert_cmpuint(results->len, ==, 1);
 
185
    hud_test_utils_source_assert_result (results, 0, "Save");
 
186
    g_ptr_array_free(results, TRUE);
 
187
  }
 
188
 
 
189
  {
 
190
    GList* apps = hud_application_list_get_apps (source);
 
191
    g_assert_cmpuint(g_list_length(apps), ==, 1);
 
192
    HudApplicationSource * app =
 
193
        HUD_APPLICATION_SOURCE(g_list_nth_data(apps, 0));
 
194
    g_assert(HUD_IS_APPLICATION_SOURCE(app));
 
195
    g_assert_cmpstr(hud_application_source_get_id (app), ==, "name");
 
196
    g_assert_cmpstr(hud_application_source_get_path (app), ==,
 
197
        "/com/canonical/hud/applications/name");
 
198
    g_list_free(apps);
 
199
  }
 
200
 
 
201
  {
 
202
    HudApplicationSource * app = hud_application_list_get_source (source,
 
203
        "name");
 
204
    g_assert(HUD_IS_APPLICATION_SOURCE(app));
 
205
    g_assert_cmpstr(hud_application_source_get_id (app), ==, "name");
 
206
    g_assert_cmpstr(hud_application_source_get_path (app), ==,
 
207
        "/com/canonical/hud/applications/name");
 
208
  }
 
209
 
 
210
  hud_source_unuse(HUD_SOURCE(source));
 
211
 
 
212
  hud_token_list_free(search);
 
213
  hud_token_list_free(search_two);
 
214
  g_object_unref (source);
 
215
 
 
216
  hud_test_utils_process_mainloop (100);
 
217
 
 
218
  g_object_unref (service);
 
219
  g_object_unref (connection);
 
220
  /* FIXME: We would like to do this here, but dbus makes us exit with a non-zero code
 
221
  hud_test_utils_wait_for_connection_close(connection);
 
222
  */
 
223
}
 
224
 
 
225
int
 
226
main (int argc, char **argv)
 
227
{
 
228
#ifndef GLIB_VERSION_2_36
 
229
  g_type_init ();
 
230
#endif
 
231
 
 
232
  g_test_init (&argc, &argv, NULL);
 
233
  g_test_add_func ("/hud/windowsource/menu_model", test_window_source_menu_model);
 
234
 
 
235
  return g_test_run ();
 
236
}