|
1 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // t_watcher.cpp |
|
15 // |
|
16 |
|
17 #include <watcher.h> |
|
18 #include <e32test.h> |
|
19 #include <bautils.h> |
|
20 #include "msvtestutils.h" |
|
21 #include "cwatcher.h" |
|
22 |
|
23 _LIT(KTestFile, "c:\\TESTWATCHER"); |
|
24 const TInt KWaitDelay = 1000000; |
|
25 const TInt KWaitCount = 30; |
|
26 |
|
27 RTest test(_L("Watcher Test Harness")); |
|
28 class CWatcherTestUtils; |
|
29 CWatcherTestUtils* theUtils = NULL; |
|
30 |
|
31 //********************************** |
|
32 // CWatcherTestUtils |
|
33 //********************************** |
|
34 |
|
35 class CWatcherTestUtils : public CTestUtils |
|
36 { |
|
37 public: |
|
38 static CWatcherTestUtils* NewL(RTest& aTest); |
|
39 CWatcherTestUtils(RTest& aTest); |
|
40 void Panic(TInt aPanic); |
|
41 // |
|
42 public: |
|
43 void Start(const TDesC& aDes); |
|
44 void Complete(); |
|
45 // |
|
46 private: |
|
47 TInt iTestNum; |
|
48 }; |
|
49 |
|
50 CWatcherTestUtils* CWatcherTestUtils::NewL(RTest& aTest) |
|
51 { |
|
52 CWatcherTestUtils* self = new(ELeave)CWatcherTestUtils(aTest); |
|
53 CleanupStack::PushL(self); |
|
54 self->ConstructL(); |
|
55 CleanupStack::Pop(); // self |
|
56 return self; |
|
57 } |
|
58 |
|
59 CWatcherTestUtils::CWatcherTestUtils(RTest& aTest) |
|
60 : CTestUtils(aTest) |
|
61 { |
|
62 } |
|
63 |
|
64 void CWatcherTestUtils::Panic(TInt) |
|
65 { |
|
66 test(EFalse); |
|
67 } |
|
68 |
|
69 void CWatcherTestUtils::Start(const TDesC& aDes) |
|
70 { |
|
71 if (iTestNum == 0) |
|
72 test.Start(aDes); |
|
73 else |
|
74 test.Next(aDes); |
|
75 |
|
76 TestStart(++iTestNum, aDes); |
|
77 } |
|
78 |
|
79 void CWatcherTestUtils::Complete() |
|
80 { |
|
81 TestFinish(iTestNum, KErrNone); |
|
82 } |
|
83 |
|
84 LOCAL_C TBool WaitForTestFile() |
|
85 { |
|
86 TInt count = KWaitCount; |
|
87 while(count--) |
|
88 { |
|
89 User::After(KWaitDelay); |
|
90 if (BaflUtils::FileExists(theUtils->FileSession(), KTestFile)) |
|
91 return ETrue; |
|
92 } |
|
93 return EFalse; |
|
94 } |
|
95 |
|
96 void DoTestsL() |
|
97 { |
|
98 // Create waiter |
|
99 CWatcherWait* wait = CWatcherWait::NewLC(); |
|
100 |
|
101 // Create the watcher |
|
102 CWatcher* watcher = CWatcher::NewL(CActive::EPriorityStandard); |
|
103 CleanupStack::PushL(watcher); |
|
104 |
|
105 theUtils->Start(_L("Watcher startup")); |
|
106 theUtils->FileSession().Delete(KTestFile); |
|
107 wait->Start(); |
|
108 watcher->Start(wait->iStatus); |
|
109 test(WaitForTestFile()); |
|
110 theUtils->Complete(); |
|
111 |
|
112 theUtils->Start(_L("Watcher death")); |
|
113 watcher->Cancel(); |
|
114 CActiveScheduler::Start(); |
|
115 test(wait->iStatus.Int() == KErrCancel); |
|
116 theUtils->Complete(); |
|
117 |
|
118 theUtils->Start(_L("Watcher restart")); |
|
119 test(theUtils->FileSession().Delete(KTestFile) == KErrNone); |
|
120 wait->Start(); |
|
121 watcher->Start(wait->iStatus); |
|
122 test(WaitForTestFile()); |
|
123 theUtils->Complete(); |
|
124 |
|
125 // Close the watcher |
|
126 theUtils->Start(_L("Closing watchers")); |
|
127 watcher->Cancel(); |
|
128 CActiveScheduler::Start(); |
|
129 test(wait->iStatus.Int() == KErrCancel); |
|
130 theUtils->Complete(); |
|
131 |
|
132 CleanupStack::PopAndDestroy(2); // watcher, wait |
|
133 } |
|
134 |
|
135 void SetupL() |
|
136 { |
|
137 CActiveScheduler::Install(new(ELeave)CActiveScheduler); |
|
138 theUtils = CWatcherTestUtils::NewL(test); |
|
139 } |
|
140 |
|
141 void Close(TInt aRet) |
|
142 { |
|
143 if (theUtils) |
|
144 { |
|
145 if (!aRet) |
|
146 theUtils->TestHarnessCompleted(); |
|
147 else |
|
148 theUtils->TestHarnessFailed(aRet); |
|
149 } |
|
150 test(aRet == KErrNone); |
|
151 |
|
152 delete theUtils; |
|
153 delete CActiveScheduler::Current(); |
|
154 } |
|
155 |
|
156 void DoMainL() |
|
157 { |
|
158 SetupL(); |
|
159 DoTestsL(); |
|
160 } |
|
161 |
|
162 GLDEF_C TInt E32Main() |
|
163 { |
|
164 __UHEAP_MARK; |
|
165 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
166 TRAPD(ret, DoMainL()); |
|
167 Close(ret); |
|
168 test.Close(); |
|
169 delete cleanup; |
|
170 __UHEAP_MARKEND; |
|
171 return(KErrNone); |
|
172 } |
|
173 |
|
174 EXPORT_C TInt WinsMain() |
|
175 { |
|
176 #if defined(__WINS__) |
|
177 E32Main(); |
|
178 #endif |
|
179 return KErrNone; |
|
180 } |
|
181 |