|
| 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.Windows.Threading; |
| 8 | + |
| 9 | +using System.Text; |
| 10 | + |
| 11 | + |
| 12 | +using System.ComponentModel; |
| 13 | +using System.Windows; |
| 14 | +using System.Windows.Controls; |
| 15 | +using System.Windows.Interop; |
| 16 | +using System.Windows.Media; |
| 17 | + |
| 18 | + |
| 19 | +namespace DRT |
| 20 | +{ |
| 21 | + public class WindowHeightWidthTopLeftApp : Application |
| 22 | + { |
| 23 | + [STAThread] |
| 24 | + public static int Main() |
| 25 | + { |
| 26 | + WindowHeightWidthTopLeftApp theApp = new WindowHeightWidthTopLeftApp(); |
| 27 | + try |
| 28 | + { |
| 29 | + theApp.Run(); |
| 30 | + } |
| 31 | + catch (ApplicationException e) |
| 32 | + { |
| 33 | + theApp._logger.Log(e); |
| 34 | + return 1; |
| 35 | + } |
| 36 | + |
| 37 | + theApp._logger.Log( "Passed" ); |
| 38 | + return 0; |
| 39 | + } |
| 40 | + |
| 41 | + private Logger _logger = new Logger("DrtWindowHeightWidthTopLeft", "Microsoft", "Testing Height/Width/Top/Left and RestoreBounds APIs on Window and the expected values at different stages"); |
| 42 | + |
| 43 | + private Window _win; |
| 44 | + private Size _size = new Size(400,400); |
| 45 | + private Point _location = new Point(400,400); |
| 46 | + |
| 47 | + // |
| 48 | + // Test steps: |
| 49 | + // 1) After creating the Window object, verify that querying Width/Height/Top/Left returns the default value |
| 50 | + // |
| 51 | + // 2) Set value and verify getting Width/Height/Top/Left returns correct value |
| 52 | + // |
| 53 | + // 3) Set WindowState to maxmized |
| 54 | + // |
| 55 | + // 4) Verify getting W/H/T/L |
| 56 | + // |
| 57 | + // 5) Show Window |
| 58 | + // |
| 59 | + // 5) Restore the Window to Normal |
| 60 | + // |
| 61 | + // 6) Verify getting Width/Height/Top/Left |
| 62 | + // |
| 63 | + // 7) Test RestoreBounds with WindowStyle.SingleBorderWindow and |
| 64 | + // WindowStyle.ToolWindow |
| 65 | + // |
| 66 | + // 8) Test setting Top/Left/Width/Height when window is maximized |
| 67 | + // or minimized. Make sure that this works for ToolWindow too. |
| 68 | + // |
| 69 | + protected override void OnStartup(StartupEventArgs e) |
| 70 | + { |
| 71 | + _win = new Window(); |
| 72 | + Button b = new Button(); |
| 73 | + b.Content = "Hello World"; |
| 74 | + _win.Content = b; |
| 75 | + |
| 76 | + _win.Title = "DrtWindowSizeLocation"; |
| 77 | + |
| 78 | + TestPropertiesBeforeShow(); |
| 79 | + _win.Show(); |
| 80 | + |
| 81 | + _win.WindowState = WindowState.Normal; |
| 82 | + |
| 83 | + Dispatcher.CurrentDispatcher.BeginInvoke( |
| 84 | + DispatcherPriority.Background, |
| 85 | + new DispatcherOperationCallback(TestPropertiesAfterShow), |
| 86 | + this); |
| 87 | + |
| 88 | + Dispatcher.CurrentDispatcher.BeginInvoke( |
| 89 | + DispatcherPriority.Background, |
| 90 | + new DispatcherOperationCallback(TestRestoreBounds), |
| 91 | + this); |
| 92 | + |
| 93 | + Dispatcher.CurrentDispatcher.BeginInvoke( |
| 94 | + DispatcherPriority.Background, |
| 95 | + new DispatcherOperationCallback(TestTLWH), |
| 96 | + this); |
| 97 | + |
| 98 | + Dispatcher.CurrentDispatcher.BeginInvoke( |
| 99 | + DispatcherPriority.Background, |
| 100 | + new DispatcherOperationCallback(ShutdownApp), |
| 101 | + this); |
| 102 | + base.OnStartup(e); |
| 103 | + } |
| 104 | + |
| 105 | + private void TestPropertiesBeforeShow() |
| 106 | + { |
| 107 | + double length; |
| 108 | + |
| 109 | + length = _win.Width; |
| 110 | + if (!Double.IsNaN(length)) |
| 111 | + { |
| 112 | + throw new ApplicationException("Querying for Width before setting it or showing window should have returned default value (NaN), it returned " + length); |
| 113 | + } |
| 114 | + |
| 115 | + length = _win.Height; |
| 116 | + if (!Double.IsNaN(length)) |
| 117 | + { |
| 118 | + throw new ApplicationException("Querying for Height before setting it or showing window should have returned default value (NaN), it returned " + length); |
| 119 | + } |
| 120 | + |
| 121 | + length = _win.Left; |
| 122 | + if (!Double.IsNaN(length)) |
| 123 | + { |
| 124 | + throw new ApplicationException("Querying for Left before setting it or showing window should have returned default value (NaN), it returned " + length); |
| 125 | + } |
| 126 | + |
| 127 | + length = _win.Top; |
| 128 | + if (!Double.IsNaN(length)) |
| 129 | + { |
| 130 | + throw new ApplicationException("Querying for Top before setting it or showing window should have returned default value (NaN), it returned " + length); |
| 131 | + } |
| 132 | + |
| 133 | + _win.Width = _size.Width; |
| 134 | + _win.Height = _size.Height; |
| 135 | + |
| 136 | + _win.Left = _location.X; |
| 137 | + _win.Top = _location.Y; |
| 138 | + |
| 139 | + VerifyPropertiesForCorrectValues(); |
| 140 | + |
| 141 | + _win.WindowState = WindowState.Minimized; |
| 142 | + |
| 143 | + VerifyPropertiesForCorrectValues(); |
| 144 | + } |
| 145 | + |
| 146 | + private object TestPropertiesAfterShow(object obj) |
| 147 | + { |
| 148 | + VerifyPropertiesForCorrectValues(); |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + private void VerifyPropertiesForCorrectValues() |
| 153 | + { |
| 154 | + if (_win.Visibility != Visibility.Visible) |
| 155 | + { |
| 156 | + if (_win.Width != _size.Width) |
| 157 | + { |
| 158 | + throw new ApplicationException(String.Format("Did not retreive expected value for Width before window was shown. Expected : {0} Actual {1}", _size.Width, _win.Width)); |
| 159 | + } |
| 160 | + if (_win.Height != _size.Height) |
| 161 | + { |
| 162 | + throw new ApplicationException(String.Format("Did not retreive expected value for Height before window was shown. Expected : {0} Actual {1}", _size.Height, _win.Height)); |
| 163 | + } |
| 164 | + } |
| 165 | + else if (_win.RenderSize != _size) |
| 166 | + { |
| 167 | + throw new ApplicationException(String.Format("Did not retreive expected value for RenderSize. Expected : {0} Actual : {1}", _size.ToString(), _win.RenderSize.ToString())); |
| 168 | + } |
| 169 | + |
| 170 | + if (_win.Left != _location.X) |
| 171 | + { |
| 172 | + throw new ApplicationException("Did not retreive correct value for Left"); |
| 173 | + } |
| 174 | + |
| 175 | + if (_win.Top != _location.Y) |
| 176 | + { |
| 177 | + throw new ApplicationException("Did not retreive correct value for Top"); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + private object TestRestoreBounds(object obj) |
| 182 | + { |
| 183 | + VerifyRestoreBounds(); |
| 184 | + _win.WindowStyle = WindowStyle.ToolWindow; |
| 185 | + VerifyRestoreBounds(); |
| 186 | + _win.WindowStyle = WindowStyle.SingleBorderWindow; |
| 187 | + return null; |
| 188 | + } |
| 189 | + |
| 190 | + private void VerifyRestoreBounds() |
| 191 | + { |
| 192 | + _win.WindowState = WindowState.Maximized; |
| 193 | + Rect rb = _win.RestoreBounds; |
| 194 | + _win.WindowState = WindowState.Normal; |
| 195 | + Rect rect = new Rect(_win.Left, _win.Top, _win.Width, _win.Height); |
| 196 | + |
| 197 | + if ((rb.X != rect.X) || |
| 198 | + (rb.Y != rect.Y) || |
| 199 | + (rb.Width != rect.Width) || |
| 200 | + (rb.Height != rect.Height)) |
| 201 | + { |
| 202 | + throw new ApplicationException(String.Format("RestoreBounds before restoring the window ({0}) did not match the actual normal window rect ({1})", rb, rect)); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + private object TestTLWH(object obj) |
| 207 | + { |
| 208 | + _win.WindowState = WindowState.Maximized; |
| 209 | + VerifyTLWH(); |
| 210 | + _win.WindowState = WindowState.Minimized; |
| 211 | + VerifyTLWH(); |
| 212 | + _win.WindowStyle = WindowStyle.ToolWindow; |
| 213 | + _win.WindowState = WindowState.Maximized; |
| 214 | + VerifyTLWH(); |
| 215 | + _win.WindowState = WindowState.Minimized; |
| 216 | + VerifyTLWH(); |
| 217 | + return null; |
| 218 | + } |
| 219 | + |
| 220 | + private void VerifyTLWH() |
| 221 | + { |
| 222 | + WindowState state = _win.WindowState; |
| 223 | + _win.Top = 300; |
| 224 | + _win.Left = 300; |
| 225 | + _win.Width = 300; |
| 226 | + _win.Height = 300; |
| 227 | + _win.WindowState = WindowState.Normal; |
| 228 | + |
| 229 | + if ((_win.Top != 300) || |
| 230 | + (_win.Left != 300) || |
| 231 | + (_win.Width != 300) || |
| 232 | + (_win.Height != 300)) |
| 233 | + { |
| 234 | + throw new ApplicationException(String.Format("Window restored to the incorrect rect when T/L/W/H was specified with window {0}. Expected T={1}, L={1}, W={1}, H={1}. Actual T={2}, L={3}, W={4}, H={5}.", state, 300, _win.Top, _win.Left, _win.Width, _win.Height)); |
| 235 | + } |
| 236 | + |
| 237 | + _win.Left = _location.X; |
| 238 | + _win.Top = _location.Y; |
| 239 | + _win.Width = _size.Width; |
| 240 | + _win.Height = _size.Height; |
| 241 | + } |
| 242 | + |
| 243 | + private object ShutdownApp(object obj) |
| 244 | + { |
| 245 | + this.Shutdown(); |
| 246 | + return null; |
| 247 | + } |
| 248 | + } |
| 249 | +} |
| 250 | + |
0 commit comments