Add tests for `VirtualKeyboard`

This commit is contained in:
Tim Abraldes 2014-11-14 17:34:25 -08:00
Родитель 9d6491a134
Коммит 724e19f6d1
4 изменённых файлов: 150 добавлений и 3 удалений

Просмотреть файл

@ -4,13 +4,26 @@ import com.nokia.mid.ui.CustomKeyboardControl;
import com.nokia.mid.ui.KeyboardVisibilityListener;
class VKVisibilityNotificationRunnable implements Runnable {
private Object listenerLock = new Object();
public void run() {
while(true) {
boolean isShow = sleepUntilVKVisibilityChange();
synchronized(this) {
synchronized(listenerLock) {
if (null == listener) {
continue;
}
// NB: A malicious or poorly-written listener can block
// in one of `showNotify` or `hideNotify`, causing
// us to stop sending visibility notifications and
// causing the next call to `setListener` to also
// block. We could alleviate this by creating a
// new Thread on which we will make the call to
// `showNotify` or `hideNotify`, then waiting for
// the thread to notify us but timing out after
// some reasonable amount of time. This is a lot
// of thread creation and will only help poorly-
// written programs so is probably overkill.
if (isShow) {
listener.showNotify(VirtualKeyboard.SYSTEM_KEYBOARD);
} else {
@ -20,8 +33,10 @@ class VKVisibilityNotificationRunnable implements Runnable {
}
}
public synchronized void setListener(KeyboardVisibilityListener listener) {
this.listener = listener;
public void setListener(KeyboardVisibilityListener listener) {
synchronized(listenerLock) {
this.listener = listener;
}
}
private KeyboardVisibilityListener listener;

Просмотреть файл

@ -0,0 +1,116 @@
package com.nokia.mid.ui;
import gnu.testlet.TestHarness;
import gnu.testlet.Testlet;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import com.nokia.mid.ui.VirtualKeyboard;
class TestKeyboardVisibilityListener implements com.nokia.mid.ui.KeyboardVisibilityListener {
boolean isExpectingShow = false;
boolean isExpectingHide = false;
TestHarness th;
TestKeyboardVisibilityListener(TestHarness harness) {
th = harness;
}
// Making this method `synchronized` caused an
// `IllegalMonitorStateException` to be thrown. Using a
// `synchronized(this)` statement around the method body
// as a workaround.
public void showNotify(int keyboardCategory) {
synchronized(this) {
th.check(isExpectingShow, true);
isExpectingShow = false;
TestVirtualKeyboard.verifyKeyboardShown(th);
notify();
}
}
// Making this method `synchronized` caused an
// `IllegalMonitorStateException` to be thrown. Using a
// `synchronized(this)` statement around the method body
// as a workaround.
public void hideNotify(int keyboardCategory) {
synchronized(this) {
th.check(isExpectingHide, true);
isExpectingHide = false;
TestVirtualKeyboard.verifyKeyboardHidden(th);
notify();
}
}
}
public class TestVirtualKeyboard extends Canvas implements Testlet {
public native static void hideKeyboard();
public native static void showKeyboard();
public static void verifyKeyboardHidden(TestHarness th) {
th.check(VirtualKeyboard.isVisible(), false);
// We may consider checking these values but it's not clear from
// documentation what we should expect them to be
// th.check(VirtualKeyboard.getXPosition(), 0);
// th.check(VirtualKeyboard.getYPosition(), /* Height of window */);
// th.check(VirtualKeyboard.getWidth(), 0);
// th.check(VirtualKeyboard.getHeight(), 0);
}
public static void verifyKeyboardShown(TestHarness th) {
th.check(VirtualKeyboard.isVisible(), true);
th.check(VirtualKeyboard.getXPosition(), 0);
// th.check(VirtualKeyboard.getYPosition() + VirtualKeyboard.getHeight(), /* Window height */);
// th.check(VirtualKeyboard.getWidth(), /* Width of window */);
// It would be nice to verify these values, but it's not clear
// what they should be (probably varies across devices)
// th.check(VirtualKeyboard.getYPosition(), /* unknown */);
// th.check(VirtualKeyboard.getHeight(), /* unknown */);
}
public void test(TestHarness th) {
TestKeyboardVisibilityListener listener = new TestKeyboardVisibilityListener(th);
th.check(null == listener, false);
VirtualKeyboard.setVisibilityListener(listener);
verifyKeyboardHidden(th);
// These are separate `synchronized` sections in case there are
// notifications pending for the listener. We want them to be
// processed between calls to `wait`.
// Test making the keyboard visible
synchronized(listener) {
listener.isExpectingShow = true;
showKeyboard();
while(true) {
try {
listener.wait();
break;
} catch (InterruptedException e) {
continue;
}
}
}
// Test hiding the keyboard
synchronized(listener) {
listener.isExpectingHide = true;
hideKeyboard();
while (true) {
try {
listener.wait();
break;
} catch (InterruptedException e) {
continue;
}
}
}
VirtualKeyboard.setVisibilityListener(null);
}
protected void paint(Graphics graphics) {}
}

Просмотреть файл

@ -1,3 +1,11 @@
DumbPipe.registerOpener("echo", function(message, sender) {
sender(message);
});
DumbPipe.registerOpener("showKeyboard", function(message, sender) {
document.getElementById("mozbrowser").style.height = "50%";
});
DumbPipe.registerOpener("hideKeyboard", function(message, sender) {
document.getElementById("mozbrowser").style.height = "100%";
});

Просмотреть файл

@ -67,3 +67,11 @@ Native.create("gnu/testlet/vm/NativeTest.dumbPipe.()Z", function() {
});
});
}, true);
Native.create("com/nokia/mid/ui/TestVirtualKeyboard.hideKeyboard.()V", function() {
DumbPipe.open("hideKeyboard", null, function(message) {});
});
Native.create("com/nokia/mid/ui/TestVirtualKeyboard.showKeyboard.()V", function() {
DumbPipe.open("showKeyboard", null, function(message) {});
});