File: ExportInterfaceTest.cs

package info (click to toggle)
dbus-sharp 0.8.1-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 868 kB
  • sloc: cs: 7,690; sh: 494; makefile: 181
file content (344 lines) | stat: -rw-r--r-- 7,884 bytes parent folder | download | duplicates (3)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Copyright 2007 Alp Toker <alp@atoker.com>
//           2011 Bertrand Lorentz <bertrand.lorentz@gmail.com>
// This software is made available under the MIT License
// See COPYING for details

using System;
using System.Collections.Generic;
using System.Threading;

using NUnit.Framework;
using DBus;
using DBus.Protocol;
using org.freedesktop.DBus;

namespace DBus.Tests
{
	[TestFixture]
	public class ExportInterfaceTest
	{
		ITestOne test;
		Test test_server;

		string bus_name = "org.dbussharp.test";
		ObjectPath path = new ObjectPath ("/org/dbussharp/test");
		
		int event_a_count = 0;
        
		[TestFixtureSetUp]
        public void Setup ()
        {
			test_server = new Test ();
			Bus.Session.Register (path, test_server);
			Assert.AreEqual (Bus.Session.RequestName (bus_name), RequestNameReply.PrimaryOwner);
			
			Assert.AreNotEqual (Bus.Session.RequestName (bus_name), RequestNameReply.PrimaryOwner);
			test = Bus.Session.GetObject<ITestOne> (bus_name, path);
        }
		
		/// <summary>
		/// 
		/// </summary>
		[Test]
		public void VoidMethods ()
		{
			test.VoidObject (1);
			Assert.IsTrue (test_server.void_object_called);
			
			test.VoidEnums (TestEnum.Bar, TestEnum.Foo);
			Assert.IsTrue (test_server.void_enums_called);

			test.VoidString ("foo");
			Assert.IsTrue (test_server.void_enums_called);
		}

		/// <summary>
		/// 
		/// </summary>
		[Test]
		public void FireEvent ()
		{
			test.SomeEvent += HandleSomeEventA;
			test.FireSomeEvent ();
			Assert.AreEqual (1, event_a_count);
			
			test.SomeEvent -= HandleSomeEventA;
			test.FireSomeEvent ();
			
			Assert.AreEqual (1, event_a_count);
		}
		
		private void HandleSomeEventA (string arg1, object arg2, double arg3, MyTuple mt)
		{
			event_a_count++;
		}

		/// <summary>
		/// 
		/// </summary>
		[Test]
		public void GetVariant ()
		{
			Assert.IsInstanceOfType (typeof (byte []), test.GetSomeVariant());
		}
		
		/// <summary>
		/// 
		/// </summary>
		[Test]
		public void WithOutParameters ()
		{
			uint n;
			string istr = "21";
			string ostr;
			test.WithOutParameters (out n, istr, out ostr);
			Assert.AreEqual (UInt32.Parse (istr), n);
			Assert.AreEqual ("." + istr + ".", ostr);
			
			uint[] a1, a2, a3;
			test.WithOutParameters2 (out a1, out a2, out a3);
			Assert.AreEqual (new uint[] { }, a1);
			Assert.AreEqual (new uint[] { 21, 23, 16 }, a2);
			Assert.AreEqual (new uint[] { 21, 23 }, a3);
		}
				
		/// <summary>
		/// 
		/// </summary>
		[Test]
		public void GetPresences ()
		{
			uint[] @contacts = new uint[] { 2 };
			IDictionary<uint,SimplePresence> presences;
			test.GetPresences (contacts, out presences);
			presences[2] = new SimplePresence { Type = ConnectionPresenceType.Offline, Status = "offline", StatusMessage = "" };
			var presence = presences[2];
			Assert.AreEqual (ConnectionPresenceType.Offline, presence.Type);
			Assert.AreEqual ("offline", presence.Status);
			Assert.AreEqual ("", presence.StatusMessage);
		}
		
		/// <summary>
		/// 
		/// </summary>
		[Test]
		public void ReturnValues ()
		{
			string str = "abcd";
			Assert.AreEqual (str.Length, test.StringLength (str));
		}
		
		/// <summary>
		/// 
		/// </summary>
		[Test]
		public void ComplexAsVariant ()
		{
			MyTuple2 cpx = new MyTuple2 ();
			cpx.A = "a";
			cpx.B = "b";
			cpx.C = new Dictionary<int,MyTuple> ();
			cpx.C[3] = new MyTuple("foo", "bar");
			object cpxRet = test.ComplexAsVariant (cpx, 12);
			//MyTuple2 mt2ret = (MyTuple2)Convert.ChangeType (cpxRet, typeof (MyTuple2));
			var mt2ret = (DBusStruct<string, string, Dictionary<int, DBusStruct<string, string>>>)cpxRet;
			Assert.AreEqual (cpx.A, mt2ret.Item1);
			Assert.AreEqual (cpx.B, mt2ret.Item2);
			Assert.AreEqual (cpx.C[3].A, mt2ret.Item3[3].Item1);
			Assert.AreEqual (cpx.C[3].B, mt2ret.Item3[3].Item2);
		}

		/// <summary>
		/// 
		/// </summary>
		[Test]
		public void Property ()
		{
			int i = 99;
			test.SomeProp = i;
			Assert.AreEqual (i, test.SomeProp);
			test.SomeProp = i + 1;
			Assert.AreEqual (i + 1, test.SomeProp);
		}

		/// <summary>
		/// 
		/// </summary>
		[Test]
		public void CatchException ()
		{
			try {
				test.ThrowSomeException ();
				Assert.Fail ("An exception should be thrown before getting here");
			} catch (Exception ex) {
				Assert.IsTrue (ex.Message.Contains ("Some exception"));
			}
		}
		
		/* Locks up ?
		/// <summary>
		/// 
		/// </summary>
		[Test]
		public void ObjectArray ()
		{
			string str = "The best of times";
			ITestOne[] objs = test.GetObjectArray ();
			foreach (ITestOne obj in objs) {
				Assert.AreEqual (str.Length, obj.StringLength (str));
			}
		}*/
	}
	
	public delegate void SomeEventHandler (string arg1, object arg2, double arg3, MyTuple mt);

	[Interface ("org.dbussharp.test")]
	public interface ITestOne
	{
		event SomeEventHandler SomeEvent;
		void FireSomeEvent ();
		void VoidObject (object obj);
		int StringLength (string str);
		void VoidEnums (TestEnum a, TestEnum b);
		void VoidString (string str);
		object GetSomeVariant ();
		void ThrowSomeException ();
		void WithOutParameters (out uint n, string str, out string ostr);
		void WithOutParameters2 (out uint[] a1, out uint[] a2, out uint[] a3);
		void GetPresences (uint[] @contacts, out IDictionary<uint,SimplePresence> @presence);
		object ComplexAsVariant (object v, int num);
	
		ITestOne[] GetEmptyObjectArray ();
		ITestOne[] GetObjectArray ();
		int SomeProp { get; set; }
	}
	
	public class Test : ITestOne
	{
		public event SomeEventHandler SomeEvent;
		
		public bool void_enums_called = false;
		public bool void_object_called = false;
		public bool void_string_called = false;
	
		public void VoidObject (object var)
		{
			void_object_called = true;
		}
		
		public int StringLength (string str)
		{
			return str.Length;
		}
	
		public void VoidEnums (TestEnum a, TestEnum b)
		{
			void_enums_called = true;
		}
	
		public virtual void VoidString (string str)
		{
			void_string_called = true;
		}
	
		/*void IDemoTwo.Say2 (string str)
		{
			Console.WriteLine ("IDemoTwo.Say2: " + str);
		}*/
	
		public void FireSomeEvent ()
		{
			MyTuple mt;
			mt.A = "a";
			mt.B = "b";
	
			if (SomeEvent != null) {
				SomeEvent ("some string", 21, 19.84, mt);
			}
		}
	
		public object GetSomeVariant ()
		{
			return new byte[0];
		}
	
		public void ThrowSomeException ()
		{
			throw new Exception ("Some exception");
		}
	
		public void WithOutParameters (out uint n, string str, out string ostr)
		{
			n = UInt32.Parse (str);
			ostr = "." + str + ".";
		}
	
		public void WithOutParameters2 (out uint[] a1, out uint[] a2, out uint[] a3)
		{
			a1 = new uint[] { };
			a2 = new uint[] { 21, 23, 16 };
			a3 = new uint[] { 21, 23 };
		}
	
		public void GetPresences (uint[] @contacts, out IDictionary<uint,SimplePresence> @presence)
		{
			Dictionary<uint,SimplePresence> presences = new Dictionary<uint,SimplePresence>();
			presences[2] = new SimplePresence { Type = ConnectionPresenceType.Offline, Status = "offline", StatusMessage = "" };
			presence = presences;
		}
	
		public object ComplexAsVariant (object v, int num)
		{
			return v;
		}
	
		public ITestOne[] GetEmptyObjectArray ()
		{
			return new Test[] {};
		}
	
		public ITestOne[] GetObjectArray ()
		{
			return new ITestOne[] {this};
		}
	
		public int SomeProp { get; set; }
	}

	public enum TestEnum : byte
	{
		Foo,
		Bar,
	}

	public struct MyTuple
	{
		public MyTuple (string a, string b)
		{
			A = a;
			B = b;
		}
	
		public string A;
		public string B;
	}

	public struct MyTuple2
	{
		public string A;
		public string B;
		public IDictionary<int,MyTuple> C;
	}

	public enum ConnectionPresenceType : uint
	{
		Unset = 0, Offline = 1, Available = 2, Away = 3, ExtendedAway = 4, Hidden = 5, Busy = 6, Unknown = 7, Error = 8, 
	}

	public struct SimplePresence
	{
		public ConnectionPresenceType Type;
		public string Status;
		public string StatusMessage;
	}
}