Merge branch 'loaded-branch'
[moon.git] / test / 2.0 / moon-unit / System.Windows.Automation.Peers / RadioButtonAutomationPeerTest.cs
blob1d4852124f80976fd614bb4e87112bffb24e1d04
1 //
2 // Unit tests for RadioButtonAutomationPeer
3 //
4 // Contact:
5 // Moonlight List (moonlight-list@lists.ximian.com)
6 //
7 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
8 //
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.
29 using System;
30 using System.Windows;
31 using System.Windows.Controls;
32 using System.Windows.Automation;
33 using System.Collections.Generic;
34 using System.Windows.Automation.Peers;
35 using System.Windows.Automation.Provider;
36 using System.Windows.Controls.Primitives;
38 using Mono.Moonlight.UnitTesting;
39 using Microsoft.Silverlight.Testing;
40 using Microsoft.VisualStudio.TestTools.UnitTesting;
42 namespace MoonTest.System.Windows.Automation.Peers {
44 [TestClass]
45 public class RadioButtonAutomationPeerTest : ToggleButtonAutomationPeerTest {
47 public class RadioButtonAutomationPeerPoker : RadioButtonAutomationPeer, FrameworkElementAutomationPeerContract {
49 public RadioButtonAutomationPeerPoker (RadioButton owner)
50 : base (owner)
54 #region Overriden methods
56 public string GetNameCore_ ()
58 return base.GetNameCore ();
61 public object GetPattern_ (PatternInterface iface)
63 return base.GetPattern (iface);
66 #endregion
68 #region Wrapper methods
70 public IRawElementProviderSimple ProviderFromPeer_ (AutomationPeer peer)
72 return ProviderFromPeer (peer);
75 public AutomationControlType GetAutomationControlTypeCore_ ()
77 return base.GetAutomationControlTypeCore ();
80 public string GetClassNameCore_ ()
82 return base.GetClassNameCore ();
85 public bool IsControlElementCore_ ()
87 return base.IsControlElementCore ();
90 public AutomationPeer GetLabeledByCore_ ()
92 return base.GetLabeledByCore ();
95 public bool IsContentElementCore_ ()
97 return base.IsContentElementCore ();
100 public string GetAcceleratorKeyCore_ ()
102 return base.GetAcceleratorKeyCore ();
105 public string GetAccessKeyCore_ ()
107 return base.GetAccessKeyCore ();
110 public string GetAutomationIdCore_ ()
112 return base.GetAutomationIdCore ();
115 public Rect GetBoundingRectangleCore_ ()
117 return base.GetBoundingRectangleCore ();
120 public List<AutomationPeer> GetChildrenCore_ ()
122 return base.GetChildrenCore ();
125 public Point GetClickablePointCore_ ()
127 return base.GetClickablePointCore ();
130 public string GetHelpTextCore_ ()
132 return base.GetHelpTextCore ();
135 public string GetItemStatusCore_ ()
137 return base.GetItemStatusCore ();
140 public string GetItemTypeCore_ ()
142 return base.GetItemTypeCore ();
145 public string GetLocalizedControlTypeCore_ ()
147 return base.GetLocalizedControlTypeCore ();
150 public AutomationOrientation GetOrientationCore_ ()
152 return base.GetOrientationCore ();
155 public bool HasKeyboardFocusCore_ ()
157 return base.HasKeyboardFocusCore ();
160 public bool IsEnabledCore_ ()
162 return base.IsEnabledCore ();
165 public bool IsKeyboardFocusableCore_ ()
167 return base.IsKeyboardFocusableCore ();
170 public bool IsOffscreenCore_ ()
172 return base.IsOffscreenCore ();
175 public bool IsPasswordCore_ ()
177 return base.IsPasswordCore ();
180 public bool IsRequiredForFormCore_ ()
182 return base.IsRequiredForFormCore ();
185 #endregion
188 [TestMethod]
189 public override void GetPattern ()
191 FrameworkElementAutomationPeer peer
192 = CreateConcreteFrameworkElementAutomationPeer (CreateConcreteFrameworkElement ())
193 as FrameworkElementAutomationPeer;
195 Assert.IsNull (peer.GetPattern (PatternInterface.Dock), "Dock");
196 Assert.IsNull (peer.GetPattern (PatternInterface.ExpandCollapse), "ExpandCollapse");
197 Assert.IsNull (peer.GetPattern (PatternInterface.Grid), "Grid");
198 Assert.IsNull (peer.GetPattern (PatternInterface.GridItem), "GridItem");
199 Assert.IsNull (peer.GetPattern (PatternInterface.Invoke), "Invoke");
200 Assert.IsNull (peer.GetPattern (PatternInterface.MultipleView), "MultipleView");
201 Assert.IsNull (peer.GetPattern (PatternInterface.RangeValue), "RangeValue");
202 Assert.IsNull (peer.GetPattern (PatternInterface.Scroll), "Scroll");
203 Assert.IsNull (peer.GetPattern (PatternInterface.ScrollItem), "ScrollItem");
204 Assert.IsNull (peer.GetPattern (PatternInterface.Selection), "Selection");
205 Assert.IsNull (peer.GetPattern (PatternInterface.Table), "Table");
206 Assert.IsNull (peer.GetPattern (PatternInterface.TableItem), "TableItem");
207 Assert.IsNull (peer.GetPattern (PatternInterface.Transform), "Transform");
208 Assert.IsNull (peer.GetPattern (PatternInterface.Window), "Window");
209 Assert.IsNull (peer.GetPattern (PatternInterface.Value), "Value");
210 Assert.IsNull (peer.GetPattern (PatternInterface.Toggle), "Toggle");
212 Assert.IsNotNull (peer.GetPattern (PatternInterface.SelectionItem), "SelectionItem");
215 [TestMethod]
216 public override void GetClassName ()
218 FrameworkElementAutomationPeerContract feap
219 = CreateConcreteFrameworkElementAutomationPeer (CreateConcreteFrameworkElement ());
220 Assert.AreEqual ("RadioButton", feap.GetClassName (), "GetClassNameCore");
221 Assert.AreEqual ("RadioButton", feap.GetClassNameCore_ (), "GetClassNameCoreCore");
224 [TestMethod]
225 public override void GetAutomationControlType ()
227 FrameworkElementAutomationPeerContract feap
228 = CreateConcreteFrameworkElementAutomationPeer (CreateConcreteFrameworkElement ());
229 Assert.AreEqual (AutomationControlType.RadioButton, feap.GetAutomationControlType (), "GetAutomationControlType");
230 Assert.AreEqual (AutomationControlType.RadioButton, feap.GetAutomationControlTypeCore_ (), "GetAutomationControlTypeCore");
233 [TestMethod]
234 public override void ToggleProvider_Toggle ()
236 RadioButton radioButton = CreateConcreteFrameworkElement () as RadioButton;
237 FrameworkElementAutomationPeerContract feap
238 = CreateConcreteFrameworkElementAutomationPeer (radioButton);
240 radioButton.IsEnabled = false;
242 IToggleProvider toggleProvider = feap as IToggleProvider;
244 try {
245 toggleProvider.Toggle ();
246 Assert.Fail ("Should throw ElementNotEnabledException");
247 } catch (ElementNotEnabledException) { }
249 radioButton.IsEnabled = true;
251 // Test two-state toggling
252 radioButton.IsThreeState = false;
254 radioButton.IsChecked = false;
255 Assert.AreEqual (ToggleState.Off, toggleProvider.ToggleState,
256 "Start two-state toggle: Unchecked");
258 toggleProvider.Toggle ();
259 Assert.AreEqual (ToggleState.On, toggleProvider.ToggleState,
260 "First two-state toggle: Checked");
262 // Test three-state toggling
263 radioButton.IsThreeState = true;
265 radioButton.IsChecked = false;
266 Assert.AreEqual (ToggleState.Off, toggleProvider.ToggleState,
267 "Start three-state Checked");
269 toggleProvider.Toggle ();
270 Assert.AreEqual (ToggleState.On, toggleProvider.ToggleState,
271 "First three-state toggle: Checked");
273 // NOTE: I don't think it will ever be indeterminate
276 [TestMethod]
277 [Asynchronous]
278 public void SelectionItemProvider_AddToSelection ()
280 RadioButton radioButton = CreateConcreteFrameworkElement () as RadioButton;
282 StackPanel stackPanel = new StackPanel ();
283 stackPanel.Children.Add (radioButton);
284 TestPanel.Children.Add (stackPanel);
286 Enqueue (( ) => {
287 AutomationPeer peer
288 = FrameworkElementAutomationPeer.CreatePeerForElement (radioButton);
290 ISelectionItemProvider selectionItem
291 = peer.GetPattern (PatternInterface.SelectionItem)
292 as ISelectionItemProvider;
293 Assert.IsNotNull (selectionItem);
295 radioButton.IsEnabled = false;
296 try {
297 selectionItem.AddToSelection ();
298 Assert.Fail ("InvalidOperationException not raised when disabled");
299 } catch (InvalidOperationException) { }
301 radioButton.IsEnabled = true;
302 try {
303 selectionItem.AddToSelection ();
304 Assert.Fail ("InvalidOperationException not raised when enabled");
305 } catch (InvalidOperationException) { }
307 EnqueueTestComplete ();
310 [TestMethod]
311 [Asynchronous]
312 public void SelectionItemProvider_RemoveFromSelection ()
314 RadioButton radioButton = CreateConcreteFrameworkElement () as RadioButton;
316 StackPanel stackPanel = new StackPanel ();
317 stackPanel.Children.Add (radioButton);
318 TestPanel.Children.Add (stackPanel);
320 Enqueue (( ) => {
321 AutomationPeer peer
322 = FrameworkElementAutomationPeer.CreatePeerForElement (radioButton);
324 ISelectionItemProvider selectionItem
325 = peer.GetPattern (PatternInterface.SelectionItem)
326 as ISelectionItemProvider;
327 Assert.IsNotNull (selectionItem);
329 radioButton.IsEnabled = false;
330 selectionItem.RemoveFromSelection ();
332 radioButton.IsChecked = true;
333 try {
334 selectionItem.RemoveFromSelection ();
335 } catch (InvalidOperationException) { }
337 radioButton.IsChecked = false;
338 radioButton.IsEnabled = true;
339 selectionItem.RemoveFromSelection ();
341 radioButton.IsChecked = true;
342 try {
343 selectionItem.RemoveFromSelection ();
344 } catch (InvalidOperationException) { }
346 EnqueueTestComplete ();
349 [TestMethod]
350 [Asynchronous]
351 public void SelectionItemProvider_Select ()
353 RadioButton rb1 = CreateConcreteFrameworkElement () as RadioButton;
354 RadioButton rb2 = CreateConcreteFrameworkElement () as RadioButton;
355 rb1.GroupName = rb2.GroupName = "Fruit";
357 StackPanel stackPanel = new StackPanel ();
358 stackPanel.Children.Add (rb1);
359 stackPanel.Children.Add (rb2);
360 TestPanel.Children.Add (stackPanel);
362 Enqueue (( ) => {
363 AutomationPeer radioPeer1
364 = FrameworkElementAutomationPeer.CreatePeerForElement (rb1);
365 AutomationPeer radioPeer2
366 = FrameworkElementAutomationPeer.CreatePeerForElement (rb2);
368 ISelectionItemProvider selectionItem1
369 = radioPeer1.GetPattern (PatternInterface.SelectionItem)
370 as ISelectionItemProvider;
371 Assert.IsNotNull (selectionItem1);
373 ISelectionItemProvider selectionItem2
374 = radioPeer2.GetPattern (PatternInterface.SelectionItem)
375 as ISelectionItemProvider;
376 Assert.IsNotNull (selectionItem2);
378 rb1.IsEnabled = false;
379 try {
380 selectionItem1.Select ();
381 Assert.Fail ("ElementNotEnabledException not raised when disabled");
382 } catch (ElementNotEnabledException) { }
384 rb1.IsEnabled = true;
385 selectionItem1.Select ();
386 Assert.IsTrue (rb1.IsChecked == true,
387 "Select didn't check rb1");
388 Assert.IsTrue (rb2.IsChecked == false,
389 "rb2 is selected when rb1 should be");
391 // This shouldn't raise any exceptions
392 selectionItem1.Select ();
394 selectionItem2.Select ();
395 Assert.IsTrue (rb2.IsChecked == true,
396 "Select didn't check rb2");
397 Assert.IsTrue (rb1.IsChecked == false,
398 "rb1 is selected when rb2 should be");
400 EnqueueTestComplete ();
403 [TestMethod]
404 [Asynchronous]
405 public void SelectionItemProvider_IsSelected ()
407 RadioButton rb1 = CreateConcreteFrameworkElement () as RadioButton;
408 RadioButton rb2 = CreateConcreteFrameworkElement () as RadioButton;
409 rb1.GroupName = rb2.GroupName = "Fruit";
411 StackPanel stackPanel = new StackPanel ();
412 stackPanel.Children.Add (rb1);
413 stackPanel.Children.Add (rb2);
414 TestPanel.Children.Add (stackPanel);
416 Enqueue (( ) => {
417 AutomationPeer radioPeer1
418 = FrameworkElementAutomationPeer.CreatePeerForElement (rb1);
419 AutomationPeer radioPeer2
420 = FrameworkElementAutomationPeer.CreatePeerForElement (rb2);
422 ISelectionItemProvider selectionItem1
423 = radioPeer1.GetPattern (PatternInterface.SelectionItem)
424 as ISelectionItemProvider;
425 Assert.IsNotNull (selectionItem1);
427 ISelectionItemProvider selectionItem2
428 = radioPeer2.GetPattern (PatternInterface.SelectionItem)
429 as ISelectionItemProvider;
430 Assert.IsNotNull (selectionItem2);
432 rb1.IsEnabled = true;
433 rb1.IsChecked = true;
434 Assert.IsTrue (selectionItem1.IsSelected,
435 "rb1 should be selected when checked");
436 Assert.IsFalse (selectionItem2.IsSelected,
437 "rb2 is selected when rb1 should be");
439 rb1.IsEnabled = false;
440 rb1.IsChecked = true;
441 Assert.IsTrue (selectionItem1.IsSelected,
442 "rb1 should be selected when checked");
443 Assert.IsFalse (selectionItem2.IsSelected,
444 "rb2 is selected when rb1 should be");
446 rb2.IsEnabled = true;
447 rb2.IsChecked = true;
448 Assert.IsTrue (selectionItem2.IsSelected,
449 "rb2 should be selected when checked");
450 Assert.IsFalse (selectionItem1.IsSelected,
451 "rb1 is selected when rb2 should be");
453 EnqueueTestComplete ();
456 [TestMethod]
457 [Asynchronous]
458 public void SelectionItemProvider_SelectionContainer ()
460 RadioButton rb1 = CreateConcreteFrameworkElement () as RadioButton;
461 RadioButton rb2 = CreateConcreteFrameworkElement () as RadioButton;
462 rb1.GroupName = rb2.GroupName = "Fruit";
464 ListBox listBox = new ListBox ();
465 listBox.Items.Add (rb1);
466 listBox.Items.Add (rb2);
467 TestPanel.Children.Add (listBox);
469 RadioButton rb3 = CreateConcreteFrameworkElement () as RadioButton;
470 RadioButton rb4 = CreateConcreteFrameworkElement () as RadioButton;
472 StackPanel stackPanel = new StackPanel ();
473 stackPanel.Children.Add (rb3);
474 stackPanel.Children.Add (rb4);
475 TestPanel.Children.Add (stackPanel);
477 Enqueue (( ) => {
478 FrameworkElementAutomationPeer radioPeer1
479 = CreateConcreteFrameworkElementAutomationPeer (rb1)
480 as FrameworkElementAutomationPeer;
481 FrameworkElementAutomationPeer radioPeer3
482 = CreateConcreteFrameworkElementAutomationPeer (rb3)
483 as FrameworkElementAutomationPeer;
485 ISelectionItemProvider selectionItem1
486 = radioPeer1.GetPattern (PatternInterface.SelectionItem)
487 as ISelectionItemProvider;
488 Assert.IsNotNull (selectionItem1);
490 ISelectionItemProvider selectionItem3
491 = radioPeer3.GetPattern (PatternInterface.SelectionItem)
492 as ISelectionItemProvider;
493 Assert.IsNotNull (selectionItem3);
495 Assert.IsNull (selectionItem1.SelectionContainer,
496 "selectionItem1's container is not null");
497 Assert.IsNull (selectionItem3.SelectionContainer,
498 "selectionItem2's container is not null");
500 EnqueueTestComplete ();
504 [TestMethod]
505 [Asynchronous]
506 public void SelectionItemProvider_IsSelectedEvent ()
508 if (!EventsManager.Instance.AutomationSingletonExists) {
509 EnqueueTestComplete ();
510 return;
513 RadioButton radioButton = CreateConcreteFrameworkElement () as RadioButton;
514 AutomationPeer peer
515 = FrameworkElementAutomationPeer.CreatePeerForElement (radioButton);
516 AutomationPropertyEventTuple tuple = null;
517 ISelectionItemProvider selectionProvider
518 = (ISelectionItemProvider) peer.GetPattern (PatternInterface.SelectionItem);
520 CreateAsyncTest (radioButton,
521 () => {
522 EventsManager.Instance.Reset ();
523 radioButton.IsThreeState = false;
524 radioButton.IsChecked = false;
526 // Test two-state toggling
527 () => {
528 tuple = EventsManager.Instance.GetAutomationEventFrom (peer,
529 SelectionItemPatternIdentifiers.IsSelectedProperty);
530 Assert.IsNull (tuple, "#0");
532 () => {
533 EventsManager.Instance.Reset ();
534 radioButton.IsChecked = true;
536 () => {
537 tuple = EventsManager.Instance.GetAutomationEventFrom (peer,
538 SelectionItemPatternIdentifiers.IsSelectedProperty);
539 Assert.IsNotNull (tuple, "#1");
540 Assert.IsFalse ((bool) tuple.OldValue, "#2");
541 Assert.IsTrue ((bool) tuple.NewValue, "#3");
543 () => {
544 EventsManager.Instance.Reset ();
545 radioButton.IsChecked = false;
547 () => {
548 tuple = EventsManager.Instance.GetAutomationEventFrom (peer,
549 SelectionItemPatternIdentifiers.IsSelectedProperty);
550 Assert.IsNotNull (tuple, "#4");
551 Assert.IsTrue ((bool) tuple.OldValue, "#5");
552 Assert.IsFalse ((bool) tuple.NewValue, "#6");
554 // Test three-state toggling
555 () => {
556 EventsManager.Instance.Reset ();
557 radioButton.IsThreeState = true;
558 radioButton.IsChecked = true;
560 () => {
561 tuple = EventsManager.Instance.GetAutomationEventFrom (peer,
562 SelectionItemPatternIdentifiers.IsSelectedProperty);
563 Assert.IsNotNull (tuple, "#7");
564 Assert.IsFalse ((bool) tuple.OldValue, "#8");
565 Assert.IsTrue ((bool) tuple.NewValue, "#9");
567 () => {
568 EventsManager.Instance.Reset ();
569 radioButton.IsChecked = null;
571 () => {
572 tuple = EventsManager.Instance.GetAutomationEventFrom (peer,
573 SelectionItemPatternIdentifiers.IsSelectedProperty);
574 Assert.IsNull (tuple, "#10");
576 () => {
577 EventsManager.Instance.Reset ();
578 radioButton.IsChecked = false;
580 () => {
581 tuple = EventsManager.Instance.GetAutomationEventFrom (peer,
582 SelectionItemPatternIdentifiers.IsSelectedProperty);
583 Assert.IsNotNull (tuple, "#13");
584 Assert.IsTrue ((bool) tuple.OldValue, "#14");
585 Assert.IsFalse ((bool) tuple.NewValue, "#15");
589 protected override FrameworkElement CreateConcreteFrameworkElement ()
591 return new RadioButton ();
594 protected override FrameworkElementAutomationPeerContract CreateConcreteFrameworkElementAutomationPeer (FrameworkElement element)
596 return new RadioButtonAutomationPeerPoker (element as RadioButton);