- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / policy / cloud / resource_cache_unittest.cc
1 // Copyright (c) 2013 The Chromium Authors. 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 "chrome/browser/policy/cloud/resource_cache.h"
6
7 #include "base/basictypes.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/test/test_simple_task_runner.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace policy {
13
14 namespace {
15
16 const char kKey1[] = "key 1";
17 const char kKey2[] = "key 2";
18 const char kKey3[] = "key 3";
19 const char kSubA[] = "a";
20 const char kSubB[] = "bb";
21 const char kSubC[] = "ccc";
22 const char kSubD[] = "dddd";
23 const char kSubE[] = "eeeee";
24
25 const char kData0[] = "{ \"key\": \"value\" }";
26 const char kData1[] = "{}";
27
28 }  // namespace
29
30 TEST(ResourceCacheTest, StoreAndLoad) {
31   base::ScopedTempDir temp_dir;
32   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
33   ResourceCache cache(temp_dir.path(),
34                       make_scoped_refptr(new base::TestSimpleTaskRunner));
35
36   // No data initially.
37   std::string data;
38   EXPECT_FALSE(cache.Load(kKey1, kSubA, &data));
39
40   // Store some data and load it.
41   EXPECT_TRUE(cache.Store(kKey1, kSubA, kData0));
42   EXPECT_TRUE(cache.Load(kKey1, kSubA, &data));
43   EXPECT_EQ(kData0, data);
44
45   // Store more data in another subkey.
46   EXPECT_TRUE(cache.Store(kKey1, kSubB, kData1));
47
48   // Write subkeys to two other keys.
49   EXPECT_TRUE(cache.Store(kKey2, kSubA, kData0));
50   EXPECT_TRUE(cache.Store(kKey2, kSubB, kData1));
51   EXPECT_TRUE(cache.Store(kKey3, kSubA, kData0));
52   EXPECT_TRUE(cache.Store(kKey3, kSubB, kData1));
53
54   // Enumerate all the subkeys.
55   std::map<std::string, std::string> contents;
56   cache.LoadAllSubkeys(kKey1, &contents);
57   EXPECT_EQ(2u, contents.size());
58   EXPECT_EQ(kData0, contents[kSubA]);
59   EXPECT_EQ(kData1, contents[kSubB]);
60
61   // Store more subkeys.
62   EXPECT_TRUE(cache.Store(kKey1, kSubC, kData1));
63   EXPECT_TRUE(cache.Store(kKey1, kSubD, kData1));
64   EXPECT_TRUE(cache.Store(kKey1, kSubE, kData1));
65
66   // Now purge some of them.
67   std::set<std::string> keep;
68   keep.insert(kSubB);
69   keep.insert(kSubD);
70   cache.PurgeOtherSubkeys(kKey1, keep);
71
72   // Enumerate all the remaining subkeys.
73   cache.LoadAllSubkeys(kKey1, &contents);
74   EXPECT_EQ(2u, contents.size());
75   EXPECT_EQ(kData1, contents[kSubB]);
76   EXPECT_EQ(kData1, contents[kSubD]);
77
78   // Delete subkeys directly.
79   cache.Delete(kKey1, kSubB);
80   cache.Delete(kKey1, kSubD);
81   cache.LoadAllSubkeys(kKey1, &contents);
82   EXPECT_EQ(0u, contents.size());
83
84   // The other two keys were not affected.
85   cache.LoadAllSubkeys(kKey2, &contents);
86   EXPECT_EQ(2u, contents.size());
87   EXPECT_EQ(kData0, contents[kSubA]);
88   EXPECT_EQ(kData1, contents[kSubB]);
89   cache.LoadAllSubkeys(kKey3, &contents);
90   EXPECT_EQ(2u, contents.size());
91   EXPECT_EQ(kData0, contents[kSubA]);
92   EXPECT_EQ(kData1, contents[kSubB]);
93
94   // Now purge all keys except the third.
95   keep.clear();
96   keep.insert(kKey3);
97   cache.PurgeOtherKeys(keep);
98
99   // The first two keys are empty.
100   cache.LoadAllSubkeys(kKey1, &contents);
101   EXPECT_EQ(0u, contents.size());
102   cache.LoadAllSubkeys(kKey1, &contents);
103   EXPECT_EQ(0u, contents.size());
104
105   // The third key is unaffected.
106   cache.LoadAllSubkeys(kKey3, &contents);
107   EXPECT_EQ(2u, contents.size());
108   EXPECT_EQ(kData0, contents[kSubA]);
109   EXPECT_EQ(kData1, contents[kSubB]);
110 }
111
112 }  // namespace policy