My favorites | Sign in
Project Logo
                
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Created on Dec 22, 2008
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* Copyright @2008 the original author or authors.
*/
package org.fest.javafx;

import static java.awt.event.InputEvent.BUTTON1_MASK;
import static org.fest.assertions.Assertions.assertThat;
import static org.fest.javafx.desktop.launcher.JavaFxClassLauncher.launch;
import static org.fest.javafx.finder.ButtonFinder.nodeWithButton;
import static org.fest.javafx.finder.TextFinder.nodeWithTextBox;

import java.awt.Point;
import java.awt.geom.Rectangle2D;

import javax.swing.JFrame;

import org.fest.swing.core.BasicRobot;
import org.fest.swing.core.Robot;
import org.fest.swing.edt.FailOnThreadViolationRepaintManager;
import org.fest.swing.timing.Pause;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.sun.javafx.scene.JSGPanelImpl;
import com.sun.scenario.scenegraph.SGNode;
import com.sun.scenario.scenegraph.SGParent;
import com.sun.scenario.scenegraph.SGText;
import com.sun.scenario.scenegraph.fx.FXNode;

/**
* Understands testing button clicks in a JavaFX UI.
*
* @author Alex Ruiz
*/
@Test
public class ClickButtonTest {

private Robot robot;
private java.awt.Robot realRobot;
private JFrame frame;

@BeforeClass public void setUpOnce() {
FailOnThreadViolationRepaintManager.install();
}

@BeforeMethod public void setUp() throws Exception {
robot = BasicRobot.robotWithNewAwtHierarchy();
realRobot = new java.awt.Robot();
frame=launch(Calculator.class);
}

@AfterMethod public void tearDown() {
robot.cleanUp();
}

public void shouldUpdateTextBoxWithPressedNumber() {
JSGPanelImpl panel = (JSGPanelImpl)frame.getLayeredPane().getComponent(0);
SGNode scene = panel.getScene();
SGParent parent = scene.getParent();
FXNode nodeWithButton8 = nodeWithButton("8", parent);
click(nodeWithButton8);
FXNode nodeWithTextBox = nodeWithTextBox(parent);
SGText textNode = (SGText)nodeWithTextBox.getLeaf();
assertThat(textNode.getText()).isEqualTo("8");
FXNode nodeWithButton6 = nodeWithButton("6", parent);
click(nodeWithButton6);
assertThat(textNode.getText()).isEqualTo("86");
Pause.pause(3000);
}

private void click(FXNode node) {
moveMouseTo(node);
realRobot.mousePress(BUTTON1_MASK);
realRobot.mouseRelease(BUTTON1_MASK);
robot.waitForIdle();
}

private void moveMouseTo(FXNode fxNode) {
Rectangle2D boundsInScene = fxNode.getBoundsInScene();
int centerX = (int)boundsInScene.getCenterX();
int centerY = (int)boundsInScene.getCenterY();
Point p = fxNode.getPanel().getLocationOnScreen();
p.translate(centerX, centerY);
realRobot.mouseMove(p.x, p.y);
}
}
Show details Hide details

Change log

r2162 by mich...@huettermann.net on Jan 06, 2009   Diff
adress via fx class files, finders,
refactoring
Go to: 
Project members, sign in to write a code review

Older revisions

r2126 by Alex.Ruiz.05 on Dec 31, 2008   Diff
Incomplete - task 275: [fest-javafx]
Find a way to start a JavaFX UI from a
Java test case
http://code.google.com/p/fest/issues/d
etail?id=275
...
r2115 by mich...@huettermann.net on Dec 29, 2008   Diff
[No log message]
r2110 by Alex.Ruiz.05 on Dec 29, 2008   Diff
First working, but hacky, experiment
with JavaFX testing.
All revisions of this file

File info

Size: 3426 bytes, 100 lines
Hosted by Google Code