package org.realityforge.gwt.webpoller.client;

import com.google.gwt.event.shared.SimpleEventBus;
import com.google.web.bindery.event.shared.HandlerRegistration;
import org.mockito.Mockito;
import org.realityforge.gwt.webpoller.client.WebPoller.RequestFactory;
import org.realityforge.gwt.webpoller.client.event.StartEvent;
import org.realityforge.gwt.webpoller.client.event.StopEvent;
import org.realityforge.gwt.webpoller.client.event.ErrorEvent;
import org.realityforge.gwt.webpoller.client.event.MessageEvent;
import org.testng.annotations.Test;
import static org.mockito.Mockito.*;
import static org.testng.Assert.*;

public class WebPollerTest
{
  @Test
  public void registryTest()
  {
    final RequestFactory requestFactory = mock( RequestFactory.class );
    assertNull( WebPoller.newWebPoller( requestFactory, true ) );
    final TestWebPoller.Factory factory = new TestWebPoller.Factory();
    WebPoller.register( factory );
    assertNotNull( WebPoller.newWebPoller( requestFactory, true ) );
    assertTrue( WebPoller.deregister( factory ) );
    assertNull( WebPoller.newWebPoller( requestFactory, true ) );
    assertFalse( WebPoller.deregister( factory ) );
  }

  @Test
  public void handlerInteractions()
  {
    final TestWebPoller webSocket = new TestWebPoller( new SimpleEventBus(), mock( RequestFactory.class ), true );

    {
      final StartEvent.Handler handler = mock( StartEvent.Handler.class );
      final HandlerRegistration registration = webSocket.addStartHandler( handler );
      webSocket.onStart();
      verify( handler, only() ).onStartEvent( refEq( new StartEvent( webSocket ), "source" ) );
      registration.removeHandler();
      webSocket.onStart();
      verify( handler, atMost( 1 ) ).onStartEvent( any( StartEvent.class ) );
    }

    {
      final StopEvent.Handler handler = mock( StopEvent.Handler.class );
      final HandlerRegistration registration = webSocket.addStopHandler( handler );
      webSocket.onStop();
      final StopEvent expected = new StopEvent( webSocket );
      verify( handler, only() ).onStopEvent( refEq( expected, "source" ) );
      registration.removeHandler();
      webSocket.onStop();
      verify( handler, atMost( 1 ) ).onStopEvent( any( StopEvent.class ) );
    }

    {
      final MessageEvent.Handler handler = mock( MessageEvent.Handler.class );
      final HandlerRegistration registration = webSocket.addMessageHandler( handler );
      webSocket.onMessage( "Blah" );
      final MessageEvent expected = new MessageEvent( webSocket, "Blah" );
      verify( handler, only() ).onMessageEvent( refEq( expected, "source" ) );
      registration.removeHandler();
      webSocket.onMessage( "Blah" );
      verify( handler, atMost( 1 ) ).onMessageEvent( any( MessageEvent.class ) );
    }

    {
      final ErrorEvent.Handler handler = mock( ErrorEvent.Handler.class );
      final HandlerRegistration registration = webSocket.addErrorHandler( handler );
      webSocket.onError();
      verify( handler, only() ).onErrorEvent( any( ErrorEvent.class ) );
      registration.removeHandler();
      webSocket.onError();
      verify( handler, atMost( 1 ) ).onErrorEvent( Mockito.<ErrorEvent>anyObject() );
    }
  }
}

@@ -1249,22 +1249,22 @@
 ller web
-Socket
+Poller
  = new T
@@ -1465,30 +1465,30 @@
 ration = web
-Socket
+Poller
 .addStartHan
@@ -1505,38 +1505,38 @@
 ler );%0A      web
-Socket
+Poller
 .onStart();%0A    
@@ -1603,22 +1603,22 @@
 ent( web
-Socket
+Poller
  ), %22sou
@@ -1668,30 +1668,30 @@
 );%0A      web
-Socket
+Poller
 .onStart();%0A
@@ -1900,30 +1900,30 @@
 ration = web
-Socket
+Poller
 .addStopHand
@@ -1939,38 +1939,38 @@
 ler );%0A      web
-Socket
+Poller
 .onStop();%0A     
@@ -2015,22 +2015,22 @@
 ent( web
-Socket
+Poller
  );%0A    
@@ -2146,22 +2146,22 @@
      web
-Socket
+Poller
 .onStop(
@@ -2377,30 +2377,30 @@
 ration = web
-Socket
+Poller
 .addMessageH
@@ -2419,38 +2419,38 @@
 ler );%0A      web
-Socket
+Poller
 .onMessage( %22Bla
@@ -2512,22 +2512,22 @@
 ent( web
-Socket
+Poller
 , %22Blah%22
@@ -2654,22 +2654,22 @@
      web
-Socket
+Poller
 .onMessa
@@ -2902,22 +2902,22 @@
 on = web
-Socket
+Poller
 .addErro
@@ -2938,38 +2938,38 @@
 ler );%0A      web
-Socket
+Poller
 .onError();%0A    
@@ -3086,14 +3086,14 @@
  web
-Socket
+Poller
 .onE

Rename variable