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: import org.apache.poi.ddf.EscherClientAnchorRecord;
022: import org.apache.poi.hssf.model.ConvertAnchor;
023:
024: /**
025: * Various tests for HSSFClientAnchor.
026: *
027: * @author Glen Stampoultzis (glens at apache.org)
028: * @author Yegor Kozlov (yegor at apache.org)
029: */
030: public class TestHSSFClientAnchor extends TestCase {
031: public void testGetAnchorHeightInPoints() throws Exception {
032: HSSFWorkbook wb = new HSSFWorkbook();
033: HSSFSheet sheet = wb.createSheet("test");
034: HSSFClientAnchor a = new HSSFClientAnchor(0, 0, 1023, 255,
035: (short) 0, 0, (short) 0, 0);
036: float p = a.getAnchorHeightInPoints(sheet);
037: assertEquals(11.953, p, 0.001);
038:
039: sheet.createRow(0).setHeightInPoints(14);
040: a = new HSSFClientAnchor(0, 0, 1023, 255, (short) 0, 0,
041: (short) 0, 0);
042: p = a.getAnchorHeightInPoints(sheet);
043: assertEquals(13.945, p, 0.001);
044:
045: a = new HSSFClientAnchor(0, 0, 1023, 127, (short) 0, 0,
046: (short) 0, 0);
047: p = a.getAnchorHeightInPoints(sheet);
048: assertEquals(6.945, p, 0.001);
049:
050: a = new HSSFClientAnchor(0, 126, 1023, 127, (short) 0, 0,
051: (short) 0, 0);
052: p = a.getAnchorHeightInPoints(sheet);
053: assertEquals(0.054, p, 0.001);
054:
055: a = new HSSFClientAnchor(0, 0, 1023, 0, (short) 0, 0,
056: (short) 0, 1);
057: p = a.getAnchorHeightInPoints(sheet);
058: assertEquals(14.0, p, 0.001);
059:
060: sheet.createRow(0).setHeightInPoints(12);
061: a = new HSSFClientAnchor(0, 127, 1023, 127, (short) 0, 0,
062: (short) 0, 1);
063: p = a.getAnchorHeightInPoints(sheet);
064: assertEquals(12.0, p, 0.001);
065:
066: }
067:
068: /**
069: * When HSSFClientAnchor is converted into EscherClientAnchorRecord
070: * check that dx1, dx2, dy1 and dy2 are writtem "as is".
071: * (Bug 42999 reported that dx1 ans dx2 are swapped if dx1>dx2. It doesn't make sense for client anchors.)
072: */
073: public void testConvertAnchor() throws Exception {
074: HSSFClientAnchor[] anchor = {
075: new HSSFClientAnchor(0, 0, 0, 0, (short) 0, 1,
076: (short) 1, 3),
077: new HSSFClientAnchor(100, 0, 900, 255, (short) 0, 1,
078: (short) 1, 3),
079: new HSSFClientAnchor(900, 0, 100, 255, (short) 0, 1,
080: (short) 1, 3) };
081: for (int i = 0; i < anchor.length; i++) {
082: EscherClientAnchorRecord record = (EscherClientAnchorRecord) ConvertAnchor
083: .createAnchor(anchor[i]);
084: assertEquals(anchor[i].getDx1(), record.getDx1());
085: assertEquals(anchor[i].getDx2(), record.getDx2());
086: assertEquals(anchor[i].getDy1(), record.getDy1());
087: assertEquals(anchor[i].getDy2(), record.getDy2());
088: assertEquals(anchor[i].getCol1(), record.getCol1());
089: assertEquals(anchor[i].getCol2(), record.getCol2());
090: assertEquals(anchor[i].getRow1(), record.getRow1());
091: assertEquals(anchor[i].getRow2(), record.getRow2());
092: }
093: }
094:
095: public void testAnchorHeightInPoints() {
096: HSSFWorkbook wb = new HSSFWorkbook();
097: HSSFSheet sheet = wb.createSheet();
098:
099: HSSFClientAnchor[] anchor = {
100: new HSSFClientAnchor(0, 0, 0, 0, (short) 0, 1,
101: (short) 1, 3),
102: new HSSFClientAnchor(0, 254, 0, 126, (short) 0, 1,
103: (short) 1, 3),
104: new HSSFClientAnchor(0, 128, 0, 128, (short) 0, 1,
105: (short) 1, 3),
106: new HSSFClientAnchor(0, 0, 0, 128, (short) 0, 1,
107: (short) 1, 3), };
108: float[] ref = { 24.0f, 18.0f, 24.0f, 30.0f };
109: for (int i = 0; i < anchor.length; i++) {
110: float height = anchor[i].getAnchorHeightInPoints(sheet);
111: assertEquals(ref[i], height, 0);
112: }
113:
114: }
115:
116: }
|