|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.Windows.Threading; |
| 9 | + |
| 10 | +using System.Text; |
| 11 | +using System.ComponentModel; |
| 12 | +using System.Windows; |
| 13 | +using System.Windows.Interop; |
| 14 | +using System.Windows.Media; |
| 15 | +using System.Windows.Controls; |
| 16 | +using System.Windows.Input; |
| 17 | +using System.Windows.Markup; |
| 18 | +using System.Windows.Automation; |
| 19 | + |
| 20 | +namespace DRT |
| 21 | +{ |
| 22 | + public class Harness |
| 23 | + { |
| 24 | + [STAThread] |
| 25 | + public static int Main() |
| 26 | + { |
| 27 | + WindowDragMoveApp.DisableProcessWindowsGhosting(); |
| 28 | + WindowDragMoveApp theApp = new WindowDragMoveApp(); |
| 29 | + try |
| 30 | + { |
| 31 | + WindowDragMoveApp.BlockInput(1); |
| 32 | + theApp.Run(); |
| 33 | + } |
| 34 | + catch (ApplicationException e) |
| 35 | + { |
| 36 | + theApp.Logger.Log(e); |
| 37 | + return 1; |
| 38 | + } |
| 39 | + finally |
| 40 | + { |
| 41 | + WindowDragMoveApp.BlockInput(0); |
| 42 | + } |
| 43 | + theApp.Logger.Log("Passed"); |
| 44 | + return 0; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + // Tests DragMove functionality |
| 50 | + /// </summary> |
| 51 | + public class WindowDragMoveApp : Application |
| 52 | + { |
| 53 | + private static Logger _logger; |
| 54 | + |
| 55 | + private Window _win; |
| 56 | + |
| 57 | + private Border _root; |
| 58 | + |
| 59 | + private Point _oldPos; |
| 60 | + private Point _newPos; |
| 61 | + private Point _expectedPos; |
| 62 | + private Point _delta; |
| 63 | + private Point _correction; |
| 64 | + private static int TestCode = 0; |
| 65 | + |
| 66 | + public WindowDragMoveApp() : base() |
| 67 | + { |
| 68 | + _logger = new Logger("DrtWindowDragMove", "Microsoft", "Testing Window DragMove behavior"); |
| 69 | + } |
| 70 | + |
| 71 | + internal Logger Logger |
| 72 | + { |
| 73 | + get |
| 74 | + { |
| 75 | + return _logger; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + protected override void OnStartup(StartupEventArgs e) |
| 80 | + { |
| 81 | + _win = new Window(); |
| 82 | + _win.WindowStyle = WindowStyle.None; |
| 83 | + _win.Width = 500; |
| 84 | + _win.Height = 400; |
| 85 | + _win.Top = 0; |
| 86 | + _win.Left = 0; |
| 87 | + |
| 88 | + _root = new Border (); |
| 89 | + _root.Width = _win.Width; |
| 90 | + _root.Height = _win.Height; |
| 91 | + _root.Background = new SolidColorBrush(Colors.Red); |
| 92 | + _win.Content = _root; |
| 93 | + _root.AddHandler (Mouse.MouseDownEvent, new MouseButtonEventHandler (OnMouseDown), false); |
| 94 | + |
| 95 | + _win.Title = "Window DragMove test"; |
| 96 | + _win.Show(); |
| 97 | + |
| 98 | + _oldPos = new Point(_win.Left, _win.Top); |
| 99 | + _delta = new Point(100, 100); |
| 100 | + _correction = new Point(10, 10); |
| 101 | + _expectedPos = new Point(_oldPos.X + _delta.X - _correction.X, _oldPos.Y + _delta.Y - _correction.Y); |
| 102 | + |
| 103 | + Dispatcher.CurrentDispatcher.BeginInvoke( |
| 104 | + DispatcherPriority.Background, |
| 105 | + new DispatcherOperationCallback(DragMoveTest), |
| 106 | + this); |
| 107 | + |
| 108 | + base.OnStartup(e); |
| 109 | + } |
| 110 | + |
| 111 | + /* |
| 112 | + * Test Case: DragMove |
| 113 | + */ |
| 114 | + private object DragMoveTest(object obj) |
| 115 | + { |
| 116 | + HwndSource hwndSource = PresentationSource.FromVisual(_root) as HwndSource; |
| 117 | + Debug.Assert(hwndSource != null, "PresentaionSource has to be HwndSource here"); |
| 118 | + |
| 119 | + Point oldPosDeviceUnits = hwndSource.CompositionTarget.TransformToDevice.Transform(_oldPos); |
| 120 | + Point deltaDeviceUnits = hwndSource.CompositionTarget.TransformToDevice.Transform(_delta); |
| 121 | + Point correctionDeviceUnits = hwndSource.CompositionTarget.TransformToDevice.Transform(_correction); |
| 122 | + |
| 123 | + Input.MoveTo(new Point(oldPosDeviceUnits.X + correctionDeviceUnits.X, oldPosDeviceUnits.Y + correctionDeviceUnits.Y)); |
| 124 | + Input.SendMouseInput(oldPosDeviceUnits.X + correctionDeviceUnits.X, oldPosDeviceUnits.Y + correctionDeviceUnits.Y, 0, SystemParameters.SwapButtons ? SendMouseInputFlags.RightDown : SendMouseInputFlags.LeftDown); |
| 125 | + Input.MoveTo(new Point(oldPosDeviceUnits.X + deltaDeviceUnits.X, oldPosDeviceUnits.Y + deltaDeviceUnits.Y)); |
| 126 | + Input.SendMouseInput(oldPosDeviceUnits.X + deltaDeviceUnits.X, oldPosDeviceUnits.Y + deltaDeviceUnits.Y, 0, SystemParameters.SwapButtons ? SendMouseInputFlags.RightUp : SendMouseInputFlags.LeftUp); |
| 127 | + |
| 128 | + Dispatcher.CurrentDispatcher.BeginInvoke( |
| 129 | + DispatcherPriority.Background, |
| 130 | + new DispatcherOperationCallback(CloseWindow), |
| 131 | + this); |
| 132 | + return null; |
| 133 | + } |
| 134 | + |
| 135 | + private void OnMouseDown(object sender, MouseButtonEventArgs e) |
| 136 | + { |
| 137 | + _win.DragMove(); |
| 138 | + e.Handled = true; |
| 139 | + } |
| 140 | + |
| 141 | + public static void SetError(string msg) |
| 142 | + { |
| 143 | + _logger.Log("FAILURE: " + msg); |
| 144 | + SetError(); |
| 145 | + } |
| 146 | + |
| 147 | + public static void SetError() |
| 148 | + { |
| 149 | + TestCode = 1; |
| 150 | + } |
| 151 | + |
| 152 | + public static bool Failed |
| 153 | + { |
| 154 | + get { return TestCode != 0; } |
| 155 | + } |
| 156 | + |
| 157 | + private object CloseWindow(object obj) |
| 158 | + { |
| 159 | + _newPos = new Point((double)_win.GetValue(Window.LeftProperty), (double)_win.GetValue(Window.TopProperty)); |
| 160 | + if ((_expectedPos.X != Math.Round(_newPos.X)) || _expectedPos.Y != Math.Round(_newPos.Y)) |
| 161 | + { |
| 162 | + SetError("DragMove failed. The old position is " + _oldPos.ToString() + ". The new position is " + _newPos.ToString() + |
| 163 | + ". The expected position is " + _expectedPos.ToString()); |
| 164 | + throw new ApplicationException("DragMove failed"); |
| 165 | + } |
| 166 | + _win.Close(); |
| 167 | + return null; |
| 168 | + } |
| 169 | + [DllImport("user32.dll", CharSet = CharSet.Auto)] |
| 170 | + public static extern int BlockInput(int fBlockIt); |
| 171 | + |
| 172 | + [DllImport("user32.dll", ExactSpelling=true, CharSet=CharSet.Auto)] |
| 173 | + internal extern static void DisableProcessWindowsGhosting(); |
| 174 | + } |
| 175 | +} |
0 commit comments