Sliverlight单元测试

来源:互联网 发布:mac上好用的md编辑器 编辑:程序博客网 时间:2024/04/27 23:47

 最近在做SliverLight beta2的控件单元测试.

控件界面调用:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;
using Microsoft.Silverlight.Testing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Windows.Markup;
using System.Globalization;
using Microsoft.Silverlight.Testing.TestHarness;
using Zenexis.Controls.ColorPicker;
namespace SL.ColorPicker.UnitTest
{
    [TestClass]
    public class Z_ShowColorPicker:SilverlightTest
    {

        const int sleepTime = 800;
        bool _isLoaded;
        string testStr;
        private bool IsLoaded()
        {
            return _isLoaded;
        }
        private Zenexis.Controls.ColorPicker.ColorPicker colorPicker;
        [TestMethod]
        [Description("Verify properties set on ColorPicker before load are reflected on the ColorPicker after load.")]
        [Asynchronous]
        [Owner("[....]")]
        public void CPVerifyBeforeLoad()
        {
            colorPicker = new Zenexis.Controls.ColorPicker.ColorPicker();
            colorPicker.ColorSpace = Zenexis.Controls.ColorPicker.ColorSpace.RGB;
            colorPicker.Color = Colors.Green;
            colorPicker.ColorSelectionPanelVisibility = Visibility.Visible;
            colorPicker.Tip = "Tip Message";
            colorPicker.IsAutoSelect = true;
            colorPicker.AutoSelectSeconds = 100;
            colorPicker.IsHiddenColorSelectionPanelWhenAccept = false;
            colorPicker.Loaded += new RoutedEventHandler(_elementToCleanUp_Loaded);
            Silverlight.TestSurface.Children.Add(colorPicker);
            EnqueueConditional(new Microsoft.Silverlight.Testing.ConditionalDelegate(IsLoaded));
            EnqueueSleep(sleepTime);
            EnqueueCallback(VerifyBeforeLoaded);
            EnqueueTestComplete();
        }
        private void _elementToCleanUp_Loaded(object sender, RoutedEventArgs e)
        {
            _isLoaded = true;
        }
        private void VerifyBeforeLoaded()
        {
            Assert.AreEqual(colorPicker.Color, Colors.Green);
            Assert.AreEqual(colorPicker.ColorSelectionPanelVisibility, Visibility.Visible);
            Assert.AreEqual(colorPicker.ColorSpace, Zenexis.Controls.ColorPicker.ColorSpace.RGB);
            Assert.AreEqual(colorPicker.Tip, "Tip Message");
            Assert.IsTrue(colorPicker.IsAutoSelect);
            Assert.AreEqual(colorPicker.AutoSelectSeconds, 100);
            Assert.IsFalse(colorPicker.IsHiddenColorSelectionPanelWhenAccept);
            colorPicker.ColorSelectionPanelVisibility = Visibility.Collapsed;
        }
        [TestMethod]
        [Description("Ensure Collapsed/Expanded events are fired.")]
        [Asynchronous]
        [Owner("[....]")]
        public void DPCalendarClosedEvent()
        {
            colorPicker = new Zenexis.Controls.ColorPicker.ColorPicker();
            Silverlight.TestSurface.Children.Add(colorPicker);

            colorPicker.Collapsed += new RoutedEventHandler(delegate
            {
                testStr = "Color Selection Collapsed!";
            });

            colorPicker.Expanded += new RoutedEventHandler(delegate
            {
                testStr = "Color Expanded Opened!";
            });
        }
    }
}

    属性和Events测试.

    namespace SL.ColorPickerUnitTesting
{
    [TestClass]
    public class ColorPickerTest : SilverlightTest
    {
        //const int sleepTime = 800;
        //bool _isLoaded;
        //string testStr;
        //private bool IsLoaded()
        //{
        //    return _isLoaded;
        //}
        //private Zenexis.Controls.ColorPicker.ColorPicker colorPicker;
        [TestMethod]
        [Owner("[]")]
        [Description("Create a ColorPicker Control.")]
        public void CreateColorPicker()
        {
            Zenexis.Controls.ColorPicker.ColorPicker colorPicker = new Zenexis.Controls.ColorPicker.ColorPicker();
            Assert.IsNotNull(colorPicker);
        }
        [TestMethod]
        [Owner("[]")]
        [Description("Ensure that SelectedColorsChanged event is fired.")]
        public void SelectedColorsChangedEvent()
        {
            string testString = null;
            Zenexis.Controls.ColorPicker.ColorPicker colorPicker = new Zenexis.Controls.ColorPicker.ColorPicker();
            colorPicker.SelectedColorChanged += new Zenexis.Controls.ColorPicker.ColorPicker.SelectedColorChangedEventHandler
            (delegate
            {
                testString = "Handled!";
            }
            );
            colorPicker.SelectedColor = Colors.Blue;
            Assert.AreNotEqual(testString, "Handled!");
        }

        [TestMethod]
        [Owner("[]")]
        [Description("Ensure that all default values are correct")]
        public void ColorPickCheckedDefaultValues()
        {
            Zenexis.Controls.ColorPicker.ColorPicker colorPicker = new Zenexis.Controls.ColorPicker.ColorPicker();
            Assert.IsTrue(colorPicker.IsEnabled, "element is disabled in UI");
            Assert.IsTrue(colorPicker.IsHiddenColorSelectionPanelWhenAccept, "hiding color selection panel fails.");
            Assert.IsFalse(colorPicker.IsAutoAccept, "Accepting mode automatically fails.");
            Assert.AreEqual(colorPicker.AutoSelectSeconds, 2);
            Assert.AreEqual(colorPicker.ColorSelectionPanelVisibility, Visibility.Collapsed);
            Assert.AreEqual(colorPicker.CurrentColorPanelVisibility, Visibility.Visible);
        }
        //[TestMethod]
        //[Description("Verify properties set on ColorPicker before load are reflected on the ColorPicker after load.")]
        //[Asynchronous]
        //[Owner("[....]")]
        //public void CPVerifyBeforeLoad()
        //{
        //    colorPicker = new Zenexis.Controls.ColorPicker.ColorPicker();
        //    colorPicker.ColorSpace = Zenexis.Controls.ColorPicker.ColorSpace.RGB;
        //    colorPicker.Color = Colors.Green;
        //    colorPicker.ColorSelectionPanelVisibility = Visibility.Visible;
        //    colorPicker.Tip = "Tip Message";
        //    colorPicker.IsAutoSelect = true;
        //    colorPicker.AutoSelectSeconds = 100;
        //    colorPicker.IsHiddenColorSelectionPanelWhenAccept = false;
        //    colorPicker.Loaded += new RoutedEventHandler(_elementToCleanUp_Loaded);
        //    Silverlight.TestSurface.Children.Add(colorPicker);
        //    EnqueueConditional(new Microsoft.Silverlight.Testing.ConditionalDelegate(IsLoaded));
        //    EnqueueSleep(sleepTime);
        //    EnqueueCallback(VerifyBeforeLoaded);
        //    EnqueueTestComplete();
        //}
        [TestMethod]
        [Owner("[]")]
        [Description("Ensure that IsEnabled1 property can be set")]
        public void SetIsEnable1()
        {
            Zenexis.Controls.ColorPicker.ColorPicker colorPicker = new Zenexis.Controls.ColorPicker.ColorPicker();
            Assert.IsTrue(colorPicker.IsEnabled);
            colorPicker.IsEnabled = false;
            Assert.IsFalse(colorPicker.IsEnabled);
            colorPicker.IsEnabled = true;
            Assert.IsTrue(colorPicker.IsEnabled);
        }
        [TestMethod]
        [Owner("[]")]
        [Description("Ensure that IsHiddenColorSelectionPanelWhenAccept property can be set ")]
        public void SetIsHiddenColorSelectionPanelWhenAccept()
        {
            Zenexis.Controls.ColorPicker.ColorPicker colorPicker = new Zenexis.Controls.ColorPicker.ColorPicker();
            Assert.IsTrue(colorPicker.IsHiddenColorSelectionPanelWhenAccept);
            colorPicker.IsHiddenColorSelectionPanelWhenAccept = false;
            Assert.IsFalse(colorPicker.IsHiddenColorSelectionPanelWhenAccept);
            colorPicker.IsHiddenColorSelectionPanelWhenAccept = true;
            Assert.IsTrue(colorPicker.IsHiddenColorSelectionPanelWhenAccept);
        }
        [TestMethod]
        [Owner("[]")]
        [Description("Ensure that IsAutoAccept property can be set.")]
        public void SetIsAutoAccept()
        {
            Zenexis.Controls.ColorPicker.ColorPicker colorPicker = new Zenexis.Controls.ColorPicker.ColorPicker();
            colorPicker.IsAutoAccept = true;
            Assert.IsTrue(colorPicker.IsAutoAccept);
            colorPicker.IsAutoAccept = false;
            Assert.IsFalse(colorPicker.IsAutoAccept);
            colorPicker.IsAutoAccept = true;
            Assert.IsTrue(colorPicker.IsAutoAccept);
        }
        //private void _elementToCleanUp_Loaded(object sender, RoutedEventArgs e)
        //{
        //    _isLoaded = true;
        //}
        //private void VerifyBeforeLoaded()
        //{
        //    Assert.AreEqual(colorPicker.Color, Colors.Green);
        //    Assert.AreEqual(colorPicker.ColorSelectionPanelVisibility, Visibility.Visible);
        //    Assert.AreEqual(colorPicker.ColorSpace, Zenexis.Controls.ColorPicker.ColorSpace.RGB);
        //    Assert.AreEqual(colorPicker.Tip, "Tip Message");
        //    Assert.IsTrue(colorPicker.IsAutoSelect);
        //    Assert.AreEqual(colorPicker.AutoSelectSeconds, 100);
        //    Assert.IsFalse(colorPicker.IsHiddenColorSelectionPanelWhenAccept);
        //    colorPicker.ColorSelectionPanelVisibility = Visibility.Collapsed;
        //}

      
        //[TestMethod]
        //[Description("Ensure Collapsed/Expanded events are fired.")]
        //[Asynchronous]
        //[Owner("[....]")]
        //public void DPCalendarClosedEvent()
        //{
        //   colorPicker  = new Zenexis.Controls.ColorPicker.ColorPicker();
        //   Silverlight.TestSurface.Children.Add(colorPicker);
         
        //   colorPicker.Collapsed += new RoutedEventHandler(delegate
        //    {
        //        testStr = "Color Selection Collapsed!";
        //    });

        //   colorPicker.Expanded += new RoutedEventHandler(delegate
        //    {
        //        testStr = "Color Expanded Opened!";
        //    });
        //}
    }
}