2
// TestKeybindingService.cs
5
// Christopher James Halse Rogers <raof@ubuntu.com>
7
// Copyright © 2012 Christopher James Halse Rogers <raof@ubuntu.com>
9
// This library is free software; you can redistribute it and/or modify
10
// it under the terms of the GNU Lesser General Public License as
11
// published by the Free Software Foundation; either version 2.1 of the
12
// License, or (at your option) any later version.
14
// This library is distributed in the hope that it will be useful, but
15
// WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
// Lesser General Public License for more details.
19
// You should have received a copy of the GNU Lesser General Public
20
// License along with this library; if not, write to the Free Software
21
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
using System.Collections.Generic;
26
using System.Reflection;
27
using NUnit.Framework;
33
using Do.Platform.Common;
37
class MockPreferencesService : IPreferencesService
39
public List<string> accessed_members = new List<string> ();
40
public List<Tuple<string, object>> set_members = new List<Tuple<string, object>> ();
42
#region IPreferencesService implementation
43
public event EventHandler<PreferencesChangedEventArgs> PreferencesChanged;
45
public bool Set<T> (string key, T val)
47
set_members.Add (new Tuple<string, object> (key, val));
51
public bool TryGet<T> (string key, out T val)
53
accessed_members.Add (key);
61
accessed_members = new List<string> ();
62
set_members = new List<Tuple<string, object>> ();
66
class MockKeybindingService : AbstractKeyBindingService
68
#region implemented abstract members of Do.Platform.Common.AbstractKeyBindingService
69
public override bool RegisterOSKey (string keyString, EventCallback cb)
75
public override bool UnRegisterOSKey (string keyString)
84
public class TestKeybindingService
86
MockKeybindingService keybinder;
87
MockPreferencesService preferences;
93
Gtk.Application.Init ();
95
Core.PluginManager.Initialize ();
96
AddinManager.Registry.Update ();
97
preferences = AddinManager.GetExtensionObjects ("/Do/Service", true).OfType<MockPreferencesService> ().First ();
98
keybinder = AddinManager.GetExtensionObjects ("/Do/Service", true).OfType<MockKeybindingService> ().First ();
99
initial_lang = System.Environment.GetEnvironmentVariable ("LANGUAGE");
100
preferences.Reset ();
104
public void TearDown ()
106
if (String.IsNullOrEmpty (initial_lang)) {
107
System.Environment.SetEnvironmentVariable ("LANGUAGE", "");
109
System.Environment.SetEnvironmentVariable ("LANGUAGE", initial_lang);
113
bool CollectionContainsSubstring (IEnumerable<string> collection, string search)
115
return collection.Aggregate<string, bool> (false, ((bool found, string str) => {
116
return found || str.Contains (search);
121
public void TestKeybindingRequestsCorrectPreferencesKey ()
123
Services.Keybinder.RegisterKeyBinding (new KeyBinding ("Summon_Do",
124
Catalog.GetString ("Summon Do"), "<Super>space", delegate {}, true));
126
Assert.True (CollectionContainsSubstring (preferences.accessed_members, "Summon_Do"));
131
public void TestKeybindingUsesUntranslatedKey ()
133
System.Environment.SetEnvironmentVariable ("LANGUAGE", "de");
134
Catalog.Init ("gnome-do", ".");
136
if ("Nächstes Element" != Catalog.GetString ("Next Item")) {
137
Assert.Inconclusive ("Translations are not properly set up, test cannot run.");
139
Services.Keybinder.RegisterKeyBinding (new KeyBinding ("Summon_Do",
140
Catalog.GetString ("Summon Do"), "<Super>space", delegate {}, true));
142
Assert.True (CollectionContainsSubstring (preferences.accessed_members, "Summon_Do"));
145
bool IsAllAscii (string text)
147
return text.All (c => c >= ' ' && c <= '~');
151
public void TestSetupKeybindingsUsesUntranslatedKeys ()
153
System.Environment.SetEnvironmentVariable ("LANGUAGE", "de");
154
Catalog.Init ("gnome-do", ".");
156
if ("Nächstes Element" != Catalog.GetString ("Next Item")) {
157
Assert.Inconclusive ("Translations are not properly set up, test cannot run.");
159
Core.Controller controller = new Core.Controller ();
160
var foo = controller.GetType ().GetMethod ("SetupKeybindings", System.Reflection.BindingFlags.NonPublic |
161
System.Reflection.BindingFlags.Instance);
162
foo.Invoke (controller, new object [] { });
164
foreach (var key in preferences.accessed_members.Concat (preferences.set_members.Select (pref => pref.Item1))) {
165
Assert.That (IsAllAscii (key), String.Format ("Key “{0}” contains non-ASCII character", key));
170
public void TestSetKeyStringUsesUntranslatedKey ()
172
System.Environment.SetEnvironmentVariable ("LANGUAGE", "de");
173
Catalog.Init ("gnome-do", ".");
175
if ("Nächstes Element" != Catalog.GetString ("Next Item")) {
176
Assert.Inconclusive ("Translations are not properly set up, test cannot run.");
179
var binding = new KeyBinding ("Next_Item", Catalog.GetString ("Next Item"), "Down", delegate {}, true);
180
keybinder.RegisterKeyBinding (binding);
182
keybinder.SetKeyString (binding, "Up");
184
foreach (var key in preferences.accessed_members.Concat (preferences.set_members.Select (pref => pref.Item1))) {
185
Assert.That (IsAllAscii (key), String.Format ("Key “{0}” contains non-ASCII character", key));