| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- using System;
- using System.Collections.Generic;
- using Avalonia.Collections;
- using Avalonia.Markup.Data;
- using Avalonia.UnitTests;
- using JetBrains.dotMemoryUnit;
- using Xunit;
- using Xunit.Abstractions;
- namespace Avalonia.LeakTests
- {
- [DotMemoryUnit(FailIfRunWithoutSupport = false)]
- public class ExpressionObserverTests
- {
- public ExpressionObserverTests(ITestOutputHelper atr)
- {
- DotMemoryUnitTestOutput.SetOutputMethod(atr.WriteLine);
- }
- [Fact]
- public void Should_Not_Keep_Source_Alive_ObservableCollection()
- {
- Func<ExpressionObserver> run = () =>
- {
- var source = new { Foo = new AvaloniaList<string> {"foo", "bar"} };
- var target = new ExpressionObserver(source, "Foo");
- target.Subscribe(_ => { });
- return target;
- };
- var result = run();
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<AvaloniaList<string>>()).ObjectsCount));
- }
- [Fact]
- public void Should_Not_Keep_Source_Alive_ObservableCollection_With_DataValidation()
- {
- Func<ExpressionObserver> run = () =>
- {
- var source = new { Foo = new AvaloniaList<string> { "foo", "bar" } };
- var target = new ExpressionObserver(source, "Foo", true);
- target.Subscribe(_ => { });
- return target;
- };
- var result = run();
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<AvaloniaList<string>>()).ObjectsCount));
- }
- [Fact]
- public void Should_Not_Keep_Source_Alive_NonIntegerIndexer()
- {
- Func<ExpressionObserver> run = () =>
- {
- var source = new { Foo = new NonIntegerIndexer() };
- var target = new ExpressionObserver(source, "Foo");
- target.Subscribe(_ => { });
- return target;
- };
- var result = run();
- dotMemory.Check(memory =>
- Assert.Equal(0, memory.GetObjects(where => where.Type.Is<NonIntegerIndexer>()).ObjectsCount));
- }
- private class NonIntegerIndexer : NotifyingBase
- {
- private readonly Dictionary<string, string> _storage = new Dictionary<string, string>();
- public string this[string key]
- {
- get
- {
- return _storage[key];
- }
- set
- {
- _storage[key] = value;
- RaisePropertyChanged(CommonPropertyNames.IndexerName);
- }
- }
- }
- }
- }
|