ApplyStyling.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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.Linq;
  5. using BenchmarkDotNet.Attributes;
  6. using Avalonia.Controls;
  7. using Avalonia.Styling;
  8. using Avalonia.UnitTests;
  9. using Avalonia.VisualTree;
  10. namespace Avalonia.Benchmarks.Styling
  11. {
  12. [MemoryDiagnoser]
  13. public class ApplyStyling : IDisposable
  14. {
  15. private IDisposable _app;
  16. private Window _window;
  17. public ApplyStyling()
  18. {
  19. _app = UnitTestApplication.Start(TestServices.StyledWindow);
  20. TextBox textBox;
  21. _window = new Window
  22. {
  23. Content = textBox = new TextBox(),
  24. };
  25. _window.ApplyTemplate();
  26. textBox.ApplyTemplate();
  27. var border = (Border)textBox.GetVisualChildren().Single();
  28. if (border.BorderThickness != new Thickness(2))
  29. {
  30. throw new Exception("Styles not applied.");
  31. }
  32. _window.Content = null;
  33. // Add a bunch of styles with lots of class selectors to complicate matters.
  34. for (int i = 0; i < 100; ++i)
  35. {
  36. _window.Styles.Add(new Style(x => x.OfType<TextBox>().Class("foo").Class("bar").Class("baz"))
  37. {
  38. Setters =
  39. {
  40. new Setter(TextBox.TextProperty, "foo"),
  41. }
  42. });
  43. }
  44. }
  45. public void Dispose()
  46. {
  47. _app.Dispose();
  48. }
  49. [Benchmark]
  50. public void Add_And_Style_TextBox()
  51. {
  52. var textBox = new TextBox();
  53. _window.Content = textBox;
  54. textBox.ApplyTemplate();
  55. }
  56. }
  57. }