public void BindToCollection() { var source = new ModelB(); var target = new TestViewFactory(); // bind to collection var binding = new CollectionBinding("List", target); binding.Bind(source); Assert.AreEqual(0, target.viewList.Count); // add item source.List.Add(new ModelA()); Assert.AreEqual(1, target.viewList.Count); Assert.AreEqual(1, target.nextIndex); // notify list changed binding.HandleSourcePropertyChanged(source, "List"); Assert.AreEqual(2, target.nextIndex); Assert.AreEqual(1, target.viewList.Count); // notify with null property name binding.HandleSourcePropertyChanged(source, null); Assert.AreEqual(3, target.nextIndex); Assert.AreEqual(1, target.viewList.Count); target.Clear(); }
public void AddAndRemoveTest() { var source = new ModelB(); var target = new TestViewFactory(); // bind to collection var binding = new CollectionBinding("List", target); binding.Bind(source); Assert.AreEqual(0, target.ViewCount); // add item source.List.Add(new ModelA()); Assert.AreEqual(1, target.ViewCount); // add item source.List.Add(new ModelA()); Assert.AreEqual(2, target.ViewCount); // remove item source.List.RemoveAt(0); Assert.AreEqual(1, target.ViewCount); // add item source.List.Add(new ModelA()); Assert.AreEqual(2, target.ViewCount); // clear source.List.Clear(); Assert.AreEqual(0, target.ViewCount); target.Destroy(); }
public void ListMethodTest() { var source = new ModelB(); var target = new TestViewFactory(); // bind to collection var binding = new CollectionBinding("List", target); binding.Bind(source); Assert.AreEqual(0, target.ViewCount); // add item source.List.Add(new ModelA() { IntValue = 1 }); source.List.Add(new ModelA() { IntValue = 2 }); source.List.Add(new ModelA() { IntValue = 3 }); // do shuffle TestUtility.Shuffle(source.List); // do sort source.List.Sort((x, y) => x.IntValue.CompareTo(y.IntValue)); target.Destroy(); }
public void DuplicateItemTest() { var source = new ModelB(); var target = new TestViewFactory(); // bind to collection var binding = new CollectionBinding("List", target); binding.Bind(source); // get ref dictionary var field = typeof(CollectionBinding).GetField("viewReferenceCountDictionary", BindingFlags.NonPublic | BindingFlags.Instance); var refDictionary = field.GetValue(binding) as Dictionary <GameObject, int>; // check initial state { Assert.AreEqual(0, target.ViewCount); Assert.IsTrue(refDictionary == null); } var item = new ModelA(); // add item { source.List.Add(item); source.List.Add(item); // verify view count Assert.AreEqual(1, binding.BindingDictionary.Count); // check ref count refDictionary = field.GetValue(binding) as Dictionary <GameObject, int>; Assert.IsTrue(refDictionary != null); Assert.AreEqual(2, refDictionary.First().Value); } // remove item { source.List.RemoveAt(0); Assert.AreEqual(1, binding.BindingDictionary.Count); // check ref count Assert.AreEqual(1, refDictionary.First().Value); } // remove item { source.List.RemoveAt(0); Assert.AreEqual(0, binding.BindingDictionary.Count); // check ref count Assert.AreEqual(0, refDictionary.Count); } target.Destroy(); }
public void UpdateItemValueTest() { var source = new ModelB(); var target = new TestViewFactory(); // bind to collection var binding = new CollectionBinding("List", target); binding.Bind(source); Assert.AreEqual(0, target.ViewCount); // add item var itemA = new ModelA(); source.List.Add(itemA); // add item var itemB = new ModelA(); source.List.Add(itemB); // add item view A var viewA = new ModelA(); var bindingA = new Binding("IntValue", viewA, "IntValue"); var dcA = binding.BindingDictionary[itemA].GetComponent <IDataContext>(); dcA.AddBinding(bindingA); // add item view B var viewB = new ModelA(); var bindingB = new Binding("IntValue", viewB, "IntValue"); var dcB = binding.BindingDictionary[itemB].GetComponent <IDataContext>(); dcB.AddBinding(bindingB); // verify state Assert.IsTrue(bindingA.IsBound); Assert.IsTrue(bindingB.IsBound); Assert.AreEqual(1, dcA.BindingList.Count); Assert.AreEqual(1, dcB.BindingList.Count); Assert.AreEqual(0, viewA.IntValue); Assert.AreEqual(0, viewB.IntValue); // update value itemA.IntValue = 1; itemB.IntValue = 2; Assert.AreEqual(1, viewA.IntValue); Assert.AreEqual(2, viewB.IntValue); target.Destroy(); }
public void TestItemNestedPropertyChanged() { var source = new ModelD(); var target = new TestViewFactory(); var go = new GameObject("Test"); var dc = go.AddComponent <DataContext>(); // create collection binding var binding = new CollectionBinding("List", target); dc.AddBinding(binding); BindingManager.Instance.AddSource(source, "Test"); BindingManager.Instance.AddDataContext(dc, "Test"); // add item var item = new ModelC(); item.NestedValue.IntValue = 2; source.List.Add(item); var itemView = binding.BindingDictionary[item]; var itemDataContext = itemView.GetComponent <IDataContext>(); // add item binding var itemTarget = new ModelA(); var itemBinding = new Binding("NestedValue.IntValue", itemTarget, "IntValue"); itemDataContext.AddBinding(itemBinding); // check item value Assert.AreEqual(2, itemTarget.IntValue); // update value item.NestedValue.IntValue = 3; Assert.AreEqual(2, itemTarget.IntValue); // notify BindingManager.Instance.NotifyItemPropertyChanged(source, source, source.List, item, item.NestedValue, "IntValue"); Assert.AreEqual(3, itemTarget.IntValue); // update value item.NestedValue.IntValue = 4; Assert.AreEqual(3, itemTarget.IntValue); // notify with null BindingManager.Instance.NotifyItemPropertyChanged(source, source, source.List, item, item.NestedValue, null); Assert.AreEqual(4, itemTarget.IntValue); BindingManager.Instance.Clear(); target.Clear(); GameObject.DestroyImmediate(go); }
private TestView TestViewFactory(TestViewModel vm, TestViewFactory factory, Transform parent) { TestView retVal = factory.Create(vm.Value, vm); retVal.transform.SetParent(parent); retVal.someValue = vm.Value; retVal.Bind <TestView, int>((int x) => retVal.ChangeSomeValue(x)).ToProperty(vm, v => v.Value, nameof(TestViewModel)); return(retVal); }
public void DuplicateItemTest() { var source = new ModelB(); var target = new TestViewFactory(); // setup function target.GetDynamicItemsFun = () => { var list = new List <object>(); foreach (var item in source.List) { list.Add(item); } return(list); }; // bind to collection var binding = new ListDynamicBinding("List", target); binding.Bind(source); // check initial state { Assert.AreEqual(0, target.ViewCount); } var model = new ModelA(); // add item { source.List.Add(model); source.List.Add(model); // verify view count Assert.AreEqual(1, binding.BindingDictionary.Count); } // remove item { source.List.RemoveAt(0); Assert.AreEqual(1, binding.BindingDictionary.Count); } // remove item { source.List.RemoveAt(0); Assert.AreEqual(0, binding.BindingDictionary.Count); } target.Destroy(); }
public void OrderTest() { var source = new ModelB(); var target = new TestViewFactory(); // setup function target.GetDynamicItemsFun = () => { var list = new List <object>(); foreach (var item in source.List) { list.Add(item); } return(list); }; // bind to collection var binding = new ListDynamicBinding("List", target); binding.Bind(source); Assert.AreEqual(0, target.ViewCount); // add item source.List.Add(new ModelA()); source.List.Add(new ModelA()); var sourceA = source.List[0]; var sourceB = source.List[1]; var viewA = binding.BindingDictionary[sourceA]; Assert.AreEqual(0, viewA.transform.GetSiblingIndex()); var viewB = binding.BindingDictionary[sourceB]; Assert.AreEqual(1, viewB.transform.GetSiblingIndex()); // switch items source.List[0] = sourceB; source.List[1] = sourceA; viewA = binding.BindingDictionary[sourceA]; Assert.AreEqual(1, viewA.transform.GetSiblingIndex()); viewB = binding.BindingDictionary[sourceB]; Assert.AreEqual(0, viewB.transform.GetSiblingIndex()); target.Destroy(); }
public void ListMethodTest() { var source = new ModelB(); var target = new TestViewFactory(); // setup function target.GetDynamicItemsFun = () => { var list = new List <object>(); foreach (var item in source.List) { list.Add(item); } return(list); }; // bind to collection var binding = new ListDynamicBinding("List", target); binding.Bind(source); Assert.AreEqual(0, target.ViewCount); // add item source.List.Add(new ModelA() { IntValue = 1 }); source.List.Add(new ModelA() { IntValue = 2 }); source.List.Add(new ModelA() { IntValue = 3 }); // do shuffle TestUtility.Shuffle(source.List); // do sort source.List.Sort((x, y) => x.IntValue.CompareTo(y.IntValue)); target.Destroy(); }
public void AddAndRemoveTest() { var source = new ModelB(); var target = new TestViewFactory(); // bind to collection var binding = new ListDynamicBinding("List", target); binding.Bind(source); // setup function target.GetDynamicItemsFun = () => { var list = new List <object>(); foreach (var item in source.List) { list.Add(item); } return(list); }; Assert.AreEqual(0, target.ViewCount); // add item source.List.Add(new ModelA()); Assert.AreEqual(1, target.ViewCount); // add item source.List.Add(new ModelA()); Assert.AreEqual(2, target.ViewCount); // remove item source.List.RemoveAt(0); Assert.AreEqual(1, target.ViewCount); // add item source.List.Add(new ModelA()); Assert.AreEqual(2, target.ViewCount); // clear source.List.Clear(); Assert.AreEqual(0, target.ViewCount); target.Destroy(); }
protected override void Configure(DynamicModelContainer <TestModel> mContainer, DynamicViewModelContainer <TestViewModel> vmContainer, TestViewContainer vContainer, TestViewFactory factory) { Debug.Log("Configure " + nameof(TestDynamicViewConfiguration)); // DEBUG REFLECTION LISTENING START ------------------------------------------------------------------------------ //var propertyInfo = // typeof(DynamicModelContainer<TestModel>).GetField("MutableCollection", // BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); //var fieldInfoVm = typeof(DynamicViewModelContainer<TestViewModel>).GetField("MutableCollection", // BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); //if (fieldInfoVm == null || propertyInfo == null) //{ // Debug.LogError("PropertyInfo is null."); //} //else //{ // var mContainerProperty = // (ObservableCollection<TestModel>)propertyInfo.GetValue(mContainer); // var vmContainerProperty = // (ObservableCollection<TestViewModel>)fieldInfoVm.GetValue(vmContainer); // mContainerProperty.CollectionChanged += MockListenerModel; // vmContainerProperty.CollectionChanged += MockListenerViewModel; //} //Debug.Log("MC Listeners: " + mContainer.Listeners.Count + " | Subscriptions: " + mContainer.Subscriptions.Count); //Debug.Log("VMC Listeners: " + vmContainer.Listeners.Count + " | Subscriptions: " + vmContainer.Subscriptions.Count); // DEBUG REFLECTION LISTENING END --------------------------------------------------------------------------------------- vmContainer.BindContainer().ToContainer(mContainer, m => TestVMFactory(m)); vContainer.BindContainer().ToContainer(vmContainer, vm => TestViewFactory( vm, factory, vContainer.transform)); }
public void UpdateItemsTest() { var source = new ModelB(); var target = new TestViewFactory(); // bind to collection var binding = new ListDynamicBinding("List", target); binding.Bind(source); Assert.AreEqual(0, target.ViewCount); // add item source.List.Add(new ModelA()); source.List.Add(new ModelA()); Assert.AreEqual(0, target.ViewCount); target.GetDynamicItemsFun = () => { var list = new List <object>(); list.Add(source.List[0]); return(list); }; // update binding binding.UpdateDynamicList(); Assert.AreEqual(1, target.ViewCount); target.GetDynamicItemsFun = () => { var list = new List <object>(); list.Add(source.List[0]); list.Add(source.List[1]); return(list); }; // update binding binding.UpdateDynamicList(); Assert.AreEqual(2, target.ViewCount); target.Destroy(); }
public void SetUp() { var atom1Specification = MockRepository.GenerateStub <IPluginComponentSpecification <IComponentWithProperties> >(); var atom2Specification = MockRepository.GenerateStub <IPluginComponentSpecification <IComponentWithProperties> >(); var atom3Specification = MockRepository.GenerateStub <IPluginComponentSpecification <IComponentWithProperties> >(); var containerSpecification = MockRepository.GenerateStub <IPluginComponentSpecification <IComponentWithProperties> >(); var renderingInstructions = MockRepository.GenerateStub <IRenderingInstructions>(); atom1Specification.ViewName = "Atom1View"; atom2Specification.ViewName = "Atom2View"; atom3Specification.ViewName = "Atom3View"; containerSpecification.ViewName = "Container1View"; var handlerFactory = MockRepository.GenerateStub <IRendererFactory>(); handlerFactory.Stub(h => h.GetAtomRenderer("atom1")).Return(new DefaultRenderer(atom1Specification)); handlerFactory.Stub(h => h.GetAtomRenderer("atom2")).Return(new DefaultRenderer(atom2Specification)); handlerFactory.Stub(h => h.GetAtomRenderer("atom3")).Return(new DefaultRenderer(atom3Specification)); handlerFactory.Stub(h => h.GetContainerRenderer("container1")).Return(new DefaultRenderer(containerSpecification)); var renderer = new MultiRenderer(handlerFactory); KolaConfigurationRegistry.RegisterRenderer(renderer); var page = new PageInstance( new ComponentInstance[] { new AtomInstance(new[] { 0 }, renderingInstructions, "atom1", Enumerable.Empty <PropertyInstance>()), new AtomInstance(new[] { 1 }, renderingInstructions, "atom2", Enumerable.Empty <PropertyInstance>()), new ContainerInstance(new[] { 2 }, renderingInstructions, "container1", null, new[] { new AtomInstance(new[] { 2, 0 }, renderingInstructions, "atom3", Enumerable.Empty <PropertyInstance>()) }) }, renderingInstructions); var viewFactory = new TestViewFactory(renderer); var viewHelper = new TestViewHelper(viewFactory); var renderedPage = renderer.Render(page); this.result = renderedPage.ToHtml(viewHelper); }
public void OrderTest() { var source = new ModelB(); var target = new TestViewFactory(); // bind to collection var binding = new CollectionBinding("List", target); binding.Bind(source); Assert.AreEqual(0, target.ViewCount); // add item source.List.Add(new ModelA()); source.List.Add(new ModelA()); var sourceA = source.List[0]; var sourceB = source.List[1]; var viewA = binding.BindingDictionary[sourceA]; Assert.AreEqual(0, viewA.transform.GetSiblingIndex()); var viewB = binding.BindingDictionary[sourceB]; Assert.AreEqual(1, viewB.transform.GetSiblingIndex()); // switch items source.List[0] = sourceB; source.List[1] = sourceA; viewA = binding.BindingDictionary[sourceA]; Assert.AreEqual(1, viewA.transform.GetSiblingIndex()); viewB = binding.BindingDictionary[sourceB]; Assert.AreEqual(0, viewB.transform.GetSiblingIndex()); target.Destroy(); }