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

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Eng committed Apr 19, 2013
1 parent a7638ae commit f896cea
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 49 deletions.
Expand Up @@ -79,6 +79,7 @@
import org.zanata.webtrans.client.view.GlossaryDetailsView;
import org.zanata.webtrans.client.view.GlossaryDisplay;
import org.zanata.webtrans.client.view.GlossaryView;
import org.zanata.webtrans.client.view.KeyShortcutDisplay;
import org.zanata.webtrans.client.view.KeyShortcutView;
import org.zanata.webtrans.client.view.NotificationDisplay;
import org.zanata.webtrans.client.view.NotificationView;
Expand Down Expand Up @@ -138,7 +139,7 @@ protected void configure()
bind(ValidationService.class).in(Singleton.class);

bindPresenter(AppPresenter.class, AppDisplay.class, AppView.class);
bindPresenter(KeyShortcutPresenter.class, KeyShortcutPresenter.Display.class, KeyShortcutView.class);
bindPresenter(KeyShortcutPresenter.class, KeyShortcutDisplay.class, KeyShortcutView.class);
bindPresenter(DocumentListPresenter.class, DocumentListDisplay.class, DocumentListView.class);
bindPresenter(SearchResultsPresenter.class, SearchResultsPresenter.Display.class, SearchResultsView.class);
bindPresenter(TranslationPresenter.class, TranslationPresenter.Display.class, TranslationView.class);
Expand Down
Expand Up @@ -27,7 +27,6 @@
import java.util.Set;

import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.widget.WidgetDisplay;
import net.customware.gwt.presenter.client.widget.WidgetPresenter;

import org.zanata.webtrans.client.events.AliasKeyChangedEvent;
Expand All @@ -40,6 +39,7 @@
import org.zanata.webtrans.client.keys.ShortcutContext;
import org.zanata.webtrans.client.keys.SurplusKeyListener;
import org.zanata.webtrans.client.resources.WebTransMessages;
import org.zanata.webtrans.client.view.KeyShortcutDisplay;

import com.allen_sauer.gwt.log.client.Log;
import com.google.common.collect.Iterables;
Expand All @@ -48,7 +48,6 @@
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.view.client.ListDataProvider;
import com.google.inject.Inject;

Expand All @@ -64,22 +63,8 @@
* @author David Mason, <a
* href="mailto:damason@redhat.com">damason@redhat.com</a> *
*/
public class KeyShortcutPresenter extends WidgetPresenter<KeyShortcutPresenter.Display>
public class KeyShortcutPresenter extends WidgetPresenter<KeyShortcutDisplay> implements KeyShortcutDisplay.Listener
{

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

void showPanel();

public void clearPanel();

boolean isShowing();

void hide(boolean autoClosed);
}

/**
* Key uses {@link Keys#hashCode()}
*/
Expand All @@ -96,22 +81,15 @@ public interface Display extends WidgetDisplay
private boolean isAliasKeyListening = false;

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

private Timer aliasKeyTimer = new Timer()
{
public void run()
{
setAliasKeyListening(false);
}
};

private void setAliasKeyListening(boolean isAliasKeyListening)

@Override
public void setAliasKeyListening(boolean isAliasKeyListening)
{
if (this.isAliasKeyListening != isAliasKeyListening)
{
Expand All @@ -120,8 +98,7 @@ private void setAliasKeyListening(boolean isAliasKeyListening)
if (!isAliasKeyListening)
{
Log.debug("canceling alias key... ");

aliasKeyTimer.cancel();
display.cancelMetaKeyTimer();
}
else
{
Expand All @@ -133,6 +110,8 @@ private void setAliasKeyListening(boolean isAliasKeyListening)
@Override
protected void onBind()
{
display.setListener(this);

ensureActiveContexts().add(ShortcutContext.Application);

event.addNativePreviewHandler(new NativePreviewHandler()
Expand All @@ -152,13 +131,12 @@ public void onPreviewNativeEvent(NativePreviewEvent nativeEvent)
else
{
Keys pressedKeys = event.createKeys(evt);

boolean isAliasKeyTriggered = Keys.ALIAS_KEY == (pressedKeys.getModifiers() | pressedKeys.getKeyCode());
boolean isAliasKeyTriggered = Keys.ALIAS_KEY == (pressedKeys.getModifiers() + pressedKeys.getKeyCode());

if (isAliasKeyTriggered)
{
setAliasKeyListening(true);
aliasKeyTimer.schedule(5000); // 5 seconds
display.startAliasKeyListen(5000);
}
else
{
Expand Down Expand Up @@ -201,6 +179,7 @@ public void onKeyShortcut(KeyShortcutEvent event)
showShortcuts();
}
}).build();

register(availableKeysShortcut);
}

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.view;

import net.customware.gwt.presenter.client.widget.WidgetDisplay;

import org.zanata.webtrans.client.keys.KeyShortcut;

import com.google.gwt.view.client.ListDataProvider;

/**
*
* @author Alex Eng <a href="mailto:aeng@redhat.com">aeng@redhat.com</a>
*
*/
public interface KeyShortcutDisplay extends WidgetDisplay
{
ListDataProvider<KeyShortcut> addContext(String contextName);

void showPanel();

public void clearPanel();

boolean isShowing();

void hide(boolean autoClosed);

interface Listener
{
void setAliasKeyListening(boolean isAliasKeyListening);
}

void setListener(Listener listener);

void cancelMetaKeyTimer();

void startAliasKeyListen(int delayMillis);

}
Expand Up @@ -23,9 +23,8 @@
import java.util.HashMap;
import java.util.Map;

import org.zanata.webtrans.client.keys.Keys;
import org.zanata.webtrans.client.keys.KeyShortcut;
import org.zanata.webtrans.client.presenter.KeyShortcutPresenter;
import org.zanata.webtrans.client.keys.Keys;
import org.zanata.webtrans.client.resources.WebTransMessages;

import com.google.common.base.Strings;
Expand All @@ -40,6 +39,7 @@
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
Expand All @@ -49,7 +49,7 @@
import com.google.gwt.view.client.ListDataProvider;
import com.google.inject.Inject;

public class KeyShortcutView extends PopupPanel implements KeyShortcutPresenter.Display
public class KeyShortcutView extends PopupPanel implements KeyShortcutDisplay
{

private static KeyShortcutViewUiBinder uiBinder = GWT.create(KeyShortcutViewUiBinder.class);
Expand All @@ -73,9 +73,19 @@ interface Styles extends CssResource

@UiField
Styles style;

private Listener listener;

private final Map<Integer, String> keyDisplayMap;

private Timer aliasKeyTimer = new Timer()
{
public void run()
{
listener.setAliasKeyListening(false);
}
};

@Inject
public KeyShortcutView(final WebTransMessages webTransMessages)
{
Expand Down Expand Up @@ -237,4 +247,22 @@ public Widget asWidget()
{
return this;
}

@Override
public void setListener(Listener listener)
{
this.listener = listener;
}

@Override
public void cancelMetaKeyTimer()
{
aliasKeyTimer.cancel();
}

@Override
public void startAliasKeyListen(int delayMillis)
{
aliasKeyTimer.schedule(delayMillis); // 5 seconds
}
}
Expand Up @@ -26,6 +26,7 @@
import org.zanata.webtrans.client.keys.Keys;
import org.zanata.webtrans.client.keys.ShortcutContext;
import org.zanata.webtrans.client.resources.TableEditorMessages;
import org.zanata.webtrans.client.view.KeyShortcutDisplay;
import org.zanata.webtrans.shared.rpc.NavOption;

import com.google.gwt.event.dom.client.KeyCodes;
Expand All @@ -49,7 +50,7 @@ public class EditorKeyShortcutsTest
@Captor
private ArgumentCaptor<KeyShortcut> keyShortcutCaptor;
@Mock
private KeyShortcutPresenter.Display keyShortcutDisplay;
private KeyShortcutDisplay keyShortcutDisplay;

@BeforeMethod
public void setUp() throws Exception
Expand Down Expand Up @@ -198,7 +199,7 @@ public void testRegisterEditorActionKeys()
List<KeyShortcut> keys = keyShortcutCaptor.getAllValues();
assertKeys(keys.get(0), "save fuzzy", true, true, new Keys(Keys.CTRL_KEY, 'S'));
assertKeys(keys.get(1), "save approved", true, true, new Keys(Keys.CTRL_KEY, KeyCodes.KEY_ENTER));
assertKeys(keys.get(2), "copy from source", true, true, new Keys(Keys.ALT_KEY, 'G'));
assertKeys(keys.get(2), "copy from source", true, true, new Keys(Keys.ALT_KEY, 'G'), new Keys(Keys.ALIAS_KEY, Keys.NO_MODIFIER, 'G'));
}

@Test
Expand All @@ -218,7 +219,7 @@ public void registerEditorActionKeysAfterChangeUserConfig()
assertKeys(keys.get(0), "save fuzzy", true, true, new Keys(Keys.CTRL_KEY, 'S'));
assertKeys(keys.get(1), "save approved", true, true, new Keys(Keys.CTRL_KEY, KeyCodes.KEY_ENTER));
assertKeys(keys.get(2), "save approved", true, true, new Keys(Keys.NO_MODIFIER, KeyCodes.KEY_ENTER));
assertKeys(keys.get(3), "copy from source", true, true, new Keys(Keys.ALT_KEY, 'G'));
assertKeys(keys.get(3), "copy from source", true, true, new Keys(Keys.ALT_KEY, 'G'), new Keys(Keys.ALIAS_KEY, Keys.NO_MODIFIER, 'G'));
}

@Test
Expand Down
Expand Up @@ -20,10 +20,18 @@
*/
package org.zanata.webtrans.client.presenter;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import net.customware.gwt.presenter.client.EventBus;

import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
Expand All @@ -33,22 +41,16 @@
import org.zanata.webtrans.client.keys.EventWrapper;
import org.zanata.webtrans.client.keys.KeyShortcut;
import org.zanata.webtrans.client.keys.Keys;
import org.zanata.webtrans.client.presenter.KeyShortcutPresenter.Display;
import org.zanata.webtrans.client.resources.WebTransMessages;
import org.zanata.webtrans.client.view.KeyShortcutDisplay;

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.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;
import com.google.gwt.view.client.ListDataProvider;

import net.customware.gwt.presenter.client.EventBus;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


/**
* @author David Mason, <a href="mailto:damason@redhat.com">damason@redhat.com</a>
Expand All @@ -72,7 +74,7 @@ public class KeyShortcutPresenterTest


@Mock
private Display mockDisplay;
private KeyShortcutDisplay mockDisplay;
@Mock
private EventWrapper mockEventWrapper;
@Mock
Expand Down Expand Up @@ -104,6 +106,8 @@ public void beforeMethod()
public void testExpectedActionsOnBind()
{
keyShortcutPresenter.bind();

verify(mockDisplay).setListener(keyShortcutPresenter);

verify(mockEventWrapper).addNativePreviewHandler(capturedNativePreviewHandler.capture());
}
Expand Down

0 comments on commit f896cea

Please sign in to comment.