aboutsummaryrefslogtreecommitdiff
path: root/tests/main.vala
blob: b606121fe2bf0f6d906af7832f265558cbd38afe (plain)
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
const int LONG_TIMEOUT = 10;
const int SHORT_TIMEOUT = 1000;

[DBus (name = "com.canonical.indicator.keyboard.test")]
public class Service : Object {

	[DBus (visible = false)]
	private string? _command;

	[DBus (visible = false)]
	public string? command {
		get { return _command; }
	}

	public void execute (string command) {
		this._command = command;

		var pspec = this.get_class ().find_property ("command");

		if (pspec != null) {
			this.notify["command"] ((!) pspec);
		}
	}
}

struct Fixture {
	TestDBus? bus;
	uint service_name;
	DBusConnection? connection;
	Service? service;
	uint object_name;
}

static void start_service (Fixture *fixture) {
	if (fixture.connection != null) {
		try {
			fixture.service = new Service ();
			fixture.object_name = ((!) fixture.connection).register_object ("/com/canonical/indicator/keyboard/test", fixture.service);
		} catch (IOError error) {
			fixture.connection = null;
			fixture.service = null;
			fixture.object_name = 0;

			Test.message ("error: %s", error.message);
			Test.fail ();
		}
	}
}

static void begin_test (void *data) {
	var fixture = (Fixture *) data;

	fixture.bus = new TestDBus (TestDBusFlags.NONE);
	((!) fixture.bus).add_service_dir (SERVICE_DIR);
	((!) fixture.bus).up ();

	var loop = new MainLoop (null, false);

	fixture.service_name = Bus.own_name (BusType.SESSION,
	                                     "com.canonical.indicator.keyboard.test",
	                                     BusNameOwnerFlags.ALLOW_REPLACEMENT | BusNameOwnerFlags.REPLACE,
	                                     (connection, name) => {
	                                         if (loop.is_running ()) {
	                                             fixture.connection = connection;

	                                             start_service (fixture);

	                                             loop.quit ();
	                                         }
	                                     },
	                                     null,
	                                     (connection, name) => {
	                                         if (loop.is_running ()) {
	                                             fixture.connection = null;
	                                             fixture.service = null;
	                                             fixture.object_name = 0;

	                                             loop.quit ();
	                                         }
	                                     });

	loop.run ();

	if (fixture.connection == null) {
		Test.message ("Unable to connect to 'com.canonical.indicator.keyboard.test'.");
		Test.fail ();
	}
}

static void end_test (void *data) {
	var fixture = (Fixture *) data;

	if (fixture.object_name != 0) {
		((!) fixture.connection).unregister_object (fixture.object_name);
		fixture.object_name = 0;
	}

	if (fixture.service_name != 0) {
		Bus.unown_name (fixture.service_name);
		fixture.service_name = 0;
	}

	fixture.service = null;
	fixture.connection = null;

	if (fixture.bus != null) {
		((!) fixture.bus).down ();
		fixture.bus = null;
	}
}

static void test_activate_input_source (void *data) {
}

static void test_activate_character_map (void *data) {
	var fixture = (Fixture *) data;

	if (fixture.object_name == 0) {
		Test.message ("Invalid test fixture.");
		Test.fail ();
		return;
	}

	/* XXX: bootstrap the settings with what we want to test with. */
	var settings = new Settings ("org.gnome.desktop.input-sources");
	settings.set_uint ("current", 0);
	settings.set_value ("sources", new Variant.parsed ("[('xkb', 'us'), ('ibus', 'pinyin')]"));

	var cancellable = new Cancellable ();
	DBusProxy action_proxy;
	DBusProxy menu_proxy;

	var source = Timeout.add_seconds (LONG_TIMEOUT, () => { cancellable.cancel (); return false; });

	try {
		action_proxy = new DBusProxy.for_bus_sync (BusType.SESSION,
		                                           DBusProxyFlags.NONE,
		                                           null,
		                                           "com.canonical.indicator.keyboard",
		                                           "/com/canonical/indicator/keyboard",
		                                           "org.gtk.Actions",
		                                           cancellable);
	} catch (Error error) {
		Test.message ("error: %s", error.message);
		Test.fail ();
		return;
	}

	Source.remove (source);

	if (cancellable.is_cancelled ()) {
		Test.message ("Unable to connect to 'com.canonical.indicator.keyboard'.\n");
		Test.fail ();
		return;
	}

	source = Timeout.add_seconds (LONG_TIMEOUT, () => { cancellable.cancel (); return false; });

	try {
		menu_proxy = new DBusProxy.for_bus_sync (BusType.SESSION,
		                                         DBusProxyFlags.NONE,
		                                         null,
		                                         "com.canonical.indicator.keyboard",
		                                         "/com/canonical/indicator/keyboard/desktop",
		                                         "org.gtk.Menus",
		                                         cancellable);
	} catch (Error error) {
		Test.message ("error: %s", error.message);
		Test.fail ();
		return;
	}

	Source.remove (source);

	if (cancellable.is_cancelled ()) {
		Test.message ("Unable to connect to 'com.canonical.indicator.keyboard'.\n");
		Test.fail ();
		return;
	}

	/* XXX: This is just to make sure the GSettings are shared between processes. */
	{
		var builder = new VariantBuilder (new VariantType ("(au)"));
		builder.open (new VariantType ("au"));
		builder.add ("u", 0);
		builder.add ("u", 1);
		builder.close ();
		var variant = builder.end ();
		stdout.printf ("%s\n", menu_proxy.call_sync ("Start", variant, DBusCallFlags.NONE, SHORT_TIMEOUT).print (true));
		menu_proxy.call_sync ("End", variant, DBusCallFlags.NONE, SHORT_TIMEOUT);
	}

	var loop = new MainLoop (null, false);

	var signal_name = ((!) fixture.service).notify["command"].connect ((pspec) => {
		loop.quit ();
	});

	try {
		var builder = new VariantBuilder (new VariantType ("(sava{sv})"));
		builder.add ("s", "chart");
		builder.add_value (new Variant.parsed ("@av []"));
		builder.add_value (new Variant.parsed ("@a{sv} {}"));
		action_proxy.call_sync ("Activate", builder.end (), DBusCallFlags.NONE, SHORT_TIMEOUT);
	} catch (Error error) {
		Test.message ("error: %s", error.message);
		Test.fail ();
		return;
	}

	source = Timeout.add_seconds (LONG_TIMEOUT, () => { loop.quit (); return false; });
	loop.run ();
	Source.remove (source);
	((!) fixture.service).disconnect (signal_name);
}

static void test_activate_keyboard_layout_chart (void *data) {
}

static void test_activate_text_entry_settings (void *data) {
}

static void test_update_input_source (void *data) {
}

static void test_update_input_sources (void *data) {
}

public int main (string[] args) {
	Environment.set_variable ("DCONF_PROFILE", DCONF_PROFILE, true);

	Test.init (ref args, null);

	var suite = new TestSuite ("indicator-keyboard");

	suite.add (new TestCase ("activate-input-source", begin_test, test_activate_input_source, end_test, sizeof (Fixture)));
	suite.add (new TestCase ("activate-character-map", begin_test, test_activate_character_map, end_test, sizeof (Fixture)));
	suite.add (new TestCase ("activate-keyboard-layout-chart", begin_test, test_activate_keyboard_layout_chart, end_test, sizeof (Fixture)));
	suite.add (new TestCase ("activate-text-entry-settings", begin_test, test_activate_text_entry_settings, end_test, sizeof (Fixture)));
	suite.add (new TestCase ("update-input-source", begin_test, test_update_input_source, end_test, sizeof (Fixture)));
	suite.add (new TestCase ("update-input-sources", begin_test, test_update_input_sources, end_test, sizeof (Fixture)));

	TestSuite.get_root ().add_suite (suite);

	return Test.run ();
}