Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: webkit/database/database_tracker_unittest.cc

Issue 334039: Adding Chromium's database tracker. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Final version? Created 11 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « webkit/database/database_tracker.cc ('k') | webkit/database/databases_table.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2009 The Chromium Authos. 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 "base/file_path.h"
6 #include "base/file_util.h"
7 #include "base/scoped_ptr.h"
8 #include "base/scoped_temp_dir.h"
9 #include "base/string_util.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "webkit/database/database_tracker.h"
12
13 namespace {
14
15 class TestObserver : public webkit_database::DatabaseTracker::Observer {
16 public:
17 TestObserver() : new_notification_received_(false) {}
18 virtual ~TestObserver() {}
19 virtual void OnDatabaseSizeChanged(const string16& origin_identifier,
20 const string16& database_name,
21 int64 database_size,
22 int64 space_available) {
23 new_notification_received_ = true;
24 origin_identifier_ = origin_identifier;
25 database_name_ = database_name;
26 database_size_ = database_size;
27 space_available_ = space_available;
28 }
29 bool DidReceiveNewNotification() {
30 bool temp_new_notification_received = new_notification_received_;
31 new_notification_received_ = false;
32 return temp_new_notification_received;
33 }
34 string16 GetNotificationOriginIdentifier() { return origin_identifier_; }
35 string16 GetNotificationDatabaseName() { return database_name_; }
36 int64 GetNotificationDatabaseSize() { return database_size_; }
37 int64 GetNotificationSpaceAvailable() { return space_available_; }
38
39 private:
40 bool new_notification_received_;
41 string16 origin_identifier_;
42 string16 database_name_;
43 int64 database_size_;
44 int64 space_available_;
45 };
46
47 void CheckNotificationReceived(TestObserver* observer,
48 const string16& expected_origin_identifier,
49 const string16& expected_database_name,
50 int64 expected_database_size,
51 int64 expected_space_available) {
52 EXPECT_TRUE(observer->DidReceiveNewNotification());
53 EXPECT_EQ(expected_origin_identifier,
54 observer->GetNotificationOriginIdentifier());
55 EXPECT_EQ(expected_database_name,
56 observer->GetNotificationDatabaseName());
57 EXPECT_EQ(expected_database_size,
58 observer->GetNotificationDatabaseSize());
59 EXPECT_EQ(expected_space_available,
60 observer->GetNotificationSpaceAvailable());
61 }
62
63 } // namespace
64
65 namespace webkit_database {
66
67 TEST(DatabaseTrackerTest, TestIt) {
68 // Initialize the tracker database.
69 ScopedTempDir temp_dir;
70 EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
71 scoped_refptr<DatabaseTracker> tracker(new DatabaseTracker(temp_dir.path()));
72
73 // Get the default quota for all origins.
74 const int64 kDefaultQuota = tracker->GetOriginQuota(EmptyString16());
75
76 // Add two observers.
77 TestObserver observer1;
78 TestObserver observer2;
79 tracker->AddObserver(&observer1);
80 tracker->AddObserver(&observer2);
81
82 // Open three new databases.
83 int64 database_size = 0;
84 int64 space_available = 0;
85 const string16 kOrigin1 = ASCIIToUTF16("kOrigin1");
86 const string16 kOrigin2 = ASCIIToUTF16("kOrigin2");
87 const string16 kDB1 = ASCIIToUTF16("kDB1");
88 const string16 kDB2 = ASCIIToUTF16("kDB2");
89 const string16 kDB3 = ASCIIToUTF16("kDB3");
90 const string16 kDescription = ASCIIToUTF16("database_kDescription");
91 tracker->DatabaseOpened(kOrigin1, kDB1, kDescription, 0,
92 &database_size, &space_available);
93 EXPECT_EQ(0, database_size);
94 EXPECT_EQ(kDefaultQuota, space_available);
95 tracker->DatabaseOpened(kOrigin2, kDB2, kDescription, 0,
96 &database_size, &space_available);
97 EXPECT_EQ(0, database_size);
98 EXPECT_EQ(kDefaultQuota, space_available);
99 tracker->DatabaseOpened(kOrigin1, kDB3, kDescription, 0,
100 &database_size, &space_available);
101 EXPECT_EQ(0, database_size);
102 EXPECT_EQ(kDefaultQuota, space_available);
103
104 // Tell the tracker that a database has changed.
105 // Even though nothing has changed, the observers should be notified.
106 tracker->DatabaseModified(kOrigin1, kDB1);
107 CheckNotificationReceived(&observer1, kOrigin1, kDB1, 0, kDefaultQuota);
108 CheckNotificationReceived(&observer2, kOrigin1, kDB1, 0, kDefaultQuota);
109
110 // Write some data to each file and check that the listeners are
111 // called with the appropriate values.
112 EXPECT_EQ(1, file_util::WriteFile(
113 tracker->GetFullDBFilePath(kOrigin1, kDB1), "a", 1));
114 EXPECT_EQ(2, file_util::WriteFile(
115 tracker->GetFullDBFilePath(kOrigin2, kDB2), "aa", 2));
116 EXPECT_EQ(4, file_util::WriteFile(
117 tracker->GetFullDBFilePath(kOrigin1, kDB3), "aaaa", 4));
118 tracker->DatabaseModified(kOrigin1, kDB1);
119 CheckNotificationReceived(&observer1, kOrigin1, kDB1, 1, kDefaultQuota - 1);
120 CheckNotificationReceived(&observer2, kOrigin1, kDB1, 1, kDefaultQuota - 1);
121 tracker->DatabaseModified(kOrigin2, kDB2);
122 CheckNotificationReceived(&observer1, kOrigin2, kDB2, 2, kDefaultQuota - 2);
123 CheckNotificationReceived(&observer2, kOrigin2, kDB2, 2, kDefaultQuota - 2);
124 tracker->DatabaseModified(kOrigin1, kDB3);
125 CheckNotificationReceived(&observer1, kOrigin1, kDB3, 4, kDefaultQuota - 5);
126 CheckNotificationReceived(&observer2, kOrigin1, kDB3, 4, kDefaultQuota - 5);
127
128 // Open an existing database and check the reported size
129 tracker->DatabaseOpened(kOrigin1, kDB1, kDescription, 0,
130 &database_size, &space_available);
131 EXPECT_EQ(1, database_size);
132 EXPECT_EQ(kDefaultQuota - 5, space_available);
133
134 // Make sure that the observers are notified even if
135 // the size of the database hasn't changed.
136 EXPECT_EQ(1, file_util::WriteFile(
137 tracker->GetFullDBFilePath(kOrigin1, kDB1), "b", 1));
138 tracker->DatabaseModified(kOrigin1, kDB1);
139 CheckNotificationReceived(&observer1, kOrigin1, kDB1, 1, kDefaultQuota - 5);
140 CheckNotificationReceived(&observer2, kOrigin1, kDB1, 1, kDefaultQuota - 5);
141
142 // Remove an observer; this should clear all caches.
143 tracker->RemoveObserver(&observer2);
144
145 // Change kDB1's and kDB3's size and call tracker->DatabaseModified()
146 // for kDB1 only. If the caches were indeed cleared, then calling
147 // tracker->DatabaseModified() should re-populate the cache for
148 // kOrigin1 == kOrigin1, and thus, should pick up kDB3's size change too.
149 EXPECT_EQ(5, file_util::WriteFile(
150 tracker->GetFullDBFilePath(kOrigin1, kDB1), "ccccc", 5));
151 EXPECT_EQ(6, file_util::WriteFile(
152 tracker->GetFullDBFilePath(kOrigin1, kDB3), "dddddd", 6));
153 tracker->DatabaseModified(kOrigin1, kDB1);
154 CheckNotificationReceived(&observer1, kOrigin1, kDB1, 5, kDefaultQuota - 11);
155 EXPECT_FALSE(observer2.DidReceiveNewNotification());
156
157 // Close the tracker database and clear all caches.
158 // Then make sure that DatabaseOpened() still returns the correct result.
159 tracker->CloseTrackerDatabaseAndClearCaches();
160 tracker->DatabaseOpened(kOrigin1, kDB1, kDescription, 0,
161 &database_size, &space_available);
162 EXPECT_EQ(5, database_size);
163 EXPECT_EQ(kDefaultQuota - 11, space_available);
164
165 // Close the tracker database and clear all caches. Then make sure that
166 // DatabaseModified() still calls the observers with correct values.
167 tracker->CloseTrackerDatabaseAndClearCaches();
168 tracker->DatabaseModified(kOrigin1, kDB3);
169 CheckNotificationReceived(&observer1, kOrigin1, kDB3, 6, kDefaultQuota - 11);
170
171 // Clean up.
172 tracker->RemoveObserver(&observer1);
173 }
174
175 } // namespace webkit_database
OLDNEW
« no previous file with comments | « webkit/database/database_tracker.cc ('k') | webkit/database/databases_table.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698