AnimatableTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. using System;
  2. using Avalonia.Controls;
  3. using Avalonia.Data;
  4. using Avalonia.Layout;
  5. using Avalonia.Media;
  6. using Avalonia.Styling;
  7. using Avalonia.UnitTests;
  8. using Moq;
  9. using Xunit;
  10. namespace Avalonia.Animation.UnitTests
  11. {
  12. public class AnimatableTests
  13. {
  14. [Fact]
  15. public void Transition_Is_Not_Applied_When_Not_Attached_To_Visual_Tree()
  16. {
  17. var target = CreateTarget();
  18. var control = new Control
  19. {
  20. Transitions = new Transitions { target.Object },
  21. };
  22. control.Opacity = 0.5;
  23. target.Verify(x => x.Apply(
  24. control,
  25. It.IsAny<IClock>(),
  26. 1.0,
  27. 0.5),
  28. Times.Never);
  29. }
  30. [Fact]
  31. public void Transition_Is_Not_Applied_To_Initial_Style()
  32. {
  33. using (UnitTestApplication.Start(TestServices.RealStyler))
  34. {
  35. var target = CreateTarget();
  36. var control = new Control
  37. {
  38. Transitions = new Transitions { target.Object },
  39. };
  40. var root = new TestRoot
  41. {
  42. Styles =
  43. {
  44. new Style(x => x.OfType<Control>())
  45. {
  46. Setters =
  47. {
  48. new Setter(Visual.OpacityProperty, 0.8),
  49. }
  50. }
  51. }
  52. };
  53. root.Child = control;
  54. Assert.Equal(0.8, control.Opacity);
  55. target.Verify(x => x.Apply(
  56. It.IsAny<Control>(),
  57. It.IsAny<IClock>(),
  58. It.IsAny<object>(),
  59. It.IsAny<object>()),
  60. Times.Never);
  61. }
  62. }
  63. [Fact]
  64. public void Transition_Is_Applied_When_Local_Value_Changes()
  65. {
  66. var target = CreateTarget();
  67. var control = CreateControl(target.Object);
  68. control.Opacity = 0.5;
  69. target.Verify(x => x.Apply(
  70. control,
  71. It.IsAny<IClock>(),
  72. 1.0,
  73. 0.5));
  74. }
  75. [Fact]
  76. public void Transition_Is_Not_Applied_When_Animated_Value_Changes()
  77. {
  78. var target = CreateTarget();
  79. var control = CreateControl(target.Object);
  80. control.SetValue(Visual.OpacityProperty, 0.5, BindingPriority.Animation);
  81. target.Verify(x => x.Apply(
  82. control,
  83. It.IsAny<IClock>(),
  84. 1.0,
  85. 0.5),
  86. Times.Never);
  87. }
  88. [Fact]
  89. public void Transition_Is_Not_Applied_When_StyleTrigger_Changes_With_LocalValue_Present()
  90. {
  91. var target = CreateTarget();
  92. var control = CreateControl(target.Object);
  93. control.SetValue(Visual.OpacityProperty, 0.5);
  94. target.Verify(x => x.Apply(
  95. control,
  96. It.IsAny<IClock>(),
  97. 1.0,
  98. 0.5));
  99. target.Invocations.Clear();
  100. control.SetValue(Visual.OpacityProperty, 0.8, BindingPriority.StyleTrigger);
  101. target.Verify(x => x.Apply(
  102. It.IsAny<Control>(),
  103. It.IsAny<IClock>(),
  104. It.IsAny<object>(),
  105. It.IsAny<object>()),
  106. Times.Never);
  107. }
  108. [Fact]
  109. public void Transition_Is_Disposed_When_Local_Value_Changes()
  110. {
  111. var target = CreateTarget();
  112. var control = CreateControl(target.Object);
  113. var sub = new Mock<IDisposable>();
  114. target.Setup(x => x.Apply(control, It.IsAny<IClock>(), 1.0, 0.5)).Returns(sub.Object);
  115. control.Opacity = 0.5;
  116. sub.Invocations.Clear();
  117. control.Opacity = 0.4;
  118. sub.Verify(x => x.Dispose());
  119. }
  120. [Fact]
  121. public void New_Transition_Is_Applied_When_Local_Value_Changes()
  122. {
  123. var target = CreateTarget();
  124. var control = CreateControl(target.Object);
  125. target.Setup(x => x.Property).Returns(Visual.OpacityProperty);
  126. target.Setup(x => x.Apply(control, It.IsAny<IClock>(), 1.0, 0.5))
  127. .Callback(() =>
  128. {
  129. control.SetValue(Visual.OpacityProperty, 0.9, BindingPriority.Animation);
  130. })
  131. .Returns(Mock.Of<IDisposable>());
  132. control.Opacity = 0.5;
  133. Assert.Equal(0.9, control.Opacity);
  134. target.Invocations.Clear();
  135. control.Opacity = 0.4;
  136. target.Verify(x => x.Apply(
  137. control,
  138. It.IsAny<IClock>(),
  139. 0.9,
  140. 0.4));
  141. }
  142. [Fact]
  143. public void Transition_Is_Not_Applied_When_Removed_From_Visual_Tree()
  144. {
  145. var target = CreateTarget();
  146. var control = CreateControl(target.Object);
  147. control.Opacity = 0.5;
  148. target.Verify(x => x.Apply(
  149. control,
  150. It.IsAny<IClock>(),
  151. 1.0,
  152. 0.5));
  153. target.Invocations.Clear();
  154. var root = (TestRoot)control.Parent;
  155. root.Child = null;
  156. control.Opacity = 0.8;
  157. target.Verify(x => x.Apply(
  158. It.IsAny<Control>(),
  159. It.IsAny<IClock>(),
  160. It.IsAny<object>(),
  161. It.IsAny<object>()),
  162. Times.Never);
  163. }
  164. [Fact]
  165. public void Animation_Is_Cancelled_When_Transition_Removed()
  166. {
  167. var target = CreateTarget();
  168. var control = CreateControl(target.Object);
  169. var sub = new Mock<IDisposable>();
  170. target.Setup(x => x.Apply(
  171. It.IsAny<Animatable>(),
  172. It.IsAny<IClock>(),
  173. It.IsAny<object>(),
  174. It.IsAny<object>())).Returns(sub.Object);
  175. control.Opacity = 0.5;
  176. control.Transitions.RemoveAt(0);
  177. sub.Verify(x => x.Dispose());
  178. }
  179. [Fact]
  180. public void Animation_Is_Cancelled_When_New_Style_Activates()
  181. {
  182. using (UnitTestApplication.Start(TestServices.RealStyler))
  183. {
  184. var target = CreateTarget();
  185. var control = CreateStyledControl(target.Object);
  186. var sub = new Mock<IDisposable>();
  187. target.Setup(x => x.Apply(
  188. control,
  189. It.IsAny<IClock>(),
  190. 1.0,
  191. 0.5)).Returns(sub.Object);
  192. control.Opacity = 0.5;
  193. target.Verify(x => x.Apply(
  194. control,
  195. It.IsAny<Clock>(),
  196. 1.0,
  197. 0.5),
  198. Times.Once);
  199. control.Classes.Add("foo");
  200. sub.Verify(x => x.Dispose());
  201. }
  202. }
  203. [Fact]
  204. public void Transition_From_Style_Trigger_Is_Applied()
  205. {
  206. using (UnitTestApplication.Start(TestServices.RealStyler))
  207. {
  208. var target = CreateTransition(Control.WidthProperty);
  209. var control = CreateStyledControl(transition2: target.Object);
  210. var sub = new Mock<IDisposable>();
  211. control.Classes.Add("foo");
  212. control.Width = 100;
  213. target.Verify(x => x.Apply(
  214. control,
  215. It.IsAny<Clock>(),
  216. double.NaN,
  217. 100.0),
  218. Times.Once);
  219. }
  220. }
  221. [Fact]
  222. public void Replacing_Transitions_During_Animation_Does_Not_Throw_KeyNotFound()
  223. {
  224. // Issue #4059
  225. using (UnitTestApplication.Start(TestServices.RealStyler))
  226. {
  227. Border target;
  228. var clock = new TestClock();
  229. var root = new TestRoot
  230. {
  231. Clock = clock,
  232. Styles =
  233. {
  234. new Style(x => x.OfType<Border>())
  235. {
  236. Setters =
  237. {
  238. new Setter(Border.TransitionsProperty,
  239. new Transitions
  240. {
  241. new DoubleTransition
  242. {
  243. Property = Border.OpacityProperty,
  244. Duration = TimeSpan.FromSeconds(1),
  245. },
  246. }),
  247. },
  248. },
  249. new Style(x => x.OfType<Border>().Class("foo"))
  250. {
  251. Setters =
  252. {
  253. new Setter(Border.TransitionsProperty,
  254. new Transitions
  255. {
  256. new DoubleTransition
  257. {
  258. Property = Border.OpacityProperty,
  259. Duration = TimeSpan.FromSeconds(1),
  260. },
  261. }),
  262. new Setter(Border.OpacityProperty, 0.0),
  263. },
  264. },
  265. },
  266. Child = target = new Border
  267. {
  268. Background = Brushes.Red,
  269. }
  270. };
  271. root.Measure(Size.Infinity);
  272. root.Arrange(new Rect(root.DesiredSize));
  273. target.Classes.Add("foo");
  274. clock.Step(TimeSpan.FromSeconds(0));
  275. clock.Step(TimeSpan.FromSeconds(0.5));
  276. Assert.Equal(0.5, target.Opacity);
  277. target.Classes.Remove("foo");
  278. }
  279. }
  280. [Fact]
  281. public void Transitions_Can_Be_Changed_To_Collection_That_Contains_The_Same_Transitions()
  282. {
  283. var target = CreateTarget();
  284. var control = CreateControl(target.Object);
  285. control.Transitions = new Transitions { target.Object };
  286. }
  287. [Fact]
  288. public void Transitions_Can_Re_Set_During_Batch_Update()
  289. {
  290. var target = CreateTarget();
  291. var control = CreateControl(target.Object);
  292. // Assigning and then clearing Transitions ensures we have a transition state
  293. // collection created.
  294. control.Transitions = null;
  295. control.BeginBatchUpdate();
  296. // Setting opacity then Transitions means that we receive the Transitions change
  297. // after the Opacity change when EndBatchUpdate is called.
  298. control.Opacity = 0.5;
  299. control.Transitions = new Transitions { target.Object };
  300. // Which means that the transition state hasn't been initialized with the new
  301. // Transitions when the Opacity change notification gets raised here.
  302. control.EndBatchUpdate();
  303. }
  304. private static Mock<ITransition> CreateTarget()
  305. {
  306. return CreateTransition(Visual.OpacityProperty);
  307. }
  308. private static Control CreateControl(ITransition transition)
  309. {
  310. var control = new Control
  311. {
  312. Transitions = new Transitions { transition },
  313. };
  314. var root = new TestRoot(control);
  315. return control;
  316. }
  317. private static Control CreateStyledControl(
  318. ITransition transition1 = null,
  319. ITransition transition2 = null)
  320. {
  321. transition1 = transition1 ?? CreateTarget().Object;
  322. transition2 = transition2 ?? CreateTransition(Control.WidthProperty).Object;
  323. var control = new Control
  324. {
  325. Styles =
  326. {
  327. new Style(x => x.OfType<Control>())
  328. {
  329. Setters =
  330. {
  331. new Setter
  332. {
  333. Property = Control.TransitionsProperty,
  334. Value = new Transitions { transition1 },
  335. }
  336. }
  337. },
  338. new Style(x => x.OfType<Control>().Class("foo"))
  339. {
  340. Setters =
  341. {
  342. new Setter
  343. {
  344. Property = Control.TransitionsProperty,
  345. Value = new Transitions { transition2 },
  346. }
  347. }
  348. }
  349. }
  350. };
  351. var root = new TestRoot(control);
  352. return control;
  353. }
  354. private static Mock<ITransition> CreateTransition(AvaloniaProperty property)
  355. {
  356. var target = new Mock<ITransition>();
  357. var sub = new Mock<IDisposable>();
  358. target.Setup(x => x.Property).Returns(property);
  359. target.Setup(x => x.Apply(
  360. It.IsAny<Animatable>(),
  361. It.IsAny<IClock>(),
  362. It.IsAny<object>(),
  363. It.IsAny<object>())).Returns(sub.Object);
  364. return target;
  365. }
  366. }
  367. }