Lines 31-36
Link Here
|
31 |
import java.util.HashMap; |
31 |
import java.util.HashMap; |
32 |
import java.util.Map; |
32 |
import java.util.Map; |
33 |
|
33 |
|
|
|
34 |
import java.awt.geom.Rectangle2D; |
35 |
|
34 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
36 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
35 |
import org.apache.poi.hssf.util.PaneInformation; |
37 |
import org.apache.poi.hssf.util.PaneInformation; |
36 |
import org.apache.poi.ss.ITestDataProvider; |
38 |
import org.apache.poi.ss.ITestDataProvider; |
Lines 416-422
Link Here
|
416 |
sheet.setColumnWidth(0, sheet.getColumnWidth(0)); // Bug 50681 reports exception at this point |
418 |
sheet.setColumnWidth(0, sheet.getColumnWidth(0)); // Bug 50681 reports exception at this point |
417 |
wb.close(); |
419 |
wb.close(); |
418 |
} |
420 |
} |
|
|
421 |
|
422 |
@Test |
423 |
public final void bug51622_testAutoSizeShouldRecognizeLeadingSpaces() throws IOException { |
424 |
Workbook wb = _testDataProvider.createWorkbook(); |
425 |
BaseTestSheetAutosizeColumn.fixFonts(wb); |
426 |
Sheet sheet = wb.createSheet(); |
427 |
Row row = sheet.createRow(0); |
428 |
Cell cell0 = row.createCell(0); |
429 |
Cell cell1 = row.createCell(1); |
430 |
Cell cell2 = row.createCell(2); |
431 |
|
432 |
cell0.setCellValue("Test Column AutoSize"); |
433 |
cell1.setCellValue(" Test Column AutoSize"); |
434 |
cell2.setCellValue("Test Column AutoSize "); |
435 |
|
436 |
sheet.autoSizeColumn(0); |
437 |
sheet.autoSizeColumn(1); |
438 |
sheet.autoSizeColumn(2); |
439 |
|
440 |
int noWhitespaceColWidth = sheet.getColumnWidth(0); |
441 |
int leadingWhitespaceColWidth = sheet.getColumnWidth(1); |
442 |
int trailingWhitespaceColWidth = sheet.getColumnWidth(2); |
443 |
|
444 |
// Based on the amount of text and whitespace used, and the default font |
445 |
// assume that the cell with whitespace should be at least 20% wider than |
446 |
// the cell without whitespace. This number is arbitrary, but should be large |
447 |
// enough to guarantee that the whitespace cell isn't wider due to chance. |
448 |
// Experimentally, I calculated the ratio as 1.2478181, though this ratio may change |
449 |
// if the default font or margins change. |
450 |
final double expectedRatioThreshold = 1.2f; |
451 |
double leadingWhitespaceRatio = ((double) leadingWhitespaceColWidth)/noWhitespaceColWidth; |
452 |
double trailingWhitespaceRatio = ((double) leadingWhitespaceColWidth)/noWhitespaceColWidth; |
453 |
|
454 |
assertGreaterThan("leading whitespace is longer than no whitespace", leadingWhitespaceRatio, expectedRatioThreshold); |
455 |
assertGreaterThan("trailing whitespace is longer than no whitespace", trailingWhitespaceRatio, expectedRatioThreshold); |
456 |
assertEquals("cells with equal leading and trailing whitespace have equal width", |
457 |
leadingWhitespaceColWidth, trailingWhitespaceColWidth); |
458 |
|
459 |
wb.close(); |
460 |
} |
461 |
|
462 |
/** |
463 |
* Test if a > b. Fails if false. |
464 |
* |
465 |
* @param message |
466 |
* @param a |
467 |
* @param b |
468 |
*/ |
469 |
private void assertGreaterThan(String message, double a, double b) { |
470 |
if (a > b) { // expected |
471 |
} else { |
472 |
String msg = "Expected: " + a + " > " + b; |
473 |
fail(message + ": " + msg); |
474 |
} |
475 |
} |
419 |
|
476 |
|
|
|
477 |
// FIXME: this function is a self-fulfilling prophecy: this test will always pass as long |
478 |
// as the code-under-test and the testcase code are written the same way (have the same bugs). |
420 |
private double computeCellWidthManually(Cell cell0, Font font) { |
479 |
private double computeCellWidthManually(Cell cell0, Font font) { |
421 |
final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true); |
480 |
final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true); |
422 |
RichTextString rt = cell0.getRichStringCellValue(); |
481 |
RichTextString rt = cell0.getRichStringCellValue(); |
Lines 432-439
Link Here
|
432 |
} |
491 |
} |
433 |
|
492 |
|
434 |
TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext); |
493 |
TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext); |
435 |
return ((layout.getBounds().getWidth() / 1) / 8); |
494 |
double frameWidth = getFrameWidth(layout); |
|
|
495 |
return ((frameWidth / 1) / 8); |
436 |
} |
496 |
} |
|
|
497 |
|
498 |
private double getFrameWidth(TextLayout layout) { |
499 |
Rectangle2D bounds = layout.getBounds(); |
500 |
double frameWidth = bounds.getX() + bounds.getWidth(); |
501 |
return frameWidth; |
502 |
} |
437 |
|
503 |
|
438 |
private double computeCellWidthFixed(Font font, String txt) { |
504 |
private double computeCellWidthFixed(Font font, String txt) { |
439 |
final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true); |
505 |
final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true); |
Lines 441-447
Link Here
|
441 |
copyAttributes(font, str, 0, txt.length()); |
507 |
copyAttributes(font, str, 0, txt.length()); |
442 |
|
508 |
|
443 |
TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext); |
509 |
TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext); |
444 |
return layout.getBounds().getWidth(); |
510 |
double frameWidth = getFrameWidth(layout); |
|
|
511 |
return frameWidth; |
445 |
} |
512 |
} |
446 |
|
513 |
|
447 |
private static void copyAttributes(Font font, AttributedString str, int startIdx, int endIdx) { |
514 |
private static void copyAttributes(Font font, AttributedString str, int startIdx, int endIdx) { |