01: /* ====================================================================
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: http://www.apache.org/licenses/LICENSE-2.0
10:
11: Unless required by applicable law or agreed to in writing, software
12: distributed under the License is distributed on an "AS IS" BASIS,
13: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: See the License for the specific language governing permissions and
15: limitations under the License.
16: ==================================================================== */
17:
18: package org.apache.poi.hssf.usermodel;
19:
20: import junit.framework.TestCase;
21:
22: import java.awt.*;
23:
24: /**
25: * Tests the capabilities of the EscherGraphics class.
26: *
27: * @author Glen Stampoultzis (glens at apache.org)
28: */
29: public class TestEscherGraphics extends TestCase {
30: private HSSFShapeGroup escherGroup;
31: private EscherGraphics graphics;
32:
33: protected void setUp() throws Exception {
34: HSSFWorkbook workbook = new HSSFWorkbook();
35: HSSFSheet sheet = workbook.createSheet("test");
36: escherGroup = sheet.createDrawingPatriarch().createGroup(
37: new HSSFClientAnchor(0, 0, 1023, 255, (short) 0, 0,
38: (short) 0, 0));
39: escherGroup = new HSSFShapeGroup(null, new HSSFChildAnchor());
40: graphics = new EscherGraphics(this .escherGroup, workbook,
41: Color.black, 1.0f);
42: super .setUp();
43: }
44:
45: public void testGetFont() throws Exception {
46: Font f = graphics.getFont();
47: if (f.toString().indexOf("dialog") == -1
48: && f.toString().indexOf("Dialog") == -1)
49: assertEquals(
50: "java.awt.Font[family=Arial,name=Arial,style=plain,size=10]",
51: f.toString());
52: }
53:
54: public void testGetFontMetrics() throws Exception {
55: Font f = graphics.getFont();
56: if (f.toString().indexOf("dialog") != -1
57: || f.toString().indexOf("Dialog") != -1)
58: return;
59: FontMetrics fontMetrics = graphics.getFontMetrics(graphics
60: .getFont());
61: assertEquals(7, fontMetrics.charWidth('X'));
62: assertEquals(
63: "java.awt.Font[family=Arial,name=Arial,style=plain,size=10]",
64: fontMetrics.getFont().toString());
65: }
66:
67: public void testSetFont() throws Exception {
68: Font f = new Font("Helvetica", 0, 12);
69: graphics.setFont(f);
70: assertEquals(f, graphics.getFont());
71: }
72:
73: public void testSetColor() throws Exception {
74: graphics.setColor(Color.red);
75: assertEquals(Color.red, graphics.getColor());
76: }
77:
78: public void testFillRect() throws Exception {
79: graphics.fillRect(10, 10, 20, 20);
80: HSSFSimpleShape s = (HSSFSimpleShape) escherGroup.getChildren()
81: .get(0);
82: assertEquals(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE, s
83: .getShapeType());
84: assertEquals(10, s.getAnchor().getDx1());
85: assertEquals(10, s.getAnchor().getDy1());
86: assertEquals(30, s.getAnchor().getDy2());
87: assertEquals(30, s.getAnchor().getDx2());
88: }
89:
90: public void testDrawString() throws Exception {
91: graphics.drawString("This is a test", 10, 10);
92: HSSFTextbox t = (HSSFTextbox) escherGroup.getChildren().get(0);
93: assertEquals("This is a test", t.getString().getString()
94: .toString());
95: }
96:
97: }
|