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 final class TestHSSFClientAnchor extends TestCase { |
031 | public void testGetAnchorHeightInPoints() { |
032 | HSSFWorkbook wb = new HSSFWorkbook(); |
033 | HSSFSheet sheet = wb.createSheet( "test" ); |
034 | HSSFClientAnchor a = new HSSFClientAnchor( 0 , 0 , 1023 , 255 ,( short ) 0 , 0 ,( short ) 0 , 0 ); |
035 | float p = a.getAnchorHeightInPoints(sheet); |
036 | assertEquals( 12.7 ,p, 0.001 ); |
037 |
038 | sheet.createRow( 0 ).setHeightInPoints( 14 ); |
039 | a = new HSSFClientAnchor( 0 , 0 , 1023 , 255 ,( short ) 0 , 0 ,( short ) 0 , 0 ); |
040 | p = a.getAnchorHeightInPoints(sheet); |
041 | assertEquals( 13.945 ,p, 0.001 ); |
042 |
043 | a = new HSSFClientAnchor( 0 , 0 , 1023 , 127 ,( short ) 0 , 0 ,( short ) 0 , 0 ); |
044 | p = a.getAnchorHeightInPoints(sheet); |
045 | assertEquals( 6.945 ,p, 0.001 ); |
046 |
047 | a = new HSSFClientAnchor( 0 , 126 , 1023 , 127 ,( short ) 0 , 0 ,( short ) 0 , 0 ); |
048 | p = a.getAnchorHeightInPoints(sheet); |
049 | assertEquals( 0.054 ,p, 0.001 ); |
050 |
051 | a = new HSSFClientAnchor( 0 , 0 , 1023 , 0 ,( short ) 0 , 0 ,( short ) 0 , 1 ); |
052 | p = a.getAnchorHeightInPoints(sheet); |
053 | assertEquals( 14.0 ,p, 0.001 ); |
054 |
055 | sheet.createRow( 0 ).setHeightInPoints( 12 ); |
056 | a = new HSSFClientAnchor( 0 , 127 , 1023 , 127 ,( short ) 0 , 0 ,( short ) 0 , 1 ); |
057 | p = a.getAnchorHeightInPoints(sheet); |
058 | assertEquals( 12.372 ,p, 0.001 ); |
059 |
060 | } |
061 |
062 | /*** |
063 | * When HSSFClientAnchor is converted into EscherClientAnchorRecord |
064 | * check that dx1, dx2, dy1 and dy2 are written "as is". |
065 | * (Bug 42999 reported that dx1 and dx2 are swapped if dx1>dx2. It doesn't make sense for client anchors.) |
066 | */ |
067 | public void testConvertAnchor() { |
068 | HSSFClientAnchor[] anchor = { |
069 | new HSSFClientAnchor( 0 , 0 , 0 , 0 ,( short ) 0 , 1 ,( short ) 1 , 3 ), |
070 | new HSSFClientAnchor( 100 , 0 , 900 , 255 ,( short ) 0 , 1 ,( short ) 1 , 3 ), |
071 | new HSSFClientAnchor( 900 , 0 , 100 , 255 ,( short ) 0 , 1 ,( short ) 1 , 3 ) |
072 | }; |
073 | for ( int i = 0 ; i < anchor.length; i++) { |
074 | EscherClientAnchorRecord record = (EscherClientAnchorRecord)ConvertAnchor.createAnchor(anchor[i]); |
075 | assertEquals(anchor[i].getDx1(), record.getDx1()); |
076 | assertEquals(anchor[i].getDx2(), record.getDx2()); |
077 | assertEquals(anchor[i].getDy1(), record.getDy1()); |
078 | assertEquals(anchor[i].getDy2(), record.getDy2()); |
079 | assertEquals(anchor[i].getCol1(), record.getCol1()); |
080 | assertEquals(anchor[i].getCol2(), record.getCol2()); |
081 | assertEquals(anchor[i].getRow1(), record.getRow1()); |
082 | assertEquals(anchor[i].getRow2(), record.getRow2()); |
083 | } |
084 | } |
085 |
086 | public void testAnchorHeightInPoints(){ |
087 | HSSFWorkbook wb = new HSSFWorkbook(); |
088 | HSSFSheet sheet = wb.createSheet(); |
089 |
090 | HSSFClientAnchor[] anchor = { |
091 | new HSSFClientAnchor( 0 , 0 , 0 , 0 ,( short ) 0 , 1 ,( short ) 1 , 3 ), |
092 | new HSSFClientAnchor( 0 , 254 , 0 , 126 ,( short ) 0 , 1 ,( short ) 1 , 3 ), |
093 | new HSSFClientAnchor( 0 , 128 , 0 , 128 ,( short ) 0 , 1 ,( short ) 1 , 3 ), |
094 | new HSSFClientAnchor( 0 , 0 , 0 , 128 ,( short ) 0 , 1 ,( short ) 1 , 3 ), |
095 | }; |
096 | float [] ref = { 25 .5f, 19 .125f, 25 .5f, 31 .875f}; |
097 | for ( int i = 0 ; i < anchor.length; i++) { |
098 | float height = anchor[i].getAnchorHeightInPoints(sheet); |
099 | assertEquals(ref[i], height, 0 ); |
100 | } |
101 | } |
102 | } |