Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / common / dom_storage / dom_storage_map_unittest.cc
blob31283fd671f7b6e618af3e37aaafbf0eb160120f
1 // Copyright 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.
5 #include "base/strings/utf_string_conversions.h"
6 #include "content/common/dom_storage/dom_storage_map.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 using base::ASCIIToUTF16;
11 namespace content {
13 TEST(DOMStorageMapTest, DOMStorageMapBasics) {
14 const base::string16 kKey(ASCIIToUTF16("key"));
15 const base::string16 kValue(ASCIIToUTF16("value"));
16 const size_t kValueBytes = kValue.size() * sizeof(base::char16);
17 const size_t kItemBytes =
18 (kKey.size() + kValue.size()) * sizeof(base::char16);
19 const base::string16 kKey2(ASCIIToUTF16("key2"));
20 const size_t kKey2Bytes = kKey2.size() * sizeof(base::char16);
21 const base::string16 kValue2(ASCIIToUTF16("value2"));
22 const size_t kItem2Bytes =
23 (kKey2.size() + kValue2.size()) * sizeof(base::char16);
24 const size_t kQuota = 1024; // 1K quota for this test.
26 scoped_refptr<DOMStorageMap> map(new DOMStorageMap(kQuota));
27 base::string16 old_value;
28 base::NullableString16 old_nullable_value;
29 DOMStorageValuesMap swap;
30 scoped_refptr<DOMStorageMap> copy;
32 // Check the behavior of an empty map.
33 EXPECT_EQ(0u, map->Length());
34 EXPECT_TRUE(map->Key(0).is_null());
35 EXPECT_TRUE(map->Key(100).is_null());
36 EXPECT_TRUE(map->GetItem(kKey).is_null());
37 EXPECT_FALSE(map->RemoveItem(kKey, &old_value));
38 EXPECT_EQ(0u, map->bytes_used());
39 copy = map->DeepCopy();
40 EXPECT_EQ(0u, copy->Length());
41 EXPECT_EQ(0u, copy->bytes_used());
42 map->SwapValues(&swap);
43 EXPECT_TRUE(swap.empty());
45 // Check the behavior of a map containing some values.
46 EXPECT_TRUE(map->SetItem(kKey, kValue, &old_nullable_value));
47 EXPECT_TRUE(old_nullable_value.is_null());
48 EXPECT_EQ(1u, map->Length());
49 EXPECT_EQ(kKey, map->Key(0).string());
50 EXPECT_TRUE(map->Key(1).is_null());
51 EXPECT_EQ(kValue, map->GetItem(kKey).string());
52 EXPECT_TRUE(map->GetItem(kKey2).is_null());
53 EXPECT_EQ(kItemBytes, map->bytes_used());
54 EXPECT_TRUE(map->RemoveItem(kKey, &old_value));
55 EXPECT_EQ(0u, map->bytes_used());
56 EXPECT_EQ(kValue, old_value);
57 old_value.clear();
59 EXPECT_TRUE(map->SetItem(kKey, kValue, &old_nullable_value));
60 EXPECT_TRUE(map->SetItem(kKey2, kValue, &old_nullable_value));
61 EXPECT_TRUE(old_nullable_value.is_null());
62 EXPECT_EQ(kItemBytes + kKey2Bytes + kValueBytes, map->bytes_used());
63 EXPECT_TRUE(map->SetItem(kKey2, kValue2, &old_nullable_value));
64 EXPECT_EQ(kItemBytes + kItem2Bytes, map->bytes_used());
65 EXPECT_EQ(kValue, old_nullable_value.string());
66 EXPECT_EQ(2u, map->Length());
67 EXPECT_EQ(kKey, map->Key(0).string());
68 EXPECT_EQ(kKey2, map->Key(1).string());
69 EXPECT_EQ(kKey, map->Key(0).string());
70 EXPECT_EQ(kItemBytes + kItem2Bytes, map->bytes_used());
72 copy = map->DeepCopy();
73 EXPECT_EQ(2u, copy->Length());
74 EXPECT_EQ(kValue, copy->GetItem(kKey).string());
75 EXPECT_EQ(kValue2, copy->GetItem(kKey2).string());
76 EXPECT_EQ(kKey, copy->Key(0).string());
77 EXPECT_EQ(kKey2, copy->Key(1).string());
78 EXPECT_TRUE(copy->Key(2).is_null());
79 EXPECT_EQ(kItemBytes + kItem2Bytes, copy->bytes_used());
81 map->SwapValues(&swap);
82 EXPECT_EQ(2ul, swap.size());
83 EXPECT_EQ(0u, map->Length());
84 EXPECT_EQ(0u, map->bytes_used());
87 TEST(DOMStorageMapTest, EnforcesQuota) {
88 const base::string16 kKey = ASCIIToUTF16("test_key");
89 const base::string16 kValue = ASCIIToUTF16("test_value");
90 const base::string16 kKey2 = ASCIIToUTF16("test_key_2");
92 // A 50 byte quota is too small to hold both keys, so we
93 // should see the DOMStorageMap enforcing it.
94 const size_t kQuota = 50;
96 base::string16 old_value;
97 base::NullableString16 old_nullable_value;
99 scoped_refptr<DOMStorageMap> map(new DOMStorageMap(kQuota));
100 EXPECT_TRUE(map->SetItem(kKey, kValue, &old_nullable_value));
101 EXPECT_FALSE(map->SetItem(kKey2, kValue, &old_nullable_value));
102 EXPECT_EQ(1u, map->Length());
104 EXPECT_TRUE(map->RemoveItem(kKey, &old_value));
105 EXPECT_EQ(kValue, old_value);
106 EXPECT_EQ(0u, map->Length());
107 EXPECT_TRUE(map->SetItem(kKey2, kValue, &old_nullable_value));
108 EXPECT_EQ(1u, map->Length());
110 // Verify that the SwapValues method does not do quota checking.
111 DOMStorageValuesMap swap;
112 swap[kKey] = base::NullableString16(kValue, false);
113 swap[kKey2] = base::NullableString16(kValue, false);
114 map->SwapValues(&swap);
115 EXPECT_GT(map->bytes_used(), kQuota);
117 // When overbudget, a new value of greater size than the existing value can
118 // not be set, but a new value of lesser or equal size can be set.
119 EXPECT_TRUE(map->SetItem(kKey, kValue, &old_nullable_value));
120 EXPECT_FALSE(map->SetItem(kKey, base::string16(kValue + kValue),
121 &old_nullable_value));
122 EXPECT_TRUE(map->SetItem(kKey, base::string16(), &old_nullable_value));
123 EXPECT_EQ(kValue, old_nullable_value.string());
126 } // namespace content