Path: blob/master/test/jdk/javax/print/attribute/ServiceDialogTest.java
41149 views
/*1* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324/**25* @test26* @bug 4910388 4871089 499862427* @summary Confirm that28* 1. After choosing Reverse Landscape in the system default print29* Print Service (2nd in the list), it30* will reset to portrait in "Test Printer"31* 2. Print To File button is not cleared when switching between the32* 2nd service (system default printer) and Test Printer.33* 3. Make sure "Postscript" printer is the default and make sure the34* "print to file" button is disabled. File Dialog should not be35* shown after pressing print button.36*37* @run main/manual ServiceDialogTest38*/39import java.awt.*;40import javax.print.*;41import javax.print.attribute.standard.*;42import javax.print.attribute.*;43import javax.print.event.*;44import java.io.*;45import java.util.Locale;4647public class ServiceDialogTest {48/**49* Constructor50*/51public ServiceDialogTest() {52super();53}54/**55* Starts the application.56*/57public static void main(java.lang.String[] args) {58ServiceDialogTest pd = new ServiceDialogTest();59PrintService services[] = new PrintService[3];60services[1] = PrintServiceLookup.lookupDefaultPrintService();6162FileOutputStream fos = null;63File f = null;64String mType = "application/postscript";65DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG;66try {67f = new File("streamexample.ps");68fos = new FileOutputStream(f);69StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, mType);70if (factories.length > 0) {71services[0] = factories[0].getPrintService(fos);72} else {73throw new RuntimeException("No StreamPrintService available which would support "+flavor);74}7576services[2] = new TestPrintService("Test Printer");7778//System.out.println("is "+flavor+" supported? "+services[0].isDocFlavorSupported(flavor));79//System.out.println("is Orientation supported? "+services[0].isAttributeCategorySupported(OrientationRequested.class));80//System.out.println("is REVERSE PORTRAIT supported ? "+services[0].isAttributeValueSupported(OrientationRequested.REVERSE_PORTRAIT, flavor, null));8182HashPrintRequestAttributeSet prSet = new HashPrintRequestAttributeSet();83prSet.add(new Destination(new File("./dest.prn").toURI()));84PrintService selService = ServiceUI.printDialog(null, 200, 200, services, services[0], flavor, prSet);85Attribute attr[] = prSet.toArray();86for (int x = 0; x < attr.length; x ++) {87System.out.println(attr[x]);88}8990//DocPrintJob pj = service.createPrintJob();91//PrintDocument prDoc = new PrintDocument();92//pj.print(prDoc, null);9394} catch (Exception e) {95e.printStackTrace();96}97}98}99100101class TestPrintService implements PrintService102{103104private static DocFlavor textByteFlavor = null;105private static final DocFlavor supportedDocFlavors[] = (new DocFlavor[] {106javax.print.DocFlavor.INPUT_STREAM.JPEG107});108109private static final Class serviceAttrCats[] = (new Class[] {110javax.print.attribute.standard.PrinterName.class111});112113private static final Class otherAttrCats[] = (new Class [] {114javax.print.attribute.standard.Copies.class,115javax.print.attribute.standard.OrientationRequested.class,116javax.print.attribute.standard.Destination.class,117});118119private String printer = null;120121public TestPrintService() {122}123124public TestPrintService(String printerName) {125if (printerName == null) {126throw new IllegalArgumentException("null printer name");127} else {128printer = printerName;129}130}131132public String getName()133{134return printer;135}136137138public DocPrintJob createPrintJob()139{140return null;141}142143public PrintServiceAttributeSet getUpdatedAttributes()144{145return null;146}147148149public void addPrintServiceAttributeListener(PrintServiceAttributeListener printserviceattributelistener)150{151}152153public void removePrintServiceAttributeListener(PrintServiceAttributeListener printserviceattributelistener)154{155}156157public PrintServiceAttribute getAttribute(Class category)158{159return null;160}161162public PrintServiceAttributeSet getAttributes()163{164HashPrintServiceAttributeSet aSet = new HashPrintServiceAttributeSet();165return aSet;166}167168public DocFlavor[] getSupportedDocFlavors()169{170int i = supportedDocFlavors.length;171DocFlavor adocflavor[] = new DocFlavor[i];172System.arraycopy(supportedDocFlavors, 0, adocflavor, 0, i);173return adocflavor;174}175176public boolean isDocFlavorSupported(DocFlavor docflavor)177{178for (int i = 0; i < supportedDocFlavors.length; i++) {179if (docflavor.equals(supportedDocFlavors[i])) {180return true;181}182}183return false;184}185186public Class[] getSupportedAttributeCategories()187{188int i = otherAttrCats.length;189Class aclass[] = new Class[i];190System.arraycopy(otherAttrCats, 0, aclass, 0, otherAttrCats.length);191return aclass;192}193194public boolean isAttributeCategorySupported(Class category)195{196if (category == null) {197throw new NullPointerException("null category");198}199200for (int i = 0; i < otherAttrCats.length; i++) {201if (category == otherAttrCats[i]) {202return true;203}204}205return false;206}207208public boolean isAttributeValueSupported(Attribute attrval, DocFlavor flavor, AttributeSet attributes) {209210if (attrval == OrientationRequested.PORTRAIT)211return true;212else if (attrval == OrientationRequested.LANDSCAPE)213return true;214else215return false;216}217218public Object getDefaultAttributeValue(Class category)219{220if (category == null) {221throw new NullPointerException("null category");222}223if (category == javax.print.attribute.standard.Copies.class)224return new Copies(1);225226if (category == javax.print.attribute.standard.OrientationRequested.class)227return OrientationRequested.PORTRAIT;228229return null;230}231232public Object getSupportedAttributeValues(Class category, DocFlavor docflavor, AttributeSet attributeset)233{234if (category == null) {235throw new NullPointerException("null category");236}237238if (docflavor != null) {239if (!isDocFlavorSupported(docflavor)) {240throw new IllegalArgumentException(docflavor + " is an unsupported flavor");241}242}243if (!isAttributeCategorySupported(category)) {244return null;245}246if (category == javax.print.attribute.standard.Copies.class ) {247return new CopiesSupported(1, 5);248}249if (category == javax.print.attribute.standard.OrientationRequested.class ) {250OrientationRequested req[] = { OrientationRequested.PORTRAIT, OrientationRequested.LANDSCAPE };251return req;252}253254return null;255}256257public AttributeSet getUnsupportedAttributes(DocFlavor docflavor, AttributeSet attributeset) {258259if (docflavor != null && !isDocFlavorSupported(docflavor)) {260throw new IllegalArgumentException("flavor " + docflavor + "is not supported");261}262if (attributeset == null) {263return null;264}265266HashAttributeSet hashattributeset = new HashAttributeSet();267Attribute attributearray[] = attributeset.toArray();268for (int i = 0; i < attributearray.length; i++) {269try {270Attribute attribute = attributearray[i];271if (!isAttributeCategorySupported(attribute.getCategory())) {272hashattributeset.add(attribute);273} else {274if (!isAttributeValueSupported(attribute, docflavor, attributeset)) {275hashattributeset.add(attribute);276}277}278}279catch (ClassCastException classcastexception) {280281}282}283284if (hashattributeset.isEmpty()) {285return null;286}287return hashattributeset;288}289290public ServiceUIFactory getServiceUIFactory() {291return null;292}293294public String toString() {295return "Printer : " + getName();296}297298public boolean equals(Object obj) {299return obj == this || (obj instanceof TestPrintService) && ((TestPrintService)obj).getName().equals(getName());300}301302public int hashCode() {303return getClass().hashCode() + getName().hashCode();304}305306}307308309