using Annulus;
using Azimuth;
using Clave;
using Glass;
using System;
using System.Collections.Generic;
using UnitTestSharp;
using Tessera.Typesetting;
namespace Prism.Glass.UnitTests
{
public class ColorPickerTests : TestFixture
{
private const double Tolerance = 1e-12;
public class FixedTexture : ITexture
{
public FixedTexture(int width, int height)
{
Size = new Point(width, height);
}
public Point Size { get; }
}
public static Material Art(int width, int height)
{
return new TextureMaterial { Texture = new FixedTexture(width, height) };
}
public static ColorPickerMaterial Look()
{
return new ColorPickerMaterial
{
Background = Material.Invisible,
Disc = Art(100, 100),
ValueStrip = Art(10, 1),
AlphaStrip = Art(10, 1),
AlphaBackdrop = Art(10, 10),
DiscMarker = Art(10, 10),
StripMarker = Art(1, 4),
Spacing = 4,
SwatchHeight = 20,
};
}
public static ColorPickerModel DarkRed()
{
return new ColorPickerModel(Color.FromArgb(51, 0, 0));
}
///
/// Digits and the # all 10 wide, and lines 10 tall.
///
public static SpriteFontDefinition HexFont()
{
var glyphs = new Dictionary();
foreach (char digit in "#0123456789abcdefABCDEF")
{
glyphs[digit] = new SpriteFontDefinition.GlyphDefinition { Width = 10 };
}
glyphs[TruncationMarker.EllipsisGlyph] = new SpriteFontDefinition.GlyphDefinition { Width = 10 };
return new SpriteFontDefinition(new SpriteFontDefinition.Data
{
GlyphDefinitions = glyphs,
InvalidGlyph = new SpriteFontDefinition.GlyphDefinition { Width = 10 },
Kernings = new Dictionary<(short, short), short>(),
Baseline = 8,
LineHeight = 10,
});
}
///
/// A box one 10 tall line high inside a border 3 thick, so 16 tall.
///
public static TextBoxMaterial HexLook()
{
return new TextBoxMaterial
{
Scale = default,
Background = new InvisibleMaterial { BorderSize = new Rect.BorderSize(3) },
InactiveSelection = default,
Composition = default,
CompositionThickness = default,
CompositionTarget = default,
CompositionTargetThickness = default,
ScrollBar = default,
HorizontalScrollBar = default,
Text = new TextMaterialStyle { Font = HexFont(), HorizontalMargin = 0, VerticalMargin = 0 },
Selection = new SolidColorMaterial(),
Caret = new SolidColorMaterial(),
CaretWidth = 1,
};
}
///
/// A picker with every behavior at its default and no look.
///
public static ColorPicker Unstyled(ColorPickerModel model, ColorPickerBehavior behavior = null)
{
return new ColorPicker(
model, behavior ?? ColorPickerBehavior.Default, new TextBoxBehavior(), new NumberFieldBehavior());
}
public static ColorPicker PickerFor(
ColorPickerModel model, ColorPickerBehavior behavior = null, ColorPickerMaterial? look = null)
{
return new ColorPicker(
model, behavior ?? ColorPickerBehavior.Default, new TextBoxBehavior(), new NumberFieldBehavior())
{
PickerMaterial = look ?? Look(),
};
}
///
/// Fields 40 wide and 20 tall, the label taking 30 of that, with letters 10 wide and 10 tall.
///
public static ColorPickerMaterial FieldLook(bool withLetters = true)
{
var style = new TextMaterialStyle { Font = HexFont(), HorizontalMargin = 0, VerticalMargin = 0 };
return Look() with
{
Field = new NumberFieldMaterial
{
Background = null,
Label = style,
Editor = default,
IncrementButton = new ButtonMaterial(Art(10, 10)),
DecrementButton = new ButtonMaterial(Art(10, 10)),
},
FieldLabel = withLetters ? style : null,
};
}
public static ColorPicker PickerWithHexBox(ColorPickerModel model, ColorPickerBehavior behavior = null)
{
return PickerFor(model, behavior, Look() with { HexBox = HexLook() });
}
///
/// Gives the box focus, typing into it as a user would, and returns the keys that hold it.
///
public static KeyboardManager Focus(ColorPicker picker)
{
var keys = new KeyboardManager();
keys.Controls.Add(picker.HexBox);
keys.TrySetFocus(picker.HexBox);
return keys;
}
public static void Type(ColorPicker picker, string typed)
{
foreach (char character in typed)
{
picker.HexBox.TextInput(character.ToString(), new KeyboardManager());
}
}
public static void PressEnter(KeyboardManager keys)
{
var tracker = new KeyTracker();
keys.KeyDown(tracker.KeyDown(KeyCode.MainEnter, KeyCode.None, isRepeat: false));
}
public static Rect Box(double x, double y, double width, double height)
{
return Rect.FromPositionAndSize(new Vector(x, y), new Vector(width, height));
}
public static Material ChildOf(Panel part)
{
return ((TintedOverlay)part.Material).ChildMaterial;
}
public static string Tint(Panel part)
{
Color? tint = ((TintedOverlay)part.Material).Tint;
return tint.HasValue ? HexColor.Format(tint.Value, includeAlpha: true) : "none";
}
public static void Press(PickSurface surface, double x, double y)
{
PickSurfaceTests.Press(surface, x, y);
}
public class CtorTests : TestFixture
{
public void WithoutAModel_Throws()
{
CheckThrow(typeof(ArgumentNullException));
new ColorPicker(null, ColorPickerBehavior.Default, new TextBoxBehavior(), new NumberFieldBehavior());
}
public void WithoutABehavior_Throws()
{
CheckThrow(typeof(ArgumentNullException));
new ColorPicker(DarkRed(), null, new TextBoxBehavior(), new NumberFieldBehavior());
}
public void WithoutAHexBehavior_Throws()
{
CheckThrow(typeof(ArgumentNullException));
new ColorPicker(DarkRed(), ColorPickerBehavior.Default, null, new NumberFieldBehavior());
}
public void WithoutAFieldBehavior_Throws()
{
CheckThrow(typeof(ArgumentNullException));
new ColorPicker(DarkRed(), ColorPickerBehavior.Default, new TextBoxBehavior(), null);
}
public void EditsTheModelItIsGiven()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = Unstyled(model);
CheckTrue(ReferenceEquals(model, picker.Model));
}
}
public class BehaviorTests : TestFixture
{
public void Setting_RefreshesTheParts()
{
ColorPicker picker = PickerFor(DarkRed());
picker.Behavior = new ColorPickerBehavior { DiscShowsValue = false };
CheckEqual("none", Tint(picker.Disc));
}
public void SettingNull_Throws()
{
ColorPicker picker = PickerFor(DarkRed());
CheckThrow(typeof(ArgumentNullException));
picker.Behavior = null;
}
public void Getting_GivesWhatWasSet()
{
ColorPicker picker = PickerFor(DarkRed());
var behavior = new ColorPickerBehavior { DiscShowsValue = false };
picker.Behavior = behavior;
CheckTrue(ReferenceEquals(behavior, picker.Behavior));
}
}
public class PickerMaterialTests : TestFixture
{
public void Getting_GivesWhatWasSet()
{
ColorPicker picker = PickerFor(DarkRed());
CheckEqual(4.0, (double)picker.PickerMaterial.Spacing);
CheckEqual(20.0, (double)picker.PickerMaterial.SwatchHeight);
}
public void SizesThePickerToItsParts()
{
ColorPicker picker = PickerFor(DarkRed());
CheckEqual(new Vector(128, 124), picker.Bounds.Size);
}
public void PutsTheDiscAndStripsInARow()
{
ColorPicker picker = PickerFor(DarkRed());
CheckEqual(Box(0, 0, 100, 100), picker.Disc.Bounds);
CheckEqual(Box(104, 0, 10, 100), picker.ValueStrip.Bounds);
CheckEqual(Box(118, 0, 10, 100), picker.AlphaStrip.Bounds);
}
public void PutsTheBackdropBehindTheAlphaStrip()
{
ColorPicker picker = PickerFor(DarkRed());
CheckEqual(picker.AlphaStrip.Bounds, picker.AlphaBackdrop.Bounds);
}
public void PutsTheSwatchesInARowBelow()
{
ColorPicker picker = PickerFor(DarkRed());
CheckEqual(Box(0, 104, 62, 20), picker.OldSwatch.Bounds);
CheckEqual(Box(66, 104, 62, 20), picker.NewSwatch.Bounds);
}
public void Setting_GivesThePartsTheirArt()
{
ColorPicker picker = PickerFor(DarkRed());
CheckTrue(ChildOf(picker.Disc) is TextureMaterial);
CheckTrue(ChildOf(picker.ValueStrip) is TextureMaterial);
CheckTrue(ChildOf(picker.AlphaStrip) is TextureMaterial);
CheckTrue(picker.AlphaBackdrop.Material is TextureMaterial);
CheckTrue(picker.DiscMarker.Material is TextureMaterial);
CheckTrue(picker.ValueMarker.Material is TextureMaterial);
CheckTrue(picker.AlphaMarker.Material is TextureMaterial);
}
public void Setting_ShowsThePartsItGaveArt()
{
ColorPicker picker = PickerFor(DarkRed());
CheckTrue(picker.Disc.Visible);
CheckTrue(picker.ValueStrip.Visible);
CheckTrue(picker.AlphaStrip.Visible);
CheckTrue(picker.AlphaBackdrop.Visible);
CheckTrue(picker.DiscMarker.Visible);
CheckTrue(picker.ValueMarker.Visible);
CheckTrue(picker.AlphaMarker.Visible);
}
public void APartLeftNull_IsHiddenAndTakesNoRoom()
{
ColorPicker picker = Unstyled(DarkRed());
CheckNull(picker.AlphaBackdrop.Material);
CheckFalse(picker.AlphaBackdrop.Visible);
CheckNull(ChildOf(picker.Disc));
CheckFalse(picker.Disc.Visible);
CheckEqual(Vector.Zero, picker.Disc.Bounds.Size);
}
public void APartMadeNullAfterwards_IsHidden()
{
ColorPicker picker = PickerFor(DarkRed());
picker.PickerMaterial = Look() with { AlphaBackdrop = null };
CheckNull(picker.AlphaBackdrop.Material);
CheckFalse(picker.AlphaBackdrop.Visible);
}
public void TheSwatchesAreAlwaysShown()
{
ColorPicker picker = Unstyled(DarkRed());
CheckTrue(picker.OldSwatch.Visible);
CheckTrue(picker.NewSwatch.Visible);
CheckTrue(ChildOf(picker.OldSwatch) is SolidColorMaterial);
CheckTrue(ChildOf(picker.NewSwatch) is SolidColorMaterial);
CheckFalse(ReferenceEquals(picker.OldSwatch.Material, picker.NewSwatch.Material));
}
public void TheBackgroundIsThePickersOwnMaterial()
{
ColorPicker picker = PickerFor(DarkRed());
CheckTrue(ReferenceEquals(Material.Invisible, picker.Material));
}
public void WithNoBackground_ThePickerHasNoMaterialAndNoBorder()
{
ColorPicker picker = Unstyled(DarkRed());
CheckNull(picker.Material);
CheckEqual(Vector.Zero, picker.Bounds.Size);
}
public void TheBackgroundsBorderIsRoomAroundTheParts()
{
ColorPicker picker = PickerFor(DarkRed());
picker.PickerMaterial = Look() with
{
Background = new InvisibleMaterial { BorderSize = new Rect.BorderSize(5) },
};
CheckEqual(new Vector(138, 134), picker.Bounds.Size);
CheckEqual(Box(0, 0, 100, 100), picker.Disc.Bounds);
}
}
public class TintTests : TestFixture
{
public void ByDefault_EachPartFollowsTheColor()
{
ColorPicker picker = PickerFor(DarkRed());
CheckEqual("#333333FF", Tint(picker.Disc));
CheckEqual("#FF0000FF", Tint(picker.ValueStrip));
CheckEqual("#330000FF", Tint(picker.AlphaStrip));
}
public void FollowTheModelAsItChanges()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerFor(model);
model.PickValue(1);
CheckEqual("#FFFFFFFF", Tint(picker.Disc));
CheckEqual("#FF0000FF", Tint(picker.AlphaStrip));
}
public void ChangingTheColor_BuildsNoNewMaterials()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerFor(model);
Material disc = picker.Disc.Material;
Material newSwatch = picker.NewSwatch.Material;
model.PickValue(1);
CheckTrue(ReferenceEquals(disc, picker.Disc.Material));
CheckTrue(ReferenceEquals(newSwatch, picker.NewSwatch.Material));
}
public void ADiscNotShowingValue_IsLeftUntinted()
{
ColorPicker picker = PickerFor(DarkRed(), new ColorPickerBehavior { DiscShowsValue = false });
CheckEqual("none", Tint(picker.Disc));
CheckEqual("#FF0000FF", Tint(picker.ValueStrip));
CheckEqual("#330000FF", Tint(picker.AlphaStrip));
}
public void AValueStripNotShowingHueAndSaturation_IsLeftUntinted()
{
ColorPicker picker = PickerFor(
DarkRed(), new ColorPickerBehavior { ValueStripShowsHueAndSaturation = false });
CheckEqual("#333333FF", Tint(picker.Disc));
CheckEqual("none", Tint(picker.ValueStrip));
CheckEqual("#330000FF", Tint(picker.AlphaStrip));
}
public void AnAlphaStripNotShowingTheColor_IsLeftUntinted()
{
ColorPicker picker = PickerFor(DarkRed(), new ColorPickerBehavior { AlphaStripShowsColor = false });
CheckEqual("#333333FF", Tint(picker.Disc));
CheckEqual("#FF0000FF", Tint(picker.ValueStrip));
CheckEqual("none", Tint(picker.AlphaStrip));
}
}
public class SwatchTests : TestFixture
{
public void OldIsTheOriginalColor()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerFor(model);
model.PickValue(1);
CheckEqual("#330000FF", Tint(picker.OldSwatch));
}
public void NewFollowsTheColor()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerFor(model);
model.PickValue(1);
CheckEqual("#FF0000FF", Tint(picker.NewSwatch));
}
}
public class MarkerTests : TestFixture
{
public void DiscMarker_SitsAtTheHueAndSaturation()
{
ColorPicker picker = PickerFor(new ColorPickerModel(Color.FromArgb(255, 0, 0)));
CheckEqual(Box(95, 45, 10, 10), picker.DiscMarker.Bounds);
}
public void DiscMarker_MovesWithTheColor()
{
ColorPickerModel model = new ColorPickerModel(Color.FromArgb(255, 0, 0));
ColorPicker picker = PickerFor(model);
model.PickDisc(0, -0.5);
CheckEqual(Box(45, 20, 10, 10), picker.DiscMarker.Bounds);
}
public void StripMarkers_SitAtTheTopForFullValueAndAlpha()
{
ColorPicker picker = PickerFor(new ColorPickerModel(Color.FromArgb(255, 0, 0)));
CheckEqual(Box(104, -2, 10, 4), picker.ValueMarker.Bounds);
CheckEqual(Box(118, -2, 10, 4), picker.AlphaMarker.Bounds);
}
public void StripMarkers_MoveWithTheColor()
{
ColorPickerModel model = new ColorPickerModel(Color.FromArgb(255, 0, 0));
ColorPicker picker = PickerFor(model);
model.PickValue(0.5);
model.PickAlpha(0.25);
CheckEqual(Box(104, 48, 10, 4), picker.ValueMarker.Bounds);
CheckEqual(Box(118, 73, 10, 4), picker.AlphaMarker.Bounds);
}
}
public class HexBoxTests : TestFixture
{
public void WithNoLook_IsHiddenAndTakesNoRoom()
{
ColorPicker picker = PickerFor(DarkRed());
CheckFalse(picker.HexBox.Visible);
CheckEqual(new Vector(128, 124), picker.Bounds.Size);
}
public void WithALook_IsShownUnderTheSwatches()
{
ColorPicker picker = PickerWithHexBox(DarkRed());
CheckTrue(picker.HexBox.Visible);
CheckEqual(Box(0, 128, 128, 16), picker.HexBox.Bounds);
CheckEqual(new Vector(128, 144), picker.Bounds.Size);
}
public void WithALook_LeavesTheSwatchesWhereTheyWere()
{
ColorPicker picker = PickerWithHexBox(DarkRed());
CheckEqual(Box(0, 104, 62, 20), picker.OldSwatch.Bounds);
CheckEqual(Box(66, 104, 62, 20), picker.NewSwatch.Bounds);
}
public void WithAScaledLook_IsTallerByTheScale()
{
ColorPicker picker = PickerFor(DarkRed(), look: Look() with { HexBox = HexLook() with { Scale = 2 } });
CheckEqual(32, picker.HexBox.Bounds.Height);
}
public void WithNoTextStyle_IsAsTallAsItsBackground()
{
ColorPickerMaterial look = Look() with { HexBox = HexLook() with { Text = null } };
ColorPicker picker = PickerFor(DarkRed(), look: look);
CheckEqual(6, picker.HexBox.Bounds.Height);
}
public void LookRemovedAfterwards_HidesIt()
{
ColorPicker picker = PickerWithHexBox(DarkRed());
picker.PickerMaterial = Look();
CheckFalse(picker.HexBox.Visible);
CheckEqual(new Vector(128, 124), picker.Bounds.Size);
}
public void IsLimitedToAColorWithItsAlpha()
{
CheckEqual(9, PickerWithHexBox(DarkRed()).HexBox.Buffer.MaxLength);
}
public void ShowsTheColorsHex()
{
CheckEqual("#330000", PickerWithHexBox(DarkRed()).HexBox.Text);
}
public void FollowsTheModel()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerWithHexBox(model);
model.PickValue(1);
CheckEqual("#FF0000", picker.HexBox.Text);
}
public void ShowsTheAlphaWhenTheColorIsNotOpaque()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerWithHexBox(model);
model.PickAlpha(0);
CheckEqual("#33000000", picker.HexBox.Text);
}
public void Committed_SetsTheColor()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerWithHexBox(model);
picker.HexBox.Text = "#00FF0";
KeyboardManager keys = Focus(picker);
Type(picker, "0");
PressEnter(keys);
CheckEqual("#00FF00", model.Hex);
}
public void Committed_WritesTheColorAsTheModelDoes()
{
ColorPicker picker = PickerWithHexBox(DarkRed());
picker.HexBox.Text = "#0f";
KeyboardManager keys = Focus(picker);
Type(picker, "0");
PressEnter(keys);
CheckEqual("#00FF00", picker.HexBox.Text);
}
public void Committed_WithSixDigits_MakesTheColorOpaque()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerWithHexBox(model);
model.PickAlpha(0);
picker.HexBox.Text = "#00FF0";
KeyboardManager keys = Focus(picker);
Type(picker, "0");
PressEnter(keys);
CheckEqual("#00FF00", model.Hex);
}
public void Committed_WithTextThatIsNotAColor_PutsTheColorBack()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerWithHexBox(model);
picker.HexBox.Text = "#00FF0";
KeyboardManager keys = Focus(picker);
Type(picker, "G");
PressEnter(keys);
CheckEqual("#330000", model.Hex);
CheckEqual("#330000", picker.HexBox.Text);
}
public void Typing_LeavesTheColorAlone()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerWithHexBox(model);
picker.HexBox.Text = "#00FF0";
Focus(picker);
Type(picker, "0");
CheckEqual("#330000", model.Hex);
}
public void TypingWhenTheBehaviorFollowsIt_SetsTheColor()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerWithHexBox(model, new ColorPickerBehavior { HexUpdatesWhileTyping = true });
picker.HexBox.Text = "#00ff0";
Focus(picker);
Type(picker, "0");
CheckEqual("#00FF00", model.Hex);
}
public void TypingWhenTheBehaviorFollowsIt_LeavesTheTextAsTyped()
{
ColorPicker picker = PickerWithHexBox(
DarkRed(), new ColorPickerBehavior { HexUpdatesWhileTyping = true });
picker.HexBox.Text = "#00ff0";
Focus(picker);
Type(picker, "0");
CheckEqual("#00ff00", picker.HexBox.Text);
}
public void TypingWhenTheBehaviorFollowsIt_IgnoresTextThatIsNotAColorYet()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerWithHexBox(model, new ColorPickerBehavior { HexUpdatesWhileTyping = true });
picker.HexBox.Text = "#00ff";
Focus(picker);
Type(picker, "0");
CheckEqual("#330000", model.Hex);
}
public void ShowingTheColorWhenTheBehaviorFollowsIt_DoesNotRoundTheModelToBytes()
{
ColorPickerModel model = DarkRed();
PickerWithHexBox(model, new ColorPickerBehavior { HexUpdatesWhileTyping = true });
model.PickValue(0.5037);
CheckEqual(0.5037, model.Current.Value);
}
}
public class FieldTests : TestFixture
{
public void WithoutALook_AreHiddenAndTakeNoRoom()
{
ColorPicker picker = PickerFor(DarkRed());
CheckFalse(picker.HueField.Visible);
CheckFalse(picker.AlphaField.Visible);
CheckFalse(picker.HueLabel.Visible);
CheckEqual(new Vector(128, 124), picker.Bounds.Size);
}
public void HaveTheLettersHEsVAndATillTheHostChangesThem()
{
ColorPicker picker = PickerFor(DarkRed());
CheckEqual("H", picker.HueLabel.Text);
CheckEqual("S", picker.SaturationLabel.Text);
CheckEqual("V", picker.ValueLabel.Text);
CheckEqual("A", picker.AlphaLabel.Text);
}
public void WithALook_AreARowOfFourCells_UnderTheSwatches()
{
ColorPicker picker = PickerFor(DarkRed(), look: FieldLook());
CheckEqual(new Vector(228, 148), picker.Bounds.Size);
CheckEqual(Box(0, 128, 10, 20), picker.HueLabel.Bounds);
CheckEqual(Box(14, 128, 40, 20), picker.HueField.Bounds);
CheckEqual(Box(58, 128, 10, 20), picker.SaturationLabel.Bounds);
CheckEqual(Box(72, 128, 40, 20), picker.SaturationField.Bounds);
CheckEqual(Box(116, 128, 10, 20), picker.ValueLabel.Bounds);
CheckEqual(Box(130, 128, 40, 20), picker.ValueField.Bounds);
CheckEqual(Box(174, 128, 10, 20), picker.AlphaLabel.Bounds);
CheckEqual(Box(188, 128, 40, 20), picker.AlphaField.Bounds);
}
public void SitUnderTheHexBox()
{
ColorPickerMaterial look = FieldLook() with { HexBox = HexLook() };
ColorPicker picker = PickerFor(DarkRed(), look: look);
CheckEqual(new Vector(228, 168), picker.Bounds.Size);
CheckEqual(Box(14, 148, 40, 20), picker.HueField.Bounds);
}
public void WithoutLetterStyle_HaveNoLettersAndTheirRoom()
{
ColorPicker picker = PickerFor(DarkRed(), look: FieldLook(withLetters: false));
CheckFalse(picker.HueLabel.Visible);
CheckEqual(new Vector(172, 148), picker.Bounds.Size);
CheckEqual(Box(0, 128, 40, 20), picker.HueField.Bounds);
CheckEqual(Box(132, 128, 40, 20), picker.AlphaField.Bounds);
}
public void LettersTheHostChanged_GetTheRoomTheyNeedOnceLaidOutAgain()
{
ColorPicker picker = PickerFor(DarkRed(), look: FieldLook());
picker.HueLabel.Text = "Hue";
picker.Layout();
CheckEqual(Box(0, 128, 30, 20), picker.HueLabel.Bounds);
CheckEqual(Box(34, 128, 40, 20), picker.HueField.Bounds);
CheckEqual(new Vector(308, 148), picker.Bounds.Size);
}
public void ShowTheColorInDegreesAndPercent()
{
ColorPicker picker = PickerFor(DarkRed());
CheckEqual(0m, picker.HueField.Value);
CheckEqual(100m, picker.SaturationField.Value);
CheckEqual(20m, picker.ValueField.Value);
CheckEqual(100m, picker.AlphaField.Value);
}
public void FollowTheModel_RoundedToWholeNumbers()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerFor(model);
model.Current = new Hsv(1.0 / 3, 0.504, 0.5, 0.2);
CheckEqual(120m, picker.HueField.Value);
CheckEqual(50m, picker.SaturationField.Value);
CheckEqual(50m, picker.ValueField.Value);
CheckEqual(20m, picker.AlphaField.Value);
}
public void ShowAHueNearlyAroundTheTurnAsZero()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerFor(model);
model.PickHue(359.7 / 360);
CheckEqual(0m, picker.HueField.Value);
}
public void HueField_SetsTheModelsHue()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerFor(model);
picker.HueField.Value = 120;
CheckTrue(Math.Abs(1.0 / 3 - model.Current.Hue) < Tolerance);
}
public void SaturationField_SetsTheModelsSaturation()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerFor(model);
picker.SaturationField.Value = 25;
CheckEqual(0.25, model.Current.Saturation);
}
public void ValueField_SetsTheModelsValue()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerFor(model);
picker.ValueField.Value = 30;
CheckEqual(0.3, model.Current.Value);
}
public void AlphaField_SetsTheModelsAlpha()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerFor(model);
picker.AlphaField.Value = 40;
CheckEqual(0.4, model.Current.Alpha);
}
public void ChangingOne_LeavesTheModelsOtherPartsUnrounded()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerFor(model);
model.PickSaturation(0.5037);
picker.ValueField.Value = 30;
CheckEqual(0.5037, model.Current.Saturation);
}
public void HueField_AtThreeSixty_KeepsShowingIt()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerFor(model);
picker.HueField.Value = 120;
picker.HueField.Value = 360;
CheckEqual(360m, picker.HueField.Value);
CheckEqual(0.0, model.Current.Hue);
}
public void WhileScrubbing_ReportEachStepUnlessTheBehaviorWaits()
{
ColorPicker picker = PickerFor(DarkRed());
CheckTrue(picker.HueField.RaisesValueChangedWhileScrubbing);
CheckTrue(picker.AlphaField.RaisesValueChangedWhileScrubbing);
picker.Behavior = new ColorPickerBehavior { FieldsUpdateWhileScrubbing = false };
CheckFalse(picker.HueField.RaisesValueChangedWhileScrubbing);
CheckFalse(picker.SaturationField.RaisesValueChangedWhileScrubbing);
CheckFalse(picker.ValueField.RaisesValueChangedWhileScrubbing);
CheckFalse(picker.AlphaField.RaisesValueChangedWhileScrubbing);
}
public void UseTheFieldBehaviorTheyWereGiven()
{
var fieldBehavior = new NumberFieldBehavior();
var picker = new ColorPicker(
DarkRed(), ColorPickerBehavior.Default, new TextBoxBehavior(), fieldBehavior);
CheckTrue(ReferenceEquals(fieldBehavior, picker.ValueField.Behavior));
}
}
public class PickingTests : TestFixture
{
private static bool Near(double expected, double actual)
{
return Math.Abs(expected - actual) < Tolerance;
}
public void PressingTheDisc_PicksHueAndSaturation()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerFor(model);
Press(picker.Disc, 50, 25);
CheckTrue(Near(0.25, model.Current.Hue));
CheckTrue(Near(0.5, model.Current.Saturation));
}
public void PressingTheValueStrip_PicksValueFromTheTop()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerFor(model);
Press(picker.ValueStrip, 109, 25);
CheckTrue(Near(0.75, model.Current.Value));
}
public void PressingTheAlphaStrip_PicksAlphaFromTheTop()
{
ColorPickerModel model = DarkRed();
ColorPicker picker = PickerFor(model);
Press(picker.AlphaStrip, 123, 100);
CheckTrue(Near(0, model.Current.Alpha));
}
}
}
}