Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
wrap static gwt event methods to improve testability
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmason committed Jul 11, 2012
1 parent a51ccda commit 699c3f9
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 21 deletions.
Expand Up @@ -45,6 +45,8 @@
import org.zanata.webtrans.client.history.Window;
import org.zanata.webtrans.client.history.WindowImpl;
import org.zanata.webtrans.client.history.WindowLocationImpl;
import org.zanata.webtrans.client.keys.EventWrapper;
import org.zanata.webtrans.client.keys.EventWrapperImpl;
import org.zanata.webtrans.client.presenter.AppPresenter;
import org.zanata.webtrans.client.presenter.DocumentListPresenter;
import org.zanata.webtrans.client.presenter.GlossaryDetailsPresenter;
Expand Down Expand Up @@ -131,6 +133,7 @@ protected void configure()
bind(ValidationMessagePanelDisplay.class).to(ValidationMessagePanelView.class).in(Singleton.class);
bindPresenter(TransMemoryMergePresenter.class, TransMemoryMergePopupPanelDisplay.class, TransMemoryMergePopupPanelView.class);

bind(EventWrapper.class).to(EventWrapperImpl.class).in(Singleton.class);
bind(HasPageNavigation.class).to(TableEditorView.class).in(Singleton.class);
bind(NativeEvent.class).to(NativeEventImpl.class).in(Singleton.class);
bind(History.class).to(HistoryImpl.class).in(Singleton.class);
Expand Down
@@ -0,0 +1,57 @@
/*
* Copyright 2012, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.webtrans.client.keys;

import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;

/**
* Wrapper around {@link com.google.gwt.user.client.Event} and related classes,
* allowing mocking for testing.
*
* @author David Mason, <a
* href="mailto:damason@redhat.com">damason@redhat.com</a>
*/
public interface EventWrapper
{
HandlerRegistration addNativePreviewHandler(final NativePreviewHandler handler);

int keyDownEvent();

int keyUpEvent();

/**
* Wrapper for {@link NativePreviewEvent#getTypeInt()}, to present a
* non-final method to allow mocking.
*
* See: http://stackoverflow.com/questions/7210171/easymock-mocked-object-is-calling-actual-method
*
* @param evt
* @return
*/
int getTypeInt(NativePreviewEvent evt);

String getType(NativeEvent evt);

Keys createKeys(NativeEvent evt);
}
@@ -0,0 +1,68 @@
/*
* Copyright 2012, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.webtrans.client.keys;

import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;

public class EventWrapperImpl implements EventWrapper
{

@Override
public HandlerRegistration addNativePreviewHandler(NativePreviewHandler handler)
{
return com.google.gwt.user.client.Event.addNativePreviewHandler(handler);
}

public int keyDownEvent()
{
return com.google.gwt.user.client.Event.ONKEYDOWN;
}

@Override
public int keyUpEvent()
{
return com.google.gwt.user.client.Event.ONKEYUP;
}

@Override
public int getTypeInt(NativePreviewEvent evt)
{
return evt.getTypeInt();
}

@Override
public Keys createKeys(NativeEvent evt)
{
int modifiers = (evt.getAltKey() ? Keys.ALT_KEY : 0) | (evt.getShiftKey() ? Keys.SHIFT_KEY : 0)
| (evt.getCtrlKey() ? Keys.CTRL_KEY : 0) | (evt.getMetaKey() ? Keys.META_KEY : 0);
return new Keys(modifiers, evt.getKeyCode());
}

@Override
public String getType(NativeEvent evt)
{
return evt.getType();
}

}
Expand Up @@ -24,8 +24,6 @@
import java.util.HashSet;
import java.util.Set;

import com.google.gwt.dom.client.NativeEvent;

/**
* Represents a combination of modifier keys and a single key code for use with
* {@link KeyShortcut}.
Expand Down Expand Up @@ -64,13 +62,6 @@ public Keys(int modifiers, int keyCode)
this.keyCode = keyCode;
}

public Keys(NativeEvent evt)
{
modifiers = (evt.getAltKey() ? ALT_KEY : 0) | (evt.getShiftKey() ? SHIFT_KEY : 0)
| (evt.getCtrlKey() ? CTRL_KEY : 0) | (evt.getMetaKey() ? META_KEY : 0);
keyCode = evt.getKeyCode();
}

public int getModifiers()
{
return modifiers;
Expand Down
Expand Up @@ -32,6 +32,7 @@

import org.zanata.webtrans.client.events.KeyShortcutEvent;
import org.zanata.webtrans.client.events.KeyShortcutEventHandler;
import org.zanata.webtrans.client.keys.EventWrapper;
import org.zanata.webtrans.client.keys.Keys;
import org.zanata.webtrans.client.keys.KeyShortcut;
import org.zanata.webtrans.client.keys.SurplusKeyListener;
Expand All @@ -43,7 +44,6 @@
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;
import com.google.gwt.view.client.ListDataProvider;
Expand All @@ -66,7 +66,7 @@ public class KeyShortcutPresenter extends WidgetPresenter<KeyShortcutPresenter.D

public interface Display extends WidgetDisplay
{
void addContext(String contextName, ListDataProvider<KeyShortcut> shortcuts);
ListDataProvider<KeyShortcut> addContext(String contextName);

void showPanel();

Expand All @@ -88,28 +88,31 @@ public interface Display extends WidgetDisplay

private WebTransMessages messages;

private EventWrapper event;

@Inject
public KeyShortcutPresenter(Display display, EventBus eventBus, final WebTransMessages webTransMessages)
public KeyShortcutPresenter(Display display, EventBus eventBus, final WebTransMessages webTransMessages, final EventWrapper event)
{
super(display, eventBus);
this.messages = webTransMessages;
this.event = event;
}

@Override
protected void onBind()
{
ensureActiveContexts().add(ShortcutContext.Application);

Event.addNativePreviewHandler(new NativePreviewHandler()
event.addNativePreviewHandler(new NativePreviewHandler()
{

@Override
public void onPreviewNativeEvent(NativePreviewEvent event)
public void onPreviewNativeEvent(NativePreviewEvent nativeEvent)
{
NativeEvent evt = event.getNativeEvent();
NativeEvent evt = nativeEvent.getNativeEvent();

// TODO enable keypress events if any shortcuts require them
if ((event.getTypeInt() & (Event.ONKEYDOWN | Event.ONKEYUP)) != 0)
if ((event.getTypeInt(nativeEvent) & (event.keyDownEvent() | event.keyUpEvent())) != 0)
{
processKeyEvent(evt);
}
Expand Down Expand Up @@ -227,7 +230,7 @@ public HandlerRegistration register(SurplusKeyListener listener)
*/
private void processKeyEvent(NativeEvent evt)
{
Keys pressedKeys = new Keys(evt);
Keys pressedKeys = event.createKeys(evt);
Set<KeyShortcut> shortcuts = ensureShortcutMap().get(pressedKeys);
boolean shortcutFound = false;
// TODO replace modifiers + keycode in event with Keys
Expand All @@ -237,7 +240,7 @@ private void processKeyEvent(NativeEvent evt)
for (KeyShortcut shortcut : shortcuts)
{
boolean contextActive = ensureActiveContexts().contains(shortcut.getContext());
boolean matchingEventType = shortcut.getKeyEvent().nativeEventType.equals(evt.getType());
boolean matchingEventType = shortcut.getKeyEvent().nativeEventType.equals(event.getType(evt));
if (contextActive && matchingEventType)
{
shortcutFound = true;
Expand Down Expand Up @@ -360,7 +363,7 @@ public void showShortcuts()
display.clearPanel();
for (ShortcutContext context : ensureActiveContexts())
{
ListDataProvider<KeyShortcut> dataProvider = new ListDataProvider<KeyShortcut>();
ListDataProvider<KeyShortcut> dataProvider = display.addContext(getContextName(context));

for (Set<KeyShortcut> shortcutSet : ensureShortcutMap().values())
{
Expand All @@ -374,7 +377,6 @@ public void showShortcuts()
}
}
Collections.sort(dataProvider.getList());
display.addContext(getContextName(context), dataProvider);
}
display.showPanel();
}
Expand Down
@@ -1,3 +1,23 @@
/*
* Copyright 2012, Red Hat, Inc. and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.zanata.webtrans.client.view;

import java.util.HashMap;
Expand Down Expand Up @@ -113,7 +133,7 @@ public String getValue(KeyShortcut keyShortcut)
}
};

public void addContext(String contextName, ListDataProvider<KeyShortcut> dataProvider)
public ListDataProvider<KeyShortcut> addContext(String contextName)
{
Label categoryTitle = new Label(contextName);
categoryTitle.addStyleName(style.keyShortcutCategoryTitle());
Expand All @@ -125,10 +145,13 @@ public void addContext(String contextName, ListDataProvider<KeyShortcut> dataPro
table.addColumn(keysColumn);
table.addColumn(descColumn);

ListDataProvider<KeyShortcut> dataProvider = new ListDataProvider<KeyShortcut>();
dataProvider.addDataDisplay(table);

// TODO adjust how shortcuts are displayed in this table
shortcutContainer.add(table);

return dataProvider;
}

private String keysDisplayString(KeyShortcut shortcut)
Expand Down

0 comments on commit 699c3f9

Please sign in to comment.