AvaloniaObjectTests_Binding.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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.ComponentModel;
  5. using System.Reactive.Linq;
  6. using System.Reactive.Subjects;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Avalonia.Data;
  10. using Avalonia.Logging;
  11. using Avalonia.Platform;
  12. using Avalonia.Threading;
  13. using Avalonia.UnitTests;
  14. using Microsoft.Reactive.Testing;
  15. using Moq;
  16. using Xunit;
  17. namespace Avalonia.Base.UnitTests
  18. {
  19. public class AvaloniaObjectTests_Binding
  20. {
  21. [Fact]
  22. public void Bind_Sets_Current_Value()
  23. {
  24. var target = new Class1();
  25. var source = new Class1();
  26. var property = Class1.FooProperty;
  27. source.SetValue(property, "initial");
  28. target.Bind(property, source.GetObservable(property));
  29. Assert.Equal("initial", target.GetValue(property));
  30. }
  31. [Fact]
  32. public void Bind_Raises_PropertyChanged()
  33. {
  34. var target = new Class1();
  35. var source = new Subject<BindingValue<string>>();
  36. bool raised = false;
  37. target.PropertyChanged += (s, e) =>
  38. raised = e.Property == Class1.FooProperty &&
  39. (string)e.OldValue == "foodefault" &&
  40. (string)e.NewValue == "newvalue" &&
  41. e.Priority == BindingPriority.LocalValue;
  42. target.Bind(Class1.FooProperty, source);
  43. source.OnNext("newvalue");
  44. Assert.True(raised);
  45. }
  46. [Fact]
  47. public void PropertyChanged_Not_Raised_When_Value_Unchanged()
  48. {
  49. var target = new Class1();
  50. var source = new Subject<BindingValue<string>>();
  51. var raised = 0;
  52. target.PropertyChanged += (s, e) => ++raised;
  53. target.Bind(Class1.FooProperty, source);
  54. source.OnNext("newvalue");
  55. source.OnNext("newvalue");
  56. Assert.Equal(1, raised);
  57. }
  58. [Fact]
  59. public void Setting_LocalValue_Overrides_Binding_Until_Binding_Produces_Next_Value()
  60. {
  61. var target = new Class1();
  62. var source = new Subject<string>();
  63. var property = Class1.FooProperty;
  64. target.Bind(property, source);
  65. source.OnNext("foo");
  66. Assert.Equal("foo", target.GetValue(property));
  67. target.SetValue(property, "bar");
  68. Assert.Equal("bar", target.GetValue(property));
  69. source.OnNext("baz");
  70. Assert.Equal("baz", target.GetValue(property));
  71. }
  72. [Fact]
  73. public void Completing_LocalValue_Binding_Reverts_To_Default_Value_Even_When_Local_Value_Set_Earlier()
  74. {
  75. var target = new Class1();
  76. var source = new Subject<string>();
  77. var property = Class1.FooProperty;
  78. target.Bind(property, source);
  79. source.OnNext("foo");
  80. target.SetValue(property, "bar");
  81. source.OnNext("baz");
  82. source.OnCompleted();
  83. Assert.Equal("foodefault", target.GetValue(property));
  84. }
  85. [Fact]
  86. public void Completing_LocalValue_Binding_Should_Not_Revert_To_Set_LocalValue()
  87. {
  88. var target = new Class1();
  89. var source = new BehaviorSubject<string>("bar");
  90. target.SetValue(Class1.FooProperty, "foo");
  91. var sub = target.Bind(Class1.FooProperty, source);
  92. Assert.Equal("bar", target.GetValue(Class1.FooProperty));
  93. sub.Dispose();
  94. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  95. }
  96. [Fact]
  97. public void Completing_Animation_Binding_Reverts_To_Set_LocalValue()
  98. {
  99. var target = new Class1();
  100. var source = new Subject<string>();
  101. var property = Class1.FooProperty;
  102. target.SetValue(property, "foo");
  103. target.Bind(property, source, BindingPriority.Animation);
  104. source.OnNext("bar");
  105. source.OnCompleted();
  106. Assert.Equal("foo", target.GetValue(property));
  107. }
  108. [Fact]
  109. public void Completing_LocalValue_Binding_Raises_PropertyChanged()
  110. {
  111. var target = new Class1();
  112. var source = new BehaviorSubject<BindingValue<string>>("foo");
  113. var property = Class1.FooProperty;
  114. var raised = 0;
  115. target.Bind(property, source);
  116. Assert.Equal("foo", target.GetValue(property));
  117. target.PropertyChanged += (s, e) =>
  118. {
  119. Assert.Equal(BindingPriority.Unset, e.Priority);
  120. Assert.Equal(property, e.Property);
  121. Assert.Equal("foo", e.OldValue as string);
  122. Assert.Equal("foodefault", e.NewValue as string);
  123. ++raised;
  124. };
  125. source.OnCompleted();
  126. Assert.Equal("foodefault", target.GetValue(property));
  127. Assert.Equal(1, raised);
  128. }
  129. [Fact]
  130. public void Completing_Style_Binding_Raises_PropertyChanged()
  131. {
  132. var target = new Class1();
  133. var source = new BehaviorSubject<BindingValue<string>>("foo");
  134. var property = Class1.FooProperty;
  135. var raised = 0;
  136. target.Bind(property, source, BindingPriority.Style);
  137. Assert.Equal("foo", target.GetValue(property));
  138. target.PropertyChanged += (s, e) =>
  139. {
  140. Assert.Equal(BindingPriority.Unset, e.Priority);
  141. Assert.Equal(property, e.Property);
  142. Assert.Equal("foo", e.OldValue as string);
  143. Assert.Equal("foodefault", e.NewValue as string);
  144. ++raised;
  145. };
  146. source.OnCompleted();
  147. Assert.Equal("foodefault", target.GetValue(property));
  148. Assert.Equal(1, raised);
  149. }
  150. [Fact]
  151. public void Completing_LocalValue_Binding_With_Style_Binding_Raises_PropertyChanged()
  152. {
  153. var target = new Class1();
  154. var source = new BehaviorSubject<BindingValue<string>>("foo");
  155. var property = Class1.FooProperty;
  156. var raised = 0;
  157. target.Bind(property, new BehaviorSubject<string>("bar"), BindingPriority.Style);
  158. target.Bind(property, source);
  159. Assert.Equal("foo", target.GetValue(property));
  160. target.PropertyChanged += (s, e) =>
  161. {
  162. Assert.Equal(BindingPriority.Style, e.Priority);
  163. Assert.Equal(property, e.Property);
  164. Assert.Equal("foo", e.OldValue as string);
  165. Assert.Equal("bar", e.NewValue as string);
  166. ++raised;
  167. };
  168. source.OnCompleted();
  169. Assert.Equal("bar", target.GetValue(property));
  170. Assert.Equal(1, raised);
  171. }
  172. [Fact]
  173. public void Disposing_LocalValue_Binding_Raises_PropertyChanged()
  174. {
  175. var target = new Class1();
  176. var source = new BehaviorSubject<BindingValue<string>>("foo");
  177. var property = Class1.FooProperty;
  178. var raised = 0;
  179. var sub = target.Bind(property, source);
  180. Assert.Equal("foo", target.GetValue(property));
  181. target.PropertyChanged += (s, e) =>
  182. {
  183. Assert.Equal(BindingPriority.Unset, e.Priority);
  184. Assert.Equal(property, e.Property);
  185. Assert.Equal("foo", e.OldValue as string);
  186. Assert.Equal("foodefault", e.NewValue as string);
  187. ++raised;
  188. };
  189. sub.Dispose();
  190. Assert.Equal("foodefault", target.GetValue(property));
  191. Assert.Equal(1, raised);
  192. }
  193. [Fact]
  194. public void Setting_Style_Value_Overrides_Binding_Permanently()
  195. {
  196. var target = new Class1();
  197. var source = new Subject<string>();
  198. target.Bind(Class1.FooProperty, source, BindingPriority.Style);
  199. source.OnNext("foo");
  200. Assert.Equal("foo", target.GetValue(Class1.FooProperty));
  201. target.SetValue(Class1.FooProperty, "bar", BindingPriority.Style);
  202. Assert.Equal("bar", target.GetValue(Class1.FooProperty));
  203. source.OnNext("baz");
  204. Assert.Equal("bar", target.GetValue(Class1.FooProperty));
  205. }
  206. [Fact]
  207. public void Second_LocalValue_Binding_Overrides_First()
  208. {
  209. var property = Class1.FooProperty;
  210. var target = new Class1();
  211. var source1 = new Subject<string>();
  212. var source2 = new Subject<string>();
  213. target.Bind(property, source1, BindingPriority.LocalValue);
  214. target.Bind(property, source2, BindingPriority.LocalValue);
  215. source1.OnNext("foo");
  216. Assert.Equal("foo", target.GetValue(property));
  217. source2.OnNext("bar");
  218. Assert.Equal("bar", target.GetValue(property));
  219. source1.OnNext("baz");
  220. Assert.Equal("bar", target.GetValue(property));
  221. }
  222. [Fact]
  223. public void Completing_Second_LocalValue_Binding_Reverts_To_First()
  224. {
  225. var property = Class1.FooProperty;
  226. var target = new Class1();
  227. var source1 = new Subject<string>();
  228. var source2 = new Subject<string>();
  229. target.Bind(property, source1, BindingPriority.LocalValue);
  230. target.Bind(property, source2, BindingPriority.LocalValue);
  231. source1.OnNext("foo");
  232. source2.OnNext("bar");
  233. source1.OnNext("baz");
  234. source2.OnCompleted();
  235. Assert.Equal("baz", target.GetValue(property));
  236. }
  237. [Fact]
  238. public void Completing_StyleTrigger_Binding_Reverts_To_StyleBinding()
  239. {
  240. var property = Class1.FooProperty;
  241. var target = new Class1();
  242. var source1 = new Subject<string>();
  243. var source2 = new Subject<string>();
  244. target.Bind(property, source1, BindingPriority.Style);
  245. target.Bind(property, source2, BindingPriority.StyleTrigger);
  246. source1.OnNext("foo");
  247. source2.OnNext("bar");
  248. source2.OnCompleted();
  249. source1.OnNext("baz");
  250. Assert.Equal("baz", target.GetValue(property));
  251. }
  252. [Fact]
  253. public void Bind_NonGeneric_Sets_Current_Value()
  254. {
  255. Class1 target = new Class1();
  256. Class1 source = new Class1();
  257. source.SetValue(Class1.FooProperty, "initial");
  258. target.Bind((AvaloniaProperty)Class1.FooProperty, source.GetObservable(Class1.FooProperty));
  259. Assert.Equal("initial", target.GetValue(Class1.FooProperty));
  260. }
  261. [Fact]
  262. public void Bind_To_ValueType_Accepts_UnsetValue()
  263. {
  264. var target = new Class1();
  265. var source = new Subject<object>();
  266. target.Bind(Class1.QuxProperty, source);
  267. source.OnNext(6.7);
  268. source.OnNext(AvaloniaProperty.UnsetValue);
  269. Assert.Equal(5.6, target.GetValue(Class1.QuxProperty));
  270. Assert.False(target.IsSet(Class1.QuxProperty));
  271. }
  272. [Fact]
  273. public void OneTime_Binding_Ignores_UnsetValue()
  274. {
  275. var target = new Class1();
  276. var source = new Subject<object>();
  277. target.Bind(Class1.QuxProperty, new TestOneTimeBinding(source));
  278. source.OnNext(AvaloniaProperty.UnsetValue);
  279. Assert.Equal(5.6, target.GetValue(Class1.QuxProperty));
  280. source.OnNext(6.7);
  281. Assert.Equal(6.7, target.GetValue(Class1.QuxProperty));
  282. }
  283. [Fact]
  284. public void OneTime_Binding_Ignores_Binding_Errors()
  285. {
  286. var target = new Class1();
  287. var source = new Subject<object>();
  288. target.Bind(Class1.QuxProperty, new TestOneTimeBinding(source));
  289. source.OnNext(new BindingNotification(new Exception(), BindingErrorType.Error));
  290. Assert.Equal(5.6, target.GetValue(Class1.QuxProperty));
  291. source.OnNext(6.7);
  292. Assert.Equal(6.7, target.GetValue(Class1.QuxProperty));
  293. }
  294. [Fact]
  295. public void Bind_Does_Not_Throw_Exception_For_Unregistered_Property()
  296. {
  297. Class1 target = new Class1();
  298. target.Bind(Class2.BarProperty, Observable.Never<string>().StartWith("foo"));
  299. Assert.Equal("foo", target.GetValue(Class2.BarProperty));
  300. }
  301. [Fact]
  302. public void Bind_Sets_Subsequent_Value()
  303. {
  304. Class1 target = new Class1();
  305. Class1 source = new Class1();
  306. source.SetValue(Class1.FooProperty, "initial");
  307. target.Bind(Class1.FooProperty, source.GetObservable(Class1.FooProperty));
  308. source.SetValue(Class1.FooProperty, "subsequent");
  309. Assert.Equal("subsequent", target.GetValue(Class1.FooProperty));
  310. }
  311. [Fact]
  312. public void Bind_Ignores_Invalid_Value_Type()
  313. {
  314. Class1 target = new Class1();
  315. target.Bind((AvaloniaProperty)Class1.FooProperty, Observable.Return((object)123));
  316. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  317. }
  318. [Fact]
  319. public void Observable_Is_Unsubscribed_When_Subscription_Disposed()
  320. {
  321. var scheduler = new TestScheduler();
  322. var source = scheduler.CreateColdObservable<string>();
  323. var target = new Class1();
  324. var subscription = target.Bind(Class1.FooProperty, source);
  325. Assert.Equal(1, source.Subscriptions.Count);
  326. Assert.Equal(Subscription.Infinite, source.Subscriptions[0].Unsubscribe);
  327. subscription.Dispose();
  328. Assert.Equal(1, source.Subscriptions.Count);
  329. Assert.Equal(0, source.Subscriptions[0].Unsubscribe);
  330. }
  331. [Fact]
  332. public void Two_Way_Separate_Binding_Works()
  333. {
  334. Class1 obj1 = new Class1();
  335. Class1 obj2 = new Class1();
  336. obj1.SetValue(Class1.FooProperty, "initial1");
  337. obj2.SetValue(Class1.FooProperty, "initial2");
  338. obj1.Bind(Class1.FooProperty, obj2.GetObservable(Class1.FooProperty));
  339. obj2.Bind(Class1.FooProperty, obj1.GetObservable(Class1.FooProperty));
  340. Assert.Equal("initial2", obj1.GetValue(Class1.FooProperty));
  341. Assert.Equal("initial2", obj2.GetValue(Class1.FooProperty));
  342. obj1.SetValue(Class1.FooProperty, "first");
  343. Assert.Equal("first", obj1.GetValue(Class1.FooProperty));
  344. Assert.Equal("first", obj2.GetValue(Class1.FooProperty));
  345. obj2.SetValue(Class1.FooProperty, "second");
  346. Assert.Equal("second", obj1.GetValue(Class1.FooProperty));
  347. Assert.Equal("second", obj2.GetValue(Class1.FooProperty));
  348. obj1.SetValue(Class1.FooProperty, "third");
  349. Assert.Equal("third", obj1.GetValue(Class1.FooProperty));
  350. Assert.Equal("third", obj2.GetValue(Class1.FooProperty));
  351. }
  352. [Fact]
  353. public void Two_Way_Binding_With_Priority_Works()
  354. {
  355. Class1 obj1 = new Class1();
  356. Class1 obj2 = new Class1();
  357. obj1.SetValue(Class1.FooProperty, "initial1", BindingPriority.Style);
  358. obj2.SetValue(Class1.FooProperty, "initial2", BindingPriority.Style);
  359. obj1.Bind(Class1.FooProperty, obj2.GetObservable(Class1.FooProperty), BindingPriority.Style);
  360. obj2.Bind(Class1.FooProperty, obj1.GetObservable(Class1.FooProperty), BindingPriority.Style);
  361. Assert.Equal("initial2", obj1.GetValue(Class1.FooProperty));
  362. Assert.Equal("initial2", obj2.GetValue(Class1.FooProperty));
  363. obj1.SetValue(Class1.FooProperty, "first", BindingPriority.Style);
  364. Assert.Equal("first", obj1.GetValue(Class1.FooProperty));
  365. Assert.Equal("first", obj2.GetValue(Class1.FooProperty));
  366. obj2.SetValue(Class1.FooProperty, "second", BindingPriority.Style);
  367. Assert.Equal("first", obj1.GetValue(Class1.FooProperty));
  368. Assert.Equal("second", obj2.GetValue(Class1.FooProperty));
  369. obj1.SetValue(Class1.FooProperty, "third", BindingPriority.Style);
  370. Assert.Equal("third", obj1.GetValue(Class1.FooProperty));
  371. Assert.Equal("second", obj2.GetValue(Class1.FooProperty));
  372. }
  373. [Fact]
  374. public void Local_Binding_Overwrites_Local_Value()
  375. {
  376. var target = new Class1();
  377. var binding = new Subject<string>();
  378. target.Bind(Class1.FooProperty, binding);
  379. binding.OnNext("first");
  380. Assert.Equal("first", target.GetValue(Class1.FooProperty));
  381. target.SetValue(Class1.FooProperty, "second");
  382. Assert.Equal("second", target.GetValue(Class1.FooProperty));
  383. binding.OnNext("third");
  384. Assert.Equal("third", target.GetValue(Class1.FooProperty));
  385. }
  386. [Fact]
  387. public void StyleBinding_Overrides_Default_Value()
  388. {
  389. Class1 target = new Class1();
  390. target.Bind(Class1.FooProperty, Single("stylevalue"), BindingPriority.Style);
  391. Assert.Equal("stylevalue", target.GetValue(Class1.FooProperty));
  392. }
  393. [Fact]
  394. public void this_Operator_Returns_Value_Property()
  395. {
  396. Class1 target = new Class1();
  397. target.SetValue(Class1.FooProperty, "newvalue");
  398. Assert.Equal("newvalue", target[Class1.FooProperty]);
  399. }
  400. [Fact]
  401. public void this_Operator_Sets_Value_Property()
  402. {
  403. Class1 target = new Class1();
  404. target[Class1.FooProperty] = "newvalue";
  405. Assert.Equal("newvalue", target.GetValue(Class1.FooProperty));
  406. }
  407. [Fact]
  408. public void this_Operator_Doesnt_Accept_Observable()
  409. {
  410. Class1 target = new Class1();
  411. Assert.Throws<ArgumentException>(() =>
  412. {
  413. target[Class1.FooProperty] = Observable.Return("newvalue");
  414. });
  415. }
  416. [Fact]
  417. public void this_Operator_Binds_One_Way()
  418. {
  419. Class1 target1 = new Class1();
  420. Class2 target2 = new Class2();
  421. IndexerDescriptor binding = Class2.BarProperty.Bind().WithMode(BindingMode.OneWay);
  422. target1.SetValue(Class1.FooProperty, "first");
  423. target2[binding] = target1[!Class1.FooProperty];
  424. target1.SetValue(Class1.FooProperty, "second");
  425. Assert.Equal("second", target2.GetValue(Class2.BarProperty));
  426. }
  427. [Fact]
  428. public void this_Operator_Binds_Two_Way()
  429. {
  430. Class1 target1 = new Class1();
  431. Class1 target2 = new Class1();
  432. target1.SetValue(Class1.FooProperty, "first");
  433. target2[!Class1.FooProperty] = target1[!!Class1.FooProperty];
  434. Assert.Equal("first", target2.GetValue(Class1.FooProperty));
  435. target1.SetValue(Class1.FooProperty, "second");
  436. Assert.Equal("second", target2.GetValue(Class1.FooProperty));
  437. target2.SetValue(Class1.FooProperty, "third");
  438. Assert.Equal("third", target1.GetValue(Class1.FooProperty));
  439. }
  440. [Fact]
  441. public void this_Operator_Binds_One_Time()
  442. {
  443. Class1 target1 = new Class1();
  444. Class1 target2 = new Class1();
  445. target1.SetValue(Class1.FooProperty, "first");
  446. target2[!Class1.FooProperty] = target1[Class1.FooProperty.Bind().WithMode(BindingMode.OneTime)];
  447. target1.SetValue(Class1.FooProperty, "second");
  448. Assert.Equal("first", target2.GetValue(Class1.FooProperty));
  449. }
  450. [Fact]
  451. public void Binding_Error_Reverts_To_Default_Value()
  452. {
  453. var target = new Class1();
  454. var source = new Subject<BindingValue<string>>();
  455. target.Bind(Class1.FooProperty, source);
  456. source.OnNext("initial");
  457. source.OnNext(BindingValue<string>.BindingError(new InvalidOperationException("Foo")));
  458. Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
  459. }
  460. [Fact]
  461. public void Binding_Error_With_FallbackValue_Causes_Target_Update()
  462. {
  463. var target = new Class1();
  464. var source = new Subject<BindingValue<string>>();
  465. target.Bind(Class1.FooProperty, source);
  466. source.OnNext("initial");
  467. source.OnNext(BindingValue<string>.BindingError(new InvalidOperationException("Foo"), "bar"));
  468. Assert.Equal("bar", target.GetValue(Class1.FooProperty));
  469. }
  470. [Fact]
  471. public void DataValidationError_Does_Not_Cause_Target_Update()
  472. {
  473. var target = new Class1();
  474. var source = new Subject<BindingValue<string>>();
  475. target.Bind(Class1.FooProperty, source);
  476. source.OnNext("initial");
  477. source.OnNext(BindingValue<string>.DataValidationError(new InvalidOperationException("Foo")));
  478. Assert.Equal("initial", target.GetValue(Class1.FooProperty));
  479. }
  480. [Fact]
  481. public void DataValidationError_With_FallbackValue_Causes_Target_Update()
  482. {
  483. var target = new Class1();
  484. var source = new Subject<BindingValue<string>>();
  485. target.Bind(Class1.FooProperty, source);
  486. source.OnNext("initial");
  487. source.OnNext(BindingValue<string>.DataValidationError(new InvalidOperationException("Foo"), "bar"));
  488. Assert.Equal("bar", target.GetValue(Class1.FooProperty));
  489. }
  490. [Fact]
  491. public void Bind_Logs_Binding_Error()
  492. {
  493. var target = new Class1();
  494. var source = new Subject<BindingValue<double>>();
  495. var called = false;
  496. var expectedMessageTemplate = "Error in binding to {Target}.{Property}: {Message}";
  497. LogCallback checkLogMessage = (level, area, src, mt, pv) =>
  498. {
  499. if (level == LogEventLevel.Warning &&
  500. area == LogArea.Binding &&
  501. mt == expectedMessageTemplate)
  502. {
  503. called = true;
  504. }
  505. };
  506. using (TestLogSink.Start(checkLogMessage))
  507. {
  508. target.Bind(Class1.QuxProperty, source);
  509. source.OnNext(6.7);
  510. source.OnNext(BindingValue<double>.BindingError(new InvalidOperationException("Foo")));
  511. Assert.Equal(5.6, target.GetValue(Class1.QuxProperty));
  512. Assert.True(called);
  513. }
  514. }
  515. [Fact]
  516. public async Task Bind_With_Scheduler_Executes_On_Scheduler()
  517. {
  518. var target = new Class1();
  519. var source = new Subject<double>();
  520. var currentThreadId = Thread.CurrentThread.ManagedThreadId;
  521. var threadingInterfaceMock = new Mock<IPlatformThreadingInterface>();
  522. threadingInterfaceMock.SetupGet(mock => mock.CurrentThreadIsLoopThread)
  523. .Returns(() => Thread.CurrentThread.ManagedThreadId == currentThreadId);
  524. var services = new TestServices(
  525. scheduler: AvaloniaScheduler.Instance,
  526. threadingInterface: threadingInterfaceMock.Object);
  527. using (UnitTestApplication.Start(services))
  528. {
  529. target.Bind(Class1.QuxProperty, source);
  530. await Task.Run(() => source.OnNext(6.7));
  531. }
  532. }
  533. [Fact]
  534. public void SetValue_Should_Not_Cause_StackOverflow_And_Have_Correct_Values()
  535. {
  536. var viewModel = new TestStackOverflowViewModel()
  537. {
  538. Value = 50
  539. };
  540. var target = new Class1();
  541. target.Bind(Class1.DoubleValueProperty,
  542. new Binding("Value") { Mode = BindingMode.TwoWay, Source = viewModel });
  543. var child = new Class1();
  544. child[!!Class1.DoubleValueProperty] = target[!!Class1.DoubleValueProperty];
  545. Assert.Equal(1, viewModel.SetterInvokedCount);
  546. // Issues #855 and #824 were causing a StackOverflowException at this point.
  547. target.DoubleValue = 51.001;
  548. Assert.Equal(2, viewModel.SetterInvokedCount);
  549. double expected = 51;
  550. Assert.Equal(expected, viewModel.Value);
  551. Assert.Equal(expected, target.DoubleValue);
  552. Assert.Equal(expected, child.DoubleValue);
  553. }
  554. [Fact]
  555. public void IsAnimating_On_Property_With_No_Value_Returns_False()
  556. {
  557. var target = new Class1();
  558. Assert.False(target.IsAnimating(Class1.FooProperty));
  559. }
  560. [Fact]
  561. public void IsAnimating_On_Property_With_Animation_Value_Returns_True()
  562. {
  563. var target = new Class1();
  564. target.SetValue(Class1.FooProperty, "foo", BindingPriority.Animation);
  565. Assert.True(target.IsAnimating(Class1.FooProperty));
  566. }
  567. [Fact]
  568. public void IsAnimating_On_Property_With_Non_Animation_Binding_Returns_False()
  569. {
  570. var target = new Class1();
  571. var source = new Subject<string>();
  572. target.Bind(Class1.FooProperty, source, BindingPriority.LocalValue);
  573. Assert.False(target.IsAnimating(Class1.FooProperty));
  574. }
  575. [Fact]
  576. public void IsAnimating_On_Property_With_Animation_Binding_Returns_True()
  577. {
  578. var target = new Class1();
  579. var source = new BehaviorSubject<string>("foo");
  580. target.Bind(Class1.FooProperty, source, BindingPriority.Animation);
  581. Assert.True(target.IsAnimating(Class1.FooProperty));
  582. }
  583. [Fact]
  584. public void IsAnimating_On_Property_With_Local_Value_And_Animation_Binding_Returns_True()
  585. {
  586. var target = new Class1();
  587. var source = new BehaviorSubject<string>("foo");
  588. target.SetValue(Class1.FooProperty, "bar");
  589. target.Bind(Class1.FooProperty, source, BindingPriority.Animation);
  590. Assert.True(target.IsAnimating(Class1.FooProperty));
  591. }
  592. [Fact]
  593. public void IsAnimating_Returns_True_When_Animated_Value_Is_Same_As_Local_Value()
  594. {
  595. var target = new Class1();
  596. var source = new BehaviorSubject<string>("foo");
  597. target.SetValue(Class1.FooProperty, "foo");
  598. target.Bind(Class1.FooProperty, source, BindingPriority.Animation);
  599. Assert.True(target.IsAnimating(Class1.FooProperty));
  600. }
  601. [Fact]
  602. public void TwoWay_Binding_Should_Not_Call_Setter_On_Creation()
  603. {
  604. var target = new Class1();
  605. var source = new TestTwoWayBindingViewModel();
  606. target.Bind(Class1.DoubleValueProperty, new Binding(nameof(source.Value), BindingMode.TwoWay) { Source = source });
  607. Assert.False(source.SetterCalled);
  608. }
  609. [Fact]
  610. public void TwoWay_Binding_Should_Not_Call_Setter_On_Creation_Indexer()
  611. {
  612. var target = new Class1();
  613. var source = new TestTwoWayBindingViewModel();
  614. target.Bind(Class1.DoubleValueProperty, new Binding("[0]", BindingMode.TwoWay) { Source = source });
  615. Assert.False(source.SetterCalled);
  616. }
  617. [Fact]
  618. public void Disposing_Completed_Binding_Does_Not_Throw()
  619. {
  620. var target = new Class1();
  621. var source = new Subject<string>();
  622. var subscription = target.Bind(Class1.FooProperty, source);
  623. source.OnCompleted();
  624. subscription.Dispose();
  625. }
  626. /// <summary>
  627. /// Returns an observable that returns a single value but does not complete.
  628. /// </summary>
  629. /// <typeparam name="T">The type of the observable.</typeparam>
  630. /// <param name="value">The value.</param>
  631. /// <returns>The observable.</returns>
  632. private IObservable<T> Single<T>(T value)
  633. {
  634. return Observable.Never<T>().StartWith(value);
  635. }
  636. private class Class1 : AvaloniaObject
  637. {
  638. public static readonly StyledProperty<string> FooProperty =
  639. AvaloniaProperty.Register<Class1, string>("Foo", "foodefault");
  640. public static readonly StyledProperty<double> QuxProperty =
  641. AvaloniaProperty.Register<Class1, double>("Qux", 5.6);
  642. public static readonly StyledProperty<double> DoubleValueProperty =
  643. AvaloniaProperty.Register<Class1, double>(nameof(DoubleValue));
  644. public double DoubleValue
  645. {
  646. get { return GetValue(DoubleValueProperty); }
  647. set { SetValue(DoubleValueProperty, value); }
  648. }
  649. }
  650. private class Class2 : Class1
  651. {
  652. public static readonly StyledProperty<string> BarProperty =
  653. AvaloniaProperty.Register<Class2, string>("Bar", "bardefault");
  654. }
  655. private class TestOneTimeBinding : IBinding
  656. {
  657. private IObservable<object> _source;
  658. public TestOneTimeBinding(IObservable<object> source)
  659. {
  660. _source = source;
  661. }
  662. public InstancedBinding Initiate(
  663. IAvaloniaObject target,
  664. AvaloniaProperty targetProperty,
  665. object anchor = null,
  666. bool enableDataValidation = false)
  667. {
  668. return InstancedBinding.OneTime(_source);
  669. }
  670. }
  671. private class TestStackOverflowViewModel : INotifyPropertyChanged
  672. {
  673. public int SetterInvokedCount { get; private set; }
  674. public const int MaxInvokedCount = 1000;
  675. private double _value;
  676. public event PropertyChangedEventHandler PropertyChanged;
  677. public double Value
  678. {
  679. get { return _value; }
  680. set
  681. {
  682. if (_value != value)
  683. {
  684. SetterInvokedCount++;
  685. if (SetterInvokedCount < MaxInvokedCount)
  686. {
  687. _value = (int)value;
  688. if (_value > 75) _value = 75;
  689. if (_value < 25) _value = 25;
  690. }
  691. else
  692. {
  693. _value = value;
  694. }
  695. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value)));
  696. }
  697. }
  698. }
  699. }
  700. private class TestTwoWayBindingViewModel
  701. {
  702. private double _value;
  703. public double Value
  704. {
  705. get => _value;
  706. set
  707. {
  708. _value = value;
  709. SetterCalled = true;
  710. }
  711. }
  712. public double this[int index]
  713. {
  714. get => _value;
  715. set
  716. {
  717. _value = value;
  718. SetterCalled = true;
  719. }
  720. }
  721. public bool SetterCalled { get; private set; }
  722. }
  723. }
  724. }