001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hssf.usermodel;
019:
020: import junit.framework.TestCase;
021:
022: import java.awt.*;
023: import java.awt.geom.Line2D;
024:
025: /**
026: * Tests the Graphics2d drawing capability.
027: *
028: * @author Glen Stampoultzis (glens at apache.org)
029: */
030: public class TestEscherGraphics2d extends TestCase {
031: private HSSFShapeGroup escherGroup;
032: private EscherGraphics2d graphics;
033:
034: protected void setUp() throws Exception {
035: super .setUp();
036:
037: HSSFWorkbook workbook = new HSSFWorkbook();
038: HSSFSheet sheet = workbook.createSheet("test");
039: escherGroup = sheet.createDrawingPatriarch().createGroup(
040: new HSSFClientAnchor(0, 0, 1023, 255, (short) 0, 0,
041: (short) 0, 0));
042: escherGroup = new HSSFShapeGroup(null, new HSSFChildAnchor());
043: EscherGraphics g = new EscherGraphics(this .escherGroup,
044: workbook, Color.black, 1.0f);
045: graphics = new EscherGraphics2d(g);
046:
047: }
048:
049: public void testDrawString() throws Exception {
050: graphics.drawString("This is a test", 10, 10);
051: HSSFTextbox t = (HSSFTextbox) escherGroup.getChildren().get(0);
052: assertEquals("This is a test", t.getString().getString()
053: .toString());
054:
055: // Check that with a valid font, it's still ok
056: Font font = new Font("Forte", Font.PLAIN, 12);
057: graphics.setFont(font);
058: graphics.drawString("This is another test", 10, 10);
059:
060: // And test with ones that need the style appending
061: font = new Font("dialog", Font.PLAIN, 12);
062: graphics.setFont(font);
063: graphics.drawString("This is another test", 10, 10);
064:
065: font = new Font("dialog", Font.BOLD, 12);
066: graphics.setFont(font);
067: graphics.drawString("This is another test", 10, 10);
068:
069: // But with an invalid font, we get an exception
070: font = new Font("IamAmadeUPfont", Font.PLAIN, 22);
071: graphics.setFont(font);
072: try {
073: graphics.drawString("This is another test", 10, 10);
074: fail();
075: } catch (IllegalArgumentException e) {
076: }
077: }
078:
079: public void testFillRect() throws Exception {
080: graphics.fillRect(10, 10, 20, 20);
081: HSSFSimpleShape s = (HSSFSimpleShape) escherGroup.getChildren()
082: .get(0);
083: assertEquals(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE, s
084: .getShapeType());
085: assertEquals(10, s.getAnchor().getDx1());
086: assertEquals(10, s.getAnchor().getDy1());
087: assertEquals(30, s.getAnchor().getDy2());
088: assertEquals(30, s.getAnchor().getDx2());
089: }
090:
091: public void testGetFontMetrics() throws Exception {
092: FontMetrics fontMetrics = graphics.getFontMetrics(graphics
093: .getFont());
094: if (graphics.getFont().toString().indexOf("dialog") != -1
095: || graphics.getFont().toString().indexOf("Dialog") != -1) // if dialog is returned we can't run the test properly.
096: return;
097: assertEquals(7, fontMetrics.charWidth('X'));
098: assertEquals(
099: "java.awt.Font[family=Arial,name=Arial,style=plain,size=10]",
100: fontMetrics.getFont().toString());
101: }
102:
103: public void testSetFont() throws Exception {
104: Font f = new Font("Helvetica", 0, 12);
105: graphics.setFont(f);
106: assertEquals(f, graphics.getFont());
107: }
108:
109: public void testSetColor() throws Exception {
110: graphics.setColor(Color.red);
111: assertEquals(Color.red, graphics.getColor());
112: }
113:
114: public void testGetFont() throws Exception {
115: Font f = graphics.getFont();
116: if (graphics.getFont().toString().indexOf("dialog") != -1
117: || graphics.getFont().toString().indexOf("Dialog") != -1) // if dialog is returned we can't run the test properly.
118: return;
119:
120: assertEquals(
121: "java.awt.Font[family=Arial,name=Arial,style=plain,size=10]",
122: f.toString());
123: }
124:
125: public void testDraw() throws Exception {
126: graphics.draw(new Line2D.Double(10, 10, 20, 20));
127: HSSFSimpleShape s = (HSSFSimpleShape) escherGroup.getChildren()
128: .get(0);
129: assertTrue(s.getShapeType() == HSSFSimpleShape.OBJECT_TYPE_LINE);
130: assertEquals(10, s.getAnchor().getDx1());
131: assertEquals(10, s.getAnchor().getDy1());
132: assertEquals(20, s.getAnchor().getDx2());
133: assertEquals(20, s.getAnchor().getDy2());
134: System.out.println("s = " + s);
135: }
136: }
|