Exemple #1
0
        public void NullServiceTest()
        {
            IViewInjectionService iService = null;

            AssertHelper.AssertThrows <ArgumentNullException>(() => { iService.Inject(null, null); });
            AssertHelper.AssertThrows <ArgumentNullException>(() => { iService.Inject(null, null, string.Empty); });
            AssertHelper.AssertThrows <ArgumentNullException>(() => { iService.Inject(null, null, typeof(string)); });
            AssertHelper.AssertThrows <ArgumentNullException>(() => { iService.GetViewModel(null); });
        }
Exemple #2
0
        public void ViewModelKeyTest()
        {
            ViewInjectionService  service  = new ViewInjectionService();
            IViewInjectionService iService = service;
            ContentControl        target   = new ContentControl();

            Interaction.GetBehaviors(target).Add(service);
            object vm1 = new object();
            object vm2 = new object();

            Window.Content = target;
            EnqueueShowWindow();
            EnqueueCallback(() => {
                iService.Inject(null, null);
                iService.Inject(null, vm1);
                Assert.AreEqual(1, iService.ViewModels.Count());
                Assert.AreSame(vm1, iService.ViewModels.ElementAt(0));
                Assert.AreEqual(vm1, iService.GetKey(vm1));
                Assert.AreSame(vm1, iService.GetViewModel(vm1));

                AssertHelper.AssertThrows <InvalidOperationException>(() => iService.Inject(null, vm1),
                                                                      x => Assert.AreEqual("A view model with the same key already exists in the ViewInjectionService region.", x.Message));
                service.RegionName = "Test";
                AssertHelper.AssertThrows <InvalidOperationException>(() => iService.Inject(null, vm1),
                                                                      x => Assert.AreEqual("A view model with the same key already exists in the Test region.", x.Message));

                iService.Inject("New", vm2);
                Assert.AreEqual(2, iService.ViewModels.Count());
                Assert.AreSame(vm2, iService.ViewModels.ElementAt(1));
                Assert.AreEqual("New", iService.GetKey(vm2));
                Assert.AreSame(vm2, iService.GetViewModel("New"));

                iService.Remove(vm1);
                Assert.AreEqual(1, iService.ViewModels.Count());
                iService.Remove(vm2);
                Assert.AreEqual(0, iService.ViewModels.Count());
            });
            EnqueueTestComplete();
        }
 public static void Inject(this IViewInjectionService service, object key, object viewModel, Type viewType)
 {
     VerifyService(service);
     service.Inject(key, viewModel, null, viewType);
 }
 public static void Inject(this IViewInjectionService service, object key, object viewModel, string viewName)
 {
     VerifyService(service);
     service.Inject(key, viewModel, viewName, null);
 }