File indexing completed on 2024-04-28 04:44:08
0001 /** 0002 * Copyright (C) 2006 Brad Hards <bradh@frogmouth.net> 0003 * 0004 * Redistribution and use in source and binary forms, with or without 0005 * modification, are permitted provided that the following conditions 0006 * are met: 0007 * 0008 * 1. Redistributions of source code must retain the above copyright 0009 * notice, this list of conditions and the following disclaimer. 0010 * 2. Redistributions in binary form must reproduce the above copyright 0011 * notice, this list of conditions and the following disclaimer in the 0012 * documentation and/or other materials provided with the distribution. 0013 * 0014 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 0015 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 0016 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 0017 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 0018 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 0019 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 0020 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 0021 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 0022 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 0023 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 0024 */ 0025 #include "filewatchunittest.h" 0026 0027 #ifdef QT_STATICPLUGIN 0028 #include "import_plugins.h" 0029 #endif 0030 0031 void FileWatchUnitTest::initTestCase() 0032 { 0033 m_init = new QCA::Initializer; 0034 } 0035 0036 void FileWatchUnitTest::cleanupTestCase() 0037 { 0038 delete m_init; 0039 } 0040 0041 void FileWatchUnitTest::filewatchTest() 0042 { 0043 QWARN("Unittest will take about 10 seconds. Please wait."); 0044 0045 QCA::FileWatch watcher; 0046 QCOMPARE(watcher.fileName(), QString()); 0047 0048 QSignalSpy spy(&watcher, &QCA::FileWatch::changed); 0049 QVERIFY(spy.isValid()); 0050 QCOMPARE(spy.count(), 0); 0051 0052 QTemporaryFile *tempFile = new QTemporaryFile; 0053 0054 tempFile->open(); 0055 0056 watcher.setFileName(tempFile->fileName()); 0057 QCOMPARE(watcher.fileName(), tempFile->fileName()); 0058 QVERIFY(!spy.wait(2000)); 0059 QCOMPARE(spy.count(), 0); 0060 tempFile->close(); 0061 QVERIFY(!spy.wait(2000)); 0062 QCOMPARE(spy.count(), 0); 0063 0064 tempFile->open(); 0065 tempFile->write("foo"); 0066 tempFile->flush(); 0067 QVERIFY(spy.wait(2000)); 0068 QCOMPARE(spy.count(), 1); 0069 0070 tempFile->close(); 0071 QVERIFY(!spy.wait(2000)); 0072 QCOMPARE(spy.count(), 1); 0073 0074 tempFile->open(); 0075 tempFile->write("foo"); 0076 tempFile->flush(); 0077 QVERIFY(spy.wait(2000)); 0078 QCOMPARE(spy.count(), 2); 0079 0080 tempFile->write("bar"); 0081 tempFile->flush(); 0082 QVERIFY(spy.wait(2000)); 0083 QCOMPARE(spy.count(), 3); 0084 0085 tempFile->close(); 0086 QVERIFY(!spy.wait(2000)); 0087 0088 QCOMPARE(spy.count(), 3); 0089 0090 delete tempFile; 0091 QVERIFY(spy.wait(2000)); 0092 QCOMPARE(spy.count(), 4); 0093 } 0094 0095 QTEST_MAIN(FileWatchUnitTest)