Beispiel #1
0
        public void hook_receives_mediator_and_mediatedItem()
        {
            SupportView mediatedItem     = new SupportView();
            object      injectedMediator = null;
            object      injectedView     = null;

            injector.Map(typeof(Action <MediatorHook>), "callback").ToValue((Action <MediatorHook>) delegate(MediatorHook hook) {
                injectedMediator = hook.mediator;
                injectedView     = hook.mediatedItem;
            });

            MediatorMapping mapping = new MediatorMapping(CreateTypeFilter(
                                                              new Type[1] {
                typeof(SupportView)
            }),
                                                          typeof(ViewInjectedMediator));

            mapping.WithHooks(typeof(MediatorHook));

            factory.CreateMediators(mediatedItem, typeof(SupportView), new List <IMediatorMapping> {
                mapping
            });

            Assert.That(injectedMediator, Is.InstanceOf <ViewInjectedMediator>());
            Assert.That(injectedView, Is.EqualTo(mediatedItem));
        }
Beispiel #2
0
        public void Mediator_Lifecycle_Methods_Are_Invoked()
        {
            List <string> expected = new List <string> {
                "preInitialize", "initialize", "postInitialize",
                "preDestroy", "destroy", "postDestroy"
            };
            List <string> actual = new List <string>();

            foreach (string phase in expected)
            {
                Action <string> callback = delegate(string ph) {
                    actual.Add(ph);
                };
                injector.Map(typeof(Action <string>), phase + "Callback").ToValue(callback);
            }
            SupportView      item     = new SupportView();
            object           mediator = injector.InstantiateUnmapped(typeof(LifecycleReportingMediator));
            IMediatorMapping mapping  = new MediatorMapping(CreateTypeFilter(new Type[1] {
                typeof(SupportView)
            }), typeof(LifecycleReportingMediator));

            manager.AddMediator(mediator, item, mapping);
            manager.RemoveMediator(mediator, item, mapping);

            Assert.That(actual, Is.EquivalentTo(expected));
        }
Beispiel #3
0
        public void Mediator_Is_Created()
        {
            IMediatorMapping mapping = new MediatorMapping(CreateTypeFilter(new Type[1] {
                typeof(SupportView)
            }), typeof(CallbackMediator));
            object mediator = factory.CreateMediators(new SupportView(), typeof(SupportView), new List <IMediatorMapping> {
                mapping
            })[0];

            Assert.That(mediator, Is.InstanceOf(typeof(CallbackMediator)));
        }
Beispiel #4
0
        /*============================================================================*/
        /* Private Functions                                                          */
        /*============================================================================*/

        private MediatorMapping CreateMapping(Type mediatorType)
        {
            MediatorMapping mapping = new MediatorMapping(_typeFilter, mediatorType);

            _handler.AddMapping(mapping);
            _mappings [mediatorType] = mapping;
            if (_logger != null)
            {
                _logger.Debug("{0} mapped to {1}", _typeFilter, mapping);
            }
            return(mapping);
        }
Beispiel #5
0
        public void mediatedItem_is_injected_as_exact_type_into_mediator()
        {
            SupportView      expected = new SupportView();
            IMediatorMapping mapping  = new MediatorMapping(CreateTypeFilter(new Type[1] {
                typeof(SupportView)
            }), typeof(ViewInjectedMediator));
            ViewInjectedMediator mediator = factory.CreateMediators(expected, typeof(SupportView), new List <IMediatorMapping> {
                mapping
            })[0] as ViewInjectedMediator;

            Assert.That(mediator.mediatedItem, Is.EqualTo(expected));
        }
Beispiel #6
0
        public void Mediator_Is_Given_NonDisplayObject_View()
        {
            float            expected = 1.5f;
            IMediatorMapping mapping  = new MediatorMapping(CreateTypeFilter(new Type[1] {
                typeof(float)
            }), typeof(LifecycleReportingMediator));
            LifecycleReportingMediator mediator = injector.InstantiateUnmapped(typeof(LifecycleReportingMediator)) as LifecycleReportingMediator;

            manager.AddMediator(mediator, expected, mapping);

            Assert.That(mediator.view, Is.EqualTo(expected));
        }
Beispiel #7
0
        public void getMediator()
        {
            SupportView      mediatedItem = new SupportView();
            IMediatorMapping mapping      =
                new MediatorMapping(CreateTypeFilter(new Type[1] {
                typeof(SupportView)
            }), typeof(CallbackMediator));

            factory.CreateMediators(mediatedItem, typeof(SupportView), new List <IMediatorMapping> {
                mapping
            });
            Assert.That(factory.GetMediator(mediatedItem, mapping), Is.Not.Null);
        }
Beispiel #8
0
        private int MediatorsCreatedWithGuards(params object[] guards)
        {
            MediatorMapping mapping = new MediatorMapping(CreateTypeFilter(new Type[1] {
                typeof(SupportView)
            }), typeof(CallbackMediator));

            mapping.WithGuards(guards);
            List <object> mediators = factory.CreateMediators(new SupportView(), typeof(SupportView), new List <IMediatorMapping> {
                mapping
            });

            return(mediators.Count);
        }
Beispiel #9
0
        public void Mediator_Is_Injected_Into()
        {
            int expected             = 128;
            IMediatorMapping mapping = new MediatorMapping(CreateTypeFilter(new Type[1] {
                typeof(SupportView)
            }), typeof(InjectedMediator));

            injector.Map(typeof(int)).ToValue(expected);
            InjectedMediator mediator = factory.CreateMediators(new SupportView(), typeof(SupportView), new List <IMediatorMapping> {
                mapping
            }) [0] as InjectedMediator;

            Assert.That(mediator.number, Is.EqualTo(expected));
        }
Beispiel #10
0
        public void Mediator_For_UIComponent_Is_Initialized()
        {
            SupportView      view    = new SupportView();
            IMediatorMapping mapping = new MediatorMapping(CreateTypeFilter(new Type[1] {
                typeof(SupportView)
            }), typeof(LifecycleReportingMediator));
            LifecycleReportingMediator mediator = injector.InstantiateUnmapped(typeof(LifecycleReportingMediator)) as LifecycleReportingMediator;

            factory.Setup(f => f.GetMediator(It.IsAny <object> (), It.IsAny <IMediatorMapping>())).Returns(mediator);
            Assert.That(mediator.initialized, Is.False);
            manager.AddMediator(mediator, view, mapping);
            container.AddChild(view);

            Assert.That(mediator.initialized, Is.True);
        }
Beispiel #11
0
        public void View_Is_Handled()
        {
            object          createdMediator = null;
            Action <object> callback        = delegate(object mediator) {
                createdMediator = mediator;
            };

            injector.Map(typeof(Action <object>), "callback").ToValue(callback);
            MediatorMapping mapping = new MediatorMapping(CreateTypeFilter(new Type [1] {
                typeof(SupportView)
            }), typeof(CallbackMediator));

            handler.AddMapping(mapping);
            handler.HandleView(new SupportView(), typeof(SupportView));
            Assert.That(createdMediator, Is.Not.Null);
        }
Beispiel #12
0
        public void expected_number_of_mediators_are_returned_for_mappings_and_mediatedItem()
        {
            SupportView     mediatedItem = new SupportView();
            MediatorMapping mapping1     =
                new MediatorMapping(CreateTypeFilter(new Type[1] {
                typeof(SupportView)
            }), typeof(ViewInjectedMediator));
            MediatorMapping mapping2 =
                new MediatorMapping(CreateTypeFilter(new Type[1] {
                typeof(SupportContainer)
            }), typeof(ViewInjectedAsRequestedMediator));
            List <object> mediators = factory.CreateMediators(mediatedItem, typeof(SupportView), new List <IMediatorMapping> {
                mapping1, mapping2
            });

            Assert.That(mediators.Count, Is.EqualTo(2));
        }
Beispiel #13
0
        public void removeMediator_removes_mediator_from_manager()
        {
            SupportView      mediatedItem = new SupportView();
            IMediatorMapping mapping      =
                new MediatorMapping(CreateTypeFilter(new Type[1] {
                typeof(SupportView)
            }), typeof(CallbackMediator));

            injector.Map <IMediatorManager> ().ToValue(manager.Object);
            factory = new MediatorFactory(injector);
            factory.CreateMediators(mediatedItem, typeof(SupportView), new List <IMediatorMapping> {
                mapping
            });
            factory.RemoveMediators(mediatedItem);
            factory.RemoveMediators(mediatedItem);

            manager.Verify(_manager => _manager.RemoveMediator(It.IsAny <CallbackMediator> (), It.Is <object> (arg2 => arg2 == mediatedItem), It.Is <IMediatorMapping> (arg3 => arg3 == mapping)), Times.Once);
        }
Beispiel #14
0
        /*============================================================================*/
        /* Private Functions                                                          */
        /*============================================================================*/

        private int HookCallCount(params object[] hooks)
        {
            int    hookCallCount = 0;
            Action hookCallback  = delegate() {
                hookCallCount++;
            };

            injector.Map(typeof(Action), "hookCallback").ToValue(hookCallback);

            MediatorMapping mapping = new MediatorMapping(CreateTypeFilter(new Type[1] {
                typeof(SupportView)
            }), typeof(CallbackMediator));

            mapping.WithHooks(hooks);
            factory.CreateMediators(new SupportView(), typeof(SupportView), new List <IMediatorMapping> {
                mapping
            });
            return(hookCallCount);
        }
Beispiel #15
0
        public void removeAllMediators_removes_all_mediators_from_manager()
        {
            SupportView      mediatedItem1 = new SupportView();
            SupportView      mediatedItem2 = new SupportView();
            IMediatorMapping mapping1      =
                new MediatorMapping(CreateTypeFilter(new Type[1] {
                typeof(SupportView)
            }), typeof(CallbackMediator));
            IMediatorMapping mapping2 =
                new MediatorMapping(CreateTypeFilter(new Type[1] {
                typeof(SupportContainer)
            }), typeof(ViewInjectedAsRequestedMediator));

            injector.Map <IMediatorManager> ().ToValue(manager.Object);
            factory = new MediatorFactory(injector);
            factory.CreateMediators(mediatedItem1, typeof(SupportView), new List <IMediatorMapping> {
                mapping1, mapping2
            });
            factory.CreateMediators(mediatedItem2, typeof(SupportView), new List <IMediatorMapping> {
                mapping1, mapping2
            });
            factory.RemoveAllMediators();

            manager.Verify(_manager => _manager.RemoveMediator(
                               It.IsAny <CallbackMediator>(),
                               It.Is <object>(arg2 => arg2 == mediatedItem1),
                               It.Is <IMediatorMapping>(arg3 => arg3 == mapping1)), Times.Once);

            manager.Verify(_manager => _manager.RemoveMediator(
                               It.IsAny <ViewInjectedAsRequestedMediator>(),
                               It.Is <object>(arg2 => arg2 == mediatedItem1),
                               It.Is <IMediatorMapping>(arg3 => arg3 == mapping2)), Times.Once);

            manager.Verify(_manager => _manager.RemoveMediator(
                               It.IsAny <CallbackMediator>(),
                               It.Is <object>(arg2 => arg2 == mediatedItem2),
                               It.Is <IMediatorMapping>(arg3 => arg3 == mapping1)), Times.Once);

            manager.Verify(_manager => _manager.RemoveMediator(
                               It.IsAny <ViewInjectedAsRequestedMediator>(),
                               It.Is <object>(arg2 => arg2 == mediatedItem2),
                               It.Is <IMediatorMapping>(arg3 => arg3 == mapping2)), Times.Once);
        }
Beispiel #16
0
        public void Mediator_Is_Removed_From_Factory_When_View_Leaves_Stage()
        {
            int    callCount  = 0;
            object actualView = null;

            manager.ViewRemoved += (Action <object>) delegate(object eventView) {
                callCount++;
                actualView = eventView;
            };
            SupportView      view    = new SupportView();
            IMediatorMapping mapping = new MediatorMapping(CreateTypeFilter(new Type[1] {
                typeof(SupportView)
            }), typeof(CallbackMediator));
            object mediator = injector.InstantiateUnmapped(typeof(CallbackMediator));

            container.AddChild(view);
            manager.AddMediator(mediator, view, mapping);
            container.RemoveChild(view);

            Assert.That(callCount, Is.EqualTo(1));
            Assert.That(actualView, Is.EqualTo(view));
        }
Beispiel #17
0
        public void Mediator_Is_NOT_Removed_When_View_Leaves_Stage_When_AutoRemove_Is_False()
        {
            int callCount = 0;

            manager.ViewRemoved += (Action <object>) delegate(object eventView) {
                callCount++;
            };

            SupportView     view    = new SupportView();
            MediatorMapping mapping = new MediatorMapping(CreateTypeFilter(new Type[1] {
                typeof(SupportView)
            }), typeof(CallbackMediator));

            mapping.AutoRemove(false);
            object mediator = injector.InstantiateUnmapped(typeof(CallbackMediator));

            container.AddChild(view);
            manager.AddMediator(mediator, view, mapping);
            container.RemoveChild(view);

            Assert.That(callCount, Is.EqualTo(0));
        }