1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
//
// Authors:
// Chris Toshok (toshok@ximian.com)
//
using System;
using System.Windows;
using System.Windows.Media;
using NUnit.Framework;
namespace MonoTests.System.Windows {
class TestDepObj : DependencyObject {
public static readonly DependencyProperty TestProp1 = DependencyProperty.Register ("property1", typeof (string), typeof (TestDepObj));
public static readonly DependencyProperty TestProp2 = DependencyProperty.Register ("property2", typeof (string), typeof (TestDepObj));
public static readonly DependencyProperty TestProp3 = DependencyProperty.Register ("property3", typeof (string), typeof (TestDepObj));
public static readonly DependencyProperty TestProp4 = DependencyProperty.Register ("property4", typeof (string), typeof (TestDepObj), new PropertyMetadata ("default", changed, coerce));
static void changed (DependencyObject d, DependencyPropertyChangedEventArgs e) { }
static object coerce (DependencyObject d, object baseValue) { return baseValue; }
}
class TestSubclass : TestDepObj {
}
public class PropertyMetadataPoker : PropertyMetadata {
public bool BaseIsSealed {
get { return base.IsSealed; }
}
public void CallApply ()
{
OnApply (TestDepObj.TestProp1, typeof (TestDepObj));
}
public void CallMerge (PropertyMetadata baseMetadata, DependencyProperty dp)
{
Merge (baseMetadata, dp);
}
protected override void Merge (PropertyMetadata baseMetadata, DependencyProperty dp)
{
Console.WriteLine (Environment.StackTrace);
base.Merge (baseMetadata, dp);
}
protected override void OnApply (DependencyProperty dp, Type targetType)
{
base.OnApply (dp, targetType);
Console.WriteLine ("IsSealed in OnApply? {0}", IsSealed);
Console.WriteLine (Environment.StackTrace);
}
}
[TestFixture]
public class PropertyMetadataTest {
[Test]
public void DefaultValues ()
{
PropertyMetadataPoker m = new PropertyMetadataPoker ();
Assert.AreEqual (null, m.DefaultValue);
Assert.AreEqual (null, m.PropertyChangedCallback);
Assert.AreEqual (null, m.CoerceValueCallback);
}
[Test]
public void IsSealed ()
{
PropertyMetadataPoker m;
Console.WriteLine (1);
// calling OnApply isn't what sets the metadata to be sealed
m = new PropertyMetadataPoker();
Assert.IsFalse (m.BaseIsSealed);
m.CallApply ();
Assert.IsFalse (m.BaseIsSealed);
Console.WriteLine (2);
// calling OverrideMetadata does, however
m = new PropertyMetadataPoker ();
TestDepObj.TestProp1.OverrideMetadata (typeof (TestSubclass), m);
Assert.IsTrue (m.BaseIsSealed);
Console.WriteLine (3);
// calling DependencyProperty.AddOwner does too, but only because it calls OverrideMetadata
m = new PropertyMetadataPoker ();
TestDepObj.TestProp2.AddOwner (typeof (TestSubclass), m);
Assert.IsTrue (m.BaseIsSealed);
Console.WriteLine (4);
// lastly, calling DependencyProperty.Register does.
m = new PropertyMetadataPoker ();
DependencyProperty.Register ("xxx", typeof (string), typeof (TestDepObj), m);
Assert.IsTrue (m.BaseIsSealed);
}
[Test]
public void TestAddOwnerResult()
{
PropertyMetadataPoker m = new PropertyMetadataPoker ();
DependencyProperty p = TestDepObj.TestProp3.AddOwner (typeof (TestSubclass), m);
// they're the same object
Assert.AreSame (p, TestDepObj.TestProp3);
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void ModifyAfterSealed1 ()
{
PropertyMetadataPoker m = new PropertyMetadataPoker ();
DependencyProperty.Register ("p1", typeof (string), typeof (TestDepObj), m);
Assert.IsTrue (m.BaseIsSealed);
m.CoerceValueCallback = null;
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void ModifyAfterSealed2 ()
{
PropertyMetadataPoker m = new PropertyMetadataPoker ();
DependencyProperty.Register ("p2", typeof (string), typeof (TestDepObj), m);
Assert.IsTrue (m.BaseIsSealed);
m.PropertyChangedCallback = null;
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void ModifyAfterSealed3 ()
{
PropertyMetadataPoker m = new PropertyMetadataPoker ();
DependencyProperty.Register ("p3", typeof (string), typeof (TestDepObj), m);
Assert.IsTrue (m.BaseIsSealed);
m.DefaultValue = "hi";
}
[Test]
public void TestMerge ()
{
PropertyMetadataPoker m = new PropertyMetadataPoker ();
m.CallMerge (TestDepObj.TestProp4.GetMetadata (typeof (TestDepObj)), TestDepObj.TestProp4);
Assert.AreEqual ("default", m.DefaultValue);
Assert.IsNotNull (m.CoerceValueCallback);
Assert.IsNotNull (m.PropertyChangedCallback);
m = new PropertyMetadataPoker ();
m.DefaultValue = "non-default";
m.CallMerge (TestDepObj.TestProp4.GetMetadata (typeof (TestDepObj)), TestDepObj.TestProp4);
Assert.AreEqual ("non-default", m.DefaultValue);
Assert.IsNotNull (m.CoerceValueCallback);
Assert.IsNotNull (m.PropertyChangedCallback);
// XXX should check overriding of coerce and
// property changed callbacks, but we'll trust
// they behave the same..
}
[Test]
[ExpectedException (typeof (ArgumentException))] // "Default value cannot be 'Unset'."
public void TestSetDefaultToUnsetValue ()
{
PropertyMetadata m = new PropertyMetadata ();
m.DefaultValue = DependencyProperty.UnsetValue;
}
[Test]
[ExpectedException (typeof (ArgumentException))] // "Default value cannot be 'Unset'."
public void TestInitDefaultToUnsetValue ()
{
new PropertyMetadata (DependencyProperty.UnsetValue);
}
}
}
|