test/jdk/java/awt/print/PrinterJob/Margins.java
author prr
Fri, 24 May 2019 09:10:10 -0700
changeset 55180 f13dba72a5ea
parent 47216 71c04702a3d5
permissions -rw-r--r--
8224705: Tests that need to be problem-listed or have printer resources Reviewed-by: serb, psadhukhan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
46151
5fa789776f7d 8185500: [TESTBUG] Add keywords headful/printer in java/awt and javax tests.
goetz
parents: 42209
diff changeset
     2
 * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
55180
f13dba72a5ea 8224705: Tests that need to be problem-listed or have printer resources
prr
parents: 47216
diff changeset
    26
 * @key headful printer
42209
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    27
 * @bug 6543815 6601097 8160888
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Image should be sent to printer, no exceptions thrown.
39528
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    29
 *    The 3 printouts should have a rectangle which is the minimum
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    30
 *    possible margins ie, the margins should be hardware margins
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    31
 *    and not java default 1 inch margins.
42209
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    32
 * @run main Margins
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
39528
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    35
import java.awt.print.PrinterJob;
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    36
import java.awt.print.Printable;
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    37
import java.awt.print.PageFormat;
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    38
import java.awt.print.Paper;
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    39
import java.awt.print.PrinterException;
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    40
import java.awt.Graphics;
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    41
import java.awt.Graphics2D;
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    42
import java.awt.Color;
42209
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    43
import java.awt.Robot;
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    44
import java.awt.event.KeyEvent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class Margins implements Printable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
42209
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    48
    public static void main(String args[]) throws Exception {
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    49
        Robot robot = new Robot();
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    50
        Thread t = new Thread (() -> {
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    51
            robot.waitForIdle();
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    52
            robot.delay(5000);
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    53
            robot.keyPress(KeyEvent.VK_ENTER);
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    54
            robot.keyRelease(KeyEvent.VK_ENTER);
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    55
            robot.waitForIdle();
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    56
            robot.delay(5000);
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    57
            robot.keyPress(KeyEvent.VK_ENTER);
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    58
            robot.keyRelease(KeyEvent.VK_ENTER);
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    59
            robot.waitForIdle();
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    60
            robot.delay(5000);
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    61
            robot.keyPress(KeyEvent.VK_ENTER);
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    62
            robot.keyRelease(KeyEvent.VK_ENTER);
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    63
        });
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    64
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        PrinterJob job = PrinterJob.getPrinterJob();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        PageFormat pageFormat = job.defaultPage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        Paper paper = pageFormat.getPaper();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        double wid = paper.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        double hgt = paper.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        paper.setImageableArea(0, -10, wid, hgt);
42209
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    71
        t.start();
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    72
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        pageFormat = job.pageDialog(pageFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        pageFormat.setPaper(paper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        job.setPrintable(new Margins(), pageFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            job.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        } catch (PrinterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
42209
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
    81
        paper.setImageableArea(0, 0, wid, hgt + 72);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        pageFormat = job.pageDialog(pageFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        pageFormat.setPaper(paper);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        job.setPrintable(new Margins(), pageFormat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
           job.print();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } catch (PrinterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
39528
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    90
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    91
        pageFormat = job.defaultPage();
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    92
        paper = pageFormat.getPaper();
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    93
        wid = paper.getWidth();
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    94
        hgt = paper.getHeight();
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    95
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    96
        paper.setImageableArea(0, -10, -wid, hgt);
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    97
        pageFormat = job.pageDialog(pageFormat);
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    98
        pageFormat.setPaper(paper);
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
    99
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
   100
        job.setPrintable(new Margins(), pageFormat);
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
   101
        try {
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
   102
           job.print();
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
   103
        } catch (PrinterException e) {
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
   104
        }
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
   105
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
   public int print(Graphics g, PageFormat pf, int page)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
       throws PrinterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
       if (page > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
           return NO_SUCH_PAGE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
       int ix = (int)pf.getImageableX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
       int iy = (int)pf.getImageableY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
       int iw = (int)pf.getImageableWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
       int ih = (int)pf.getImageableHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
       System.out.println("ix="+ix+" iy="+iy+" iw="+iw+" ih="+ih);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
       if ((ix < 0) || (iy < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
           throw new RuntimeException("Imageable x or y is a negative value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
39528
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
   122
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
       Paper paper = pf.getPaper();
42209
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
   124
       int wid = (int)paper.getWidth();
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
   125
       int hgt = (int)paper.getHeight();
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
   126
       System.out.println("wid="+wid+" hgt="+hgt);
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
   127
       /*
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
   128
        * If imageable width/height is -ve, then print was done with 1" margin
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
   129
        * e.g. ix=72 iy=72 iw=451 ih=697 and paper wid=595
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
   130
        * but with fix, we get print with hardware margin e.g.
39528
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
   131
        * ix=12, iy=12, iw=571, ih=817
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
   132
        */
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
   133
       if ((wid - iw > 72) || (hgt - ih > 72)) {
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
   134
           throw new RuntimeException("Imageable width or height is negative value");
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
   135
       }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
       if ((ix+iw > wid) || (iy+ih > hgt)) {
39528
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
   137
           throw new RuntimeException("Printable width or height "
0544281c5bd3 6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
psadhukhan
parents: 5506
diff changeset
   138
                   + "exceeds paper width or height.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
       }
42209
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
   140
       // runtime checking to see if the margins/printable area
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
   141
       // correspond to the entire size of the paper, for now, make it pass
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
   142
       // as for linux, the hwmargin is not taken into account - bug6574279
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
   143
       if (ix == 0 && iy == 0 && (ix+iw == wid) && (iy+ih == hgt)) {
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
   144
           return PAGE_EXISTS;
2b05a23bc5ee 8160888: [PIT] What to expect of updated java/awt/print/PrinterJob/Margins.java
psadhukhan
parents: 39528
diff changeset
   145
       }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
       Graphics2D g2d = (Graphics2D)g;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
       g2d.translate(ix, iy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
       g2d.setColor(Color.black);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
       g2d.drawRect(1, 1, iw-2, ih-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
       return PAGE_EXISTS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
}