Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / xwalk / sysapps / device_capabilities / storage_info_provider_unittest.cc
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "xwalk/sysapps/device_capabilities/storage_info_provider.h"
6
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "content/public/test/test_browser_thread_bundle.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "xwalk/sysapps/common/sysapps_manager.h"
12 #include "xwalk/sysapps/device_capabilities/device_capabilities.h"
13
14 using xwalk::jsapi::device_capabilities::StorageUnit;
15 using xwalk::jsapi::device_capabilities::SystemStorage;
16 using xwalk::sysapps::StorageInfoProvider;
17 using xwalk::sysapps::SysAppsManager;
18
19 namespace {
20
21 class TestObserver : public StorageInfoProvider::Observer {
22  public:
23   TestObserver() {}
24
25  private:
26   void OnStorageAttached(const StorageUnit& storage) override {}
27   void OnStorageDetached(const StorageUnit& storage) override {}
28 };
29
30 void TestClosure() {
31   StorageInfoProvider* provider(SysAppsManager::GetStorageInfoProvider());
32   EXPECT_TRUE(provider != NULL);
33   EXPECT_TRUE(provider->IsInitialized());
34
35   scoped_ptr<SystemStorage> info(provider->storage_info());
36   EXPECT_TRUE(info != NULL);
37
38   std::vector<linked_ptr<StorageUnit> > storages = info->storages;
39
40   // We should have at least one storage, otherwise where is the binary
41   // of this unit test being stored?
42   unsigned storage_count = storages.size();
43   EXPECT_GT(storage_count, 0u);
44
45   // The only information we can verify is the fact that the storage
46   // has some capacity and has an ID. The rest, like the name, can be empty.
47   for (size_t i = 0; i < storage_count; ++i) {
48     EXPECT_FALSE(storages[i]->id.empty());
49     EXPECT_GE(storages[i]->capacity, 0);
50   }
51
52   // We can just test if adding and removing the observers works, but
53   // without a fake backend, we cannot verify if the observers are getting
54   // called.
55   TestObserver test_observer;
56   provider->AddObserver(&test_observer);
57   provider->RemoveObserver(&test_observer);
58
59   base::MessageLoop::current()->Quit();
60 }
61
62 }  // namespace
63
64 TEST(XWalkSysAppsDeviceCapabilitiesTest, StorageInfoProvider) {
65   // The StorageMonitor backend requires an UIThread up and running. Luckily
66   // the abstraction bellow does all the heavy lifting for us.
67   content::TestBrowserThreadBundle thread_bundle(
68       content::TestBrowserThreadBundle::IO_MAINLOOP);
69
70   StorageInfoProvider* provider(SysAppsManager::GetStorageInfoProvider());
71   EXPECT_TRUE(provider != NULL);
72
73   base::Closure closure = base::Bind(&TestClosure);
74
75   if (!provider->IsInitialized())
76     provider->AddOnInitCallback(closure);
77   else
78     base::MessageLoop::current()->PostTask(FROM_HERE, closure);
79
80   base::MessageLoop::current()->Run();
81 }