michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "VolumeServiceTest.h" michael@0: michael@0: #include "base/message_loop.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsIObserver.h" michael@0: #include "nsIObserverService.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "nsThreadUtils.h" michael@0: #include "nsIVolume.h" michael@0: #include "nsIVolumeService.h" michael@0: #include "nsIVolumeStat.h" michael@0: #include "nsXULAppAPI.h" michael@0: michael@0: #include "mozilla/Services.h" michael@0: michael@0: #define VOLUME_MANAGER_LOG_TAG "VolumeServiceTest" michael@0: #include "VolumeManagerLog.h" michael@0: michael@0: using namespace mozilla::services; michael@0: michael@0: namespace mozilla { michael@0: namespace system { michael@0: michael@0: #define TEST_NSVOLUME_OBSERVER 0 michael@0: michael@0: #if TEST_NSVOLUME_OBSERVER michael@0: michael@0: /*************************************************************************** michael@0: * A test class to verify that the Observer stuff is working properly. michael@0: */ michael@0: class VolumeTestObserver : public nsIObserver michael@0: { michael@0: public: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSIOBSERVER michael@0: michael@0: VolumeTestObserver() michael@0: { michael@0: nsCOMPtr obs = GetObserverService(); michael@0: if (!obs) { michael@0: return; michael@0: } michael@0: obs->AddObserver(this, NS_VOLUME_STATE_CHANGED, false); michael@0: } michael@0: ~VolumeTestObserver() michael@0: { michael@0: nsCOMPtr obs = GetObserverService(); michael@0: if (!obs) { michael@0: return; michael@0: } michael@0: obs->RemoveObserver(this, NS_VOLUME_STATE_CHANGED); michael@0: } michael@0: michael@0: void LogVolume(nsIVolume* vol) michael@0: { michael@0: nsString volName; michael@0: nsString mountPoint; michael@0: int32_t volState; michael@0: michael@0: vol->GetName(volName); michael@0: vol->GetMountPoint(mountPoint); michael@0: vol->GetState(&volState); michael@0: michael@0: LOG(" Volume: %s MountPoint: %s State: %s", michael@0: NS_LossyConvertUTF16toASCII(volName).get(), michael@0: NS_LossyConvertUTF16toASCII(mountPoint).get(), michael@0: NS_VolumeStateStr(volState)); michael@0: michael@0: nsCOMPtr stat; michael@0: nsresult rv = vol->GetStats(getter_AddRefs(stat)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: int64_t totalBytes; michael@0: int64_t freeBytes; michael@0: michael@0: stat->GetTotalBytes(&totalBytes); michael@0: stat->GetFreeBytes(&freeBytes); michael@0: michael@0: LOG(" Total Space: %llu Mb Free Bytes: %llu Mb", michael@0: totalBytes / (1024LL * 1024LL), freeBytes / (1024LL * 1024LL)); michael@0: } michael@0: else { michael@0: LOG(" Unable to retrieve stats"); michael@0: } michael@0: } michael@0: }; michael@0: static nsCOMPtr sTestObserver; michael@0: michael@0: NS_IMPL_ISUPPORTS(VolumeTestObserver, nsIObserver) michael@0: michael@0: NS_IMETHODIMP michael@0: VolumeTestObserver::Observe(nsISupports* aSubject, michael@0: const char* aTopic, michael@0: const char16_t* aData) michael@0: { michael@0: LOG("TestObserver: topic: %s", aTopic); michael@0: michael@0: if (strcmp(aTopic, NS_VOLUME_STATE_CHANGED) != 0) { michael@0: return NS_OK; michael@0: } michael@0: nsCOMPtr vol = do_QueryInterface(aSubject); michael@0: if (vol) { michael@0: LogVolume(vol); michael@0: } michael@0: michael@0: // Since this observe method was called then we know that the service michael@0: // has been initialized so we can do the VolumeService tests. michael@0: michael@0: nsCOMPtr vs = do_GetService(NS_VOLUMESERVICE_CONTRACTID); michael@0: if (!vs) { michael@0: ERR("do_GetService('%s') failed", NS_VOLUMESERVICE_CONTRACTID); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: nsresult rv = vs->GetVolumeByName(NS_LITERAL_STRING("sdcard"), getter_AddRefs(vol)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: LOG("GetVolumeByName( 'sdcard' ) succeeded (expected)"); michael@0: LogVolume(vol); michael@0: } else { michael@0: ERR("GetVolumeByName( 'sdcard' ) failed (unexpected)"); michael@0: } michael@0: michael@0: rv = vs->GetVolumeByName(NS_LITERAL_STRING("foo"), getter_AddRefs(vol)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: ERR("GetVolumeByName( 'foo' ) succeeded (unexpected)"); michael@0: } else { michael@0: LOG("GetVolumeByName( 'foo' ) failed (expected)"); michael@0: } michael@0: michael@0: rv = vs->GetVolumeByPath(NS_LITERAL_STRING("/mnt/sdcard"), getter_AddRefs(vol)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: LOG("GetVolumeByPath( '/mnt/sdcard' ) succeeded (expected)"); michael@0: LogVolume(vol); michael@0: } else { michael@0: ERR("GetVolumeByPath( '/mnt/sdcard' ) failed (unexpected"); michael@0: } michael@0: michael@0: rv = vs->GetVolumeByPath(NS_LITERAL_STRING("/mnt/sdcard/foo"), getter_AddRefs(vol)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: LOG("GetVolumeByPath( '/mnt/sdcard/foo' ) succeeded (expected)"); michael@0: LogVolume(vol); michael@0: } else { michael@0: LOG("GetVolumeByPath( '/mnt/sdcard/foo' ) failed (unexpected)"); michael@0: } michael@0: michael@0: rv = vs->GetVolumeByPath(NS_LITERAL_STRING("/mnt/sdcardfoo"), getter_AddRefs(vol)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: ERR("GetVolumeByPath( '/mnt/sdcardfoo' ) succeeded (unexpected)"); michael@0: } else { michael@0: LOG("GetVolumeByPath( '/mnt/sdcardfoo' ) failed (expected)"); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: class InitVolumeServiceTestIO : public nsRunnable michael@0: { michael@0: public: michael@0: NS_IMETHOD Run() michael@0: { michael@0: MOZ_ASSERT(NS_IsMainThread()); michael@0: michael@0: DBG("InitVolumeServiceTest called"); michael@0: nsCOMPtr vs = do_GetService(NS_VOLUMESERVICE_CONTRACTID); michael@0: if (!vs) { michael@0: ERR("do_GetService('%s') failed", NS_VOLUMESERVICE_CONTRACTID); michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: sTestObserver = new VolumeTestObserver(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: }; michael@0: #endif // TEST_NSVOLUME_OBSERVER michael@0: michael@0: void michael@0: InitVolumeServiceTestIOThread() michael@0: { michael@0: MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop()); michael@0: michael@0: #if TEST_NSVOLUME_OBSERVER michael@0: // Now that the volume manager is initialized we can go michael@0: // ahead and do our test (on main thread). michael@0: NS_DispatchToMainThread(new InitVolumeServiceTestIO()); michael@0: #endif michael@0: } michael@0: michael@0: void michael@0: ShutdownVolumeServiceTest() michael@0: { michael@0: #if TEST_NSVOLUME_OBSERVER michael@0: DBG("ShutdownVolumeServiceTestIOThread called"); michael@0: sTestObserver = nullptr; michael@0: #endif michael@0: } michael@0: michael@0: } // system michael@0: } // mozilla