| 1 | /** |
| 2 | * Copyright (C) 2006 Brad Hards <bradh@frogmouth.net> |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | #include "filewatchunittest.h" |
| 26 | |
| 27 | #include <QSignalSpy> |
| 28 | #include <QTemporaryFile> |
| 29 | |
| 30 | #ifdef QT_STATICPLUGIN |
| 31 | #include "import_plugins.h" |
| 32 | #endif |
| 33 | |
| 34 | void FileWatchUnitTest::initTestCase() |
| 35 | { |
| 36 | m_init = new QCA::Initializer; |
| 37 | } |
| 38 | |
| 39 | void FileWatchUnitTest::cleanupTestCase() |
| 40 | { |
| 41 | delete m_init; |
| 42 | } |
| 43 | |
| 44 | void FileWatchUnitTest::filewatchTest() |
| 45 | { |
| 46 | QWARN("Unittest will take about 10 seconds. Please wait." ); |
| 47 | |
| 48 | QCA::FileWatch watcher; |
| 49 | QCOMPARE(watcher.fileName(), QString()); |
| 50 | |
| 51 | QSignalSpy spy(&watcher, &QCA::FileWatch::changed); |
| 52 | QVERIFY(spy.isValid()); |
| 53 | QCOMPARE(spy.count(), 0); |
| 54 | |
| 55 | QTemporaryFile *tempFile = new QTemporaryFile; |
| 56 | |
| 57 | tempFile->open(); |
| 58 | |
| 59 | watcher.setFileName(tempFile->fileName()); |
| 60 | QCOMPARE(watcher.fileName(), tempFile->fileName()); |
| 61 | QVERIFY(!spy.wait(2000)); |
| 62 | QCOMPARE(spy.count(), 0); |
| 63 | tempFile->close(); |
| 64 | QVERIFY(!spy.wait(2000)); |
| 65 | QCOMPARE(spy.count(), 0); |
| 66 | |
| 67 | tempFile->open(); |
| 68 | tempFile->write(data: "foo" ); |
| 69 | tempFile->flush(); |
| 70 | QVERIFY(spy.wait(2000)); |
| 71 | QCOMPARE(spy.count(), 1); |
| 72 | |
| 73 | tempFile->close(); |
| 74 | QVERIFY(!spy.wait(2000)); |
| 75 | QCOMPARE(spy.count(), 1); |
| 76 | |
| 77 | tempFile->open(); |
| 78 | tempFile->write(data: "foo" ); |
| 79 | tempFile->flush(); |
| 80 | QVERIFY(spy.wait(2000)); |
| 81 | QCOMPARE(spy.count(), 2); |
| 82 | |
| 83 | tempFile->write(data: "bar" ); |
| 84 | tempFile->flush(); |
| 85 | QVERIFY(spy.wait(2000)); |
| 86 | QCOMPARE(spy.count(), 3); |
| 87 | |
| 88 | tempFile->close(); |
| 89 | QVERIFY(!spy.wait(2000)); |
| 90 | |
| 91 | QCOMPARE(spy.count(), 3); |
| 92 | |
| 93 | delete tempFile; |
| 94 | QVERIFY(spy.wait(2000)); |
| 95 | QCOMPARE(spy.count(), 4); |
| 96 | } |
| 97 | |
| 98 | QTEST_MAIN(FileWatchUnitTest) |
| 99 | |