* @author Emmanuel Puybaret
*/
public class PrintTest extends ComponentTestFixture {
public void testPageSetupAndPrintPreview() throws ComponentSearchException, InterruptedException,
NoSuchFieldException, IllegalAccessException, InvocationTargetException, IOException {
UserPreferences preferences = new DefaultUserPreferences();
ViewFactory viewFactory = new SwingViewFactory();
Home home = new Home();
ContentManager contentManager = new FileContentManager(preferences) {
@Override
public String showSaveDialog(View parentView, String dialogTitle, ContentType contentType, String name) {
String os = System.getProperty("os.name");
if (OperatingSystem.isMacOSX()) {
// Let's pretend the OS isn't Mac OS X to get a JFileChooser instance that works better in test
System.setProperty("os.name", "dummy");
}
try {
return super.showSaveDialog(parentView, dialogTitle, contentType, name);
} finally {
System.setProperty("os.name", os);
}
}
};
final HomeController controller =
new HomeController(home, preferences, viewFactory, contentManager);
// 1. Create a frame that displays a home view
JFrame frame = new JFrame("Home Print Test");
frame.add((JComponent)controller.getView());
frame.pack();
// Show home frame
showWindow(frame);
JComponentTester tester = new JComponentTester();
tester.waitForIdle();
// Add a piece of furniture to home
List<CatalogPieceOfFurniture> selectedPieces = Arrays.asList(
new CatalogPieceOfFurniture [] {preferences.getFurnitureCatalog().getCategories().get(0).getFurniture().get(0)});
controller.getFurnitureCatalogController().setSelectedFurniture(selectedPieces);
tester.invokeAndWait(new Runnable() {
public void run() {
runAction(controller, HomePane.ActionType.ADD_HOME_FURNITURE);
}
});
// Check home contains one piece
assertEquals("Home doesn't contain any furniture", 1, home.getFurniture().size());
// 2. Edit page setup dialog box
tester.invokeLater(new Runnable() {
public void run() {
// Display dialog box later in Event Dispatch Thread to avoid blocking test thread
runAction(controller, HomePane.ActionType.PAGE_SETUP);
}
});
// Wait for page setup to be shown
tester.waitForFrameShowing(new AWTHierarchy(), preferences.getLocalizedString(
PageSetupPanel.class, "pageSetup.title"));
// Check dialog box is displayed
JDialog pageSetupDialog = (JDialog)TestUtilities.findComponent(
frame, JDialog.class);
assertTrue("Page setup dialog not showing", pageSetupDialog.isShowing());
// Retrieve PageSetupPanel components
PageSetupPanel pageSetupPanel = (PageSetupPanel)TestUtilities.findComponent(
frame, PageSetupPanel.class);
JCheckBox furniturePrintedCheckBox =
(JCheckBox)TestUtilities.getField(pageSetupPanel, "furniturePrintedCheckBox");
JCheckBox planPrintedCheckBox =
(JCheckBox)TestUtilities.getField(pageSetupPanel, "planPrintedCheckBox");;
JCheckBox view3DPrintedCheckBox =
(JCheckBox)TestUtilities.getField(pageSetupPanel, "view3DPrintedCheckBox");
// Check default edited values
assertTrue("Furniture printed not checked", furniturePrintedCheckBox.isSelected());
assertTrue("Plan printed not checked", planPrintedCheckBox.isSelected());
assertTrue("View 3D printed not checked", view3DPrintedCheckBox.isSelected());
// 3. Change dialog box values
planPrintedCheckBox.setSelected(false);
// Click on Ok in dialog box
final JOptionPane pageSetupOptionPane = (JOptionPane)TestUtilities.findComponent(
pageSetupDialog, JOptionPane.class);
tester.invokeAndWait(new Runnable() {
public void run() {
// Select Ok option to hide dialog box in Event Dispatch Thread
pageSetupOptionPane.setValue(JOptionPane.OK_OPTION);
}
});
assertFalse("Page setup dialog still showing", pageSetupDialog.isShowing());
PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage();
// Check home print attributes are modified accordingly
assertHomePrintEqualPrintAttributes(pageFormat, true, false, true, home);
// 4. Undo changes
runAction(controller, HomePane.ActionType.UNDO);
// Check home attributes have previous values
assertEquals("Home print set", null, home.getPrint());
// Redo
runAction(controller, HomePane.ActionType.REDO);
// Check home attributes are modified accordingly
assertHomePrintEqualPrintAttributes(pageFormat, true, false, true, home);
// 5. Show print preview dialog box
tester.invokeLater(new Runnable() {
public void run() {
// Display dialog box later in Event Dispatch Thread to avoid blocking test thread
runAction(controller, HomePane.ActionType.PRINT_PREVIEW);
}
});
// Wait for print preview to be shown
tester.waitForFrameShowing(new AWTHierarchy(), preferences.getLocalizedString(
PrintPreviewPanel.class, "printPreview.title"));
// Check dialog box is displayed
JDialog printPreviewDialog = (JDialog)new BasicFinder().find(frame,
new ClassMatcher (JDialog.class, true));
assertTrue("Print preview dialog not showing", printPreviewDialog.isShowing());
// Retrieve PageSetupPanel components
PrintPreviewPanel printPreviewPanel = (PrintPreviewPanel)TestUtilities.findComponent(
frame, PrintPreviewPanel.class);
JToolBar toolBar =
(JToolBar)TestUtilities.getField(printPreviewPanel, "toolBar");
JButton previousButton = (JButton)toolBar.getComponent(0);
final JButton nextButton = (JButton)toolBar.getComponent(1);
HomePrintableComponent printableComponent =
(HomePrintableComponent)TestUtilities.getField(printPreviewPanel, "printableComponent");;
// Check if buttons are enabled and if printable component displays the first page
assertFalse("Previous button is enabled", previousButton.isEnabled());
assertTrue("Next button is disabled", nextButton.isEnabled());
assertEquals("Printable component doesn't display first page", 0, printableComponent.getPage());
assertEquals("Wrong printable component page count", 2, printableComponent.getPageCount());
// 6. Click on next page button
tester.invokeAndWait(new Runnable() {
public void run() {
nextButton.doClick();
}
});
// Check if buttons are enabled and if printable component displays the second page
assertTrue("Previous button is enabled", previousButton.isEnabled());
assertFalse("Next button is disabled", nextButton.isEnabled());
assertEquals("Printable component doesn't display second page", 1, printableComponent.getPage());
// Click on Ok in dialog box
final JOptionPane printPreviewOptionPane = (JOptionPane)TestUtilities.findComponent(
printPreviewDialog, JOptionPane.class);
tester.invokeAndWait(new Runnable() {
public void run() {
// Select Ok option to hide dialog box in Event Dispatch Thread
printPreviewOptionPane.setValue(JOptionPane.OK_OPTION);
}
});
assertFalse("Print preview dialog still showing", printPreviewDialog.isShowing());
// 7. Check the created PDF file doesn't exist
String pdfFileBase = "testsdfghjk";
File pdfFile = new File(pdfFileBase + ".pdf");
assertFalse("PDF file already exists, delete it first", pdfFile.exists());
// Show print to PDF dialog box
tester.invokeLater(new Runnable() {
public void run() {
// Display dialog box later in Event Dispatch Thread to avoid blocking test thread
runAction(controller, HomePane.ActionType.PRINT_TO_PDF);
}
});
// Wait for print to PDF file chooser to be shown
tester.waitForFrameShowing(new AWTHierarchy(), preferences.getLocalizedString(
HomePane.class, "printToPDFDialog.title"));
// Check dialog box is displayed
final Dialog printToPdfDialog = (Dialog)new BasicFinder().find(frame,
new ClassMatcher (Dialog.class, true));
assertTrue("Print to pdf dialog not showing", printToPdfDialog.isShowing());