summaryrefslogtreecommitdiffstats
path: root/webkit/dom_storage/dom_storage_map_unittest.cc
blob: 89edbc3f2d666eaa81acd41c775ec1f57644c656 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/dom_storage/dom_storage_map.h"

namespace dom_storage {

TEST(DomStorageMapTest, DomStorageMapBasics) {
  const string16 kKey(ASCIIToUTF16("key"));
  const string16 kValue(ASCIIToUTF16("value"));
  const size_t kValueBytes = kValue.size() * sizeof(char16);
  const size_t kItemBytes =
      (kKey.size() + kValue.size()) * sizeof(char16);
  const string16 kKey2(ASCIIToUTF16("key2"));
  const size_t kKey2Bytes = kKey2.size() * sizeof(char16);
  const string16 kValue2(ASCIIToUTF16("value2"));
  const size_t kItem2Bytes =
      (kKey2.size() + kValue2.size()) * sizeof(char16);
  const size_t kQuota = 1024;  // 1K quota for this test.

  scoped_refptr<DomStorageMap> map(new DomStorageMap(kQuota));
  string16 old_value;
  NullableString16 old_nullable_value;
  ValuesMap swap;
  scoped_refptr<DomStorageMap> copy;

  // Check the behavior of an empty map.
  EXPECT_EQ(0u, map->Length());
  EXPECT_TRUE(map->Key(0).is_null());
  EXPECT_TRUE(map->Key(100).is_null());
  EXPECT_TRUE(map->GetItem(kKey).is_null());
  EXPECT_FALSE(map->RemoveItem(kKey, &old_value));
  EXPECT_EQ(0u, map->bytes_used());
  copy = map->DeepCopy();
  EXPECT_EQ(0u, copy->Length());
  EXPECT_EQ(0u, copy->bytes_used());
  map->SwapValues(&swap);
  EXPECT_TRUE(swap.empty());

  // Check the behavior of a map containing some values.
  EXPECT_TRUE(map->SetItem(kKey, kValue, &old_nullable_value));
  EXPECT_TRUE(old_nullable_value.is_null());
  EXPECT_EQ(1u, map->Length());
  EXPECT_EQ(kKey, map->Key(0).string());
  EXPECT_TRUE(map->Key(1).is_null());
  EXPECT_EQ(kValue, map->GetItem(kKey).string());
  EXPECT_TRUE(map->GetItem(kKey2).is_null());
  EXPECT_EQ(kItemBytes, map->bytes_used());
  EXPECT_TRUE(map->RemoveItem(kKey, &old_value));
  EXPECT_EQ(0u, map->bytes_used());
  EXPECT_EQ(kValue, old_value);
  old_value.clear();

  EXPECT_TRUE(map->SetItem(kKey, kValue, &old_nullable_value));
  EXPECT_TRUE(map->SetItem(kKey2, kValue, &old_nullable_value));
  EXPECT_TRUE(old_nullable_value.is_null());
  EXPECT_EQ(kItemBytes + kKey2Bytes + kValueBytes, map->bytes_used());
  EXPECT_TRUE(map->SetItem(kKey2, kValue2, &old_nullable_value));
  EXPECT_EQ(kItemBytes + kItem2Bytes, map->bytes_used());
  EXPECT_EQ(kValue, old_nullable_value.string());
  EXPECT_EQ(2u, map->Length());
  EXPECT_EQ(kKey, map->Key(0).string());
  EXPECT_EQ(kKey2, map->Key(1).string());
  EXPECT_EQ(kKey, map->Key(0).string());
  EXPECT_EQ(kItemBytes + kItem2Bytes, map->bytes_used());

  copy = map->DeepCopy();
  EXPECT_EQ(2u, copy->Length());
  EXPECT_EQ(kValue, copy->GetItem(kKey).string());
  EXPECT_EQ(kValue2, copy->GetItem(kKey2).string());
  EXPECT_EQ(kKey, copy->Key(0).string());
  EXPECT_EQ(kKey2, copy->Key(1).string());
  EXPECT_TRUE(copy->Key(2).is_null());
  EXPECT_EQ(kItemBytes + kItem2Bytes, copy->bytes_used());

  map->SwapValues(&swap);
  EXPECT_EQ(2ul, swap.size());
  EXPECT_EQ(0u, map->Length());
  EXPECT_EQ(0u, map->bytes_used());
}

TEST(DomStorageMapTest, EnforcesQuota) {
  const string16 kKey = ASCIIToUTF16("test_key");
  const string16 kValue = ASCIIToUTF16("test_value");
  const string16 kKey2 = ASCIIToUTF16("test_key_2");

  // A 50 byte quota is too small to hold both keys, so we
  // should see the DomStorageMap enforcing it.
  const size_t kQuota = 50;

  string16 old_value;
  NullableString16 old_nullable_value;

  scoped_refptr<DomStorageMap> map(new DomStorageMap(kQuota));
  EXPECT_TRUE(map->SetItem(kKey, kValue, &old_nullable_value));
  EXPECT_FALSE(map->SetItem(kKey2, kValue, &old_nullable_value));
  EXPECT_EQ(1u, map->Length());

  EXPECT_TRUE(map->RemoveItem(kKey, &old_value));
  EXPECT_EQ(kValue, old_value);
  EXPECT_EQ(0u, map->Length());
  EXPECT_TRUE(map->SetItem(kKey2, kValue, &old_nullable_value));
  EXPECT_EQ(1u, map->Length());

  // Verify that the SwapValues method does not do quota checking.
  ValuesMap swap;
  swap[kKey] = NullableString16(kValue, false);
  swap[kKey2] = NullableString16(kValue, false);
  map->SwapValues(&swap);
  EXPECT_GT(map->bytes_used(), kQuota);

  // When overbudget, a new value of greater size than the existing value can
  // not be set, but a new value of lesser or equal size can be set.
  EXPECT_TRUE(map->SetItem(kKey, kValue, &old_nullable_value));
  EXPECT_FALSE(map->SetItem(kKey, string16(kValue + kValue),
                            &old_nullable_value));
  EXPECT_TRUE(map->SetItem(kKey, string16(), &old_nullable_value));
  EXPECT_EQ(kValue, old_nullable_value.string());
}

}  // namespace dom_storage