/
SpinnerPresser.java
72 lines (54 loc) · 1.91 KB
/
SpinnerPresser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.leandog.brazenhead;
import android.app.Instrumentation;
import android.view.KeyEvent;
import android.widget.Spinner;
import com.robotium.solo.BrazenheadSleeper;
import com.robotium.solo.Solo;
public class SpinnerPresser {
private final BrazenheadSleeper sleeper;
private final Instrumentation instrumentation;
public SpinnerPresser(final Instrumentation instrumentation) {
this.instrumentation = instrumentation;
sleeper = new BrazenheadSleeper();
}
public SpinnerPresser(final Instrumentation instrumentation, final BrazenheadSleeper sleeper) {
this.instrumentation = instrumentation;
this.sleeper = sleeper;
}
public void pressSpinnerItemById(int spinnerId, int itemIndex) {
pressSpinnerItem((Spinner) solo().getView(spinnerId), itemIndex);
}
public void pressSpinnerItem(final Spinner spinner, int itemIndex) {
final int direction = whichDirection(itemIndex);
final int numberOfMoves = Math.abs(itemIndex);
setTheInitialFocus(spinner);
for (int moves = 0; moves < numberOfMoves; moves++) {
sleeper.sleepMini();
move(direction);
}
selectTheItem();
}
private void selectTheItem() {
sendKey(KeyEvent.KEYCODE_ENTER);
}
private int whichDirection(int itemIndex) {
return itemIndex < 0 ? KeyEvent.KEYCODE_DPAD_UP : KeyEvent.KEYCODE_DPAD_DOWN;
}
private void move(int direction) {
sendKey(direction);
}
private void setTheInitialFocus(final Spinner spinner) {
solo().clickOnView(spinner);
sleeper.sleep();
sendKey(KeyEvent.KEYCODE_DPAD_DOWN);
}
private void sendKey(int key) {
try {
instrumentation.sendKeyDownUpSync(key);
} catch (SecurityException ignored) {
}
}
private Solo solo() {
return TestRunInformation.getSolo();
}
}