dom/system/gonk/VolumeServiceTest.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/system/gonk/VolumeServiceTest.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,201 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "VolumeServiceTest.h"
     1.9 +
    1.10 +#include "base/message_loop.h"
    1.11 +#include "nsCOMPtr.h"
    1.12 +#include "nsIObserver.h"
    1.13 +#include "nsIObserverService.h"
    1.14 +#include "nsServiceManagerUtils.h"
    1.15 +#include "nsThreadUtils.h"
    1.16 +#include "nsIVolume.h"
    1.17 +#include "nsIVolumeService.h"
    1.18 +#include "nsIVolumeStat.h"
    1.19 +#include "nsXULAppAPI.h"
    1.20 +
    1.21 +#include "mozilla/Services.h"
    1.22 +
    1.23 +#define VOLUME_MANAGER_LOG_TAG  "VolumeServiceTest"
    1.24 +#include "VolumeManagerLog.h"
    1.25 +
    1.26 +using namespace mozilla::services;
    1.27 +
    1.28 +namespace mozilla {
    1.29 +namespace system {
    1.30 +
    1.31 +#define TEST_NSVOLUME_OBSERVER  0
    1.32 +
    1.33 +#if TEST_NSVOLUME_OBSERVER
    1.34 +
    1.35 +/***************************************************************************
    1.36 +* A test class to verify that the Observer stuff is working properly.
    1.37 +*/
    1.38 +class VolumeTestObserver : public nsIObserver
    1.39 +{
    1.40 +public:
    1.41 +  NS_DECL_ISUPPORTS
    1.42 +  NS_DECL_NSIOBSERVER
    1.43 +
    1.44 +  VolumeTestObserver()
    1.45 +  {
    1.46 +    nsCOMPtr<nsIObserverService> obs = GetObserverService();
    1.47 +    if (!obs) {
    1.48 +      return;
    1.49 +    }
    1.50 +    obs->AddObserver(this, NS_VOLUME_STATE_CHANGED, false);
    1.51 +  }
    1.52 +  ~VolumeTestObserver()
    1.53 +  {
    1.54 +    nsCOMPtr<nsIObserverService> obs = GetObserverService();
    1.55 +    if (!obs) {
    1.56 +      return;
    1.57 +    }
    1.58 +    obs->RemoveObserver(this, NS_VOLUME_STATE_CHANGED);
    1.59 +  }
    1.60 +
    1.61 +  void LogVolume(nsIVolume* vol)
    1.62 +  {
    1.63 +    nsString volName;
    1.64 +    nsString mountPoint;
    1.65 +    int32_t volState;
    1.66 +
    1.67 +    vol->GetName(volName);
    1.68 +    vol->GetMountPoint(mountPoint);
    1.69 +    vol->GetState(&volState);
    1.70 +
    1.71 +    LOG("  Volume: %s MountPoint: %s State: %s",
    1.72 +        NS_LossyConvertUTF16toASCII(volName).get(),
    1.73 +        NS_LossyConvertUTF16toASCII(mountPoint).get(),
    1.74 +        NS_VolumeStateStr(volState));
    1.75 +
    1.76 +    nsCOMPtr<nsIVolumeStat> stat;
    1.77 +    nsresult rv = vol->GetStats(getter_AddRefs(stat));
    1.78 +    if (NS_SUCCEEDED(rv)) {
    1.79 +      int64_t totalBytes;
    1.80 +      int64_t freeBytes;
    1.81 +
    1.82 +      stat->GetTotalBytes(&totalBytes);
    1.83 +      stat->GetFreeBytes(&freeBytes);
    1.84 +
    1.85 +      LOG("  Total Space: %llu Mb  Free Bytes: %llu Mb",
    1.86 +          totalBytes / (1024LL * 1024LL), freeBytes / (1024LL * 1024LL));
    1.87 +    }
    1.88 +    else {
    1.89 +      LOG("  Unable to retrieve stats");
    1.90 +    }
    1.91 +  }
    1.92 +};
    1.93 +static nsCOMPtr<VolumeTestObserver>  sTestObserver;
    1.94 +
    1.95 +NS_IMPL_ISUPPORTS(VolumeTestObserver, nsIObserver)
    1.96 +
    1.97 +NS_IMETHODIMP
    1.98 +VolumeTestObserver::Observe(nsISupports* aSubject,
    1.99 +                            const char* aTopic,
   1.100 +                            const char16_t* aData)
   1.101 +{
   1.102 +  LOG("TestObserver: topic: %s", aTopic);
   1.103 +
   1.104 +  if (strcmp(aTopic, NS_VOLUME_STATE_CHANGED) != 0) {
   1.105 +    return NS_OK;
   1.106 +  }
   1.107 +  nsCOMPtr<nsIVolume> vol = do_QueryInterface(aSubject);
   1.108 +  if (vol) {
   1.109 +    LogVolume(vol);
   1.110 +  }
   1.111 +
   1.112 +  // Since this observe method was called then we know that the service
   1.113 +  // has been initialized so we can do the VolumeService tests.
   1.114 +
   1.115 +  nsCOMPtr<nsIVolumeService> vs = do_GetService(NS_VOLUMESERVICE_CONTRACTID);
   1.116 +  if (!vs) {
   1.117 +    ERR("do_GetService('%s') failed", NS_VOLUMESERVICE_CONTRACTID);
   1.118 +    return NS_ERROR_FAILURE;
   1.119 +  }
   1.120 +
   1.121 +  nsresult rv = vs->GetVolumeByName(NS_LITERAL_STRING("sdcard"), getter_AddRefs(vol));
   1.122 +  if (NS_SUCCEEDED(rv)) {
   1.123 +    LOG("GetVolumeByName( 'sdcard' ) succeeded (expected)");
   1.124 +    LogVolume(vol);
   1.125 +  } else {
   1.126 +    ERR("GetVolumeByName( 'sdcard' ) failed (unexpected)");
   1.127 +  }
   1.128 +
   1.129 +  rv = vs->GetVolumeByName(NS_LITERAL_STRING("foo"), getter_AddRefs(vol));
   1.130 +  if (NS_SUCCEEDED(rv)) {
   1.131 +    ERR("GetVolumeByName( 'foo' ) succeeded (unexpected)");
   1.132 +  } else {
   1.133 +    LOG("GetVolumeByName( 'foo' ) failed (expected)");
   1.134 +  }
   1.135 +
   1.136 +  rv = vs->GetVolumeByPath(NS_LITERAL_STRING("/mnt/sdcard"), getter_AddRefs(vol));
   1.137 +  if (NS_SUCCEEDED(rv)) {
   1.138 +    LOG("GetVolumeByPath( '/mnt/sdcard' ) succeeded (expected)");
   1.139 +    LogVolume(vol);
   1.140 +  } else {
   1.141 +    ERR("GetVolumeByPath( '/mnt/sdcard' ) failed (unexpected");
   1.142 +  }
   1.143 +
   1.144 +  rv = vs->GetVolumeByPath(NS_LITERAL_STRING("/mnt/sdcard/foo"), getter_AddRefs(vol));
   1.145 +  if (NS_SUCCEEDED(rv)) {
   1.146 +    LOG("GetVolumeByPath( '/mnt/sdcard/foo' ) succeeded (expected)");
   1.147 +    LogVolume(vol);
   1.148 +  } else {
   1.149 +    LOG("GetVolumeByPath( '/mnt/sdcard/foo' ) failed (unexpected)");
   1.150 +  }
   1.151 +
   1.152 +  rv = vs->GetVolumeByPath(NS_LITERAL_STRING("/mnt/sdcardfoo"), getter_AddRefs(vol));
   1.153 +  if (NS_SUCCEEDED(rv)) {
   1.154 +    ERR("GetVolumeByPath( '/mnt/sdcardfoo' ) succeeded (unexpected)");
   1.155 +  } else {
   1.156 +    LOG("GetVolumeByPath( '/mnt/sdcardfoo' ) failed (expected)");
   1.157 +  }
   1.158 +
   1.159 +  return NS_OK;
   1.160 +}
   1.161 +
   1.162 +class InitVolumeServiceTestIO : public nsRunnable
   1.163 +{
   1.164 +public:
   1.165 +  NS_IMETHOD Run()
   1.166 +  {
   1.167 +    MOZ_ASSERT(NS_IsMainThread());
   1.168 +
   1.169 +    DBG("InitVolumeServiceTest called");
   1.170 +    nsCOMPtr<nsIVolumeService> vs = do_GetService(NS_VOLUMESERVICE_CONTRACTID);
   1.171 +    if (!vs) {
   1.172 +      ERR("do_GetService('%s') failed", NS_VOLUMESERVICE_CONTRACTID);
   1.173 +      return NS_ERROR_FAILURE;
   1.174 +    }
   1.175 +    sTestObserver = new VolumeTestObserver();
   1.176 +
   1.177 +    return NS_OK;
   1.178 +  }
   1.179 +};
   1.180 +#endif // TEST_NSVOLUME_OBSERVER
   1.181 +
   1.182 +void
   1.183 +InitVolumeServiceTestIOThread()
   1.184 +{
   1.185 +  MOZ_ASSERT(MessageLoop::current() == XRE_GetIOMessageLoop());
   1.186 +
   1.187 +#if TEST_NSVOLUME_OBSERVER
   1.188 +  // Now that the volume manager is initialized we can go
   1.189 +  // ahead and do our test (on main thread).
   1.190 +  NS_DispatchToMainThread(new InitVolumeServiceTestIO());
   1.191 +#endif
   1.192 +}
   1.193 +
   1.194 +void
   1.195 +ShutdownVolumeServiceTest()
   1.196 +{
   1.197 +#if TEST_NSVOLUME_OBSERVER
   1.198 +  DBG("ShutdownVolumeServiceTestIOThread called");
   1.199 +  sTestObserver = nullptr;
   1.200 +#endif
   1.201 +}
   1.202 +
   1.203 +} // system
   1.204 +} // mozilla

mercurial