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
|
// 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) 2012 SIL International (http://sil.org)
//
// Authors:
// Stephen McConnel (stephen_mcconnel@sil.org)
//
using System;
using System.Drawing;
using System.Windows.Forms;
using NUnit.Framework;
namespace MonoTests.System.Windows.Forms
{
[TestFixture]
public class ScreenTest
{
[TestFixtureSetUp]
public void FixtureSetup ()
{
// If there is only one screen, then FromRectangle always returns that screen,
// so this test would not test anything (and would fail on the second Assert
// below).
int screenCount = Screen.AllScreens.Length;
if (screenCount == 1)
Assert.Ignore ("These tests require at least 2 screens");
}
static Rectangle GetLowestScreenBounds ()
{
Rectangle lowestScreenBounds = new Rectangle (int.MaxValue, int.MaxValue, int.MaxValue, int.MaxValue);
foreach (Screen scrn in Screen.AllScreens) {
if (scrn.Bounds.X < lowestScreenBounds.X || scrn.Bounds.Y < lowestScreenBounds.Y)
lowestScreenBounds = scrn.Bounds;
}
return lowestScreenBounds;
}
[Test]
public void FromRectangleTest_ContainedWithinLowestScreen ()
{
var lowestScreenBounds = GetLowestScreenBounds ();
// If a rectangle is contained within the lowest screen, then the lowest screen
// should be found for that rectangle.
Rectangle testRect1 = new Rectangle (lowestScreenBounds.X + lowestScreenBounds.Width / 4,
lowestScreenBounds.Width / 2,
lowestScreenBounds.Y + lowestScreenBounds.Height / 4,
lowestScreenBounds.Height / 2);
Screen scrn1 = Screen.FromRectangle (testRect1);
Assert.AreEqual (lowestScreenBounds, scrn1.Bounds,
"Wrong screen was found for rectangle contained in the first screen");
}
[Test]
public void FromRectangleTest_SlightOverlapWithLowestScreen ()
{
var lowestScreenBounds = GetLowestScreenBounds ();
// If a rectangle overlaps only slightly within the lowest screen, then the lowest screen
// should not be found for that rectangle. (This is where the original implementation
// fails.)
Rectangle testRect2 = new Rectangle (lowestScreenBounds.X + lowestScreenBounds.Width - 15,
lowestScreenBounds.Width / 2,
lowestScreenBounds.Y + lowestScreenBounds.Height - 15,
lowestScreenBounds.Height / 2);
Screen scrn2 = Screen.FromRectangle (testRect2);
Assert.AreNotEqual (lowestScreenBounds, scrn2.Bounds,
"Wrong screen was found for rectangle slightly overlapping the first screen");
}
[Test]
public void FromRectangleTest_MostlyOverlapWithLowestScreen ()
{
var lowestScreenBounds = GetLowestScreenBounds ();
// If a rectangle overlaps mostly within the lowest screen, then the lowest screen
// should be found for that rectangle.
Rectangle testRect3 = new Rectangle (lowestScreenBounds.X + (lowestScreenBounds.Width / 2) + 15,
lowestScreenBounds.Width / 2,
lowestScreenBounds.Y + (lowestScreenBounds.Height / 2) + 15,
lowestScreenBounds.Height / 2);
Screen scrn3 = Screen.FromRectangle (testRect3);
Assert.AreEqual (lowestScreenBounds, scrn3.Bounds,
"Wrong screen was found for rectangle mostly overlapping the first screen");
}
}
}
|