2 // Unit tests for ButtonBaseAutomationPeer
5 // Moonlight List (moonlight-list@lists.ximian.com)
7 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System
.Windows
.Controls
;
32 using System
.Windows
.Automation
;
33 using System
.Windows
.Automation
.Peers
;
34 using System
.Windows
.Automation
.Provider
;
35 using System
.Windows
.Controls
.Primitives
;
37 using Mono
.Moonlight
.UnitTesting
;
38 using Microsoft
.VisualStudio
.TestTools
.UnitTesting
;
39 using Microsoft
.Silverlight
.Testing
;
41 namespace MoonTest
.System
.Windows
.Automation
.Peers
{
44 public class ButtonBaseAutomationPeerTest
: FrameworkElementAutomationPeerTest
{
46 public class ButtonBasePoker
: ButtonBase
{
47 public ButtonBasePoker ()
51 protected override AutomationPeer
OnCreateAutomationPeer ()
53 return new ButtonBaseAutomationPeerPoker (this);
57 public class ButtonBaseAutomationPeerPoker
: ButtonBaseAutomationPeer
, FrameworkElementAutomationPeerContract
{
59 public ButtonBaseAutomationPeerPoker (ButtonBase owner
)
64 #region Overridden methods
66 public string GetNameCore_ ()
68 return base.GetNameCore ();
73 #region Wrapper Methods
75 public AutomationPeer
GetLabeledByCore_ ()
77 return base.GetLabeledByCore ();
80 public bool IsContentElementCore_ ()
82 return base.IsContentElementCore ();
85 public bool IsControlElementCore_ ()
87 return base.IsControlElementCore ();
90 public string GetAcceleratorKeyCore_ ()
92 return base.GetAcceleratorKeyCore ();
95 public string GetAccessKeyCore_ ()
97 return base.GetAccessKeyCore ();
100 public AutomationControlType
GetAutomationControlTypeCore_ ()
102 return base.GetAutomationControlTypeCore ();
105 public string GetAutomationIdCore_ ()
107 return base.GetAutomationIdCore ();
110 public global::System
.Windows
.Rect
GetBoundingRectangleCore_ ()
112 return base.GetBoundingRectangleCore ();
115 public global::System
.Collections
.Generic
.List
<AutomationPeer
> GetChildrenCore_ ()
117 return base.GetChildrenCore ();
120 public global::System
.Windows
.Point
GetClickablePointCore_ ()
122 return base.GetClickablePointCore ();
125 public string GetHelpTextCore_ ()
127 return base.GetHelpTextCore ();
130 public string GetItemStatusCore_ ()
132 return base.GetItemStatusCore ();
135 public string GetItemTypeCore_ ()
137 return base.GetItemTypeCore ();
140 public string GetLocalizedControlTypeCore_ ()
142 return base.GetLocalizedControlTypeCore ();
145 public AutomationOrientation
GetOrientationCore_ ()
147 return base.GetOrientationCore ();
150 public bool HasKeyboardFocusCore_ ()
152 return base.HasKeyboardFocusCore ();
155 public bool IsEnabledCore_ ()
157 return base.IsEnabledCore ();
160 public bool IsKeyboardFocusableCore_ ()
162 return base.IsKeyboardFocusableCore ();
165 public bool IsOffscreenCore_ ()
167 return base.IsOffscreenCore ();
170 public bool IsPasswordCore_ ()
172 return base.IsPasswordCore ();
175 public bool IsRequiredForFormCore_ ()
177 return base.IsRequiredForFormCore ();
180 public string GetClassNameCore_ ()
182 return base.GetClassNameCore ();
190 public override void ContentTest ()
192 ContentTest ((ButtonBase
) CreateConcreteFrameworkElement ());
195 protected void ContentTest (ButtonBase button
)
197 Assert
.IsTrue (IsContentPropertyElement (), "ButtonElement ContentElement.");
199 bool buttonLoaded
= false;
200 button
.Loaded
+= (o
, e
) => buttonLoaded
= true;
201 TestPanel
.Children
.Add (button
);
203 // StackPanel and two TextBlocks
204 bool stackPanelLoaded
= false;
205 StackPanel stackPanel
= new StackPanel ();
206 stackPanel
.Children
.Add (new TextBlock () { Text = "Text0" }
);
207 stackPanel
.Children
.Add (new TextBlock () { Text = "Text1" }
);
208 stackPanel
.Loaded
+= (o
, e
) => stackPanelLoaded
= true;
210 EnqueueConditional (() => buttonLoaded
, "ButtonLoaded #0");
212 AutomationPeer peer
= FrameworkElementAutomationPeer
.CreatePeerForElement (button
);
213 Assert
.IsNotNull (peer
, "FrameworkElementAutomationPeer.CreatePeerForElement");
215 Assert
.IsNull (peer
.GetChildren (), "GetChildren #0");
216 button
.Content
= stackPanel
;
219 EnqueueConditional (() => buttonLoaded
&& stackPanelLoaded
, "ButtonLoaded #1");
221 AutomationPeer peer
= FrameworkElementAutomationPeer
.CreatePeerForElement (button
);
222 Assert
.IsNotNull (peer
.GetChildren (), "GetChildren #1");
223 Assert
.AreEqual (2, peer
.GetChildren ().Count
, "GetChildren.Count #1");
224 // We add one TextBlock
225 stackPanel
.Children
.Add (new TextBlock () { Text = "Text2" }
);
226 Assert
.IsNotNull (peer
.GetChildren (), "GetChildren #2");
227 Assert
.AreEqual (3, peer
.GetChildren ().Count
, "GetChildren.Count #2");
229 EnqueueTestComplete ();
233 public override void GetName ()
237 // LAMESPEC: MSDN: A string that contains the name, minus the accelerator key.
239 ButtonBase button
= CreateConcreteFrameworkElement () as ButtonBase
;
240 AutomationPeer peer
= FrameworkElementAutomationPeer
.CreatePeerForElement (button
);
241 Assert
.IsNotNull (peer
, "IsNotNull #0");
243 Assert
.AreEqual (string.Empty
, peer
.GetName (), "GetName #0");
245 button
.Content
= "Hello I'm button";
246 Assert
.AreEqual ("Hello I'm button", peer
.GetName (), "GetName #1");
248 button
.Content
= "Hello I'm &button";
249 Assert
.AreEqual ("Hello I'm &button", peer
.GetName (), "GetName #2");
251 button
.Content
= "Hello I'm &&button";
252 Assert
.AreEqual ("Hello I'm &&button", peer
.GetName (), "GetName #3");
254 button
.Content
= null;
255 Assert
.AreEqual (string.Empty
, peer
.GetName (), "GetName #4");
257 TextBox textbox
= new TextBox ();
258 textbox
.Text
= "I'm textbox";
259 button
.Content
= textbox
;
261 Assert
.AreEqual (string.Empty
, peer
.GetName (), "GetName #5");
263 TextBlock textblock
= new TextBlock ();
264 textblock
.Text
= "I'm textblock";
265 button
.Content
= textblock
;
267 Assert
.AreEqual ("I'm textblock", peer
.GetName (), "GetName #6");
269 button
.Content
= "I'm a button";
270 Assert
.AreEqual ("I'm a button", peer
.GetName (), "GetName #7");
272 // Now usin a stack panel with a textblock
273 StackPanel panel
= new StackPanel ();
274 panel
.Children
.Add (new TextBlock () { Text = "Textblock in Stackpanel1" }
);
276 button
.Content
= panel
;
277 Assert
.AreEqual (string.Empty
, peer
.GetName (), "GetName #8");
279 panel
.Children
.Add (new TextBlock () { Text = "Textblock in Stackpanel2" }
);
280 Assert
.AreEqual (string.Empty
, peer
.GetName (), "GetName #9");
282 button
.Content
= "What's up?";
283 Assert
.AreEqual ("What's up?", peer
.GetName (), "GetName #10");
288 public override void GetName_AttachedProperty0Event ()
290 if (!EventsManager
.Instance
.AutomationSingletonExists
) {
291 EnqueueTestComplete ();
295 ButtonBase fe
= CreateConcreteFrameworkElement () as ButtonBase
;
296 TextBlock textblock
= new TextBlock () { Text = "Textblock" }
;
297 fe
.Content
= textblock
;
299 AutomationPeer peer
= FrameworkElementAutomationPeer
.CreatePeerForElement (fe
);
300 AutomationPropertyEventTuple tuple
= null;
304 EventsManager
.Instance
.Reset ();
305 tuple
= EventsManager
.Instance
.GetAutomationEventFrom (peer
, AutomationElementIdentifiers
.NameProperty
);
306 Assert
.IsNull (tuple
, "#0");
309 EventsManager
.Instance
.Reset ();
310 textblock
.Text
= "Hi";
313 tuple
= EventsManager
.Instance
.GetAutomationEventFrom (peer
, AutomationElementIdentifiers
.NameProperty
);
314 Assert
.IsNotNull (tuple
, "#1");
315 Assert
.AreEqual ("Hi", (string) tuple
.NewValue
, "#2");
316 Assert
.AreEqual ("Textblock", tuple
.OldValue
, "#3");
319 EventsManager
.Instance
.Reset ();
320 fe
.SetValue (AutomationProperties
.NameProperty
, "Attached Name");
323 tuple
= EventsManager
.Instance
.GetAutomationEventFrom (peer
, AutomationElementIdentifiers
.NameProperty
);
324 Assert
.IsNotNull (tuple
, "#4");
325 Assert
.AreEqual ("Attached Name", (string) tuple
.NewValue
, "#5");
326 Assert
.AreEqual ("Hi", tuple
.OldValue
, "#6");
329 EventsManager
.Instance
.Reset ();
330 fe
.SetValue (AutomationProperties
.NameProperty
, "Name");
333 tuple
= EventsManager
.Instance
.GetAutomationEventFrom (peer
, AutomationElementIdentifiers
.NameProperty
);
334 Assert
.IsNotNull (tuple
, "#7");
335 Assert
.AreEqual ("Name", (string) tuple
.NewValue
, "#8");
336 Assert
.AreEqual ("Attached Name", (string) tuple
.OldValue
, "#9");
339 // Even if change TextBlock.Text (our current Content) the value will be the same
340 EventsManager
.Instance
.Reset ();
341 textblock
.Text
= "New value";
344 tuple
= EventsManager
.Instance
.GetAutomationEventFrom (peer
, AutomationElementIdentifiers
.NameProperty
);
345 Assert
.IsNull (tuple
, "#10");
348 EventsManager
.Instance
.Reset ();
349 fe
.SetValue (AutomationProperties
.NameProperty
, null);
352 tuple
= EventsManager
.Instance
.GetAutomationEventFrom (peer
, AutomationElementIdentifiers
.NameProperty
);
353 Assert
.IsNotNull (tuple
, "#11");
354 Assert
.AreEqual ("New value", (string) tuple
.NewValue
, "#12");
355 Assert
.AreEqual ("Name", (string) tuple
.OldValue
, "#13");
358 EventsManager
.Instance
.Reset ();
359 textblock
.Text
= "What's up?";
362 tuple
= EventsManager
.Instance
.GetAutomationEventFrom (peer
, AutomationElementIdentifiers
.NameProperty
);
363 Assert
.IsNotNull (tuple
, "#14");
364 Assert
.AreEqual ("What's up?", (string) tuple
.NewValue
, "#15");
365 Assert
.AreEqual ("New value", (string) tuple
.OldValue
, "#16");
368 EventsManager
.Instance
.Reset ();
372 tuple
= EventsManager
.Instance
.GetAutomationEventFrom (peer
, AutomationElementIdentifiers
.NameProperty
);
373 Assert
.IsNotNull (tuple
, "#17");
374 Assert
.AreEqual ("Hola", (string) tuple
.NewValue
, "#18");
375 Assert
.AreEqual ("What's up?", (string) tuple
.OldValue
, "#19");
379 protected override FrameworkElement
CreateConcreteFrameworkElement ()
381 return new ButtonBasePoker ();
384 protected override FrameworkElementAutomationPeerContract
CreateConcreteFrameworkElementAutomationPeer (FrameworkElement element
)
386 return new ButtonBaseAutomationPeerPoker (element
as ButtonBasePoker
);
389 protected void Test_InvokeProvider_Events (ButtonBase element
)
391 AutomationPeer peer
= FrameworkElementAutomationPeer
.CreatePeerForElement (element
);
393 IInvokeProvider provider
394 = (IInvokeProvider
) peer
.GetPattern (PatternInterface
.Invoke
);
395 Assert
.IsNotNull (provider
, "GetPattern #0");
397 EventsManager
.Instance
.Reset ();
399 AutomationEventTuple tuple
400 = EventsManager
.Instance
.GetAutomationEventFrom (peer
, AutomationEvents
.InvokePatternOnInvoked
);
401 Assert
.IsNull (tuple
, "GetAutomationEventFrom #0");
404 tuple
= EventsManager
.Instance
.GetAutomationEventFrom (peer
, AutomationEvents
.InvokePatternOnInvoked
);
405 Assert
.IsNotNull (tuple
, "GetAutomationEventFrom #1");
408 protected void Test_InvokeProvider_Invoke (ButtonBase button
)
411 = FrameworkElementAutomationPeer
.CreatePeerForElement (button
);
412 IInvokeProvider invoke
= peer
.GetPattern (PatternInterface
.Invoke
) as IInvokeProvider
;
413 Assert
.IsNotNull (invoke
, "InvokeProvider is null");
416 button
.IsEnabled
= false;
418 Assert
.Throws
<ElementNotEnabledException
> (() => invoke
.Invoke ());
420 button
.IsEnabled
= true;