ExpressionObserverTests_Property.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Reactive;
  6. using System.Reactive.Linq;
  7. using System.Reactive.Subjects;
  8. using Microsoft.Reactive.Testing;
  9. using Avalonia.Data;
  10. using Avalonia.Markup.Data;
  11. using Avalonia.UnitTests;
  12. using Xunit;
  13. namespace Avalonia.Markup.UnitTests.Data
  14. {
  15. public class ExpressionObserverTests_Property
  16. {
  17. [Fact]
  18. public async void Should_Get_Simple_Property_Value()
  19. {
  20. var data = new { Foo = "foo" };
  21. var target = new ExpressionObserver(data, "Foo");
  22. var result = await target.Take(1);
  23. Assert.Equal("foo", result);
  24. }
  25. [Fact]
  26. public void Should_Get_Simple_Property_Value_Type()
  27. {
  28. var data = new { Foo = "foo" };
  29. var target = new ExpressionObserver(data, "Foo");
  30. target.Subscribe(_ => { });
  31. Assert.Equal(typeof(string), target.ResultType);
  32. }
  33. [Fact]
  34. public async void Should_Get_Simple_Property_Value_Null()
  35. {
  36. var data = new { Foo = (string)null };
  37. var target = new ExpressionObserver(data, "Foo");
  38. var result = await target.Take(1);
  39. Assert.Null(result);
  40. }
  41. [Fact]
  42. public async void Should_Get_Simple_Property_From_Base_Class()
  43. {
  44. var data = new Class3 { Foo = "foo" };
  45. var target = new ExpressionObserver(data, "Foo");
  46. var result = await target.Take(1);
  47. Assert.Equal("foo", result);
  48. }
  49. [Fact]
  50. public async void Should_Return_BindingNotification_Error_For_Root_Null()
  51. {
  52. var data = new Class3 { Foo = "foo" };
  53. var target = new ExpressionObserver(default(object), "Foo");
  54. var result = await target.Take(1);
  55. Assert.Equal(
  56. new BindingNotification(
  57. new NullReferenceException(),
  58. BindingErrorType.Error,
  59. AvaloniaProperty.UnsetValue),
  60. result);
  61. }
  62. [Fact]
  63. public async void Should_Return_BindingNotification_Error_For_Root_UnsetValue()
  64. {
  65. var data = new Class3 { Foo = "foo" };
  66. var target = new ExpressionObserver(AvaloniaProperty.UnsetValue, "Foo");
  67. var result = await target.Take(1);
  68. Assert.Equal(
  69. new BindingNotification(
  70. new NullReferenceException(),
  71. BindingErrorType.Error,
  72. AvaloniaProperty.UnsetValue),
  73. result);
  74. }
  75. [Fact]
  76. public async void Should_Return_BindingNotification_Error_For_Observable_Root_Null()
  77. {
  78. var data = new Class3 { Foo = "foo" };
  79. var target = new ExpressionObserver(Observable.Return(default(object)), "Foo");
  80. var result = await target.Take(1);
  81. Assert.Equal(
  82. new BindingNotification(
  83. new NullReferenceException(),
  84. BindingErrorType.Error,
  85. AvaloniaProperty.UnsetValue),
  86. result);
  87. }
  88. [Fact]
  89. public async void Should_Return_BindingNotification_Error_For_Observable_Root_UnsetValue()
  90. {
  91. var data = new Class3 { Foo = "foo" };
  92. var target = new ExpressionObserver(Observable.Return(AvaloniaProperty.UnsetValue), "Foo");
  93. var result = await target.Take(1);
  94. Assert.Equal(
  95. new BindingNotification(
  96. new NullReferenceException(),
  97. BindingErrorType.Error,
  98. AvaloniaProperty.UnsetValue),
  99. result);
  100. }
  101. [Fact]
  102. public async void Should_Get_Simple_Property_Chain()
  103. {
  104. var data = new { Foo = new { Bar = new { Baz = "baz" } } };
  105. var target = new ExpressionObserver(data, "Foo.Bar.Baz");
  106. var result = await target.Take(1);
  107. Assert.Equal("baz", result);
  108. }
  109. [Fact]
  110. public void Should_Get_Simple_Property_Chain_Type()
  111. {
  112. var data = new { Foo = new { Bar = new { Baz = "baz" } } };
  113. var target = new ExpressionObserver(data, "Foo.Bar.Baz");
  114. target.Subscribe(_ => { });
  115. Assert.Equal(typeof(string), target.ResultType);
  116. }
  117. [Fact]
  118. public async void Should_Return_BindingNotification_Error_For_Broken_Chain()
  119. {
  120. var data = new { Foo = new { Bar = 1 } };
  121. var target = new ExpressionObserver(data, "Foo.Bar.Baz");
  122. var result = await target.Take(1);
  123. Assert.IsType<BindingNotification>(result);
  124. Assert.Equal(
  125. new BindingNotification(
  126. new MissingMemberException("Could not find CLR property 'Baz' on '1'"), BindingErrorType.Error),
  127. result);
  128. }
  129. [Fact]
  130. public void Should_Return_BindingNotification_Error_For_Chain_With_Null_Value()
  131. {
  132. var data = new { Foo = default(object) };
  133. var target = new ExpressionObserver(data, "Foo.Bar.Baz");
  134. var result = new List<object>();
  135. target.Subscribe(x => result.Add(x));
  136. Assert.Equal(
  137. new[]
  138. {
  139. new BindingNotification(
  140. new NullReferenceException(),
  141. BindingErrorType.Error,
  142. AvaloniaProperty.UnsetValue),
  143. },
  144. result);
  145. }
  146. [Fact]
  147. public void Should_Have_Null_ResultType_For_Broken_Chain()
  148. {
  149. var data = new { Foo = new { Bar = 1 } };
  150. var target = new ExpressionObserver(data, "Foo.Bar.Baz");
  151. Assert.Null(target.ResultType);
  152. }
  153. [Fact]
  154. public void Should_Track_Simple_Property_Value()
  155. {
  156. var data = new Class1 { Foo = "foo" };
  157. var target = new ExpressionObserver(data, "Foo");
  158. var result = new List<object>();
  159. var sub = target.Subscribe(x => result.Add(x));
  160. data.Foo = "bar";
  161. Assert.Equal(new[] { "foo", "bar" }, result);
  162. sub.Dispose();
  163. Assert.Equal(0, data.PropertyChangedSubscriptionCount);
  164. }
  165. [Fact]
  166. public void Should_Trigger_PropertyChanged_On_Null_Or_Empty_String()
  167. {
  168. var data = new Class1 { Bar = "foo" };
  169. var target = new ExpressionObserver(data, "Bar");
  170. var result = new List<object>();
  171. var sub = target.Subscribe(x => result.Add(x));
  172. Assert.Equal(new[] { "foo" }, result);
  173. data.Bar = "bar";
  174. Assert.Equal(new[] { "foo" }, result);
  175. data.RaisePropertyChanged(string.Empty);
  176. Assert.Equal(new[] { "foo", "bar" }, result);
  177. data.RaisePropertyChanged(null);
  178. Assert.Equal(new[] { "foo", "bar", "bar" }, result);
  179. sub.Dispose();
  180. Assert.Equal(0, data.PropertyChangedSubscriptionCount);
  181. }
  182. [Fact]
  183. public void Should_Track_End_Of_Property_Chain_Changing()
  184. {
  185. var data = new Class1 { Next = new Class2 { Bar = "bar" } };
  186. var target = new ExpressionObserver(data, "Next.Bar");
  187. var result = new List<object>();
  188. var sub = target.Subscribe(x => result.Add(x));
  189. ((Class2)data.Next).Bar = "baz";
  190. ((Class2)data.Next).Bar = null;
  191. Assert.Equal(new[] { "bar", "baz", null }, result);
  192. sub.Dispose();
  193. Assert.Equal(0, data.PropertyChangedSubscriptionCount);
  194. Assert.Equal(0, data.Next.PropertyChangedSubscriptionCount);
  195. }
  196. [Fact]
  197. public void Should_Track_Property_Chain_Changing()
  198. {
  199. var data = new Class1 { Next = new Class2 { Bar = "bar" } };
  200. var target = new ExpressionObserver(data, "Next.Bar");
  201. var result = new List<object>();
  202. var sub = target.Subscribe(x => result.Add(x));
  203. var old = data.Next;
  204. data.Next = new Class2 { Bar = "baz" };
  205. data.Next = new Class2 { Bar = null };
  206. Assert.Equal(new[] { "bar", "baz", null }, result);
  207. sub.Dispose();
  208. Assert.Equal(0, data.PropertyChangedSubscriptionCount);
  209. Assert.Equal(0, data.Next.PropertyChangedSubscriptionCount);
  210. Assert.Equal(0, old.PropertyChangedSubscriptionCount);
  211. }
  212. [Fact]
  213. public void Should_Track_Property_Chain_Breaking_With_Null_Then_Mending()
  214. {
  215. var data = new Class1 { Next = new Class2 { Bar = "bar" } };
  216. var target = new ExpressionObserver(data, "Next.Bar");
  217. var result = new List<object>();
  218. var sub = target.Subscribe(x => result.Add(x));
  219. var old = data.Next;
  220. data.Next = null;
  221. data.Next = new Class2 { Bar = "baz" };
  222. Assert.Equal(
  223. new object[]
  224. {
  225. "bar",
  226. new BindingNotification(
  227. new NullReferenceException(),
  228. BindingErrorType.Error,
  229. AvaloniaProperty.UnsetValue),
  230. "baz"
  231. },
  232. result);
  233. sub.Dispose();
  234. Assert.Equal(0, data.PropertyChangedSubscriptionCount);
  235. Assert.Equal(0, data.Next.PropertyChangedSubscriptionCount);
  236. Assert.Equal(0, old.PropertyChangedSubscriptionCount);
  237. }
  238. [Fact]
  239. public void Should_Track_Property_Chain_Breaking_With_Object_Then_Mending()
  240. {
  241. var data = new Class1 { Next = new Class2 { Bar = "bar" } };
  242. var target = new ExpressionObserver(data, "Next.Bar");
  243. var result = new List<object>();
  244. var sub = target.Subscribe(x => result.Add(x));
  245. var old = data.Next;
  246. var breaking = new WithoutBar();
  247. data.Next = breaking;
  248. data.Next = new Class2 { Bar = "baz" };
  249. Assert.Equal(3, result.Count);
  250. Assert.Equal("bar", result[0]);
  251. Assert.IsType<BindingNotification>(result[1]);
  252. Assert.Equal("baz", result[2]);
  253. sub.Dispose();
  254. Assert.Equal(0, data.PropertyChangedSubscriptionCount);
  255. Assert.Equal(0, data.Next.PropertyChangedSubscriptionCount);
  256. Assert.Equal(0, breaking.PropertyChangedSubscriptionCount);
  257. Assert.Equal(0, old.PropertyChangedSubscriptionCount);
  258. }
  259. [Fact]
  260. public void Empty_Expression_Should_Track_Root()
  261. {
  262. var data = new Class1 { Foo = "foo" };
  263. var update = new Subject<Unit>();
  264. var target = new ExpressionObserver(() => data.Foo, "", update);
  265. var result = new List<object>();
  266. target.Subscribe(x => result.Add(x));
  267. data.Foo = "bar";
  268. update.OnNext(Unit.Default);
  269. Assert.Equal(new[] { "foo", "bar" }, result);
  270. }
  271. [Fact]
  272. public void Should_Track_Property_Value_From_Observable_Root()
  273. {
  274. var scheduler = new TestScheduler();
  275. var source = scheduler.CreateColdObservable(
  276. OnNext(1, new Class1 { Foo = "foo" }),
  277. OnNext(2, new Class1 { Foo = "bar" }));
  278. var target = new ExpressionObserver(source, "Foo");
  279. var result = new List<object>();
  280. using (target.Subscribe(x => result.Add(x)))
  281. {
  282. scheduler.Start();
  283. }
  284. Assert.Equal(new[] { "foo", "bar" }, result);
  285. Assert.All(source.Subscriptions, x => Assert.NotEqual(Subscription.Infinite, x.Unsubscribe));
  286. }
  287. [Fact]
  288. public void Subscribing_Multiple_Times_Should_Return_Values_To_All()
  289. {
  290. var data = new Class1 { Foo = "foo" };
  291. var target = new ExpressionObserver(data, "Foo");
  292. var result1 = new List<object>();
  293. var result2 = new List<object>();
  294. var result3 = new List<object>();
  295. target.Subscribe(x => result1.Add(x));
  296. target.Subscribe(x => result2.Add(x));
  297. data.Foo = "bar";
  298. target.Subscribe(x => result3.Add(x));
  299. Assert.Equal(new[] { "foo", "bar" }, result1);
  300. Assert.Equal(new[] { "foo", "bar" }, result2);
  301. Assert.Equal(new[] { "bar" }, result3);
  302. }
  303. [Fact]
  304. public void Subscribing_Multiple_Times_Should_Only_Add_PropertyChanged_Handlers_Once()
  305. {
  306. var data = new Class1 { Foo = "foo" };
  307. var target = new ExpressionObserver(data, "Foo");
  308. var sub1 = target.Subscribe(x => { });
  309. var sub2 = target.Subscribe(x => { });
  310. Assert.Equal(1, data.PropertyChangedSubscriptionCount);
  311. sub1.Dispose();
  312. sub2.Dispose();
  313. Assert.Equal(0, data.PropertyChangedSubscriptionCount);
  314. }
  315. [Fact]
  316. public void SetValue_Should_Set_Simple_Property_Value()
  317. {
  318. var data = new Class1 { Foo = "foo" };
  319. var target = new ExpressionObserver(data, "Foo");
  320. using (target.Subscribe(_ => { }))
  321. {
  322. Assert.True(target.SetValue("bar"));
  323. }
  324. Assert.Equal("bar", data.Foo);
  325. }
  326. [Fact]
  327. public void SetValue_Should_Set_Property_At_The_End_Of_Chain()
  328. {
  329. var data = new Class1 { Next = new Class2 { Bar = "bar" } };
  330. var target = new ExpressionObserver(data, "Next.Bar");
  331. using (target.Subscribe(_ => { }))
  332. {
  333. Assert.True(target.SetValue("baz"));
  334. }
  335. Assert.Equal("baz", ((Class2)data.Next).Bar);
  336. }
  337. [Fact]
  338. public void SetValue_Should_Return_False_For_Missing_Property()
  339. {
  340. var data = new Class1 { Next = new WithoutBar()};
  341. var target = new ExpressionObserver(data, "Next.Bar");
  342. using (target.Subscribe(_ => { }))
  343. {
  344. Assert.False(target.SetValue("baz"));
  345. }
  346. }
  347. [Fact]
  348. public void SetValue_Should_Return_False_For_Missing_Object()
  349. {
  350. var data = new Class1();
  351. var target = new ExpressionObserver(data, "Next.Bar");
  352. using (target.Subscribe(_ => { }))
  353. {
  354. Assert.False(target.SetValue("baz"));
  355. }
  356. }
  357. [Fact]
  358. public async void Should_Handle_Null_Root()
  359. {
  360. var target = new ExpressionObserver((object)null, "Foo");
  361. var result = await target.Take(1);
  362. Assert.Equal(
  363. new BindingNotification(
  364. new NullReferenceException(),
  365. BindingErrorType.Error,
  366. AvaloniaProperty.UnsetValue),
  367. result);
  368. }
  369. [Fact]
  370. public void Can_Replace_Root()
  371. {
  372. var first = new Class1 { Foo = "foo" };
  373. var second = new Class1 { Foo = "bar" };
  374. var root = first;
  375. var update = new Subject<Unit>();
  376. var target = new ExpressionObserver(() => root, "Foo", update);
  377. var result = new List<object>();
  378. var sub = target.Subscribe(x => result.Add(x));
  379. root = second;
  380. update.OnNext(Unit.Default);
  381. root = null;
  382. update.OnNext(Unit.Default);
  383. Assert.Equal(
  384. new object[]
  385. {
  386. "foo",
  387. "bar",
  388. new BindingNotification(
  389. new NullReferenceException(),
  390. BindingErrorType.Error,
  391. AvaloniaProperty.UnsetValue),
  392. },
  393. result);
  394. Assert.Equal(0, first.PropertyChangedSubscriptionCount);
  395. Assert.Equal(0, second.PropertyChangedSubscriptionCount);
  396. }
  397. [Fact]
  398. public void Should_Not_Keep_Source_Alive()
  399. {
  400. Func<Tuple<ExpressionObserver, WeakReference>> run = () =>
  401. {
  402. var source = new Class1 { Foo = "foo" };
  403. var target = new ExpressionObserver(source, "Foo");
  404. return Tuple.Create(target, new WeakReference(source));
  405. };
  406. var result = run();
  407. result.Item1.Subscribe(x => { });
  408. GC.Collect();
  409. Assert.Null(result.Item2.Target);
  410. }
  411. private interface INext
  412. {
  413. int PropertyChangedSubscriptionCount { get; }
  414. }
  415. private class Class1 : NotifyingBase
  416. {
  417. private string _foo;
  418. private INext _next;
  419. public string Foo
  420. {
  421. get { return _foo; }
  422. set
  423. {
  424. _foo = value;
  425. RaisePropertyChanged(nameof(Foo));
  426. }
  427. }
  428. private string _bar;
  429. public string Bar
  430. {
  431. get { return _bar; }
  432. set { _bar = value; }
  433. }
  434. public INext Next
  435. {
  436. get { return _next; }
  437. set
  438. {
  439. _next = value;
  440. RaisePropertyChanged(nameof(Next));
  441. }
  442. }
  443. }
  444. private class Class2 : NotifyingBase, INext
  445. {
  446. private string _bar;
  447. public string Bar
  448. {
  449. get { return _bar; }
  450. set
  451. {
  452. _bar = value;
  453. RaisePropertyChanged(nameof(Bar));
  454. }
  455. }
  456. }
  457. private class Class3 : Class1
  458. {
  459. }
  460. private class WithoutBar : NotifyingBase, INext
  461. {
  462. }
  463. private Recorded<Notification<object>> OnNext(long time, object value)
  464. {
  465. return new Recorded<Notification<object>>(time, Notification.CreateOnNext<object>(value));
  466. }
  467. }
  468. }