ganeswidgets/tsrc/unit/unittest_ganeswidgets.cpp
changeset 3 c863538fcbb6
parent 1 e48454f237ca
child 6 1cdcc61142d2
equal deleted inserted replaced
2:49c70dcc3f17 3:c863538fcbb6
    29 #include <hgwidgets/hgmediawall.h>
    29 #include <hgwidgets/hgmediawall.h>
    30 
    30 
    31 Q_DECLARE_METATYPE(QItemSelection)
    31 Q_DECLARE_METATYPE(QItemSelection)
    32 Q_DECLARE_METATYPE(QModelIndex)
    32 Q_DECLARE_METATYPE(QModelIndex)
    33 
    33 
    34 static const QPointF grid_portrait_pos0(70, 30);
    34 static const QPointF grid_portrait_pos0(60, 60);
    35 static const QPointF grid_portrait_pos1(180, 30);
    35 static const QPointF grid_portrait_pos1(180, 60);
    36 static const QPointF grid_portrait_pos2(280, 30);
    36 static const QPointF grid_portrait_pos2(300, 60);
    37 static const QPointF grid_portrait_pos3(70, 120);
    37 static const QPointF grid_portrait_pos3(60, 180);
    38 static const QPointF grid_portrait_pos4(180, 120);
    38 static const QPointF grid_portrait_pos4(180, 180);
    39 static const QPointF grid_portrait_pos5(280, 120);
    39 static const QPointF grid_portrait_pos5(300, 180);
    40 static const QPointF grid_portrait_pos6(70, 200);
    40 static const QPointF grid_portrait_pos6(60, 300);
    41 static const QPointF grid_portrait_pos7(180, 200);
    41 static const QPointF grid_portrait_pos7(180, 300);
    42 static const QPointF grid_portrait_pos8(280, 200);
    42 static const QPointF grid_portrait_pos8(300, 300);
    43 
       
    44 // These work with 360x640 resolution
       
    45 static const QPointF expected_label_pos_above_alone(180, 144);
       
    46 static const QPointF expected_label_pos_above_top(180, 132);
       
    47 static const QPointF expected_label_pos_above_bottom(180, 163);
       
    48 static const QPointF expected_label_pos_below_alone(180, 324);
       
    49 static const QPointF expected_label_pos_below_top(180, 312);
       
    50 static const QPointF expected_label_pos_below_bottom(180, 342);
       
    51 
    43 
    52 static const int default_delay(1500);
    44 static const int default_delay(1500);
    53 
    45 
    54 class TestGanesWidgets : public QObject
    46 class TestGanesWidgets : public QObject
    55 {
    47 {
    86     void test_selectionModel();
    78     void test_selectionModel();
    87     void test_scrollTo();
    79     void test_scrollTo();
    88     void test_addItemsCoverFlow();
    80     void test_addItemsCoverFlow();
    89     void test_removeItemsCoverFlow();
    81     void test_removeItemsCoverFlow();
    90     void test_moveItemsCoverFlow();
    82     void test_moveItemsCoverFlow();
    91     void test_labelPositionsCoverFlow();
       
    92     void test_labelFontSpecsCoverFlow();
    83     void test_labelFontSpecsCoverFlow();
    93     void test_resetModelCoverFlow();
    84     void test_resetModelCoverFlow();
    94     void test_resetModelGrid();
    85     void test_resetModelGrid();
    95 
    86 
    96 private:
    87 private:
    97 
    88 
    98     void pan( Qt::Orientation, TBool begin );
    89     void pan( Qt::Orientation, TBool begin );
    99     bool checkLabelAt(HbMainWindow *window, const QPointF &pos, const QString &expectedText);
       
   100     HbLabel *findLabelAt(QGraphicsItem *parent, const QPointF &pos);
       
   101 
    90 
   102 private:
    91 private:
   103 
    92 
   104     HbMainWindow* mWindow;
    93     HbMainWindow* mWindow;
   105     HgWidget* mWidget;
    94     HgWidget* mWidget;
   845 {
   834 {
   846     mWindow = new HbMainWindow;
   835     mWindow = new HbMainWindow;
   847     mWindow->viewport()->grabGesture(Qt::PanGesture);
   836     mWindow->viewport()->grabGesture(Qt::PanGesture);
   848     mWindow->viewport()->grabGesture(Qt::TapGesture); // Add TapAndHoldGesture once it's working
   837     mWindow->viewport()->grabGesture(Qt::TapGesture); // Add TapAndHoldGesture once it's working
   849     mWidget = new HgGrid( Qt::Vertical);
   838     mWidget = new HgGrid( Qt::Vertical);
       
   839     mWidget->setItemSizePolicy(HgWidget::ItemSizeUserDefined);
       
   840     mWidget->setItemSize(QSizeF(120, 120));
   850 
   841 
   851     TestModel model;
   842     TestModel model;
   852     model.generateItems(50);
   843     model.generateItems(50);
   853     mWindow->addView(mWidget);
   844     mWindow->addView(mWidget);
   854     mWidget->setModel(&model);
   845     mWidget->setModel(&model);
  1934 
  1925 
  1935     delete mWindow;
  1926     delete mWindow;
  1936     mWindow = 0;
  1927     mWindow = 0;
  1937 }
  1928 }
  1938 
  1929 
  1939 void TestGanesWidgets::test_labelPositionsCoverFlow()
  1930 void TestGanesWidgets::test_labelFontSpecsCoverFlow()
  1940 {
  1931 {
  1941     mWindow = new HbMainWindow;
  1932     mWindow = new HbMainWindow;
  1942     mMediawall = new HgMediawall();
  1933     mMediawall = new HgMediawall();
  1943 
  1934 
  1944     TestModel model;
  1935     TestModel model;
  1945     model.generateItems(50);
  1936     model.generateItems(50);
  1946     mWindow->addView(mMediawall);
  1937     mWindow->addView(mMediawall);
  1947     mMediawall->setModel(&model);
  1938     mMediawall->setModel(&model);
  1948     mWindow->show();
  1939     mWindow->show();
  1949 
  1940 
  1950     mMediawall->setTitlePosition(HgMediawall::PositionNone);
  1941     mMediawall->setTitleFontSpec(HbFontSpec(HbFontSpec::Primary));
  1951     mMediawall->setDescriptionPosition(HgMediawall::PositionNone);
  1942     QVERIFY(mMediawall->titleFontSpec() == HbFontSpec(HbFontSpec::Primary));
  1952     QVERIFY(mMediawall->titlePosition() == HgMediawall::PositionNone);
  1943 
  1953     QVERIFY(mMediawall->descriptionPosition() == HgMediawall::PositionNone);
  1944     mMediawall->setDescriptionFontSpec(HbFontSpec(HbFontSpec::Secondary));
  1954 
  1945     QVERIFY(mMediawall->descriptionFontSpec() == HbFontSpec(HbFontSpec::Secondary));
  1955     // This updates the title and description label. But how to test they are in correct positions?
  1946 
  1956     mMediawall->setCurrentIndex(model.index(1, 0));
  1947     mMediawall->setTitleFontSpec(HbFontSpec(HbFontSpec::Title));
  1957     QTest::qWait(1000);
  1948     QVERIFY(mMediawall->titleFontSpec() == HbFontSpec(HbFontSpec::Title));
  1958 
  1949 
  1959     mMediawall->setDescriptionPosition(HgMediawall::PositionAboveImage);
  1950     mMediawall->setDescriptionFontSpec(HbFontSpec(HbFontSpec::PrimarySmall));
  1960     QVERIFY(mMediawall->titlePosition() == HgMediawall::PositionNone);
  1951     QVERIFY(mMediawall->descriptionFontSpec() == HbFontSpec(HbFontSpec::PrimarySmall));
  1961     QVERIFY(mMediawall->descriptionPosition() == HgMediawall::PositionAboveImage);
  1952 
  1962     mMediawall->setCurrentIndex(model.index(2, 0));
  1953     QTest::qWait(2000);
  1963     QTest::qWait(1000);
  1954 
  1964     QVERIFY(checkLabelAt(mWindow, expected_label_pos_above_alone, "Secondary 2"));
  1955     delete mWindow;
  1965 
  1956     mWindow = 0;
  1966     mMediawall->setDescriptionPosition(HgMediawall::PositionBelowImage);
  1957 }
  1967     QVERIFY(mMediawall->titlePosition() == HgMediawall::PositionNone);
  1958 
  1968     QVERIFY(mMediawall->descriptionPosition() == HgMediawall::PositionBelowImage);
  1959 void TestGanesWidgets::test_resetModelCoverFlow()
  1969     mMediawall->setCurrentIndex(model.index(3, 0));
       
  1970     QTest::qWait(1000);
       
  1971     QVERIFY(checkLabelAt(mWindow, expected_label_pos_below_alone, "Secondary 3"));
       
  1972 
       
  1973     mMediawall->setTitlePosition(HgMediawall::PositionAboveImage);
       
  1974     QVERIFY(mMediawall->titlePosition() == HgMediawall::PositionAboveImage);
       
  1975     QVERIFY(mMediawall->descriptionPosition() == HgMediawall::PositionBelowImage);
       
  1976     mMediawall->setCurrentIndex(model.index(4, 0));
       
  1977     QTest::qWait(1000);
       
  1978     QVERIFY(checkLabelAt(mWindow, expected_label_pos_above_alone, "Primary 4"));
       
  1979     QVERIFY(checkLabelAt(mWindow, expected_label_pos_below_alone, "Secondary 4"));
       
  1980 
       
  1981     mMediawall->setDescriptionPosition(HgMediawall::PositionAboveImage);
       
  1982     QVERIFY(mMediawall->titlePosition() == HgMediawall::PositionAboveImage);
       
  1983     QVERIFY(mMediawall->descriptionPosition() == HgMediawall::PositionAboveImage);
       
  1984     mMediawall->setCurrentIndex(model.index(5, 0));
       
  1985     QTest::qWait(1000);
       
  1986     QVERIFY(checkLabelAt(mWindow, expected_label_pos_above_top, "Primary 5"));
       
  1987     QVERIFY(checkLabelAt(mWindow, expected_label_pos_above_bottom, "Secondary 5"));
       
  1988 
       
  1989     mMediawall->setDescriptionPosition(HgMediawall::PositionNone);
       
  1990     QVERIFY(mMediawall->titlePosition() == HgMediawall::PositionAboveImage);
       
  1991     QVERIFY(mMediawall->descriptionPosition() == HgMediawall::PositionNone);
       
  1992     mMediawall->setCurrentIndex(model.index(6, 0));
       
  1993     QTest::qWait(1000);
       
  1994     QVERIFY(checkLabelAt(mWindow, expected_label_pos_above_alone, "Primary 6"));
       
  1995 
       
  1996     mMediawall->setTitlePosition(HgMediawall::PositionBelowImage);
       
  1997     QVERIFY(mMediawall->titlePosition() == HgMediawall::PositionBelowImage);
       
  1998     QVERIFY(mMediawall->descriptionPosition() == HgMediawall::PositionNone);
       
  1999     mMediawall->setCurrentIndex(model.index(7, 0));
       
  2000     QTest::qWait(1000);
       
  2001     QVERIFY(checkLabelAt(mWindow, expected_label_pos_below_alone, "Primary 7"));
       
  2002 
       
  2003     mMediawall->setDescriptionPosition(HgMediawall::PositionAboveImage);
       
  2004     QVERIFY(mMediawall->titlePosition() == HgMediawall::PositionBelowImage);
       
  2005     QVERIFY(mMediawall->descriptionPosition() == HgMediawall::PositionAboveImage);
       
  2006     mMediawall->setCurrentIndex(model.index(8, 0));
       
  2007     QTest::qWait(1000);
       
  2008     QVERIFY(checkLabelAt(mWindow, expected_label_pos_above_alone, "Secondary 8"));
       
  2009     QVERIFY(checkLabelAt(mWindow, expected_label_pos_below_alone, "Primary 8"));
       
  2010 
       
  2011     mMediawall->setDescriptionPosition(HgMediawall::PositionBelowImage);
       
  2012     QVERIFY(mMediawall->titlePosition() == HgMediawall::PositionBelowImage);
       
  2013     QVERIFY(mMediawall->descriptionPosition() == HgMediawall::PositionBelowImage);
       
  2014     mMediawall->setCurrentIndex(model.index(9, 0));
       
  2015     QTest::qWait(1000);
       
  2016     QVERIFY(checkLabelAt(mWindow, expected_label_pos_below_top, "Primary 9"));
       
  2017     QVERIFY(checkLabelAt(mWindow, expected_label_pos_below_bottom, "Secondary 9"));
       
  2018 
       
  2019     QTest::qWait(2000);
       
  2020 
       
  2021     delete mWindow;
       
  2022     mWindow = 0;
       
  2023 }
       
  2024 
       
  2025 void TestGanesWidgets::test_labelFontSpecsCoverFlow()
       
  2026 {
  1960 {
  2027     mWindow = new HbMainWindow;
  1961     mWindow = new HbMainWindow;
  2028     mMediawall = new HgMediawall();
  1962     mMediawall = new HgMediawall();
  2029 
  1963 
  2030     TestModel model;
  1964     TestModel model;
  2031     model.generateItems(50);
  1965     model.generateItems(50);
  2032     mWindow->addView(mMediawall);
  1966     mWindow->addView(mMediawall);
  2033     mMediawall->setModel(&model);
  1967     mMediawall->setModel(&model);
  2034     mWindow->show();
  1968     mWindow->show();
  2035 
  1969 
  2036     mMediawall->setTitleFontSpec(HbFontSpec(HbFontSpec::Primary));
       
  2037     QVERIFY(mMediawall->titleFontSpec() == HbFontSpec(HbFontSpec::Primary));
       
  2038 
       
  2039     mMediawall->setDescriptionFontSpec(HbFontSpec(HbFontSpec::Secondary));
       
  2040     QVERIFY(mMediawall->descriptionFontSpec() == HbFontSpec(HbFontSpec::Secondary));
       
  2041 
       
  2042     mMediawall->setTitleFontSpec(HbFontSpec(HbFontSpec::Title));
       
  2043     QVERIFY(mMediawall->titleFontSpec() == HbFontSpec(HbFontSpec::Title));
       
  2044 
       
  2045     mMediawall->setDescriptionFontSpec(HbFontSpec(HbFontSpec::PrimarySmall));
       
  2046     QVERIFY(mMediawall->descriptionFontSpec() == HbFontSpec(HbFontSpec::PrimarySmall));
       
  2047 
       
  2048     QTest::qWait(2000);
       
  2049 
       
  2050     delete mWindow;
       
  2051     mWindow = 0;
       
  2052 }
       
  2053 
       
  2054 void TestGanesWidgets::test_resetModelCoverFlow()
       
  2055 {
       
  2056     mWindow = new HbMainWindow;
       
  2057     mMediawall = new HgMediawall();
       
  2058 
       
  2059     TestModel model;
       
  2060     model.generateItems(50);
       
  2061     mWindow->addView(mMediawall);
       
  2062     mMediawall->setModel(&model);
       
  2063     mWindow->show();
       
  2064 
       
  2065     // Reset with same item count
  1970     // Reset with same item count
  2066     model.reset(50);
  1971     model.reset(50);
  2067     QTest::qWait(2000);
  1972     QTest::qWait(2000);
  2068 
  1973 
  2069     // Reset with smaller item count
  1974     // Reset with smaller item count
  2105     QTest::qWait(2000);
  2010     QTest::qWait(2000);
  2106     
  2011     
  2107     delete mWindow;
  2012     delete mWindow;
  2108     mWindow = 0;
  2013     mWindow = 0;
  2109 
  2014 
  2110 }
       
  2111 
       
  2112 bool TestGanesWidgets::checkLabelAt(HbMainWindow *window, const QPointF &pos, const QString &expectedText)
       
  2113 {
       
  2114     HbLabel *label = findLabelAt(window->currentView(), pos);
       
  2115     if (label) {
       
  2116         if (label->plainText() == expectedText) {
       
  2117             return true;
       
  2118         }
       
  2119         else {
       
  2120             qDebug() << "Label text did not match: expected" << expectedText << "got" << label->plainText();
       
  2121             return false;
       
  2122         }
       
  2123     }
       
  2124     qDebug() << "Label not found at pos" << pos;
       
  2125     return false;
       
  2126 }
       
  2127 
       
  2128 HbLabel *TestGanesWidgets::findLabelAt(QGraphicsItem *parent, const QPointF &pos)
       
  2129 {
       
  2130     QList<QGraphicsItem *> subItems = parent->childItems();
       
  2131     int count = subItems.count();
       
  2132     for (int i = 0; i < count; i++) {
       
  2133         QGraphicsItem *item = subItems.at(i);
       
  2134         QRectF itemRect(item->pos(), item->boundingRect().size());
       
  2135         if (itemRect.contains(pos)) {
       
  2136             HbLabel *label = qgraphicsitem_cast<HbLabel *>(subItems.at(i));
       
  2137             if (label) {
       
  2138                 return label;
       
  2139             }
       
  2140             else {
       
  2141                 return findLabelAt(subItems.at(i), pos);
       
  2142             }
       
  2143         }
       
  2144     }
       
  2145     return NULL;
       
  2146 }
  2015 }
  2147 
  2016 
  2148 #ifdef _UNITTEST_GANESWIDGETS_LOG_TO_C_
  2017 #ifdef _UNITTEST_GANESWIDGETS_LOG_TO_C_
  2149     int main (int argc, char* argv[])
  2018     int main (int argc, char* argv[])
  2150     {
  2019     {