|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "TestCommon.h" |
|
6 #include "nsXPCOM.h" |
|
7 #include "nsIServiceManager.h" |
|
8 #include "nsServiceManagerUtils.h" |
|
9 #include "nsIEventTarget.h" |
|
10 #include "nsCOMPtr.h" |
|
11 #include "nsNetCID.h" |
|
12 #include "prlog.h" |
|
13 |
|
14 #if defined(PR_LOGGING) |
|
15 // |
|
16 // set NSPR_LOG_MODULES=Test:5 |
|
17 // |
|
18 static PRLogModuleInfo *gTestLog = nullptr; |
|
19 #endif |
|
20 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args) |
|
21 |
|
22 class nsIOEvent : public nsIRunnable { |
|
23 public: |
|
24 NS_DECL_THREADSAFE_ISUPPORTS |
|
25 |
|
26 nsIOEvent(int i) : mIndex(i) {} |
|
27 |
|
28 NS_IMETHOD Run() { |
|
29 LOG(("Run [%d]\n", mIndex)); |
|
30 return NS_OK; |
|
31 } |
|
32 |
|
33 private: |
|
34 int mIndex; |
|
35 }; |
|
36 NS_IMPL_ISUPPORTS(nsIOEvent, nsIRunnable) |
|
37 |
|
38 static nsresult RunTest() |
|
39 { |
|
40 nsresult rv; |
|
41 nsCOMPtr<nsIEventTarget> target = |
|
42 do_GetService(NS_STREAMTRANSPORTSERVICE_CONTRACTID, &rv); |
|
43 if (NS_FAILED(rv)) |
|
44 return rv; |
|
45 |
|
46 for (int i=0; i<10; ++i) { |
|
47 nsCOMPtr<nsIRunnable> event = new nsIOEvent(i); |
|
48 LOG(("Dispatch %d\n", i)); |
|
49 target->Dispatch(event, NS_DISPATCH_NORMAL); |
|
50 } |
|
51 |
|
52 return NS_OK; |
|
53 } |
|
54 |
|
55 int main(int argc, char **argv) |
|
56 { |
|
57 if (test_common_init(&argc, &argv) != 0) |
|
58 return -1; |
|
59 |
|
60 nsresult rv; |
|
61 |
|
62 #if defined(PR_LOGGING) |
|
63 gTestLog = PR_NewLogModule("Test"); |
|
64 #endif |
|
65 |
|
66 rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); |
|
67 if (NS_FAILED(rv)) |
|
68 return rv; |
|
69 |
|
70 rv = RunTest(); |
|
71 if (NS_FAILED(rv)) |
|
72 LOG(("RunTest failed [rv=%x]\n", rv)); |
|
73 |
|
74 LOG(("sleeping main thread for 2 seconds...\n")); |
|
75 PR_Sleep(PR_SecondsToInterval(2)); |
|
76 |
|
77 NS_ShutdownXPCOM(nullptr); |
|
78 return 0; |
|
79 } |