public void ValueChangedEventHandlerTest() { // Create a person with null children var p = new Person(); bool raised = false; // Create a NestedPropertyDescriptor var prop = "Address.City.Name"; var pd = new NestedPropertyDescriptor(typeof(Person), prop); EventHandler handler = (s, a) => raised = true; // Try to add an eventhandler Assert.Throws <ArgumentNullException>( () => pd.AddValueChanged(p, handler)); // Set values so there's no nulls in the tree p.Address = new Address() { City = new City() }; // Now the eventhandler can be added Assert.DoesNotThrow( () => pd.AddValueChanged(p, handler)); // Setting a different property directly on an // INotifyPropertyChanged object will not raise the event p.Address.City.PostCode = "1234"; Assert.False(raised); // Setting the property directly on an INotifyPropertyChanged // object will raise the event p.Address.City.Name = "Blah"; Assert.True(raised); raised = false; // Setting the property using the PropertyDescriptor // will raise the event pd.SetValue(p, "Blahville"); Assert.True(raised); raised = false; // Setting an intermediate property will remove the handler p.Address.City = new City(); Assert.False(raised); p.Address.City.Name = "Blahed"; Assert.False(raised); pd.SetValue(p, "No Blah"); Assert.False(raised); }
public void GetSetValueTest() { string cityname = "Brisbane"; var a = new Address() { City = new City() { Name = cityname, State = State.QLD } }; Person p = new Person { Address = a }; string propname = "Address.City.Name"; var npd = new NestedPropertyDescriptor(typeof(Person), propname); // Get the value Assert.Equal(p.Address.City.Name, npd.GetValue(p)); Assert.Equal(cityname, npd.GetValue(p)); // Set the city name cityname = "Gold Coast"; npd.SetValue(p, cityname); Assert.Equal(p.Address.City.Name, npd.GetValue(p)); Assert.Equal(cityname, npd.GetValue(p)); }
public void GetSetValueTest() { string cityname = "Brisbane"; var a = new Address() { City = new City() { Name = cityname, State = State.QLD } }; Person p = new Person { Address = a }; string propname = "Address.City.Name"; var npd = new NestedPropertyDescriptor(typeof(Person), propname); // Get the value Assert.Equal(p.Address.City.Name, npd.GetValue(p)); Assert.Equal(cityname, npd.GetValue(p)); // Set the city name cityname = "Gold Coast"; npd.SetValue(p, cityname); Assert.Equal(p.Address.City.Name, npd.GetValue(p)); Assert.Equal(cityname, npd.GetValue(p)); }
public void ValueChangedEventHandlerTest() { // Create a person with null children var p = new Person(); bool raised = false; // Create a NestedPropertyDescriptor var prop = "Address.City.Name"; var pd = new NestedPropertyDescriptor(typeof(Person), prop); EventHandler handler = (s, a) => raised = true; // Try to add an eventhandler Assert.Throws<ArgumentNullException>( () => pd.AddValueChanged(p, handler)); // Set values so there's no nulls in the tree p.Address = new Address() { City = new City() }; // Now the eventhandler can be added Assert.DoesNotThrow( () => pd.AddValueChanged(p, handler)); // Setting a different property directly on an // INotifyPropertyChanged object will not raise the event p.Address.City.PostCode = "1234"; Assert.False(raised); // Setting the property directly on an INotifyPropertyChanged // object will raise the event p.Address.City.Name = "Blah"; Assert.True(raised); raised = false; // Setting the property using the PropertyDescriptor // will raise the event pd.SetValue(p, "Blahville"); Assert.True(raised); raised = false; // Setting an intermediate property will remove the handler p.Address.City = new City(); Assert.False(raised); p.Address.City.Name = "Blahed"; Assert.False(raised); pd.SetValue(p, "No Blah"); Assert.False(raised); }