public void AdapterDoesNotPreventControlFromBeingGarbageCollectedWhenSettingItemsSourceToNull()
        {
            var control = new OutlookBarControl();
            object model = new object();
            IRegionAdapter adapter = new OutlookBarControlRegionAdapter(new MockEventAggregator());

            var region = adapter.Initialize(control);
            region.Add(model);

            WeakReference controlWeakReference = new WeakReference(control);
            Assert.IsTrue(controlWeakReference.IsAlive);

            control.ItemsSource = null;
            control = null;
            GC.Collect();
            GC.Collect();

            Assert.IsFalse(controlWeakReference.IsAlive);
        }
        public void ActivatingTheViewShouldUpdateTheSelectedItem()
        {
            var control = new OutlookBarControl();
            var view1 = new object();
            var view2 = new object();

            IRegionAdapter adapter = new OutlookBarControlRegionAdapter(new MockEventAggregator());

            var region = adapter.Initialize(control);
            region.Add(view1);
            region.Add(view2);

            Assert.AreNotEqual(view1, control.SelectedItem);

            region.Activate(view1);

            Assert.AreEqual(view1, control.SelectedItem);

            region.Activate(view2);

            Assert.AreEqual(view2, control.SelectedItem);
        }
        public void SelectingViewInOutlookBarControlPublishOutlookBarEvent()
        {
            var control = new OutlookBarControl();
            DependencyObject model = new DependencyObject();
            MockEventAggregator eventAggregator = new MockEventAggregator();
            MockOutlookBarEvent outlookBarEvent = new MockOutlookBarEvent();
            eventAggregator.AddMapping<OutlookBarEvent>(outlookBarEvent);
            IRegionAdapter adapter = new OutlookBarControlRegionAdapter(eventAggregator);

            var region = adapter.Initialize(control);
            region.Add(model);

            Assert.IsFalse(outlookBarEvent.PublishCalled);
            control.SelectedItem = model;

            Assert.IsTrue(outlookBarEvent.PublishCalled);
            Assert.IsNull(outlookBarEvent.PublishArgumentPayload);
        }
        public void RegionDoesNotGetUpdatedIfTheItemsSourceWasManuallyChanged()
        {
            var control = new OutlookBarControl();
            object model = new object();
            IRegionAdapter adapter = new OutlookBarControlRegionAdapter(new MockEventAggregator());

            var region = adapter.Initialize(control);
            region.Add(model);

            control.ItemsSource = null;
            control.Items.Add(model);
            Assert.IsFalse(region.ActiveViews.Contains(model));

            control.SelectedItem = model;

            Assert.IsFalse(region.ActiveViews.Contains(model), "The region should not be updated");
        }
        public void IfOutlookBarMetadataIsSetSelectingViewInOutlookBarControlPublishOutlookBarEventWithCorrectPayload()
        {
            var control = new OutlookBarControl();
            DependencyObject model = new DependencyObject();
            MockEventAggregator eventAggregator = new MockEventAggregator();
            MockOutlookBarEvent outlookBarEvent = new MockOutlookBarEvent();
            eventAggregator.AddMapping<OutlookBarEvent>(outlookBarEvent);
            IRegionAdapter adapter = new OutlookBarControlRegionAdapter(eventAggregator);

            var region = adapter.Initialize(control);
            OutlookBarControl.SetOutlookBarMetadata(model, new OutlookBarMetadata { Payload = "MyPayload" });
            region.Add(model);

            Assert.IsFalse(outlookBarEvent.PublishCalled);
            Assert.IsNull(outlookBarEvent.PublishArgumentPayload);
            control.SelectedItem = model;

            Assert.IsTrue(outlookBarEvent.PublishCalled);
            Assert.IsNotNull(outlookBarEvent.PublishArgumentPayload);
            Assert.AreEqual("MyPayload", outlookBarEvent.PublishArgumentPayload);
        }
        public void AdapterSynchronizesOutlookBarControlSelectionWithRegion()
        {
            var control = new OutlookBarControl();
            object model1 = new object();
            object model2 = new object();
            IRegionAdapter adapter = new OutlookBarControlRegionAdapter(new MockEventAggregator());

            var region = adapter.Initialize(control);
            region.Add(model1);
            region.Add(model2);

            Assert.IsFalse(region.ActiveViews.Contains(model2));

            control.SelectedItem = model2;

            Assert.IsTrue(region.ActiveViews.Contains(model2));
            Assert.IsFalse(region.ActiveViews.Contains(model1));

            control.SelectedItem = model1;

            Assert.IsTrue(region.ActiveViews.Contains(model1));
            Assert.IsFalse(region.ActiveViews.Contains(model2));
        }