public void TestOneTimeBinding() { var model = new ModelTestClass { Number = 1, Text = "First" }; var control = new ControlTestClass { Text = "Wrong", Number = -1 }; var propInfo = typeof(ModelTestClass).GetProperty("Text"); var textProp = new BindableProperty<ControlTestClass, string>(control, o => o.Text, (o, action) => o.TextChanged += (sender, args) => action()); int controlCount = 0; textProp.PropertyChanged += (sender, args) => controlCount++; int modelCount = 0; model.PropertyChanged += (sender, args) => modelCount ++; Assert.Equal("Wrong", control.Text); Assert.Equal("First", model.Text); var binding = new BindingOneTime<ModelTestClass, ControlTestClass, string, string>(model, propInfo, o => o.Text, textProp, DataConverter<string>.EmptyConverter); TestOneTimeBindingInfo(binding, model, control, ref modelCount, ref controlCount); }
public void TestBinder() { var model = new ModelTestClass { Number = 1, Text = "First" }; var control = new ControlTestClass { Text = "Wrong", Number = -1 }; var textProp = new BindableProperty<ControlTestClass, string>(control, o => o.Text, (o, action) => o.TextChanged += (sender, args) => action()); int[] controlCounts = { 0 }; textProp.PropertyChanged += (sender, args) => controlCounts[0]++; int[] modelCounts = { 0 }; model.PropertyChanged += (sender, args) => modelCounts[0]++; var binder = new Binder<ModelTestClass, ControlTestClass, string, string>(m => m.Text, () => DataConverter<string>.EmptyConverter); var bInfo = binder.Bind(model, textProp, BindingMode.OneTime); TestOneTimeBindingInfo(bInfo, model, control, ref modelCounts[0], ref controlCounts[0]); bInfo.Unbind(); model.Text = "First"; modelCounts[0] = 0; control.Text = "Wrong"; controlCounts[0] = 0; textProp = new BindableProperty<ControlTestClass, string>(control, o => o.Text, (o, action) => o.TextChanged += (sender, args) => action()); bInfo = binder.Bind(model, textProp, BindingMode.OneWay); TestOneWayBindingInfo(bInfo, model, control, ref modelCounts[0], ref controlCounts[0]); bInfo.Unbind(); model.Text = "Wrong"; control.Text = "First"; modelCounts[0] = 0; controlCounts[0] = 0; textProp = new BindableProperty<ControlTestClass, string>(control, o => o.Text, (o, action) => o.TextChanged += (sender, args) => action()); bInfo = binder.Bind(model, textProp, BindingMode.OneWayToSource); TestOneWayToSourceBindingInfo(bInfo, model, control, modelCounts, controlCounts); bInfo.Unbind(); model.Text = "First"; modelCounts[0] = 0; control.Text = "Wrong"; controlCounts[0] = 0; textProp = new BindableProperty<ControlTestClass, string>(control, o => o.Text, (o, action) => o.TextChanged += (sender, args) => action()); bInfo = binder.Bind(model, textProp, BindingMode.TwoWay); TestTwoWayBindingInfo(bInfo, model, control, ref modelCounts[0], ref controlCounts[0]); bInfo.Unbind(); }
public void TestBindTo() { IPropertyBinder<ControlTestClass, string> binder = new PropertyBinder<ControlTestClass, string>( z => z.Text, (cls, action) => cls.TextChanged += (sender, args) => action()); var instance = new ControlTestClass { Text = "First" }; TestBindableProperty(instance, binder.BindTo(instance)); binder = new PropertyBinder<ControlTestClass, int, string>( z => z.Number, (cls, action) => cls.NumberChanged += (sender, args) => action(), new DataConverter<int, string>(Helper.ToText, Helper.ToInt32)); instance = new ControlTestClass { Number = 1 }; var property = binder.BindTo(instance); Assert.Equal("First", property.Value); Assert.Equal(instance.Number.ToText(), property.Value); instance.Number = 2; Assert.Equal("Second", property.Value); property.Value = "Third"; Assert.Equal("Third", instance.Number.ToText()); int numberChangedCount = 0; int propertyChangedCount = 0; instance.NumberChanged += (sender, args) => numberChangedCount++; property.PropertyChanged += (sender, args) => { Assert.Equal("Number", args.PropertyName); propertyChangedCount++; }; instance.Number = 4; Assert.Equal(1, numberChangedCount); Assert.Equal(1, propertyChangedCount); property.Value = "Fifth"; Assert.Equal(2, numberChangedCount); Assert.Equal(2, propertyChangedCount); }
private static void TestTwoWayBindingInfo(IBindingInfo<ModelTestClass, ControlTestClass, string, string> binding, ModelTestClass model, ControlTestClass control, ref int modelCount, ref int controlCount) { Assert.Equal(BindingMode.TwoWay, binding.Direction); Assert.Equal("First", control.Text); Assert.Equal(1, controlCount); Assert.Equal(0, modelCount); model.Text = 2.ToText(); Assert.Equal(1, modelCount); Assert.Equal(2, controlCount); Assert.Equal("Second", control.Text); binding.Model = null; model.Text = 3.ToText(); Assert.Equal(2, modelCount); binding.Model = model; Assert.Equal("Third", control.Text); Assert.Equal(3, controlCount); control.Text = "Forth"; Assert.Equal(4, controlCount); Assert.Equal(3, modelCount); Assert.Equal("Forth", model.Text); }
private static void TestOneWayToSourceBindingInfo(IBindingInfo<ModelTestClass, ControlTestClass, string, string> binding, ModelTestClass model, ControlTestClass control, int[] modelCounts, int[] controlCounts) { Assert.Equal(BindingMode.OneWayToSource, binding.Direction); Assert.Equal("First", model.Text); Assert.Equal(0, controlCounts[0]); Assert.Equal(1, modelCounts[0]); control.Text = 2.ToText(); Assert.Equal(2, modelCounts[0]); Assert.Equal(1, controlCounts[0]); Assert.Equal("Second", model.Text); binding.Model = null; control.Text = 3.ToText(); Assert.Equal(2, controlCounts[0]); binding.Model = model; Assert.Equal("Third", model.Text); Assert.Equal(3, modelCounts[0]); Assert.Equal(2, controlCounts[0]); model.Text = "Forth"; Assert.Equal(4, modelCounts[0]); Assert.Equal(2, controlCounts[0]); }
public void TestCreate() { var instance = new ControlTestClass { Text = "First" }; var property = new BindableProperty<ControlTestClass, string>( instance, o => o.Text, (o, action) => o.TextChanged += (sender, args) => action()); Assert.Equal("First", property.Value); Assert.Equal(instance.Text, property.Value); instance.Text = "Second"; Assert.Equal("Second", property.Value); property.Value = "Third"; Assert.Equal("Third", instance.Text); int textChangedCount = 0; int propertyChangedCount = 0; instance.TextChanged += (sender, args) => textChangedCount++; property.PropertyChanged += (sender, args) => { Assert.Equal("Text", args.PropertyName); propertyChangedCount++; }; instance.Text = "Forth"; Assert.Equal(1, textChangedCount); Assert.Equal(1, propertyChangedCount); property.Value = "Fifth"; Assert.Equal(2, textChangedCount); Assert.Equal(2, propertyChangedCount); instance = new ControlTestClass { Number = 1 }; property = new BindableProperty<ControlTestClass, string>( instance, "Text", o => o.Number.ToText(), (o, s) => o.Number = s.ToInt32(), (o, action) => o.NumberChanged += (sender, args) => action()); Assert.Equal("First", property.Value); Assert.Equal(instance.Number, property.Value.ToInt32()); instance.Number = 2; Assert.Equal("Second", property.Value); property.Value = "Third"; Assert.Equal("Third", instance.Number.ToText()); textChangedCount = 0; propertyChangedCount = 0; instance.NumberChanged += (sender, args) => textChangedCount++; property.PropertyChanged += (sender, args) => { Assert.Equal("Text", args.PropertyName); propertyChangedCount++; }; instance.Number = 4; Assert.Equal(1, textChangedCount); Assert.Equal(1, propertyChangedCount); property.Value = "Fifth"; Assert.Equal(2, textChangedCount); Assert.Equal(2, propertyChangedCount); }
internal static void TestBindableProperty(ControlTestClass instance, IBindableProperty<ControlTestClass, string> property) { Assert.Equal("First", property.Value); Assert.Equal(instance.Text, property.Value); instance.Text = "Second"; Assert.Equal("Second", property.Value); property.Value = "Third"; Assert.Equal( "Third", instance.Text); int textChangedCount = 0; int propertyChangedCount = 0; instance.TextChanged += (sender, args) => textChangedCount++; property.PropertyChanged += (sender, args) => { Assert.Equal("Text", args.PropertyName); propertyChangedCount++; }; instance.Text = "Forth"; Assert.Equal(1, textChangedCount); Assert.Equal(1, propertyChangedCount); property.Value = "Fifth"; Assert.Equal(2, textChangedCount); Assert.Equal(2, propertyChangedCount); }