public void NativeWindow_DefWndProc_InvokeWithoutHandle_Nop(int msg)
 {
     using (new NoAssertContext())
     {
         var window = new WndProcTrackingNativeWindow();
         var m      = new Message
         {
             Msg    = msg,
             Result = (IntPtr)1
         };
         window.DefWndProc(ref m);
         Assert.Equal(IntPtr.Zero, m.Result);
         Assert.Empty(window.Messages);
     }
 }
 public void NativeWindow_DefWndProc_InvokeWithInvalidHandle_Nop(int msg)
 {
     using (new NoAssertContext())
     {
         var window = new WndProcTrackingNativeWindow();
         window.AssignHandle((IntPtr)250);
         var m = new Message
         {
             Msg    = msg,
             Result = (IntPtr)1
         };
         window.DefWndProc(ref m);
         Assert.Equal(IntPtr.Zero, m.Result);
         Assert.Equal((IntPtr)250, window.Handle);
     }
 }
        public void NativeWindow_DefWindProc_InvokeAfterAssignHandle_Success()
        {
            using var control = new Control();
            var window1 = new WndProcTrackingNativeWindow
            {
                MessageTypePredicate = (msg) => msg == 123456
            };

            window1.AssignHandle(control.Handle);

            Message m = new Message
            {
                Msg = 123456
            };

            // As we don't have a "Previous" the default window procedure gets called
            window1.DefWndProc(ref m);
            Assert.Empty(window1.Messages);

            // This will set the existing window as Previous. When Previous NativeWindows
            // are registered for the same handle, a chain of calls will happen until the original
            // registered NativeWindow for a given handle is reached (i.e. no Previous). The
            // call chain is like this:
            //
            //   DefWndProc() -> PreviousWindow.CallBack() -> WndProc() -> DefWndProc()
            var window2 = new WndProcTrackingNativeWindow
            {
                MessageTypePredicate = (msg) => msg == 123456
            };

            window2.AssignHandle(window1.Handle);
            window2.DefWndProc(ref m);
            Assert.Single(window1.Messages);
            Assert.Empty(window2.Messages);

            // Check that the message continues to work back.
            var window3 = new WndProcTrackingNativeWindow
            {
                MessageTypePredicate = (msg) => msg == 123456
            };

            window3.AssignHandle(window1.Handle);
            window3.DefWndProc(ref m);
            Assert.Equal(2, window1.Messages.Count);
            Assert.Single(window2.Messages);
            Assert.Empty(window3.Messages);
        }
        public void NativeWindow_DefWndProc_InvokeWithCreatedHandle_Nop(int msg)
        {
            var window = new WndProcTrackingNativeWindow();

            window.CreateHandle(new CreateParams());
            try
            {
                var m = new Message
                {
                    Msg    = msg,
                    Result = (IntPtr)1
                };
                window.DefWndProc(ref m);
                Assert.Equal(IntPtr.Zero, m.Result);
                Assert.NotEqual(IntPtr.Zero, window.Handle);
            }
            finally
            {
                window.DestroyHandle();
            }
        }
        public void NativeWindow_DefWndProc_InvokeWithValidAssignedHandle_Nop(int msg)
        {
            using var control = new Control();
            var window = new WndProcTrackingNativeWindow();

            window.AssignHandle(control.Handle);

            try
            {
                var m = new Message
                {
                    Msg    = msg,
                    Result = (IntPtr)1
                };
                window.DefWndProc(ref m);
                Assert.Equal(IntPtr.Zero, m.Result);
                Assert.Equal(control.Handle, window.Handle);
            }
            finally
            {
                window.ReleaseHandle();
            }
        }