tests/auto/qfontmetrics/tst_qfontmetrics.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    70     void elidedText_data();
    70     void elidedText_data();
    71     void elidedText();
    71     void elidedText();
    72     void veryNarrowElidedText();
    72     void veryNarrowElidedText();
    73     void averageCharWidth();
    73     void averageCharWidth();
    74     void elidedMultiLength();
    74     void elidedMultiLength();
       
    75     void elidedMultiLengthF();
    75     void bearingIncludedInBoundingRect();
    76     void bearingIncludedInBoundingRect();
    76 };
    77 };
    77 
    78 
    78 tst_QFontMetrics::tst_QFontMetrics()
    79 tst_QFontMetrics::tst_QFontMetrics()
    79 
    80 
    98     QFont font;
    99     QFont font;
    99     font.setBold(true);
   100     font.setBold(true);
   100     QFontMetrics fm(font);
   101     QFontMetrics fm(font);
   101     const QString text = QLatin1String("Some stupid STRING");
   102     const QString text = QLatin1String("Some stupid STRING");
   102     QCOMPARE(fm.size(0, text), fm.size(0, text)) ;
   103     QCOMPARE(fm.size(0, text), fm.size(0, text)) ;
       
   104 
       
   105     {
       
   106         QImage image;
       
   107         QFontMetrics fm2(font, &image);
       
   108         QString text2 =  QLatin1String("Foo Foo");
       
   109         QCOMPARE(fm2.size(0, text2), fm2.size(0, text2));  //used to crash
       
   110     }
       
   111 
       
   112     {
       
   113         QImage image;
       
   114         QFontMetricsF fm3(font, &image);
       
   115         QString text2 =  QLatin1String("Foo Foo");
       
   116         QCOMPARE(fm3.size(0, text2), fm3.size(0, text2));  //used to crash
       
   117     }
   103 }
   118 }
   104 
   119 
   105 
   120 
   106 void tst_QFontMetrics::metrics()
   121 void tst_QFontMetrics::metrics()
   107 {
   122 {
   202     QVERIFY(fm.averageCharWidth() != 0);
   217     QVERIFY(fm.averageCharWidth() != 0);
   203     QFontMetricsF fmf(f);
   218     QFontMetricsF fmf(f);
   204     QVERIFY(fmf.averageCharWidth() != 0);
   219     QVERIFY(fmf.averageCharWidth() != 0);
   205 }
   220 }
   206 
   221 
   207 void tst_QFontMetrics::elidedMultiLength()
   222 template<class FontMetrics> void elidedMultiLength_helper()
   208 {
   223 {
   209     QString text1 = "Long Text 1\x9cShorter\x9csmall";
   224     QString text1 = "Long Text 1\x9cShorter\x9csmall";
   210     QString text1_long = "Long Text 1";
   225     QString text1_long = "Long Text 1";
   211     QString text1_short = "Shorter";
   226     QString text1_short = "Shorter";
   212     QString text1_small = "small";
   227     QString text1_small = "small";
   213     QFontMetrics fm = QFontMetrics(QFont());
   228     FontMetrics fm = FontMetrics(QFont());
   214     int width_long = fm.size(0, text1_long).width();
   229     int width_long = fm.size(0, text1_long).width();
   215     QCOMPARE(fm.elidedText(text1,Qt::ElideRight, 8000), text1_long);
   230     QCOMPARE(fm.elidedText(text1,Qt::ElideRight, 8000), text1_long);
   216     QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_long + 1), text1_long);
   231     QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_long + 1), text1_long);
   217     QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_long - 1), text1_short);
   232     QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_long - 1), text1_short);
   218     int width_short = fm.size(0, text1_short).width();
   233     int width_short = fm.size(0, text1_short).width();
   222     // Not even wide enough for "small" - should use ellipsis
   237     // Not even wide enough for "small" - should use ellipsis
   223     QChar ellipsisChar(0x2026);
   238     QChar ellipsisChar(0x2026);
   224     QString text1_el = QString::fromLatin1("s") + ellipsisChar;
   239     QString text1_el = QString::fromLatin1("s") + ellipsisChar;
   225     int width_small = fm.width(text1_el);
   240     int width_small = fm.width(text1_el);
   226     QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_small + 1), text1_el);
   241     QCOMPARE(fm.elidedText(text1,Qt::ElideRight, width_small + 1), text1_el);
   227 
   242 }
       
   243 
       
   244 void tst_QFontMetrics::elidedMultiLength()
       
   245 {
       
   246     elidedMultiLength_helper<QFontMetrics>();
       
   247 }
       
   248 
       
   249 void tst_QFontMetrics::elidedMultiLengthF()
       
   250 {
       
   251     elidedMultiLength_helper<QFontMetricsF>();
   228 }
   252 }
   229 
   253 
   230 void tst_QFontMetrics::bearingIncludedInBoundingRect()
   254 void tst_QFontMetrics::bearingIncludedInBoundingRect()
   231 {
   255 {
   232     QFont font;
   256     QFont font;