Added special events for sleep monitoring
Removed old test

Change-Id: I709c0c1ba578191058197b206bf8e2cb983948e5
Signed-off-by: foremans <foremans@google.com>
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java
index 7e1054a..381ad37 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/widgets/Display.java
@@ -4195,6 +4195,18 @@
 	}
 }
 
+void sendSleepEvent() {
+  if (this.eventTable != null && this.eventTable.hooks(SWT.Sleep)) {
+    sendEvent(SWT.Sleep, null);
+  }
+}
+
+void sendWakeupEvent() {
+  if (this.eventTable != null && this.eventTable.hooks(SWT.Wakeup)) {
+    sendEvent(SWT.Wakeup, null);
+  }
+}
+
 static NSString getApplicationName() {
 	NSString name = null;
 	int pid = OS.getpid ();
@@ -4635,6 +4647,7 @@
 public boolean sleep () {
 	checkDevice ();
 	if (getMessageCount () != 0) return true;
+	sendSleepEvent();
 	try {
 		addPool();
 		allowTimers = runAsyncMessages = false;
@@ -4643,6 +4656,7 @@
 	} finally {
 		removePool();
 	}
+	sendWakeupEvent();
 	return true;
 }
 
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/SWT.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/SWT.java
index 38462df..5ce5f25 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/SWT.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/SWT.java
@@ -773,26 +773,48 @@
 	
 	/**
 	 * The PreEvent event type (value is 50).
-	 * 
+	 *
 	 * <p>
 	 * This event is sent before an event is dispatched.
 	 * </p>
-     * 
-     * @since 3.103
+	 *
+	 * @since 3.103
 	 */
 	public static final int PreEvent = 50;
-	
+
 	/**
 	 * The PostEvent event type (value is 51).
-	 * 
+	 *
 	 * <p>
 	 * This event is sent after an event is dispatched.
 	 * </p>
-	 *  
-     * @since 3.103
+	 *
+	 * @since 3.103
 	 */
 	public static final int PostEvent = 51;
 
+	/**
+	 * The Sleep event type (value is 52).
+	 *
+	 * <p>
+	 * This event is sent before Display.sleep goes into an interruptible sleep.
+	 * </p>
+	 *
+	 * @since 3.103
+	 */
+	public static final int Sleep = 52;
+
+	/**
+	 * The Wakeup event type (value is 53).
+	 *
+	 * <p>
+	 * This event is sent after Display.sleep wakes up from an interruptible sleep.
+	 * </p>
+	 *
+	 * @since 3.103
+	 */
+	public static final int Wakeup = 53;
+
 	/* Event Details */
 	
 	/**
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java
index d016ca9..33efc3a 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Display.java
@@ -4227,6 +4227,7 @@
 		return false;
 	}
 	if (getMessageCount () != 0) return true;
+	sendSleepEvent();
 	if (fds == 0) {
 		allocated_nfds = 2;
 		fds = OS.g_malloc (OS.GPollFD_sizeof () * allocated_nfds);
@@ -4271,6 +4272,7 @@
 		}
 	} while (!result && getMessageCount () == 0 && !wake);
 	wake = false;
+	sendWakeupEvent();
 	return true;
 }
 
@@ -4440,6 +4442,18 @@
 	}
 }
 
+void sendSleepEvent() {
+  if (this.eventTable != null && this.eventTable.hooks(SWT.Sleep)) {
+    sendEvent(SWT.Sleep, null);
+  }
+}
+
+void sendWakeupEvent() {
+  if (this.eventTable != null && this.eventTable.hooks(SWT.Wakeup)) {
+    sendEvent(SWT.Wakeup, null);
+  }
+}
+
 void setCurrentCaret (Caret caret) {
 	if (caretId != 0) OS.g_source_remove(caretId);
 	caretId = 0;
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java
index c14ca5f..9a6d405 100644
--- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java
+++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/Display.java
@@ -1380,6 +1380,7 @@
 long /*int*/ foregroundIdleProc (long /*int*/ code, long /*int*/ wParam, long /*int*/ lParam) {
 	if (code >= 0) {
 		if (runMessages && getMessageCount () != 0) {
+			sendWakeupEvent();
 			if (runMessagesInIdle) {
 				if (runMessagesInMessageProc) {
 					OS.PostMessage (hwndMessage, SWT_RUNASYNC, 0, 0);
@@ -1403,6 +1404,7 @@
 			MSG msg = new MSG();
 			int flags = OS.PM_NOREMOVE | OS.PM_NOYIELD | OS.PM_QS_INPUT;
 			if (!OS.PeekMessage (msg, 0, 0, 0, flags)) wakeThread ();
+			sendSleepEvent();
 		}
 	}
 	return OS.CallNextHookEx (idleHook, (int)/*64*/code, wParam, lParam);
@@ -4369,6 +4371,19 @@
 		}
 	}
 }
+
+void sendSleepEvent() {
+  if (this.eventTable != null && this.eventTable.hooks(SWT.Sleep)) {
+    sendEvent(SWT.Sleep, null);
+  }
+}
+
+void sendWakeupEvent() {
+  if (this.eventTable != null && this.eventTable.hooks(SWT.Wakeup)) {
+    sendEvent(SWT.Wakeup, null);
+  }
+}
+
 /**
  * Sets the location of the on-screen pointer relative to the top left corner
  * of the screen.  <b>Note: It is typically considered bad practice for a
@@ -4674,11 +4689,15 @@
 public boolean sleep () {
 	checkDevice ();
 	if (runMessages && getMessageCount () != 0) return true;
+	sendSleepEvent();
 	if (OS.IsWinCE) {
 		OS.MsgWaitForMultipleObjectsEx (0, 0, OS.INFINITE, OS.QS_ALLINPUT, OS.MWMO_INPUTAVAILABLE);
+		sendWakeupEvent();
 		return true;
 	}
-	return OS.WaitMessage ();
+	boolean result = OS.WaitMessage ();
+	sendWakeupEvent();
+	return result;
 }
 
 /**
diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Display.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Display.java
index b9c65d4..54f9d8c 100644
--- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Display.java
+++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Display.java
@@ -742,52 +742,11 @@
 	}
 }
 
-public void test_LongEventWatchdog() {
-	final int DURATION_MILLIS = 10;
-	Display display = new Display();
-	
-	try {
-		final boolean[] beginCalled = {false};
-		final boolean[] endCalled = {false};
-		final boolean[] eventHasRun = {false};
-
-		Listener preListener = new Listener() {
-			public void handleEvent(Event event) {
-				beginCalled[0] = true;
-			}
-		};
-		
-		Listener postListener = new Listener() {
-			public void handleEvent(Event event) {
-				endCalled[0] = true;
-			}
-		};
-		
-		display.addListener(SWT.PreEvent, preListener);
-		display.addListener(SWT.PostEvent, postListener);
-		
-		display.asyncExec(new Runnable() {
-			public void run() {
-				try {
-					Thread.sleep(DURATION_MILLIS);
-				} catch (InterruptedException e) {
-					fail();
-				}
-				eventHasRun[0] = true;
-			}
-		});
-
-		while (display.readAndDispatch()) {
-		}
-		
-		display.removeListener(SWT.PreEvent, preListener);
-		display.removeListener(SWT.PostEvent, preListener);
-		
-		
-		assertTrue(beginCalled[0] && endCalled[0] && eventHasRun[0]);
-	} finally {
-		display.dispose();
-	}
+public void test_readAndDispatch() {
+	// The following tests rely on readAndDispatch in order to succeed,
+	// thus no test is needed here.
+	//    test_getSyncThread()
+	//    test_postLorg_eclipse_swt_widgets_Event()
 }
 
 public void test_removeFilterILorg_eclipse_swt_widgets_Listener() {
@@ -1070,6 +1029,9 @@
 	}
 }
 
+public void test_wake() {
+	// tested in sleep() method
+}
 
 /* custom */
 boolean disposeExecRan;