1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 /*
  24  * @test
  25  * @bug 8162796
  26  * @summary  Verifies if LinearGradientPaint is printed in osx
  27  * @requires (os.family == "mac")
  28  * @run main/manual LinearGradientPrintingTest
  29  */
  30 import java.awt.BorderLayout;
  31 import java.awt.Color;
  32 import java.awt.Component;
  33 import java.awt.Container;
  34 import java.awt.Dimension;
  35 import java.awt.FlowLayout;
  36 import java.awt.Graphics;
  37 import java.awt.Graphics2D;
  38 import java.awt.LinearGradientPaint;
  39 import java.awt.Shape;
  40 import java.awt.event.ActionEvent;
  41 import java.awt.event.WindowAdapter;
  42 import java.awt.event.WindowEvent;
  43 import java.awt.geom.Point2D;
  44 import java.awt.geom.Rectangle2D;
  45 import java.awt.print.PageFormat;
  46 import java.awt.print.Printable;
  47 import java.awt.print.PrinterException;
  48 import java.awt.print.PrinterJob;
  49 import javax.swing.AbstractAction;
  50 import javax.swing.JButton;
  51 import javax.swing.JDialog;
  52 import javax.swing.JFrame;
  53 import javax.swing.JPanel;
  54 import javax.swing.JTextArea;
  55 import javax.swing.SwingUtilities;
  56 import javax.swing.WindowConstants;
  57 
  58 public class LinearGradientPrintingTest extends Component implements Printable {
  59     private static Thread mainThread;
  60     private static boolean testPassed;
  61     private static boolean testGeneratedInterrupt;
  62     private static JFrame f = null;
  63     
  64     public static void main(String[] args) {
  65         SwingUtilities.invokeLater(new Runnable() {
  66             @Override
  67             public void run() {
  68                 //createUI();
  69                 doTest(LinearGradientPrintingTest::createUI);
  70             }
  71         });
  72         mainThread = Thread.currentThread();
  73         try {
  74             Thread.sleep(60000);
  75         } catch (InterruptedException e) {
  76             if (!testPassed && testGeneratedInterrupt) {
  77                 throw new RuntimeException("LinearGradientPaint did not print");
  78             }
  79         }
  80         if (!testGeneratedInterrupt) {
  81             throw new RuntimeException("user has not executed the test");
  82         }
  83     }
  84 
  85     public static void createUI() {
  86         f = new JFrame("LinearGradient Printing Test");
  87         f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  88         final LinearGradientPrintingTest gpt = new LinearGradientPrintingTest();
  89         Container c = f.getContentPane();
  90         c.add(BorderLayout.CENTER, gpt);
  91         
  92         final JButton print = new JButton("Print");
  93         print.addActionListener(new AbstractAction() {
  94             @Override
  95             public void actionPerformed(ActionEvent e) {
  96                 PrinterJob job = PrinterJob.getPrinterJob();
  97                 job.setPrintable(gpt);
  98                 final boolean doPrint = job.printDialog();
  99                 if (doPrint) {
 100                     try {
 101                         job.print();
 102                     } catch (PrinterException ex) {
 103                         throw new RuntimeException(ex);
 104                     }
 105                 }
 106             }
 107         });
 108         c.add(print, BorderLayout.SOUTH);
 109         
 110         f.pack();
 111         f.setVisible(true);
 112     }
 113 
 114     public Dimension getPreferredSize() {
 115         return new Dimension(500,500);
 116     }
 117 
 118     public void paint(Graphics g) {
 119         doPaint((Graphics2D)g);
 120     }
 121 
 122     public int print( Graphics graphics, PageFormat format, int index ) {
 123         Graphics2D g2d = (Graphics2D)graphics;
 124         g2d.translate(format.getImageableX(), format.getImageableY());
 125         doPaint(g2d);
 126         return index == 0 ? PAGE_EXISTS : NO_SUCH_PAGE;
 127     }
 128 
 129     static final float DIM = 100;
 130     public void doPaint(Graphics2D g2d) {
 131 
 132         g2d.translate(DIM*0.2, DIM*0.2);
 133         Shape s = new Rectangle2D.Float(0, 0, DIM, DIM);
 134         Point2D.Double p1 = new Point2D.Double(0.0, 0.0);
 135         Point2D.Double p2 = new Point2D.Double(DIM, DIM);
 136         
 137         // LinearGradientPaint
 138         g2d.translate(DIM*1.2, 0);
 139         Color colors[] =  { Color.blue, Color.white} ;
 140         float fractions[]  = { 0.0f, 1.0f };
 141 
 142        LinearGradientPaint lgp =
 143            new LinearGradientPaint(p1, p2, fractions, colors,
 144               LinearGradientPaint.CycleMethod.NO_CYCLE);
 145         g2d.setPaint(lgp);
 146         g2d.fill(s);    
 147                      
 148     }
 149     
 150     public static synchronized void pass() {
 151         testPassed = true;
 152         testGeneratedInterrupt = true;
 153         mainThread.interrupt();
 154     }
 155 
 156     public static synchronized void fail() {
 157         testPassed = false;
 158         testGeneratedInterrupt = true;
 159         mainThread.interrupt();
 160     }
 161     
 162     private static void doTest(Runnable action) {
 163         String description
 164                 = " A LinearGradientPaint graphics will be shown on console.\n"
 165                 + " The same graphics is sent to printer.\n"
 166                 + " Please verify if LinearGradientPaint shading is printed.\n"
 167                 + " If none is printed, press FAIL else press PASS";
 168 
 169         final JDialog dialog = new JDialog();
 170         dialog.setTitle("printSelectionTest");
 171         JTextArea textArea = new JTextArea(description);
 172         textArea.setEditable(false);
 173         final JButton testButton = new JButton("Start Test");
 174         final JButton passButton = new JButton("PASS");
 175         passButton.setEnabled(false);
 176         passButton.addActionListener((e) -> {
 177             f.dispose();
 178             dialog.dispose();
 179             pass();
 180         });
 181         final JButton failButton = new JButton("FAIL");
 182         failButton.setEnabled(false);
 183         failButton.addActionListener((e) -> {
 184             f.dispose();
 185             dialog.dispose();
 186             fail();
 187         });
 188         testButton.addActionListener((e) -> {
 189             testButton.setEnabled(false);
 190             action.run();
 191             passButton.setEnabled(true);
 192             failButton.setEnabled(true);
 193         });
 194         JPanel mainPanel = new JPanel(new BorderLayout());
 195         mainPanel.add(textArea, BorderLayout.CENTER);
 196         JPanel buttonPanel = new JPanel(new FlowLayout());
 197         buttonPanel.add(testButton);
 198         buttonPanel.add(passButton);
 199         buttonPanel.add(failButton);
 200         mainPanel.add(buttonPanel, BorderLayout.SOUTH);
 201         dialog.add(mainPanel);
 202         dialog.pack();
 203         dialog.setVisible(true);
 204         dialog.addWindowListener(new WindowAdapter() {
 205            @Override
 206             public void windowClosing(WindowEvent e) {
 207                 System.out.println("main dialog closing");
 208                 testGeneratedInterrupt = false;
 209                 mainThread.interrupt();
 210             }
 211         });
 212     }
 213 
 214 }