Browse Source
Usually controls are only registered with their name scope when they get added to a rooted visual tree, but this causes problems for tests, and would mean the templated control wouldn't work if the client called ApplyTemplate themselves before the control is added to the visual tree. Work around this by explicitly registering template controls with the name scope when the template is applied. To allow this, we need to allow controls to be registered with a name scope multiple times.pull/325/head
7 changed files with 102 additions and 24 deletions
@ -0,0 +1,55 @@ |
|||
// Copyright (c) The Perspex Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using System; |
|||
using Xunit; |
|||
|
|||
namespace Perspex.SceneGraph.UnitTests |
|||
{ |
|||
public class NameScopeTests |
|||
{ |
|||
[Fact] |
|||
public void Register_Registers_Element() |
|||
{ |
|||
var target = new NameScope(); |
|||
var element = new object(); |
|||
|
|||
target.Register("foo", element); |
|||
|
|||
Assert.Same(element, target.Find("foo")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Unregister_Unregisters_Element() |
|||
{ |
|||
var target = new NameScope(); |
|||
var element = new object(); |
|||
|
|||
target.Register("foo", element); |
|||
target.Unregister("foo"); |
|||
|
|||
Assert.Null(target.Find("foo")); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Cannot_Register_New_Element_With_Existing_Name() |
|||
{ |
|||
var target = new NameScope(); |
|||
|
|||
target.Register("foo", new object()); |
|||
Assert.Throws<ArgumentException>(() => target.Register("foo", new object())); |
|||
} |
|||
|
|||
[Fact] |
|||
public void Can_Register_Same_Element_More_Than_Once() |
|||
{ |
|||
var target = new NameScope(); |
|||
var element = new object(); |
|||
|
|||
target.Register("foo", element); |
|||
target.Register("foo", element); |
|||
|
|||
Assert.Same(element, target.Find("foo")); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue