AvaloniaObjectTests_Binding.cs 37 KB

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