|
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 file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #include "VolumeServiceTest.h" |
|
6 |
|
7 #include "base/message_loop.h" |
|
8 #include "nsCOMPtr.h" |
|
9 #include "nsIObserver.h" |
|
10 #include "nsIObserverService.h" |
|
11 #include "nsServiceManagerUtils.h" |
|
12 #include "nsThreadUtils.h" |
|
13 #include "nsIVolume.h" |
|
14 #include "nsIVolumeService.h" |
|
15 #include "nsIVolumeStat.h" |
|
16 #include "nsXULAppAPI.h" |
|
17 |
|
18 #include "mozilla/Services.h" |
|
19 |
|
20 #define VOLUME_MANAGER_LOG_TAG "VolumeServiceTest" |
|
21 #include "VolumeManagerLog.h" |
|
22 |
|
23 using namespace mozilla::services; |
|
24 |
|
25 namespace mozilla { |
|
26 namespace system { |
|
27 |
|
28 #define TEST_NSVOLUME_OBSERVER 0 |
|
29 |
|
30 #if TEST_NSVOLUME_OBSERVER |
|
31 |
|
32 /*************************************************************************** |
|
33 * A test class to verify that the Observer stuff is working properly. |
|
34 */ |
|
35 class VolumeTestObserver : public nsIObserver |
|
36 { |
|
37 public: |
|
38 NS_DECL_ISUPPORTS |
|
39 NS_DECL_NSIOBSERVER |
|
40 |
|
41 VolumeTestObserver() |
|
42 { |
|
43 nsCOMPtr<nsIObserverService> obs = GetObserverService(); |
|
44 if (!obs) { |
|
45 return; |
|
46 } |
|
47 obs->AddObserver(this, NS_VOLUME_STATE_CHANGED, false); |
|
48 } |
|
49 ~VolumeTestObserver() |
|
50 { |
|
51 nsCOMPtr<nsIObserverService> obs = GetObserverService(); |
|
52 if (!obs) { |
|
53 return; |
|
54 } |
|
55 obs->RemoveObserver(this, NS_VOLUME_STATE_CHANGED); |
|
56 } |
|
57 |
|
58 void LogVolume(nsIVolume* vol) |
|
59 { |
|
60 nsString volName; |
|
61 nsString mountPoint; |
|
62 int32_t volState; |
|
63 |
|
64 vol->GetName(volName); |
|
65 vol->GetMountPoint(mountPoint); |
|
66 vol->GetState(&volState); |
|
67 |
|
68 LOG(" Volume: %s MountPoint: %s State: %s", |
|
69 NS_LossyConvertUTF16toASCII(volName).get(), |
|
70 NS_LossyConvertUTF16toASCII(mountPoint).get(), |
|
71 NS_VolumeStateStr(volState)); |
|
72 |
|
73 nsCOMPtr<nsIVolumeStat> stat; |
|
74 nsresult rv = vol->GetStats(getter_AddRefs(stat)); |
|
75 if (NS_SUCCEEDED(rv)) { |
|
76 int64_t totalBytes; |
|
77 int64_t freeBytes; |
|
78 |
|
79 stat->GetTotalBytes(&totalBytes); |
|
80 stat->GetFreeBytes(&freeBytes); |
|
81 |
|
82 LOG(" Total Space: %llu Mb Free Bytes: %llu Mb", |
|
83 totalBytes / (1024LL * 1024LL), freeBytes / (1024LL * 1024LL)); |
|
84 } |
|
85 else { |
|
86 LOG(" Unable to retrieve stats"); |
|
87 } |
|
88 } |
|
89 }; |
|
90 static nsCOMPtr<VolumeTestObserver> sTestObserver; |
|
91 |
|
92 NS_IMPL_ISUPPORTS(VolumeTestObserver, nsIObserver) |
|
93 |
|
94 NS_IMETHODIMP |
|
95 VolumeTestObserver::Observe(nsISupports* aSubject, |
|
96 const char* aTopic, |
|
97 const char16_t* aData) |
|
98 { |
|
99 LOG("TestObserver: topic: %s", aTopic); |
|
100 |
|
101 if (strcmp(aTopic, NS_VOLUME_STATE_CHANGED) != 0) { |
|
102 return NS_OK; |
|
103 } |
|
104 nsCOMPtr<nsIVolume> vol = do_QueryInterface(aSubject); |
|
105 if (vol) { |
|
106 LogVolume(vol); |
|
107 } |
|
108 |
|
109 // Since this observe method was called then we know that the service |
|
110 // has been initialized so we can do the VolumeService tests. |
|
111 |
|
112 nsCOMPtr<nsIVolumeService> vs = do_GetService(NS_VOLUMESERVICE_CONTRACTID); |
|
113 if (!vs) { |
|
114 ERR("do_GetService('%s') failed", NS_VOLUMESERVICE_CONTRACTID); |
|
115 return NS_ERROR_FAILURE; |
|
116 } |
|
117 |
|
118 nsresult rv = vs->GetVolumeByName(NS_LITERAL_STRING("sdcard"), getter_AddRefs(vol)); |
|
119 if (NS_SUCCEEDED(rv)) { |
|
120 LOG("GetVolumeByName( 'sdcard' ) succeeded (expected)"); |
|
121 LogVolume(vol); |
|
122 } else { |
|
123 ERR("GetVolumeByName( 'sdcard' ) failed (unexpected)"); |
|
124 } |
|
125 |
|
126 rv = vs->GetVolumeByName(NS_LITERAL_STRING("foo"), getter_AddRefs(vol)); |
|
127 if (NS_SUCCEEDED(rv)) { |
|
128 ERR("GetVolumeByName( 'foo' ) succeeded (unexpected)"); |
|
129 } else { |
|
130 LOG("GetVolumeByName( 'foo' ) failed (expected)"); |
|
131 } |
|
132 |
|
133 rv = vs->GetVolumeByPath(NS_LITERAL_STRING("/mnt/sdcard"), getter_AddRefs(vol)); |
|
134 if (NS_SUCCEEDED(rv)) { |
|
135 LOG("GetVolumeByPath( '/mnt/sdcard' ) succeeded (expected)"); |
|
136 LogVolume(vol); |
|
137 } else { |
|
138 ERR("GetVolumeByPath( '/mnt/sdcard' ) failed (unexpected"); |
|
139 } |
|
140 |
|
141 rv = vs->GetVolumeByPath(NS_LITERAL_STRING("/mnt/sdcard/foo"), getter_AddRefs(vol)); |
|
142 if (NS_SUCCEEDED(rv)) { |
|
143 LOG("GetVolumeByPath( '/mnt/sdcard/foo' ) succeeded (expected)"); |
|
144 LogVolume(vol); |
|
145 } else { |
|
146 LOG("GetVolumeByPath( '/mnt/sdcard/foo' ) failed (unexpected)"); |
|
147 } |
|
148 |
|
149 rv = vs->GetVolumeByPath(NS_LITERAL_STRING("/mnt/sdcardfoo"), getter_AddRefs(vol)); |
|
150 if (NS_SUCCEEDED(rv)) { |
|
151 ERR("GetVolumeByPath( '/mnt/sdcardfoo' ) succeeded (unexpected)"); |
|
152 } else { |
|
153 LOG("GetVolumeByPath( '/mnt/sdcardfoo' ) failed (expected)"); |
|
154 } |
|
155 |
|
156 return NS_OK; |
|
157 } |
|
158 |
|
159 class InitVolumeServiceTestIO : public nsRunnable |
|
160 { |
|
161 public: |
|
162 NS_IMETHOD Run() |
|
163 { |
|
164 MOZ_ASSERT(NS_IsMainThread()); |
|
165 |
|
166 DBG("InitVolumeServiceTest called"); |
|
167 nsCOMPtr<nsIVolumeService> vs = do_GetService(NS_VOLUMESERVICE_CONTRACTID); |
|
168 if (!vs) { |
|
169 ERR("do_GetService('%s') failed", NS_VOLUMESERVICE_CONTRACTID); |
|
170 return NS_ERROR_FAILURE; |
|
171 } |
|
172 sTestObserver = new VolumeTestObserver(); |
|
173 |
|
174 return NS_OK; |
|
175 } |
|
176 }; |
|
177 #endif // TEST_NSVOLUME_OBSERVER |
|
178 |
|
179 void |
|
180 InitVolumeServiceTestIOThread() |
|
181 { |
|
182 MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop()); |
|
183 |
|
184 #if TEST_NSVOLUME_OBSERVER |
|
185 // Now that the volume manager is initialized we can go |
|
186 // ahead and do our test (on main thread). |
|
187 NS_DispatchToMainThread(new InitVolumeServiceTestIO()); |
|
188 #endif |
|
189 } |
|
190 |
|
191 void |
|
192 ShutdownVolumeServiceTest() |
|
193 { |
|
194 #if TEST_NSVOLUME_OBSERVER |
|
195 DBG("ShutdownVolumeServiceTestIOThread called"); |
|
196 sTestObserver = nullptr; |
|
197 #endif |
|
198 } |
|
199 |
|
200 } // system |
|
201 } // mozilla |