Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
DotNet
acad.assignment.mngr
academic
adonet
aspnet
aspnet.announcements
aspnet.build.controls
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
clr
compactframework
component_services
datatools
distributed_apps
drawing
faqs
framework
framework.wmi
general
internationalization
interop
languages.csharp
languages.jscript
languages.vb
languages.vb.controls
languages.vb.data
languages.vb.upgrade
languages.vc
languages.vc.libraries
myservices
odbcnet
performance
remoting
scripting
sdk
security
setup
vjsharp
vsa
webservi.enhancements
webservices
windowsforms
windowsforms.controls
winforms.databinding
winforms.designtime
xml
  
 
date: Tue, 1 Jul 2008 18:43:58 +0200,    group: microsoft.public.dotnet.framework.windowsforms.controls        back       


Databinding not working in test environment, but does work on form   
I have a windows form project with a textbox, of which the Text property is 
bound to a GUIHandler object's property "ParseString". 
(textbox.DataBindings.Add("Text",guihandler,"ParseString")
The GUIHandler impements the INotifyPropertyChanged interface and fires the 
PropertyChanged event when its ParseString property is modified.
The textbox on the windows form matches the GUIHandler's ParseString 
property nicely. So far so good.

Now I wrote a test routine inside Vstudio's Unittest environment to test 
this behaviour (that I already know to work, but there you have it ;-))
[TestMethod]
DisplayTest
{
            TextBox box = new TextBox {Text = "Test"};
            GUIHandler guih = new GUIHandler { ParseString = 
"BeforeClick" };
            box.DataBindings.Add("Text", guih, "ParseString");
            guih.PropertyChanged+=guih_PropertyChanged;
            parsestringchanged.HasFired = false;//parsestringchanged is of 
type WaitForEvent.and initialized elsewhere
            guih.ParseString = "AfterClick";//this fires the PropertyChanged 
event
            AwaitEvent(parsestringchanged);
            Assert.AreEqual("AfterClick",box.Text);//fails: box.text="Test"
}
private void guih_PropertyChanged(object sender, PropertyChangedEventArgs 
e)//Eventhandler
 {
            parsestringchanged.HasFired = true;
            Assert.AreEqual(((GUIHandler)sender).ParseString,"AfterClick");//returns 
true
}

private void AwaitEvent(WaitForEvent waiter)
        {
            bool failed=true;
            for (int x = 0; x < 5000;x+=10 )
            {
                if (waiter.HasFired)
                {
                    failed=false;
                    break;
                   Application.DoEvents();
                }
             }
                if(failed)Assert.Fail("Event "+waiter.Name+" did not fire");

        }
The test routine creates a Textbox and a GUIHandler and binds the Textbox's 
Text property to the GUIHandler's Parsestring property.
I hook up to the GUIHandler's PropertyChanged event and set the HasFired 
property of the WaitForEventClass instance parsestringchanged to false.
The WaitForEvent class exposes just one property: HasFired.
I then change the Parsestring property to "AfterClick". This fires the 
PropertyChanged event as I can establish that the guih_PropertyChanged 
method is executed. The Assertion inside it returns true.
I wait for the event to have been fired using the AwaitEvent method. After 
this the TextBox's Text property should read "AfterClick", but it does not!

Q #1:What is wrong?
Q #2: Surely there must be a more elegant way for the test routine to be 
notified of the firing of an event in an object that is being tested?

For completeness I give the code for the WaitEvent class below:
private class WaitForEvent
        {
            private bool hasEventFired;
            private readonly object syncRoot;
            private string name;
            private object param;

            public bool HasFired
            {
                get { return hasEventFired; }
                set
                {
                    lock(syncRoot)
                    {
                        hasEventFired = value;
                    }
                }
            }

            public string Name
            {
                get { return name; }
                set { name = value; }
            }

            public object Param
            {
                get { return param; }
                set
                {
                    lock(syncRoot)
                    {
                        param = value;
                    }
                }
            }

            public WaitForEvent(string eventname)
            {
                Name = eventname;
                syncRoot=new object();
                hasEventFired = false;
            }
        }
date: Tue, 1 Jul 2008 18:43:58 +0200   author:   DabblerNL am

RE: Databinding not working in test environment, but does work on form   
Hi DabblerNL,

I performed a test based on your description and did see the same thing as 
you did.

> Q #1:What is wrong?

The reason why the line of code "ssert.AreEqual("AfterClick",box.Text);" 
fails in the DisplayTest test method is that the TextBox instance box is 
not shown at all. In fact, when a control is bound to a data source, data 
binding engine won't push values from the data source into the control 
until the control is shown.

> Q #2: Surely there must be a more elegant way for the test routine to be 
notified of the firing of an event in an object that is being tested?

You may subscribe the event of the object to be tested in the test method 
to get notified of the firing of the event.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and 
suggestions about how we can improve the support we provide to you. Please 
feel free to let my manager know what you think of the level of service 
provided. You can send feedback directly to my manager at: 
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to 
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues 
where an initial response from the community or a Microsoft Support 
Engineer within 1 business day is acceptable. Please note that each follow 
up response may take approximately 2 business days as the support 
professional working with you may need further investigation to reach the 
most efficient resolution. The offering is not appropriate for situations 
that require urgent, real-time or phone-based interactions or complex 
project analysis and dump analysis issues. Issues of this nature are best 
handled working with a dedicated Microsoft Support Engineer by contacting 
Microsoft Customer Support Services (CSS) at 
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
date: Wed, 02 Jul 2008 03:43:38 GMT   author:   (Linda Liu[MSFT])

Re: Databinding not working in test environment, but does work on form   
----- Original Message ----- 
From: "Linda Liu[MSFT]" 
Newsgroups: microsoft.public.dotnet.framework.windowsforms.controls
Sent: Wednesday, July 02, 2008 5:43 AM
Subject: RE: Databinding not working in test environment, but does work on 
form


> Hi DabblerNL,
>
> I performed a test based on your description and did see the same thing as
> you did.
>
>> Q #1:What is wrong?
>
> The reason why the line of code "ssert.AreEqual("AfterClick",box.Text);"
> fails in the DisplayTest test method is that the TextBox instance box is
> not shown at all. In fact, when a control is bound to a data source, data
> binding engine won't push values from the data source into the control
> until the control is shown.
>
Thank you! I will adjust the testing routine to see if I can get it to work 
with this info.

>> Q #2: Surely there must be a more elegant way for the test routine to be
> notified of the firing of an event in an object that is being tested?
>
> You may subscribe the event of the object to be tested in the test method
> to get notified of the firing of the event.
>
As you see in the code I already did that. The problem is that the MAIN BODY 
of the the test method must become aware of the event being fired. so that 
it can Assert that a property has changed to the expected value. If I put 
the assertion in the method that the event triggers, the assertion will only 
be performed IF the event is fired. If it is not, the assertion is never 
carried out and it may seem that the test succeeds while actually its result 
is unknown. So I would like to have code like:
[TestMethod]
void SomeTest()
{
ObjectToBeTested myTestObject=new ObjectToBetested(some arguments);
myTestObject.ProperyChanged+=DoThis;
DreamedUpWaitHandleObject waithandle;
waithandle.Subscribe(myTestObject.PropertyChanged);
int timeout=10000
WaitHandleResult result=waithandle.WaitForEvent(timeout);
Assert.IsTrue(result==WaitHandleResult.HasFired), "Event did not fire!");
//more testing...
}
void DoThis(object sender, EventArgs e)
{
//some code
}
Is this possible using the System.Threading classes?
date: Thu, 3 Jul 2008 21:31:36 +0200   author:   DabblerNL am

Re: Databinding not working in test environment, but does work on form   
Hi DabblerNL,

Thank you for your rely!

>  I will adjust the testing routine to see if I can get it to work with 
this info.

When you run the unit test, the form that contains the TextBox will never 
show so data binding will never push data retrieved from data source to the 
TextBox in this case and the line of code 
"Assert.AreEqual("AfterClick",box.Text);" in the test method will always 
fail.

> The problem is that the MAIN BODY of the the test method must become 
aware of the event being fired. 

For single-threaded WinForm application, events are always handled in the 
UI thread. Thus, if a method to be tested fires an event, it is after the 
event handler finishes execution that the method goes on its execution. 

So in your case, you needn't be aware of the event being fired within the 
main body of the test method at all. Just check whether the result is 
correct immediately after the line of code that calls the method to be 
tested.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and 
suggestions about how we can improve the support we provide to you. Please 
feel free to let my manager know what you think of the level of service 
provided. You can send feedback directly to my manager at: 
msdnmg@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
date: Fri, 04 Jul 2008 10:34:16 GMT   author:   (Linda Liu[MSFT])

Re: Databinding not working in test environment, but does work on form   
Thanks! I reference the GUI form directly  from within my testing routines 
now and have added these lines of code:
form.Visible=false;
form. Show();

The tests are "green".

"Linda Liu[MSFT]"  schreef in bericht 
news:a5bcKGc3IHA.4056@TK2MSFTNGHUB02.phx.gbl...
> Hi DabblerNL,
>
> Thank you for your rely!
>
>>  I will adjust the testing routine to see if I can get it to work with
> this info.
>
> When you run the unit test, the form that contains the TextBox will never
> show so data binding will never push data retrieved from data source to 
> the
> TextBox in this case and the line of code
> "Assert.AreEqual("AfterClick",box.Text);" in the test method will always
> fail.
>
>> The problem is that the MAIN BODY of the the test method must become
> aware of the event being fired.
>
> For single-threaded WinForm application, events are always handled in the
> UI thread. Thus, if a method to be tested fires an event, it is after the
> event handler finishes execution that the method goes on its execution.
>
> So in your case, you needn't be aware of the event being fired within the
> main body of the test method at all. Just check whether the result is
> correct immediately after the line of code that calls the method to be
> tested.
>
> Hope this helps.
> If you have any question, please feel free to let me know.
>
> Sincerely,
> Linda Liu
> Microsoft Online Community Support
>
> Delighting our customers is our #1 priority. We welcome your comments and
> suggestions about how we can improve the support we provide to you. Please
> feel free to let my manager know what you think of the level of service
> provided. You can send feedback directly to my manager at:
> msdnmg@microsoft.com.
>
> This posting is provided "AS IS" with no warranties, and confers no 
> rights.
>
date: Thu, 10 Jul 2008 20:01:50 +0200   author:   DabblerNL am

Re: Databinding not working in test environment, but does work on form   
Hi A.Steffens,

Thank you for your reply and sharing with us how you solved the problem 
successfully!

If you have any other questions in the future, please don't hesitate to 
contact us. It's always our pleasure to be of assistance.

Have a nice weekend!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and 
suggestions about how we can improve the support we provide to you. Please 
feel free to let my manager know what you think of the level of service 
provided. You can send feedback directly to my manager at: 
msdnmg@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
date: Fri, 11 Jul 2008 02:23:29 GMT   author:   (Linda Liu[MSFT])

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us