Ejemplo n.º 1
0
    public void WeakEventManager_AddWeakEventListener_Property_FiresEvent()
    {
        const string propertyName = "TestProperty";

        var source = new Mock <INotifyPropertyChanged>();

        var invocations = 0;

        var wem = new WeakEventManager();

        wem.AddWeakEventListener(
            source.Object,
            (s, a) =>
        {
            s.ShouldBe(source.Object);
            a.PropertyName.ShouldBe(propertyName);

            invocations++;
        }
            );

        source.Raise(x => x.PropertyChanged += null, new PropertyChangedEventArgs(propertyName));

        invocations.ShouldBe(1);
    }
Ejemplo n.º 2
0
    public void WeakEventManager_AddWeakEventListener_Collection_FiresEvent()
    {
        var collectionAddObject = new object();
        var source = new Mock <INotifyCollectionChanged>();

        var invocations = 0;

        var wem = new WeakEventManager();

        wem.AddWeakEventListener(
            source.Object,
            (s, a) =>
        {
            s.ShouldBe(source.Object);
            a.Action.ShouldBe(NotifyCollectionChangedAction.Add);
            a.NewItems.Count.ShouldBe(1);
            a.NewItems[0].ShouldBe(collectionAddObject);

            invocations++;
        }
            );

        source.Raise(
            x => x.CollectionChanged += null,
            new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, collectionAddObject)
            );

        invocations.ShouldBe(1);
    }
Ejemplo n.º 3
0
    public void WeakEventManager_AddWeakEventListener_FiresEventAfterGC()
    {
        var source = new Mock <INotifyPropertyChanged>();

        var invocations = 0;

        var wem = new WeakEventManager();

        wem.AddWeakEventListener <INotifyPropertyChanged, PropertyChangedEventArgs>(
            source.Object,
            nameof(INotifyPropertyChanged.PropertyChanged),
            (s, a) =>
        {
            s.ShouldBe(source.Object);

            invocations++;
        }
            );

        GC.Collect();

        source.Raise(x => x.PropertyChanged += null, new PropertyChangedEventArgs("Foo"));

        invocations.ShouldBe(1);
    }
Ejemplo n.º 4
0
    public void WeakEventManager_ClearWeakEventListeners_EventNoLongerFires()
    {
        var source = new Mock <INotifyPropertyChanged>();

        var invocations = 0;

        var wem = new WeakEventManager();

        wem.AddWeakEventListener <INotifyPropertyChanged, PropertyChangedEventArgs>(
            source.Object,
            nameof(INotifyPropertyChanged.PropertyChanged),
            (s, a) =>
        {
            s.ShouldBe(source.Object);

            invocations++;
        }
            );

        wem.ClearWeakEventListeners();

        source.Raise(x => x.PropertyChanged += null, new PropertyChangedEventArgs("Foo"));

        invocations.ShouldBe(0);
    }
 public TestSubscriber(TestPublisher publisher)
 {
     Publisher = publisher;
     _manager.AddWeakEventListener <TestPublisher, TestEventArgs>(publisher, nameof(publisher.TheEvent), OnTheEvent);
 }