1
0

SelectableTextBlockTests.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System.Linq;
  2. using Avalonia.Controls.Documents;
  3. using Avalonia.Media;
  4. using Avalonia.Media.TextFormatting;
  5. using Avalonia.UnitTests;
  6. using Xunit;
  7. namespace Avalonia.Controls.UnitTests
  8. {
  9. public class SelectableTextBlockTests : ScopedTestBase
  10. {
  11. [Fact]
  12. public void SelectionForeground_Should_Not_Reset_Run_Typeface_And_Style()
  13. {
  14. using (UnitTestApplication.Start(TestServices.MockPlatformRenderInterface))
  15. {
  16. var target = new SelectableTextBlock
  17. {
  18. SelectionForegroundBrush = Brushes.Red
  19. };
  20. var run = new Run("Hello")
  21. {
  22. FontWeight = FontWeight.Bold,
  23. FontStyle = FontStyle.Italic,
  24. FontSize = 20
  25. };
  26. target.Inlines.Add(run);
  27. target.Measure(Size.Infinity);
  28. target.SelectionStart = 0;
  29. target.SelectionEnd = run.Text.Length;
  30. target.Measure(Size.Infinity);
  31. var textLayout = target.TextLayout;
  32. Assert.NotNull(textLayout);
  33. var textRuns = textLayout.TextLines
  34. .SelectMany(l => l.TextRuns)
  35. .OfType<ShapedTextRun>()
  36. .ToList();
  37. Assert.NotEmpty(textRuns);
  38. var selectedRun = textRuns[0];
  39. var props = selectedRun.Properties;
  40. Assert.Equal(FontWeight.Bold, props.Typeface.Weight);
  41. Assert.Equal(FontStyle.Italic, props.Typeface.Style);
  42. Assert.Same(target.SelectionForegroundBrush, props.ForegroundBrush);
  43. }
  44. }
  45. }
  46. }