aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache/poi/ss/usermodel/BaseTestFont.java
blob: d5c8e21683459dd22b2ec46d43b8a95cb1075e2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */

package org.apache.poi.ss.usermodel;

import junit.framework.TestCase;

import org.apache.poi.ss.ITestDataProvider;

/**
 * @author Yegor Kozlov
 */
public abstract class BaseTestFont extends TestCase {

    protected abstract ITestDataProvider getTestDataProvider();

    public void baseTestDefaultFont(String defaultName, short defaultSize, short defaultColor){
        //get default font and check against default value
        Workbook workbook = getTestDataProvider().createWorkbook();
        Font fontFind=workbook.findFont(Font.BOLDWEIGHT_NORMAL, defaultColor, defaultSize, defaultName, false, false, Font.SS_NONE, Font.U_NONE);
        assertNotNull(fontFind);

        //get default font, then change 2 values and check against different values (height changes)
        Font font=workbook.createFont();
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        assertEquals(Font.BOLDWEIGHT_BOLD, font.getBoldweight());
        font.setUnderline(Font.U_DOUBLE);
        assertEquals(Font.U_DOUBLE, font.getUnderline());
        font.setFontHeightInPoints((short)15);
        assertEquals(15*20, font.getFontHeight());
        assertEquals(15, font.getFontHeightInPoints());
        fontFind=workbook.findFont(Font.BOLDWEIGHT_BOLD, defaultColor, (short)(15*20), defaultName, false, false, Font.SS_NONE, Font.U_DOUBLE);
        assertNotNull(fontFind);
    }

    public void testGetNumberOfFonts(){
        Workbook wb = getTestDataProvider().createWorkbook();
        int num0 = wb.getNumberOfFonts();

        Font f1=wb.createFont();
        f1.setBoldweight(Font.BOLDWEIGHT_BOLD);
        short idx1 = f1.getIndex();
        wb.createCellStyle().setFont(f1);

        Font f2=wb.createFont();
        f2.setUnderline(Font.U_DOUBLE);
        short idx2 = f2.getIndex();
        wb.createCellStyle().setFont(f2);

        Font f3=wb.createFont();
        f3.setFontHeightInPoints((short)23);
        short idx3 = f3.getIndex();
        wb.createCellStyle().setFont(f3);

        assertEquals(num0 + 3,wb.getNumberOfFonts());
        assertEquals(Font.BOLDWEIGHT_BOLD,wb.getFontAt(idx1).getBoldweight());
        assertEquals(Font.U_DOUBLE,wb.getFontAt(idx2).getUnderline());
        assertEquals(23,wb.getFontAt(idx3).getFontHeightInPoints());
	}

    /**
     * Tests that we can define fonts to a new
     *  file, save, load, and still see them
     */
    public void testCreateSave() {
        Workbook wb = getTestDataProvider().createWorkbook();
        Sheet s1 = wb.createSheet();
        Row r1 = s1.createRow(0);
        Cell r1c1 = r1.createCell(0);
        r1c1.setCellValue(2.2);

        int num0 = wb.getNumberOfFonts();

        Font font=wb.createFont();
        font.setBoldweight(Font.BOLDWEIGHT_BOLD);
        font.setStrikeout(true);
        font.setColor(IndexedColors.YELLOW.getIndex());
        font.setFontName("Courier");
        short font1Idx = font.getIndex();
        wb.createCellStyle().setFont(font);
        assertEquals(num0 + 1, wb.getNumberOfFonts());

        CellStyle cellStyleTitle=wb.createCellStyle();
        cellStyleTitle.setFont(font);
        r1c1.setCellStyle(cellStyleTitle);

        // Save and re-load
        wb = getTestDataProvider().writeOutAndReadBack(wb);
        s1 = wb.getSheetAt(0);

        assertEquals(num0 + 1, wb.getNumberOfFonts());
        short idx = s1.getRow(0).getCell(0).getCellStyle().getFontIndex();
        Font fnt = wb.getFontAt(idx);
        assertNotNull(fnt);
        assertEquals(IndexedColors.YELLOW.getIndex(), fnt.getColor());
        assertEquals("Courier", fnt.getFontName());

        // Now add an orphaned one
        Font font2 = wb.createFont();
        font2.setItalic(true);
        font2.setFontHeightInPoints((short)15);
        short font2Idx = font2.getIndex();
        wb.createCellStyle().setFont(font2);
        assertEquals(num0 + 2, wb.getNumberOfFonts());

        // Save and re-load
        wb = getTestDataProvider().writeOutAndReadBack(wb);
        s1 = wb.getSheetAt(0);

        assertEquals(num0 + 2, wb.getNumberOfFonts());
        assertNotNull(wb.getFontAt(font1Idx));
        assertNotNull(wb.getFontAt(font2Idx));

        assertEquals(15, wb.getFontAt(font2Idx).getFontHeightInPoints());
        assertEquals(true, wb.getFontAt(font2Idx).getItalic());
    }

    /**
     * Test that fonts get added properly
     *
     * @see org.apache.poi.hssf.usermodel.TestBugs#test45338()
     */
    public void test45338() {
        Workbook wb = getTestDataProvider().createWorkbook();
        int num0 = wb.getNumberOfFonts();

        Sheet s = wb.createSheet();
        s.createRow(0);
        s.createRow(1);
        s.getRow(0).createCell(0);
        s.getRow(1).createCell(0);

        //default font
        Font f1 = wb.getFontAt((short)0);
        assertEquals(Font.BOLDWEIGHT_NORMAL, f1.getBoldweight());

        // Check that asking for the same font
        //  multiple times gives you the same thing.
        // Otherwise, our tests wouldn't work!
        assertSame(wb.getFontAt((short)0), wb.getFontAt((short)0));

        // Look for a new font we have
        //  yet to add
        assertNull(
            wb.findFont(
                Font.BOLDWEIGHT_BOLD, (short)123, (short)(22*20),
                "Thingy", false, true, (short)2, (byte)2
            )
        );

        Font nf = wb.createFont();
        short nfIdx = nf.getIndex();
        assertEquals(num0 + 1, wb.getNumberOfFonts());

        assertSame(nf, wb.getFontAt(nfIdx));

        nf.setBoldweight(Font.BOLDWEIGHT_BOLD);
        nf.setColor((short)123);
        nf.setFontHeightInPoints((short)22);
        nf.setFontName("Thingy");
        nf.setItalic(false);
        nf.setStrikeout(true);
        nf.setTypeOffset((short)2);
        nf.setUnderline((byte)2);

        assertEquals(num0 + 1, wb.getNumberOfFonts());
        assertEquals(nf, wb.getFontAt(nfIdx));

        assertEquals(wb.getFontAt(nfIdx), wb.getFontAt(nfIdx));
        assertTrue(wb.getFontAt((short)0) != wb.getFontAt(nfIdx));

        // Find it now
        assertNotNull(
            wb.findFont(
                Font.BOLDWEIGHT_BOLD, (short)123, (short)(22*20),
                "Thingy", false, true, (short)2, (byte)2
            )
        );
        assertSame(nf,
               wb.findFont(
                   Font.BOLDWEIGHT_BOLD, (short)123, (short)(22*20),
                   "Thingy", false, true, (short)2, (byte)2
               )
        );
    }
}