~ubuntu-branches/ubuntu/trusty/gnome-do/trusty

« back to all changes in this revision

Viewing changes to Tests/TestKeybindingService.cs

  • Committer: Package Import Robot
  • Author(s): Christopher James Halse Rogers
  • Date: 2012-03-26 11:12:21 UTC
  • mfrom: (0.1.12 sid)
  • Revision ID: package-import@ubuntu.com-20120326111221-1jk143fy37zxi3e4
Tags: 0.9-1
* New upstream version no longer uses deprecated internal glib headers.
  (Closes: #665537)
* [59fa37b9] Fix watch file
* [63486516] Imported Upstream version 0.9
* [8c636d84] Disable testsuite for now; requires running dbus and gconf daemons
* [e46de4b9] Remove inaccurate README.Source
* [4591d677] Add git-buildpackage configuration to default to pristine-tar

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
//  TestKeybindingService.cs
 
3
//  
 
4
//  Author:
 
5
//       Christopher James Halse Rogers <raof@ubuntu.com>
 
6
// 
 
7
//  Copyright © 2012 Christopher James Halse Rogers <raof@ubuntu.com>
 
8
// 
 
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.
 
13
// 
 
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.
 
18
// 
 
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
 
22
 
 
23
using System;
 
24
using System.Linq;
 
25
using System.Collections.Generic;
 
26
using System.Reflection;
 
27
using NUnit.Framework;
 
28
 
 
29
using Mono.Addins;
 
30
using Mono.Unix;
 
31
 
 
32
using Do.Platform;
 
33
using Do.Platform.Common;
 
34
 
 
35
namespace Do
 
36
{
 
37
        class MockPreferencesService : IPreferencesService
 
38
        {
 
39
                public List<string> accessed_members = new List<string> ();
 
40
                public List<Tuple<string, object>> set_members = new List<Tuple<string, object>> ();
 
41
 
 
42
                #region IPreferencesService implementation
 
43
                public event EventHandler<PreferencesChangedEventArgs> PreferencesChanged;
 
44
 
 
45
                public bool Set<T> (string key, T val)
 
46
                {
 
47
                        set_members.Add (new Tuple<string, object> (key, val));
 
48
                        return true;
 
49
                }
 
50
 
 
51
                public bool TryGet<T> (string key, out T val)
 
52
                {
 
53
                        accessed_members.Add (key);
 
54
                        val = default (T);
 
55
                        return false;
 
56
                }
 
57
                #endregion
 
58
 
 
59
                public void Reset ()
 
60
                {
 
61
                        accessed_members = new List<string> ();
 
62
                        set_members = new List<Tuple<string, object>> ();
 
63
                }
 
64
        }
 
65
 
 
66
        class MockKeybindingService : AbstractKeyBindingService
 
67
        {
 
68
                #region implemented abstract members of Do.Platform.Common.AbstractKeyBindingService
 
69
                public override bool RegisterOSKey (string keyString, EventCallback cb)
 
70
                {
 
71
                        // No-op
 
72
                        return true;
 
73
                }
 
74
 
 
75
                public override bool UnRegisterOSKey (string keyString)
 
76
                {
 
77
                        // No-op
 
78
                        return true;
 
79
                }
 
80
                #endregion
 
81
        }
 
82
 
 
83
        [TestFixture()]
 
84
        public class TestKeybindingService
 
85
        {
 
86
                MockKeybindingService keybinder;
 
87
                MockPreferencesService preferences;
 
88
                string initial_lang;
 
89
 
 
90
                [SetUp()]
 
91
                public void SetUp ()
 
92
                {
 
93
                        Gtk.Application.Init ();
 
94
                        Gdk.Threads.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 ();
 
101
                }
 
102
 
 
103
                [TearDown]
 
104
                public void TearDown ()
 
105
                {
 
106
                        if (String.IsNullOrEmpty (initial_lang)) {
 
107
                                System.Environment.SetEnvironmentVariable ("LANGUAGE", "");
 
108
                        } else {
 
109
                                System.Environment.SetEnvironmentVariable ("LANGUAGE", initial_lang);
 
110
                        }
 
111
                }
 
112
 
 
113
                bool CollectionContainsSubstring (IEnumerable<string> collection, string search)
 
114
                {
 
115
                        return collection.Aggregate<string, bool> (false, ((bool found, string str) => {
 
116
                                return found || str.Contains (search);
 
117
                        }));
 
118
                }
 
119
 
 
120
                [Test()]
 
121
                public void TestKeybindingRequestsCorrectPreferencesKey ()
 
122
                {
 
123
                        Services.Keybinder.RegisterKeyBinding (new KeyBinding ("Summon_Do",
 
124
                                        Catalog.GetString ("Summon Do"), "<Super>space", delegate {}, true));
 
125
 
 
126
                        Assert.True (CollectionContainsSubstring (preferences.accessed_members, "Summon_Do"));
 
127
                }
 
128
 
 
129
 
 
130
                [Test()]
 
131
                public void TestKeybindingUsesUntranslatedKey ()
 
132
                {
 
133
                        System.Environment.SetEnvironmentVariable ("LANGUAGE", "de");
 
134
                        Catalog.Init ("gnome-do", ".");
 
135
 
 
136
                        if ("Nächstes Element" != Catalog.GetString ("Next Item")) {
 
137
                                Assert.Inconclusive ("Translations are not properly set up, test cannot run.");
 
138
                        }
 
139
                        Services.Keybinder.RegisterKeyBinding (new KeyBinding ("Summon_Do",
 
140
                                        Catalog.GetString ("Summon Do"), "<Super>space", delegate {}, true));
 
141
 
 
142
                        Assert.True (CollectionContainsSubstring (preferences.accessed_members, "Summon_Do"));
 
143
                }
 
144
 
 
145
                bool IsAllAscii (string text)
 
146
                {
 
147
                        return text.All (c => c >= ' ' && c <= '~');
 
148
                }
 
149
 
 
150
                [Test]
 
151
                public void TestSetupKeybindingsUsesUntranslatedKeys ()
 
152
                {
 
153
                        System.Environment.SetEnvironmentVariable ("LANGUAGE", "de");
 
154
                        Catalog.Init ("gnome-do", ".");
 
155
 
 
156
                        if ("Nächstes Element" != Catalog.GetString ("Next Item")) {
 
157
                                Assert.Inconclusive ("Translations are not properly set up, test cannot run.");
 
158
                        }
 
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 [] { });
 
163
 
 
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));
 
166
                        }
 
167
                }
 
168
 
 
169
                [Test]
 
170
                public void TestSetKeyStringUsesUntranslatedKey ()
 
171
                {
 
172
                        System.Environment.SetEnvironmentVariable ("LANGUAGE", "de");
 
173
                        Catalog.Init ("gnome-do", ".");
 
174
 
 
175
                        if ("Nächstes Element" != Catalog.GetString ("Next Item")) {
 
176
                                Assert.Inconclusive ("Translations are not properly set up, test cannot run.");
 
177
                        }
 
178
 
 
179
                        var binding = new KeyBinding ("Next_Item", Catalog.GetString ("Next Item"), "Down", delegate {}, true);
 
180
                        keybinder.RegisterKeyBinding (binding);
 
181
 
 
182
                        keybinder.SetKeyString (binding, "Up");
 
183
 
 
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));
 
186
                        }
 
187
                }
 
188
        }
 
189
}
 
190