notes/tsrc/unittest_notesmodel/src/unittest_notesmodel.cpp
changeset 49 5de72ea7a065
child 55 2c54b51f39c4
equal deleted inserted replaced
37:360d55486d7f 49:5de72ea7a065
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 * NotesModel unit-test class.
       
    16 *
       
    17 */
       
    18 
       
    19 // System includes
       
    20 #include <QtTest/QtTest>
       
    21 #include <QtGui>
       
    22 #include <QPointer>
       
    23 #include <e32base.h>
       
    24 
       
    25 // User includes
       
    26 #include "notesmodel.h"
       
    27 #include "agendautil.h"
       
    28 
       
    29 class TestNotesModel : public QObject
       
    30 {
       
    31 	Q_OBJECT
       
    32 
       
    33 public slots:
       
    34 	void handleEntriesDeletion(int var);
       
    35 
       
    36 private slots:
       
    37 	// Test case setup.
       
    38 	void initTestCase();
       
    39 	void cleanupTestCase();
       
    40 
       
    41 	// Test cases.
       
    42 	void test_construction();
       
    43 	void test_sourceModelAddNote();
       
    44 	void test_sourceModelDeleteNote();
       
    45 	void test_sourceModelUpdateNote();
       
    46 
       
    47 private:
       
    48 	QPointer<NotesModel> mNotesModel;
       
    49 	QPointer<AgendaUtil> mAgendaUtil;
       
    50 
       
    51 	QDateTime mStart;
       
    52 	QDateTime mEnd;
       
    53 
       
    54 	CActiveSchedulerWait *mWait;
       
    55 };
       
    56 
       
    57 /*!
       
    58 	\class TestNotesModel
       
    59 
       
    60 	This is unittest class for NotesModel apis.
       
    61  */
       
    62 
       
    63 /*!
       
    64 	Slot called when all the entries have been deleted.
       
    65  */
       
    66 void TestNotesModel::handleEntriesDeletion(int var)
       
    67 {
       
    68 	Q_UNUSED(var)
       
    69 
       
    70 	if (mWait && mWait->IsStarted()) {
       
    71 		mWait->AsyncStop();
       
    72 	}
       
    73 }
       
    74 
       
    75 /*!
       
    76 	This function is called to init the testcase.
       
    77 	Called for every test case execution.
       
    78  */
       
    79 void TestNotesModel::initTestCase()
       
    80 {
       
    81 	QT_TRAP_THROWING(mAgendaUtil = new AgendaUtil(this););
       
    82 	QVERIFY(mAgendaUtil != 0);
       
    83 
       
    84 	// Connect to the required signals
       
    85 	connect(
       
    86 			mAgendaUtil, SIGNAL(entriesDeleted(int)),
       
    87 			this, SLOT(handleEntriesDeletion(int)));
       
    88 
       
    89 	mWait = 0;
       
    90 
       
    91 	mStart.setDate(QDate(1900, 1, 1));
       
    92 	mStart.setTime(QTime(0, 0, 0));
       
    93 	mEnd.setDate(QDate(2100, 30, 12));
       
    94 	mEnd.setTime(QTime(23, 59, 0));
       
    95 }
       
    96 
       
    97 /*!
       
    98 	This function is called when the testcase execution is
       
    99 	completed, for cleaning up.
       
   100  */
       
   101 void TestNotesModel::cleanupTestCase()
       
   102 {
       
   103 	if (mAgendaUtil) {
       
   104 		delete mAgendaUtil;
       
   105 	}
       
   106 	if (mNotesModel) {
       
   107 		delete mNotesModel;
       
   108 	}
       
   109 	if (mWait && mWait->IsStarted()) {
       
   110 		mWait->AsyncStop();
       
   111 	}
       
   112 	if (mWait) {
       
   113 		delete mWait;
       
   114 	}
       
   115 }
       
   116 
       
   117 /*!
       
   118 	This test functions tests the construction of NotesModel object.
       
   119  */
       
   120 void TestNotesModel::test_construction()
       
   121 {
       
   122 	mNotesModel = new NotesModel(mAgendaUtil, this);
       
   123 	QVERIFY(mNotesModel);
       
   124 
       
   125 	if (mNotesModel) {
       
   126 		delete mNotesModel;
       
   127 		QVERIFY(!mNotesModel);
       
   128 	}
       
   129 }
       
   130 
       
   131 /*!
       
   132 	This test function tests if the source model is updated when a note is
       
   133 	added.
       
   134  */
       
   135 void TestNotesModel::test_sourceModelAddNote()
       
   136 {
       
   137 	mAgendaUtil->deleteEntries(mStart, mEnd, AgendaUtil::IncludeAll);
       
   138 	if (!mWait) {
       
   139 		mWait =  new CActiveSchedulerWait;
       
   140 	}
       
   141 	if (!mWait->IsStarted()) {
       
   142 		mWait->Start();
       
   143 	}
       
   144 	mNotesModel = new NotesModel(mAgendaUtil, this);
       
   145 	QVERIFY(mNotesModel);
       
   146 
       
   147 	QAbstractItemModel *model = 0;
       
   148 	model = mNotesModel->sourceModel();
       
   149 	QVERIFY(model);
       
   150 	QVERIFY(!model->rowCount());
       
   151 
       
   152 	// Now save a note.
       
   153 	AgendaEntry entry;
       
   154 	entry.setType(AgendaEntry::TypeNote);
       
   155 	entry.setDescription("Hello");
       
   156 	entry.setLastModifiedDateTime(
       
   157 			QDateTime(QDate::currentDate(), QTime::currentTime()));
       
   158 	QVERIFY(mAgendaUtil->addEntry(entry));
       
   159 	
       
   160 	// Now check the count in the model.
       
   161 	startTimer(1000);
       
   162 	QVERIFY(model->rowCount());
       
   163 	QVERIFY(model->rowCount() == 1);
       
   164 }
       
   165 
       
   166 /*!
       
   167 	This test function tests if the source model is updated when a note is
       
   168 	deleted.
       
   169  */
       
   170 void TestNotesModel::test_sourceModelDeleteNote()
       
   171 {
       
   172 	mAgendaUtil->deleteEntries(mStart, mEnd, AgendaUtil::IncludeAll);
       
   173 	if (!mWait) {
       
   174 		mWait =  new CActiveSchedulerWait;
       
   175 	}
       
   176 	if (!mWait->IsStarted()) {
       
   177 		mWait->Start();
       
   178 	}
       
   179 	mNotesModel = new NotesModel(mAgendaUtil, this);
       
   180 	QVERIFY(mNotesModel);
       
   181 
       
   182 	QAbstractItemModel *model = 0;
       
   183 	model = mNotesModel->sourceModel();
       
   184 	QVERIFY(model);
       
   185 	QVERIFY(!model->rowCount());
       
   186 
       
   187 	// Now save a note.
       
   188 	AgendaEntry entry;
       
   189 	entry.setType(AgendaEntry::TypeNote);
       
   190 	entry.setDescription("Hello");
       
   191 	entry.setLastModifiedDateTime(
       
   192 			QDateTime(QDate::currentDate(), QTime::currentTime()));
       
   193 	ulong id;
       
   194 	QVERIFY(id = mAgendaUtil->addEntry(entry));
       
   195 
       
   196 	// Now check the count in the model.
       
   197 	startTimer(1000);
       
   198 	QVERIFY(model->rowCount());
       
   199 	QVERIFY(model->rowCount() == 1);
       
   200 
       
   201 	// Verify if the model has the updated content.
       
   202 	startTimer(1000);
       
   203 	QModelIndex index = model->index(0, 0);
       
   204 	QVERIFY(index.isValid());
       
   205 	QStringList list = index.data(Qt::DisplayRole).value<QStringList>();
       
   206 	QCOMPARE(entry.description(), list.at(0));
       
   207 
       
   208 	// Delete the note saved above.
       
   209 	QVERIFY(mAgendaUtil->deleteEntry(id));
       
   210 	// Now check the count in the model.
       
   211 	startTimer(1000);
       
   212 	QVERIFY(!model->rowCount());
       
   213 }
       
   214 
       
   215 /*!
       
   216 	This test function tests if the source model is updated when a note is
       
   217 	modified.
       
   218  */
       
   219 void TestNotesModel::test_sourceModelUpdateNote()
       
   220 {
       
   221 	mAgendaUtil->deleteEntries(mStart, mEnd, AgendaUtil::IncludeAll);
       
   222 	if (!mWait) {
       
   223 		mWait =  new CActiveSchedulerWait;
       
   224 	}
       
   225 	if (!mWait->IsStarted()) {
       
   226 		mWait->Start();
       
   227 	}
       
   228 	mNotesModel = new NotesModel(mAgendaUtil, this);
       
   229 	QVERIFY(mNotesModel);
       
   230 
       
   231 	QAbstractItemModel *model = 0;
       
   232 	model = mNotesModel->sourceModel();
       
   233 	QVERIFY(model);
       
   234 	QVERIFY(!model->rowCount());
       
   235 
       
   236 	// Now save a note.
       
   237 	AgendaEntry entry;
       
   238 	entry.setType(AgendaEntry::TypeNote);
       
   239 	entry.setDescription("Hello");
       
   240 	entry.setLastModifiedDateTime(
       
   241 			QDateTime(QDate::currentDate(), QTime::currentTime()));
       
   242 	ulong id;
       
   243 	QVERIFY(id = mAgendaUtil->addEntry(entry));
       
   244 	// Now check the count in the model.
       
   245 	startTimer(1000);
       
   246 	QVERIFY(model->rowCount());
       
   247 	QVERIFY(model->rowCount() == 1);
       
   248 
       
   249 	// Now update the content on of the note created above.
       
   250 	entry = mAgendaUtil->fetchById(id);
       
   251 	QVERIFY(!entry.isNull());
       
   252 	entry.setDescription("Hello world");
       
   253 	QVERIFY(mAgendaUtil->updateEntry(entry));
       
   254 
       
   255 	// Verify if the model has the updated content.
       
   256 	startTimer(1000);
       
   257 	QModelIndex index = model->index(0, 0);
       
   258 	QVERIFY(index.isValid());
       
   259 	QStringList list = index.data(Qt::DisplayRole).value<QStringList>();
       
   260 	QCOMPARE(entry.description(), list.at(0));
       
   261 
       
   262 	// Delete the note saved above.
       
   263 	QVERIFY(mAgendaUtil->deleteEntry(id));
       
   264 	// Now check the count in the model.
       
   265 	startTimer(1000);
       
   266 	QVERIFY(!model->rowCount());
       
   267 }
       
   268 
       
   269 QTEST_MAIN(TestNotesModel)
       
   270 #include "unittest_notesmodel.moc"
       
   271 
       
   272 // End of file	--Don't remove this.