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 | #ifdef QT_STATICPLUGIN |
28 | #include "import_plugins.h" |
29 | #endif |
30 | |
31 | void FileWatchUnitTest::initTestCase() |
32 | { |
33 | m_init = new QCA::Initializer; |
34 | } |
35 | |
36 | void FileWatchUnitTest::cleanupTestCase() |
37 | { |
38 | delete m_init; |
39 | } |
40 | |
41 | void FileWatchUnitTest::filewatchTest() |
42 | { |
43 | QWARN("Unittest will take about 10 seconds. Please wait." ); |
44 | |
45 | QCA::FileWatch watcher; |
46 | QCOMPARE(watcher.fileName(), QString()); |
47 | |
48 | QSignalSpy spy(&watcher, &QCA::FileWatch::changed); |
49 | QVERIFY(spy.isValid()); |
50 | QCOMPARE(spy.count(), 0); |
51 | |
52 | QTemporaryFile *tempFile = new QTemporaryFile; |
53 | |
54 | tempFile->open(); |
55 | |
56 | watcher.setFileName(tempFile->fileName()); |
57 | QCOMPARE(watcher.fileName(), tempFile->fileName()); |
58 | QVERIFY(!spy.wait(2000)); |
59 | QCOMPARE(spy.count(), 0); |
60 | tempFile->close(); |
61 | QVERIFY(!spy.wait(2000)); |
62 | QCOMPARE(spy.count(), 0); |
63 | |
64 | tempFile->open(); |
65 | tempFile->write(data: "foo" ); |
66 | tempFile->flush(); |
67 | QVERIFY(spy.wait(2000)); |
68 | QCOMPARE(spy.count(), 1); |
69 | |
70 | tempFile->close(); |
71 | QVERIFY(!spy.wait(2000)); |
72 | QCOMPARE(spy.count(), 1); |
73 | |
74 | tempFile->open(); |
75 | tempFile->write(data: "foo" ); |
76 | tempFile->flush(); |
77 | QVERIFY(spy.wait(2000)); |
78 | QCOMPARE(spy.count(), 2); |
79 | |
80 | tempFile->write(data: "bar" ); |
81 | tempFile->flush(); |
82 | QVERIFY(spy.wait(2000)); |
83 | QCOMPARE(spy.count(), 3); |
84 | |
85 | tempFile->close(); |
86 | QVERIFY(!spy.wait(2000)); |
87 | |
88 | QCOMPARE(spy.count(), 3); |
89 | |
90 | delete tempFile; |
91 | QVERIFY(spy.wait(2000)); |
92 | QCOMPARE(spy.count(), 4); |
93 | } |
94 | |
95 | QTEST_MAIN(FileWatchUnitTest) |
96 | |