public class HomeCameraTest extends ComponentTestFixture {
public void testHomeCamera() throws ComponentSearchException, InterruptedException,
NoSuchFieldException, IllegalAccessException, InvocationTargetException {
Locale.setDefault(Locale.FRANCE);
UserPreferences preferences = new DefaultUserPreferences();
Home home = new Home();
home.getCompass().setVisible(false);
final HomeController controller =
new HomeController(home, preferences, new SwingViewFactory());
JComponent homeView = (JComponent)controller.getView();
PlanComponent planComponent = (PlanComponent)TestUtilities.findComponent(
homeView, PlanComponent.class);
HomeComponent3D component3D = (HomeComponent3D)TestUtilities.findComponent(
homeView, HomeComponent3D.class);
// 1. Create a frame that displays a home view
JFrame frame = new JFrame("Home Camera Test");
frame.add(homeView);
frame.pack();
// Show home plan frame
showWindow(frame);
JComponentTester tester = new JComponentTester();
tester.waitForIdle();
// Transfer focus to plan view
planComponent.requestFocusInWindow();
tester.waitForIdle();
// Check plan view has focus
assertTrue("Plan component doesn't have the focus", planComponent.isFocusOwner());
// Check default camera is the top camera
assertSame("Default camera isn't top camera",
home.getTopCamera(), home.getCamera());
// Check default camera location and angles
assertCoordinatesAndAnglesEqualCameraLocationAndAngles(500, 1500, 1000,
(float)Math.PI, (float)Math.PI / 4, home.getCamera());
// 2. Create one wall between points (50, 50) and (150, 50) at a bigger scale
runAction(controller, HomePane.ActionType.CREATE_WALLS, tester);
runAction(controller, HomePane.ActionType.ZOOM_IN, tester);
tester.actionKeyPress(KeyEvent.VK_SHIFT);
tester.actionClick(planComponent, 50, 50);
tester.actionClick(planComponent, 150, 50, InputEvent.BUTTON1_MASK, 2);
tester.actionKeyRelease(KeyEvent.VK_SHIFT);
// Check wall length is 100 * plan scale
Wall wall = home.getWalls().iterator().next();
assertTrue("Incorrect wall length " + 100 / planComponent.getScale()
+ " " + (wall.getXEnd() - wall.getXStart()),
Math.abs(wall.getXEnd() - wall.getXStart() - 100 / planComponent.getScale()) < 1E-3);
float xWallMiddle = (wall.getXEnd() + wall.getXStart()) / 2;
float yWallMiddle = (wall.getYEnd() + wall.getYStart()) / 2;
// Check camera location and angles
assertCoordinatesAndAnglesEqualCameraLocationAndAngles(xWallMiddle, yWallMiddle + 1000, 1000,
(float)Math.PI, (float)Math.PI / 4, home.getCamera());
// 3. Transfer focus to 3D view with TAB key
tester.actionKeyStroke(KeyEvent.VK_TAB);
// Check 3D view has focus
assertTrue("3D component doesn't have the focus", component3D.isFocusOwner());
// Add 1� to camera pitch
tester.actionKeyStroke(KeyEvent.VK_PAGE_UP);
// Check camera location and angles
assertCoordinatesAndAnglesEqualCameraLocationAndAngles(xWallMiddle, 1000.1468f, 1025.8342f,
(float)Math.PI, (float)Math.PI / 4 + (float)Math.PI / 120, home.getCamera());
// 4. Remove 10� from camera yaw
tester.actionKeyStroke(KeyEvent.VK_LEFT);
// Check camera location and angles
assertCoordinatesAndAnglesEqualCameraLocationAndAngles(144.2812f, 998.8128f, 1025.8342f,
(float)Math.PI - (float)Math.PI / 60, (float)Math.PI / 4 + (float)Math.PI / 120, home.getCamera());
// Add 1� to camera yaw
tester.actionKeyPress(KeyEvent.VK_SHIFT);
tester.actionKeyStroke(KeyEvent.VK_RIGHT);
tester.actionKeyRelease(KeyEvent.VK_SHIFT);
// Check camera location and angles
assertCoordinatesAndAnglesEqualCameraLocationAndAngles(-109.0647f, 978.874f, 1025.8342f,
(float)Math.PI - (float)Math.PI / 60 + (float)Math.PI / 12, (float)Math.PI / 4 + (float)Math.PI / 120, home.getCamera());
// 5. Move camera 1cm forward
tester.actionKeyPress(KeyEvent.VK_SHIFT);
tester.actionKeyStroke(KeyEvent.VK_UP);
tester.actionKeyRelease(KeyEvent.VK_SHIFT);
// Check camera location and angles
assertCoordinatesAndAnglesEqualCameraLocationAndAngles(-85.8082f, 869.4608f, 907.961f,
(float)Math.PI - (float)Math.PI / 60 + (float)Math.PI / 12, (float)Math.PI / 4 + (float)Math.PI / 120, home.getCamera());
// Move camera 10 backward
tester.actionKeyStroke(KeyEvent.VK_DOWN);
// Check camera location and angles
assertCoordinatesAndAnglesEqualCameraLocationAndAngles(-90.4595f, 891.3434f, 931.5356f,
(float)Math.PI - (float)Math.PI / 60 + (float)Math.PI / 12, (float)Math.PI / 4 + (float)Math.PI / 120, home.getCamera());
// 6. View from observer
runAction(controller, HomePane.ActionType.VIEW_FROM_OBSERVER, tester);
tester.waitForIdle();
ObserverCamera observerCamera = home.getObserverCamera();
// Check camera is the observer camera
assertSame("Camera isn't observer camera", observerCamera, home.getCamera());
// Check default camera location and angles
assertCoordinatesAndAnglesEqualCameraLocationAndAngles(50, 50, 170,
7 * (float)Math.PI / 4, (float)Math.PI / 16, home.getCamera());
// Change camera location and angles
observerCamera.setX(100);
observerCamera.setY(100);
observerCamera.setYaw(3 * (float)Math.PI / 4);
assertCoordinatesAndAnglesEqualCameraLocationAndAngles(100, 100, 170,
3 * (float)Math.PI / 4, (float)Math.PI / 16, home.getCamera());
// Check observer camera is selected
assertEquals("Wrong selected items count", 1, home.getSelectedItems().size());
assertTrue("Camera isn't selected", home.getSelectedItems().contains(home.getCamera()));
// Try to select wall and observer camera
runAction(controller, HomePane.ActionType.SELECT, tester);
tester.actionClick(planComponent, 50, 50);
tester.actionKeyPress(KeyEvent.VK_SHIFT);
tester.actionClick(planComponent, (int)(140 * planComponent.getScale()),
(int)(140 * planComponent.getScale()));
tester.actionKeyRelease(KeyEvent.VK_SHIFT);
// Check selected items contains only wall
assertEquals("Wrong selected items count", 1, home.getSelectedItems().size());
assertTrue("Wall isn't selected", home.getSelectedItems().contains(wall));
// Select observer camera
Thread.sleep(1000); // Wait 1s to avoid double click
tester.actionClick(planComponent, (int)(140 * planComponent.getScale()),
(int)(140 * planComponent.getScale()));
// Check observer camera is selected
assertEquals("Wrong selected items count", 1, home.getSelectedItems().size());
assertTrue("Camera isn't selected", home.getSelectedItems().contains(home.getCamera()));
// 7. Move observer camera at right and down
tester.actionKeyStroke(KeyEvent.VK_RIGHT);
tester.actionKeyStroke(KeyEvent.VK_DOWN);
// Check camera location and angles
assertCoordinatesAndAnglesEqualCameraLocationAndAngles(100 + 1 / planComponent.getScale(),
100 + 1 / planComponent.getScale(), 170,
3 * (float)Math.PI / 4, (float)Math.PI / 16, home.getCamera());
// 8. Change observer camera yaw by moving its yaw indicator
float [][] cameraPoints = observerCamera.getPoints();
int xYawIndicator = (int)(((40 + (cameraPoints[0][0] + cameraPoints[3][0]) / 2)) * planComponent.getScale());
int yYawIndicator = (int)(((40 + (cameraPoints[0][1] + cameraPoints[3][1]) / 2)) * planComponent.getScale());
tester.actionMousePress(planComponent, new ComponentLocation(
new Point(xYawIndicator, yYawIndicator)));
tester.actionMouseMove(planComponent, new ComponentLocation(
new Point(xYawIndicator + 2, yYawIndicator + 2)));
tester.actionMouseRelease();
// Check camera yaw angle changed
assertCoordinatesAndAnglesEqualCameraLocationAndAngles(100 + 1 / planComponent.getScale(),
100 + 1 / planComponent.getScale(), 170,
2.5156f, (float)Math.PI / 16, home.getCamera());
// Change observer camera pitch by moving its pitch indicator
cameraPoints = observerCamera.getPoints();
int xPitchIndicator = (int)(((40 + (cameraPoints[1][0] + cameraPoints[2][0]) / 2)) * planComponent.getScale());
int yPitchIndicator = (int)(((40 + (cameraPoints[1][1] + cameraPoints[2][1]) / 2)) * planComponent.getScale());
tester.actionMousePress(planComponent, new ComponentLocation(
new Point(xPitchIndicator, yPitchIndicator)));
tester.actionMouseMove(planComponent, new ComponentLocation(
new Point(xPitchIndicator + 2, yPitchIndicator + 2)));
tester.actionMouseRelease();
// Check camera pitch angle changed
assertCoordinatesAndAnglesEqualCameraLocationAndAngles(100 + 1 / planComponent.getScale(),
100 + 1 / planComponent.getScale(), 170,
2.5156f, 0.1639f, home.getCamera());
// 9. Change observer camera location with mouse in 3D view
tester.actionMousePress(component3D, new ComponentLocation(new Point(10, 10)));
tester.actionKeyPress(KeyEvent.VK_ALT);
tester.actionMouseMove(component3D, new ComponentLocation(new Point(10, 20)));
tester.actionKeyRelease(KeyEvent.VK_ALT);
tester.actionMouseRelease();
// Check camera location changed
assertCoordinatesAndAnglesEqualCameraLocationAndAngles(108.657f, 111.4631f, 170,
2.5156f, 0.1639f, home.getCamera());
// 10. Change observer camera yaw with mouse in 3D view
tester.actionMousePress(component3D, new ComponentLocation(new Point(10, 20)));
tester.actionMouseMove(component3D, new ComponentLocation(new Point(20, 20)));
tester.actionMouseRelease();
// Check camera yaw changed
assertCoordinatesAndAnglesEqualCameraLocationAndAngles(108.657f, 111.4631f, 170,
2.5656f, 0.1639f, home.getCamera());
// Change camera pitch with mouse in 3D view
tester.actionMousePress(component3D, new ComponentLocation(new Point(20, 20)));
tester.actionMouseMove(component3D, new ComponentLocation(new Point(20, 30)));
tester.actionMouseRelease();
// Check camera yaw changed
assertCoordinatesAndAnglesEqualCameraLocationAndAngles(108.657f, 111.4631f, 170,
2.5656f, 0.2139f, home.getCamera());
// 11. Edit 3D view modal dialog box
JDialog attributesDialog = showHome3DAttributesPanel(preferences, controller, frame, tester);
// Retrieve Home3DAttributesPanel components
Home3DAttributesPanel panel = (Home3DAttributesPanel)TestUtilities.findComponent(
attributesDialog, Home3DAttributesPanel.class);
Home3DAttributesController panelController =
(Home3DAttributesController)TestUtilities.getField(panel, "controller");
JSpinner observerFieldOfViewSpinner =
(JSpinner)TestUtilities.getField(panel, "observerFieldOfViewSpinner");
JSpinner observerHeightSpinner =
(JSpinner)TestUtilities.getField(panel, "observerHeightSpinner");
ColorButton groundColorButton =
(ColorButton)TestUtilities.getField(panel, "groundColorButton");
ColorButton skyColorButton =
(ColorButton)TestUtilities.getField(panel, "skyColorButton");
JSlider brightnessSlider =
(JSlider)TestUtilities.getField(panel, "brightnessSlider");
JSlider wallsTransparencySlider =
(JSlider)TestUtilities.getField(panel, "wallsTransparencySlider");
// Check edited values
float oldCameraFieldOfView = observerCamera.getFieldOfView();
float oldCameraHeight = observerCamera.getHeight();
int oldGroundColor = home.getEnvironment().getGroundColor();
TextureImage oldGroundTexture = home.getEnvironment().getGroundTexture();
int oldSkyColor = home.getEnvironment().getSkyColor();
int oldLightColor = home.getEnvironment().getLightColor();
float oldWallsAlpha = home.getEnvironment().getWallsAlpha();
assertEquals("Wrong field of view", (int)Math.round(Math.toDegrees(oldCameraFieldOfView)),
observerFieldOfViewSpinner.getValue());
assertEquals("Wrong height", (float)Math.round(oldCameraHeight * 100) / 100,
observerHeightSpinner.getValue());
assertEquals("Wrong ground color", oldGroundColor,