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

« back to all changes in this revision

Viewing changes to tests/test-keyword-mapping.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
Test code for keyword mapping
 
3
 
 
4
Copyright 2012 Canonical Ltd.
 
5
 
 
6
This program is free software: you can redistribute it and/or modify it 
 
7
under the terms of the GNU General Public License version 3, as published 
 
8
by the Free Software Foundation.
 
9
 
 
10
This program is distributed in the hope that it will be useful, but 
 
11
WITHOUT ANY WARRANTY; without even the implied warranties of 
 
12
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
 
13
PURPOSE.  See the GNU General Public License for more details.
 
14
 
 
15
You should have received a copy of the GNU General Public License along 
 
16
with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
*/
 
18
 
 
19
#include <glib.h>
 
20
#include <glib-object.h>
 
21
#include <glib/gi18n.h>
 
22
#include <locale.h>
 
23
 
 
24
#include <string.h>
 
25
#include <stdlib.h>
 
26
 
 
27
#include "hudsettings.h"
 
28
#include "hudkeywordmapping.h"
 
29
 
 
30
/* hardcode some parameters so the test doesn't fail if the user
 
31
 * has bogus things in GSettings.
 
32
 */
 
33
HudSettings hud_settings = {
 
34
        .indicator_penalty = 50,
 
35
        .add_penalty = 10,
 
36
        .drop_penalty = 10,
 
37
        .end_drop_penalty = 1,
 
38
        .swap_penalty = 15,
 
39
        .max_distance = 30
 
40
};
 
41
 
 
42
static
 
43
const gchar*
 
44
get_language ()
 
45
{
 
46
  return g_getenv ("LANGUAGE");
 
47
}
 
48
 
 
49
/**
 
50
 *
 
51
 */
 
52
static void
 
53
set_language (const gchar* language)
 
54
{
 
55
  /* Change language.  */
 
56
  g_setenv ("LANGUAGE", language, 1);
 
57
 
 
58
  /* Make change known.  */
 
59
  {
 
60
    extern int _nl_msg_cat_cntr;
 
61
    ++_nl_msg_cat_cntr;
 
62
  }
 
63
}
 
64
 
 
65
static void
 
66
test_keyword_mapping_unknown_action (void)
 
67
{
 
68
  HudKeywordMapping *mapping;
 
69
  GPtrArray *results, *results2;
 
70
 
 
71
  mapping = hud_keyword_mapping_new ();
 
72
 
 
73
  const gchar* original_language = get_language ();
 
74
  set_language ("en_EVENMOREFAKELANG");
 
75
  hud_keyword_mapping_load (mapping, "gnome-terminal",
 
76
      KEYWORD_MAPPING, KEYWORD_LOCALE_DIR);
 
77
  set_language (original_language);
 
78
 
 
79
  results = hud_keyword_mapping_transform (mapping, "random action string");
 
80
  g_assert_cmpint(results->len, ==, 0);
 
81
 
 
82
  results2 = hud_keyword_mapping_transform (mapping, "random action string");
 
83
  g_assert_cmpint(results2->len, ==, 0);
 
84
 
 
85
  /* Check the keyword miss was cached */
 
86
  g_assert(results == results2);
 
87
 
 
88
  g_object_unref (mapping);
 
89
}
 
90
 
 
91
static void
 
92
test_keyword_mapping_open_tab (void)
 
93
{
 
94
  HudKeywordMapping *mapping;
 
95
  GPtrArray* results;
 
96
 
 
97
  mapping = hud_keyword_mapping_new ();
 
98
 
 
99
  const gchar* original_language = get_language ();
 
100
  set_language ("en_EVENMOREFAKELANG");
 
101
  hud_keyword_mapping_load (mapping, "gnome-terminal",
 
102
      KEYWORD_MAPPING, KEYWORD_LOCALE_DIR);
 
103
  set_language (original_language);
 
104
 
 
105
  results = hud_keyword_mapping_transform (mapping, "Open Ta_b");
 
106
 
 
107
  g_assert_cmpint(results->len, ==, 2);
 
108
  g_assert_cmpstr((gchar*) g_ptr_array_index(results, 0), ==, "New Tab");
 
109
  g_assert_cmpstr((gchar*) g_ptr_array_index(results, 1), ==, "Another Tab");
 
110
 
 
111
  g_object_unref (mapping);
 
112
}
 
113
 
 
114
static void
 
115
test_keyword_mapping_open_tab_with_translation (void)
 
116
{
 
117
  HudKeywordMapping *mapping;
 
118
  GPtrArray* results;
 
119
 
 
120
  const gchar* original_language = get_language ();
 
121
  mapping = hud_keyword_mapping_new ();
 
122
 
 
123
  /* Temporarily change to language to read in our translations */
 
124
  set_language ("en_FAKELANG");
 
125
  hud_keyword_mapping_load (mapping, "gnome-terminal",
 
126
      KEYWORD_MAPPING, KEYWORD_LOCALE_DIR);
 
127
  set_language (original_language);
 
128
 
 
129
  results = hud_keyword_mapping_transform (mapping, "Open Ta_b");
 
130
 
 
131
  g_assert_cmpint(results->len, ==, 3);
 
132
  g_assert_cmpstr((gchar*) g_ptr_array_index(results, 0), ==, "More Tabs");
 
133
  g_assert_cmpstr((gchar*) g_ptr_array_index(results, 1), ==,
 
134
      "Gimme a Tab Bro");
 
135
  g_assert_cmpstr((gchar*) g_ptr_array_index(results, 2), ==, "Giv Tab Plz");
 
136
 
 
137
  g_object_unref (mapping);
 
138
}
 
139
 
 
140
static void
 
141
test_keyword_mapping_open_terminal (void)
 
142
{
 
143
  HudKeywordMapping *mapping;
 
144
  GPtrArray* results;
 
145
 
 
146
  mapping = hud_keyword_mapping_new ();
 
147
 
 
148
  const gchar* original_language = get_language ();
 
149
  set_language ("en_EVENMOREFAKELANG");
 
150
  hud_keyword_mapping_load (mapping, "gnome-terminal", KEYWORD_MAPPING,
 
151
      KEYWORD_LOCALE_DIR);
 
152
  set_language (original_language);
 
153
 
 
154
  results = hud_keyword_mapping_transform (mapping, "Open _Terminal");
 
155
 
 
156
  g_assert_cmpint(results->len, ==, 2);
 
157
  g_assert_cmpstr((gchar*) g_ptr_array_index(results, 0), ==, "New Terminal");
 
158
  g_assert_cmpstr((gchar*) g_ptr_array_index(results, 1), ==,
 
159
      "Another Terminal");
 
160
 
 
161
  g_object_unref (mapping);
 
162
}
 
163
 
 
164
/* Build the test suite */
 
165
static void test_keyword_mapping_suite (void)
 
166
{
 
167
  g_test_add_func ("/hud/keyword/unknown_action", test_keyword_mapping_unknown_action);
 
168
  g_test_add_func ("/hud/keyword/open_tab", test_keyword_mapping_open_tab);
 
169
  g_test_add_func ("/hud/keyword/open_tab_with_translation",
 
170
      test_keyword_mapping_open_tab_with_translation);
 
171
  g_test_add_func ("/hud/keyword/open_terminal",
 
172
      test_keyword_mapping_open_terminal);
 
173
  return;
 
174
}
 
175
 
 
176
gint
 
177
main (gint argc, gchar * argv[])
 
178
{
 
179
  g_setenv ("LANG", "C.UTF-8", 1);
 
180
  g_unsetenv("LC_ALL");
 
181
  setlocale (LC_ALL, "");
 
182
 
 
183
#ifndef GLIB_VERSION_2_36
 
184
  g_type_init();
 
185
#endif
 
186
 
 
187
  g_test_init (&argc, &argv, NULL );
 
188
 
 
189
  /* Test suites */
 
190
  test_keyword_mapping_suite ();
 
191
 
 
192
  gint result = g_test_run ();
 
193
 
 
194
  return result;
 
195
}