dom/system/gonk/VolumeServiceTest.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial