2 // Unit tests for SelectorAutomationPeer
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.
30 using System
.Collections
.Generic
;
32 using System
.Windows
.Automation
;
33 using System
.Windows
.Automation
.Peers
;
34 using System
.Windows
.Automation
.Provider
;
35 using System
.Windows
.Controls
;
36 using System
.Windows
.Controls
.Primitives
;
38 using Microsoft
.Silverlight
.Testing
;
39 using Mono
.Moonlight
.UnitTesting
;
40 using Microsoft
.VisualStudio
.TestTools
.UnitTesting
;
42 namespace MoonTest
.System
.Windows
.Automation
.Peers
{
45 public class SelectorAutomationPeerTest
: ItemsControlAutomationPeerTest
{
47 public class SelectorAutomationPeerConcrete
: SelectorAutomationPeer
, FrameworkElementAutomationPeerContract
{
48 public SelectorAutomationPeerConcrete (SelectorConcrete owner
)
53 #region Overridden methods
55 public AutomationControlType
GetAutomationControlTypeCore_ ()
57 return base.GetAutomationControlTypeCore ();
62 #region Wrapper Methods
65 public AutomationPeer
GetLabeledByCore_ ()
67 return base.GetLabeledByCore ();
70 public string GetNameCore_ ()
72 return base.GetNameCore ();
75 public bool IsContentElementCore_ ()
77 return base.IsContentElementCore ();
80 public bool IsControlElementCore_ ()
82 return base.IsControlElementCore ();
85 public string GetAcceleratorKeyCore_ ()
87 return base.GetAcceleratorKeyCore ();
90 public string GetAccessKeyCore_ ()
92 return base.GetAccessKeyCore ();
95 public string GetAutomationIdCore_ ()
97 return base.GetAutomationIdCore ();
100 public Rect
GetBoundingRectangleCore_ ()
102 return base.GetBoundingRectangleCore ();
105 public List
<AutomationPeer
> GetChildrenCore_ ()
107 return base.GetChildrenCore ();
110 public Point
GetClickablePointCore_ ()
112 return base.GetClickablePointCore ();
115 public string GetHelpTextCore_ ()
117 return base.GetHelpTextCore ();
120 public string GetItemStatusCore_ ()
122 return base.GetItemStatusCore ();
125 public string GetItemTypeCore_ ()
127 return base.GetItemTypeCore ();
130 public string GetLocalizedControlTypeCore_ ()
132 return base.GetLocalizedControlTypeCore ();
135 public AutomationOrientation
GetOrientationCore_ ()
137 return base.GetOrientationCore ();
140 public bool HasKeyboardFocusCore_ ()
142 return base.HasKeyboardFocusCore ();
145 public bool IsEnabledCore_ ()
147 return base.IsEnabledCore ();
150 public bool IsKeyboardFocusableCore_ ()
152 return base.IsKeyboardFocusableCore ();
155 public bool IsOffscreenCore_ ()
157 return base.IsOffscreenCore ();
160 public bool IsPasswordCore_ ()
162 return base.IsPasswordCore ();
165 public bool IsRequiredForFormCore_ ()
167 return base.IsRequiredForFormCore ();
170 public string GetClassNameCore_ ()
172 return base.GetClassNameCore ();
178 // Subclassing ListBox instead of Selector because there's no public nor
179 // protected constructor defined on Selcetor.
180 public class SelectorConcrete
: ListBox
{
182 public SelectorConcrete () : base ()
186 protected override AutomationPeer
OnCreateAutomationPeer ()
188 return new SelectorAutomationPeerConcrete (this);
193 public override void GetPattern ()
195 AutomationPeer feap
= FrameworkElementAutomationPeer
.CreatePeerForElement (CreateConcreteFrameworkElement ());
197 Assert
.IsNull (feap
.GetPattern (PatternInterface
.Dock
), "Dock");
198 Assert
.IsNull (feap
.GetPattern (PatternInterface
.ExpandCollapse
), "ExpandCollapse");
199 Assert
.IsNull (feap
.GetPattern (PatternInterface
.Grid
), "Grid");
200 Assert
.IsNull (feap
.GetPattern (PatternInterface
.GridItem
), "GridItem");
201 Assert
.IsNull (feap
.GetPattern (PatternInterface
.Invoke
), "Invoke");
202 Assert
.IsNull (feap
.GetPattern (PatternInterface
.MultipleView
), "MultipleView");
203 Assert
.IsNull (feap
.GetPattern (PatternInterface
.RangeValue
), "RangeValue");
204 Assert
.IsNull (feap
.GetPattern (PatternInterface
.Scroll
), "Scroll");
205 Assert
.IsNull (feap
.GetPattern (PatternInterface
.ScrollItem
), "ScrollItem");
206 Assert
.IsNull (feap
.GetPattern (PatternInterface
.SelectionItem
), "SelectionItem");
207 Assert
.IsNull (feap
.GetPattern (PatternInterface
.Table
), "Table");
208 Assert
.IsNull (feap
.GetPattern (PatternInterface
.TableItem
), "TableItem");
209 Assert
.IsNull (feap
.GetPattern (PatternInterface
.Toggle
), "Toggle");
210 Assert
.IsNull (feap
.GetPattern (PatternInterface
.Transform
), "Transform");
211 Assert
.IsNull (feap
.GetPattern (PatternInterface
.Value
), "Value");
212 Assert
.IsNull (feap
.GetPattern (PatternInterface
.Window
), "Window");
214 Assert
.IsNotNull (feap
.GetPattern (PatternInterface
.Selection
), "Selection");
218 public override void GetAutomationControlType ()
220 // This is returning "AutomationControlType.List" because subclassed ListBox instead
221 // of Selector (because you just can't subclass selector!), however I think Custom should
222 // be returned instead. So, our Moonlight implementation is returning "AutomationControlType.Custom"
223 // instead of "AutomationControlType.List"
228 public virtual void ContentTest_NoTemplate ()
230 ContentTest_Template (new SelectorConcrete () { Template = null }
);
235 public override void ContentTest ()
237 ContentTest_Template (new SelectorConcrete ());
240 protected virtual void ContentTest_Template (Selector concrete
)
242 // We are going to add a lot of elements to show the scrollbars
243 // notice we are using default Template
244 bool concreteLoaded
= false;
245 bool concreteLayoutUpdate
= false;
246 concrete
.Width
= 250;
247 concrete
.Height
= 300;
248 concrete
.Loaded
+= (o
, e
) => concreteLoaded
= true;
249 concrete
.LayoutUpdated
+= (o
, e
) => concreteLayoutUpdate
= true;
250 TestPanel
.Children
.Add (concrete
);
252 // StackPanel with two TextBlocks
253 EnqueueConditional (() => concreteLoaded
, "ConcreteLoaded #0");
255 AutomationPeer peer
= FrameworkElementAutomationPeer
.CreatePeerForElement (concrete
);
256 Assert
.IsNotNull (peer
, "FrameworkElementAutomationPeer.CreatePeerForElement");
257 Assert
.IsNull (peer
.GetChildren (), "GetChildren #0");
259 concreteLayoutUpdate
= false;
260 for (int index
= 0; index
< 100; index
++)
261 concrete
.Items
.Add (string.Format ("Item {0}", index
));
263 EnqueueConditional (() => concreteLoaded
&& concreteLayoutUpdate
, "ConcreteLoaded #1");
265 AutomationPeer peer
= FrameworkElementAutomationPeer
.CreatePeerForElement (concrete
);
266 Assert
.IsNotNull (peer
.GetChildren (), "GetChildren #1");
268 Assert
.AreEqual (concrete
.Template
== null,
269 peer
.GetPattern (PatternInterface
.Scroll
) == null,
271 Assert
.AreEqual (100, peer
.GetChildren ().Count
, "GetChildren.Count #1");
272 concreteLayoutUpdate
= false;
273 concrete
.Items
.Add ("I'm looooooooooooooooooooooooooooooooooooooooooooong!");
275 EnqueueConditional (() => concreteLoaded
&& concreteLayoutUpdate
, "ConcreteLoaded #1");
277 AutomationPeer peer
= FrameworkElementAutomationPeer
.CreatePeerForElement (concrete
);
278 Assert
.IsNotNull (peer
.GetChildren (), "GetChildren #1");
279 Assert
.AreEqual (101, peer
.GetChildren ().Count
, "GetChildren.Count #2");
281 EnqueueTestComplete ();
284 #region Pattern Tests
288 [SilverlightBug("A11y implementation doesn't work")]
289 public virtual void ISelectionProvider_Methods ()
291 bool concreteLoaded
= false;
292 Selector selector
= CreateConcreteFrameworkElement () as Selector
;
293 selector
.Width
= 300;
294 selector
.Height
= 400;
295 selector
.Loaded
+= (o
, e
) => concreteLoaded
= true;
297 selector
.Items
.Add (new TextBlock () { Text = "Item0" }
);
298 selector
.Items
.Add (new TextBlock () { Text = "Item1" }
);
300 TestPanel
.Children
.Add (selector
);
302 EnqueueConditional (() => concreteLoaded
, "ConcreteLoaded #0");
304 AutomationPeer peer
= FrameworkElementAutomationPeer
.CreatePeerForElement (selector
);
306 ISelectionProvider selectionProvider
= peer
.GetPattern (PatternInterface
.Selection
) as ISelectionProvider
;
307 Assert
.IsNotNull (selectionProvider
, "Selection Provider");
309 Assert
.IsFalse (selectionProvider
.CanSelectMultiple
, "CanSelectMultiple #0");
310 Assert
.IsFalse (selectionProvider
.IsSelectionRequired
, "IsSelectionRequired #0");
312 IRawElementProviderSimple
[] selection
= selectionProvider
.GetSelection ();
313 Assert
.IsNull (selection
, "GetSelection #0");
315 selector
.SelectedIndex
= 1;
316 selection
= selectionProvider
.GetSelection ();
317 Assert
.IsNotNull (selection
, "GetSelection #1");
319 Assert
.AreEqual (1, selector
.SelectedIndex
, "SelectedIndex #0");
321 selectionProvider
= peer
.GetPattern (PatternInterface
.Selection
) as ISelectionProvider
;
322 Assert
.AreEqual (1, selection
.Length
, "GetSelection #2");
323 IRawElementProviderSimple first
= selection
[0];
325 selector
.Items
.Add (new TextBlock () { Text = "Item1" }
);
326 selector
.SelectedIndex
= 0;
327 selection
= selectionProvider
.GetSelection ();
328 Assert
.IsNotNull (selection
, "GetSelection #3");
329 Assert
.AreEqual (1, selection
.Length
, "GetSelection #4");
331 selector
.SelectedIndex
= 1;
332 selection
= selectionProvider
.GetSelection ();
333 Assert
.IsNotNull (selection
, "GetSelection #5");
334 Assert
.AreEqual (1, selection
.Length
, "GetSelection #6");
336 EnqueueTestComplete ();
341 public virtual void ISelectionProvider_Events ()
343 if (!EventsManager
.Instance
.AutomationSingletonExists
) {
344 EnqueueTestComplete ();
348 bool concreteLoaded
= false;
349 Selector selector
= CreateConcreteFrameworkElement () as Selector
;
350 selector
.Width
= 300;
351 selector
.Height
= 400;
352 selector
.Loaded
+= (o
, e
) => concreteLoaded
= true;
354 selector
.Items
.Add (new TextBlock () { Text = "Item0" }
);
355 selector
.Items
.Add (new TextBlock () { Text = "Item1" }
);
357 AutomationPeer peer
= null;
358 AutomationPeer childPeer
= null;
359 IRawElementProviderSimple
[] selection
= null;
360 AutomationPropertyEventTuple propertyTuple
= null;
361 ISelectionProvider selectionProvider
= null;
363 CreateAsyncTest (selector
,
365 peer
= FrameworkElementAutomationPeer
.CreatePeerForElement (selector
);
367 selectionProvider
= peer
.GetPattern (PatternInterface
.Selection
) as ISelectionProvider
;
368 Assert
.IsNotNull (selectionProvider
, "Selection Provider");
370 Assert
.IsFalse (selectionProvider
.CanSelectMultiple
, "CanSelectMultiple #0");
371 Assert
.IsFalse (selectionProvider
.IsSelectionRequired
, "IsSelectionRequired #0");
373 selection
= selectionProvider
.GetSelection ();
374 Assert
.IsNull (selection
, "GetSelection #0");
377 EventsManager
.Instance
.Reset ();
378 selector
.SelectedIndex
= 1;
381 selection
= selectionProvider
.GetSelection ();
382 Assert
.IsNotNull (selection
, "GetSelection #1");
383 Assert
.AreEqual (1, selection
.Length
, "GetSelection #2");
385 propertyTuple
= EventsManager
.Instance
.GetAutomationEventFrom (peer
, SelectionPatternIdentifiers
.SelectionProperty
);
386 Assert
.IsNotNull (propertyTuple
, "GetAutomationPropertyEventFrom #0");
387 Assert
.IsNull (propertyTuple
.OldValue
, "GetPropertyAutomationEventFrom.OldValue #0");
388 Assert
.IsNotNull (propertyTuple
.NewValue
, "GetPropertyAutomationEventFrom.NewValue #0");
390 childPeer
= new PeerFromProvider ().GetPeerFromProvider (selection
[0]);
393 EventsManager
.Instance
.Reset ();
394 selector
.Items
.Add (new TextBlock () { Text = "Item1" }
);
395 selector
.SelectedIndex
= 0;
398 selection
= selectionProvider
.GetSelection ();
399 Assert
.IsNotNull (selection
, "GetSelection #3");
400 Assert
.AreEqual (1, selection
.Length
, "GetSelection #4");
402 propertyTuple
= EventsManager
.Instance
.GetAutomationEventFrom (peer
, SelectionPatternIdentifiers
.SelectionProperty
);
403 Assert
.IsNotNull (propertyTuple
, "GetAutomationPropertyEventFrom #1");
404 Assert
.IsNotNull (propertyTuple
.OldValue
, "GetPropertyAutomationEventFrom.OldValue #1");
405 Assert
.IsNotNull (propertyTuple
.NewValue
, "GetPropertyAutomationEventFrom.NewValue #1");
407 Assert
.AreNotEqual (selection
[0],childPeer
,"GetSelection #5");
410 EventsManager
.Instance
.Reset ();
411 selector
.SelectedIndex
= -1;
414 selection
= selectionProvider
.GetSelection ();
415 Assert
.IsNull (selection
, "GetSelection #6");
417 propertyTuple
= EventsManager
.Instance
.GetAutomationEventFrom (peer
, SelectionPatternIdentifiers
.SelectionProperty
);
418 Assert
.IsNotNull (propertyTuple
, "GetAutomationPropertyEventFrom #2");
419 Assert
.IsNotNull (propertyTuple
.OldValue
, "GetPropertyAutomationEventFrom.OldValue #2");
420 Assert
.IsNull (propertyTuple
.NewValue
, "GetPropertyAutomationEventFrom.NewValue #2");
426 protected override FrameworkElement
CreateConcreteFrameworkElement ()
428 return new SelectorConcrete ();
431 protected override FrameworkElementAutomationPeerContract
CreateConcreteFrameworkElementAutomationPeer (FrameworkElement element
)
433 return new SelectorAutomationPeerConcrete (element
as SelectorConcrete
);