/// <summary> /// Save the restore bounds of the window. /// </summary> /// <param name="filename">The file to store the Rect data.</param> /// <param name="restoreBounds">The Rect object.</param> static public void SaveWindowBounds(string filename, Rect restoreBounds) { // Save restore bounds for the next time this window is opened IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForAssembly(); using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(filename, FileMode.Create, storage)) using (StreamWriter writer = new StreamWriter(stream)) { // Write restore bounds value to file writer.WriteLine(restoreBounds.ToString()); } }
public void ToString_FormatException () { // This test does not currently work because // String.Format does not throw all necessary exceptions IFormattable rFormattable = new Rect (1.0, 2.5, 3, 4); bool exceptionRaised = false; try { rFormattable.ToString ("{", null); } catch (FormatException) { exceptionRaised = true; } Assert.IsTrue (exceptionRaised, "Expected FormatException with IFormattable.ToString (\"{\", null)"); }
public void ToStringTest () { Rect r = new Rect (1.0, 2.5, 3, 4); string expectedStringOutput = "1,2.5,3,4"; Assert.AreEqual (expectedStringOutput, r.ToString ()); Assert.AreEqual (expectedStringOutput, r.ToString (null)); Assert.AreEqual ("Empty", Rect.Empty.ToString ()); // IFormattable.ToString IFormattable rFormattable = r; Assert.AreEqual (expectedStringOutput, rFormattable.ToString (null, null), "IFormattable.ToString with null format"); Assert.AreEqual (expectedStringOutput, rFormattable.ToString (string.Empty, null), "IFormattable.ToString with empty format"); Assert.AreEqual ("1.00,2.50,3.00,4.00", rFormattable.ToString ("N2", null), "IFormattable.ToString with N2 format"); Assert.AreEqual ("blah,blah,blah,blah", rFormattable.ToString ("blah", null), "IFormattable.ToString with blah format"); Assert.AreEqual (":,:,:,:", rFormattable.ToString (":", null), "IFormattable.ToString with : format"); Assert.AreEqual ("Empty", ((IFormattable) Rect.Empty).ToString ("blah", null), "IFormattable.ToString on Rect.Empty with blah format"); foreach (CultureInfo culture in CultureInfo.GetCultures (CultureTypes.AllCultures)) { if (culture.IsNeutralCulture) continue; string separator = ","; if (culture.NumberFormat.NumberDecimalSeparator == separator) separator = ";"; expectedStringOutput = 1.ToString (culture) + separator + 2.5.ToString (culture) + separator + 3.ToString (culture) + separator + 4.ToString (culture); Assert.AreEqual (expectedStringOutput, r.ToString (culture), "ToString with Culture: " + culture.Name); Assert.AreEqual ("Empty", Rect.Empty.ToString (culture), "ToString on Empty with Culture: " + culture.Name); // IFormattable.ToString Assert.AreEqual (expectedStringOutput, rFormattable.ToString (null, culture), "IFormattable.ToString with null format with Culture: " + culture.Name); Assert.AreEqual (expectedStringOutput, rFormattable.ToString (string.Empty, culture), "IFormattable.ToString with empty format with Culture: " + culture.Name); expectedStringOutput = 1.ToString ("N2", culture) + separator + 2.5.ToString ("N2", culture) + separator + 3.ToString ("N2", culture) + separator + 4.ToString ("N2", culture); Assert.AreEqual (expectedStringOutput, rFormattable.ToString ("N2", culture), "IFormattable.ToString with N2 format with Culture: " + culture.Name); } }
public static string AsString(this Rect rect) => rect.ToString(CultureInfo.InvariantCulture);
static void TS_VerifyWithinRects(Rect outerRect, Rect[] innerRects, GetBoundRectResult getBoundRectResult, CheckType checkType) { double areaOuter = 0; double areaInner = 0; // Sanity check... are any of our inners outside our outer? for (int i = 0; i < innerRects.Length; i++) { if ((innerRects[i].Top < outerRect.Top) || (innerRects[i].Left < outerRect.Left) || (innerRects[i].Right > outerRect.Right) || (innerRects[i].Bottom > outerRect.Bottom)) { ThrowMe(checkType, "InnerRect " + innerRects[i].ToString(CultureInfo.InvariantCulture) + " incorrectly outside autoElement.BoundRect " + outerRect.ToString(CultureInfo.InvariantCulture)); } else { areaInner += (innerRects[i].Bottom - innerRects[i].Top) * (innerRects[i].Right - innerRects[i].Left); Comment("Bounding Rectangle # " + i + " (" + innerRects[i].ToString(CultureInfo.InvariantCulture) + ") within autoElement.BoundRect " + outerRect.ToString(CultureInfo.InvariantCulture)); } } // Now, lets split some hairs areaOuter = (outerRect.Bottom - outerRect.Top) * (outerRect.Right - outerRect.Left); Comment("Inner area = " + areaInner); Comment("Outer area = " + areaOuter); // Now, validate results switch (getBoundRectResult) { case GetBoundRectResult.SubsetOfAutoElementBoundRect: if (areaInner < areaOuter) Comment("Inner rectangle(s) is/are expected size, i.e. a small portion of the outer rectangle"); else if (innerRects.Length == 0) ThrowMe(checkType, "Was not expecting an EMPTY array of bounding rectangles"); else ThrowMe(checkType, "Unexpected state for test"); break; case GetBoundRectResult.EmptyArray: if (innerRects.Length == 0) Comment("Empty array of bounding rectangles, as expected"); else ThrowMe(checkType, "Was not expecting an a NON-EMPTY array of bounding rectangles"); break; default: throw new ArgumentException("TS_VerifyWithinRects() has no support for " + ParseType(getBoundRectResult)); } m_TestStep++; }
//--------------------------------------------------------------------------- // Acquire valid bounding rectangle for AutomationElement //--------------------------------------------------------------------------- internal void GetAutomationElementBoundingRectangle(out Rect boundingRect) { // FxCop doesn't like calls that return object and box value types // hence the two step process for getting a property object temp = m_le.GetCurrentPropertyValue( AutomationElement.BoundingRectangleProperty); boundingRect = (Rect)temp; Comment("AutomationElement Bounding Rectangle = " + boundingRect.ToString(CultureInfo.InvariantCulture)); }
internal void TS_CreatePoint(ref TextPatternRange range, Rect[] boundRects, out Point screenLocation, BoundingRectangleLocation boundRectLoc, CheckType checkType) { int rectIdx = 0; Rect autoElementRect = new Rect(); Rect[] tempRects = new Rect[0]; TextPatternRange documentRange = Pattern_DocumentRange(CheckType.Verification); screenLocation = new Point(); // Sanity check Library.ValidateArgumentNonNull(range, "range argument cannot be null"); if ((boundRects.Length == 0) && (boundRectLoc != BoundingRectangleLocation.OutsideAutomationElement)) { throw new ArgumentException("TS_CreatePoint requires non-empty array of bounding rectangles"); } // Finally, generate the point! switch (boundRectLoc) { case BoundingRectangleLocation.InsideTopLeft: rectIdx = 0; screenLocation.X = boundRects[rectIdx].Left + 1; screenLocation.Y = boundRects[rectIdx].Top + 1; break; case BoundingRectangleLocation.Middle: screenLocation.X = (boundRects[rectIdx].Left + boundRects[rectIdx].Right) / 2; screenLocation.Y = (boundRects[rectIdx].Top + boundRects[rectIdx].Bottom) / 2; break; case BoundingRectangleLocation.InsideBottomRight: rectIdx = boundRects.Length - 1; screenLocation.X = boundRects[rectIdx].Right - 1; screenLocation.Y = boundRects[rectIdx].Bottom - 1; break; case BoundingRectangleLocation.OutsideBottomRight: rectIdx = boundRects.Length - 1; screenLocation.X = boundRects[rectIdx].Right + 1; screenLocation.Y = boundRects[rectIdx].Bottom + 1; break; case BoundingRectangleLocation.OutsideTopLeft: rectIdx = 0; screenLocation.X = boundRects[rectIdx].Left - 1; screenLocation.Y = boundRects[rectIdx].Top - 1; break; case BoundingRectangleLocation.OutsideAutomationElement: // Get automation element bounding rectangle GetAutomationElementBoundingRectangle(out autoElementRect); screenLocation.X = autoElementRect.Left - 1; screenLocation.Y = autoElementRect.Top - 1; break; case BoundingRectangleLocation.FirstChar: tempRects = null; Range_GetBoundingRectangles(documentRange, ref tempRects, null, checkType); if (tempRects.Length == 0) ThrowMe(checkType, "TS_CreatePoint expects non-empy bounding rectangles array for document"); screenLocation.X = tempRects[0].Left + 1; // essentially top-left of first rect screenLocation.Y = tempRects[0].Top + 1; break; case BoundingRectangleLocation.FirstCharInRange: rectIdx = 0; screenLocation.X = boundRects[0].Left + 1; // essentially top-left of first rect screenLocation.Y = boundRects[0].Top + 1; break; case BoundingRectangleLocation.LastCharInRange: rectIdx = boundRects.Length - 1; screenLocation.X = boundRects[rectIdx].Right - 1; // essentially bottom-right of last rect screenLocation.Y = boundRects[rectIdx].Bottom - 1; break; default: throw new ArgumentException("TS_CreatePoint() has no support for " + ParseType(boundRectLoc)); } Comment("Created Point (" + screenLocation.ToString(CultureInfo.InvariantCulture) + ") at " + Parse(boundRectLoc) + " relative to boundRect " + (boundRectLoc != BoundingRectangleLocation.OutsideAutomationElement ? boundRects[rectIdx].ToString(CultureInfo.InvariantCulture) : autoElementRect.ToString(CultureInfo.InvariantCulture))); m_TestStep++; }
public void ToStringIFormatProvider () { Rect r = new Rect (-2, -1, 1, 2); RectFormatter.CallCount = 0; Assert.AreEqual ("-2,-1,1,2", r.ToString (null), "null"); Assert.AreEqual (0, RectFormatter.CallCount, "CallCount-a"); Assert.AreEqual ("[-2]#[-1]#[1]#[2]", r.ToString (new RectFormatter ()), "RectFormatter"); // 7 times: one per double (4) and 3 for ',' Assert.AreEqual (7, RectFormatter.CallCount, "CallCount"); }
public void TestToString () { Rect rect = new Rect (10, 20, 30, 40); Assert.AreEqual ("10,20,30,40", rect.ToString ()); }
public virtual void ClickablePointPropertyTest () { IRawElementProviderSimple provider = GetProvider (); bool offscreen = (bool) provider.GetPropertyValue (AutomationElementIdentifiers.IsOffscreenProperty.Id); object clickablePointObj = provider.GetPropertyValue (AutomationElementIdentifiers.ClickablePointProperty.Id); Point clickablePoint = new Point (0, 0); // Clickable point should be either null or Rect.Empty if (offscreen) { if (clickablePointObj != null) { try { clickablePoint = (Point) clickablePointObj; Assert.IsTrue (clickablePoint.X >= 0, string.Format ("X is negative, your provider should be OffScreen: {0}", clickablePoint.X)); Assert.IsTrue (clickablePoint.Y >= 0, string.Format ("Y is negative, your provider should be OffScreen: {0}", clickablePoint.Y)); } catch (InvalidCastException) { Assert.Fail (string.Format ("You are not returning System.Windows.Point in ClickablePointProperty: {0}", clickablePoint,GetType ())); } } // Clickable point should intersect bounding rectangle... } else { if (clickablePointObj == null) // ...unless you are not clickable at all return; Assert.IsNotNull (clickablePoint, "Your ClickablePointProperty should not be null."); try { clickablePoint = (Point) clickablePointObj; Assert.IsTrue (clickablePoint.X >= 0, string.Format ("X is negative, your provider should be OffScreen: {0}", clickablePoint.X)); Assert.IsTrue (clickablePoint.Y >= 0, string.Format ("Y is negative, your provider should be OffScreen: {0}", clickablePoint.Y)); } catch (InvalidCastException) { Assert.Fail (string.Format ("You are not returning System.Windows.Point in ClickablePointProperty: {0}", clickablePoint.GetType ())); } object boundingRectangle = provider.GetPropertyValue (AutomationElementIdentifiers.BoundingRectangleProperty.Id); Assert.IsNotNull (boundingRectangle, "You need to return BoundingRectangle if you return ClickablePointProperty."); try { Rect boundingRectangleRect = (Rect) boundingRectangle; Assert.AreNotEqual (Rect.Empty, boundingRectangleRect, "BoundingRectangle SHOULD NOT be Rect.Empty"); Rect clickablePointRect = new Rect (clickablePoint.X, clickablePoint.Y, 1, 1); Assert.IsTrue (boundingRectangleRect.Contains (clickablePointRect), string.Format ("ClickablePoint ({0}) SHOULD Intersect with BoundingRectangle: {1}", clickablePointRect.ToString (), boundingRectangleRect.ToString ())); } catch (InvalidCastException) { Assert.Fail (string.Format ("You are not returning System.Windows.Rect in BoundingRectangle: {0}", boundingRectangle.GetType ())); } } }
private static void SaveWindowSize(object sender, EventArgs e) { WindowBaseViewModel model = (WindowBaseViewModel)sender; string key = model.GetType().AssemblyQualifiedName; Rect rect = new Rect(model.Surface.Top, model.Surface.Left, model.Surface.Width, model.Surface.Height); if (Properties.Settings.Default.WindowSizeState == null) Properties.Settings.Default.WindowSizeState = new System.Collections.Specialized.StringDictionary(); if (Properties.Settings.Default.WindowSizeState.ContainsKey(key)) Properties.Settings.Default.WindowSizeState[key] = rect.ToString(); else Properties.Settings.Default.WindowSizeState.Add(key, rect.ToString()); Properties.Settings.Default.Save(); }