|
| 1 | +/******************************************************************************* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2018 Jean-David Gadina - www.imazing.com |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + ******************************************************************************/ |
| 24 | + |
| 25 | +using System; |
| 26 | +using System.Reflection; |
| 27 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 28 | +using ColorSetKit; |
| 29 | +using System.Windows.Media; |
| 30 | + |
| 31 | +namespace ColorSetKit_Test |
| 32 | +{ |
| 33 | + [TestClass] |
| 34 | + public class Test |
| 35 | + { |
| 36 | + [TestMethod] |
| 37 | + public void TestInitWithPath() |
| 38 | + { |
| 39 | + string path = System.IO.Path.Combine( System.IO.Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location ), "Colors.colorset" ); |
| 40 | + ColorSet set = new ColorSet( path ); |
| 41 | + |
| 42 | + Assert.IsTrue( set.Colors.Count > 0 ); |
| 43 | + } |
| 44 | + |
| 45 | + [TestMethod] |
| 46 | + public void TestInitWithData() |
| 47 | + { |
| 48 | + string path = System.IO.Path.Combine( System.IO.Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location ), "Colors.colorset" ); |
| 49 | + Data data = new Data( path ); |
| 50 | + ColorSet set = new ColorSet( data ); |
| 51 | + |
| 52 | + Assert.IsTrue( set.Colors.Count > 0 ); |
| 53 | + } |
| 54 | + |
| 55 | + [TestMethod] |
| 56 | + public void TestShared() |
| 57 | + { |
| 58 | + Assert.AreEqual( ColorSet.Shared.Colors.Count, 2 ); |
| 59 | + |
| 60 | + Assert.IsTrue( ColorSet.Shared.Colors.ContainsKey( "NoVariant" ) ); |
| 61 | + Assert.IsTrue( ColorSet.Shared.Colors.ContainsKey( "Variant" ) ); |
| 62 | + |
| 63 | + ColorPair p1 = ColorSet.Shared.Colors[ "NoVariant" ]; |
| 64 | + ColorPair p2 = ColorSet.Shared.Colors[ "Variant" ]; |
| 65 | + |
| 66 | + if( p1.Color is SolidColorBrush c ) |
| 67 | + { |
| 68 | + Assert.AreEqual( c.Color.R, 50 ); |
| 69 | + Assert.AreEqual( c.Color.G, 100 ); |
| 70 | + Assert.AreEqual( c.Color.B, 150 ); |
| 71 | + Assert.AreEqual( ( double )( c.Color.A ) / 256, 0.5 ); |
| 72 | + } |
| 73 | + else |
| 74 | + { |
| 75 | + Assert.Fail( "Color is not defined" ); |
| 76 | + } |
| 77 | + |
| 78 | + if( p1.Variant != null ) |
| 79 | + { |
| 80 | + Assert.Fail( "No variant should be defined" ); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /* |
| 85 | + func testShared() |
| 86 | + { |
| 87 | + if let c = p2.color |
| 88 | + { |
| 89 | + XCTAssertEqual( c.redComponent * 255, 250.0 ) |
| 90 | + XCTAssertEqual( c.greenComponent * 255, 200.0 ) |
| 91 | + XCTAssertEqual( c.blueComponent * 255, 150.0 ) |
| 92 | + XCTAssertEqual( c.alphaComponent, 0.75 ) |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + XCTFail( "Color is not defined" ) |
| 97 | + } |
| 98 | + |
| 99 | + if let c = p2.variant |
| 100 | + { |
| 101 | + XCTAssertEqual( c.redComponent * 255, 200.0 ) |
| 102 | + XCTAssertEqual( c.greenComponent * 255, 150.0 ) |
| 103 | + XCTAssertEqual( c.blueComponent * 255, 250.0 ) |
| 104 | + XCTAssertEqual( c.alphaComponent, 0.25 ) |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + XCTFail( "Color is not defined" ) |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + func testNSColor() |
| 113 | + { |
| 114 | + if let c = NSColor.colorFrom( colorSet: "NoVariant" ) |
| 115 | + { |
| 116 | + XCTAssertEqual( c.redComponent * 255, 50.0 ) |
| 117 | + XCTAssertEqual( c.greenComponent * 255, 100.0 ) |
| 118 | + XCTAssertEqual( c.blueComponent * 255, 150.0 ) |
| 119 | + XCTAssertEqual( c.alphaComponent, 0.5 ) |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + XCTFail( "Cannot retrieve color from NSColor" ); return |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + func testCreate() |
| 128 | + { |
| 129 | + var set = ColorSet() |
| 130 | + |
| 131 | + set.add( color: NSColor( red: 0.1, green: 0.2, blue: 0.3, alpha: 0.4 ), forName: "NoVariant" ) |
| 132 | + set.add( color: NSColor( red: 0.5, green: 0.6, blue: 0.7, alpha: 0.8 ), variant: NSColor( red: 0.8, green: 0.7, blue: 0.6, alpha: 0.5 ), forName: "Variant" ) |
| 133 | + |
| 134 | + set = ColorSet( data: set.data ) ?? ColorSet() |
| 135 | + |
| 136 | + XCTAssertEqual( set.colors.count, 2 ) |
| 137 | + |
| 138 | + guard let p1 = set.colors[ "NoVariant" ] else |
| 139 | + { |
| 140 | + XCTFail( "Cannot retrieve color from shared color set file" ); return |
| 141 | + } |
| 142 | + |
| 143 | + guard let p2 = set.colors[ "Variant" ] else |
| 144 | + { |
| 145 | + XCTFail( "Cannot retrieve color from shared color set file" ); return |
| 146 | + } |
| 147 | + |
| 148 | + if let c = p1.color |
| 149 | + { |
| 150 | + XCTAssertEqual( c.redComponent, 0.1 ) |
| 151 | + XCTAssertEqual( c.greenComponent, 0.2 ) |
| 152 | + XCTAssertEqual( c.blueComponent, 0.3 ) |
| 153 | + XCTAssertEqual( c.alphaComponent, 0.4 ) |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + XCTFail( "Color is not defined" ) |
| 158 | + } |
| 159 | + |
| 160 | + if let _ = p1.variant |
| 161 | + { |
| 162 | + XCTFail( "No variant should be defined" ) |
| 163 | + } |
| 164 | + |
| 165 | + if let c = p2.color |
| 166 | + { |
| 167 | + XCTAssertEqual( c.redComponent, 0.5 ) |
| 168 | + XCTAssertEqual( c.greenComponent, 0.6 ) |
| 169 | + XCTAssertEqual( c.blueComponent, 0.7 ) |
| 170 | + XCTAssertEqual( c.alphaComponent, 0.8 ) |
| 171 | + } |
| 172 | + else |
| 173 | + { |
| 174 | + XCTFail( "Color is not defined" ) |
| 175 | + } |
| 176 | + |
| 177 | + if let c = p2.variant |
| 178 | + { |
| 179 | + XCTAssertEqual( c.redComponent, 0.8 ) |
| 180 | + XCTAssertEqual( c.greenComponent, 0.7 ) |
| 181 | + XCTAssertEqual( c.blueComponent, 0.6 ) |
| 182 | + XCTAssertEqual( c.alphaComponent, 0.5 ) |
| 183 | + } |
| 184 | + else |
| 185 | + { |
| 186 | + XCTFail( "Color is not defined" ) |
| 187 | + } |
| 188 | + } |
| 189 | + */ |
| 190 | + } |
| 191 | +} |
0 commit comments