1 // Copyright (c) 2012 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.
6 #include "base/message_loop.h"
7 #include "base/threading/thread.h"
8 #include "base/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "webkit/database/database_connections.h"
12 namespace webkit_database
{
16 void RemoveConnectionTask(
17 const string16
& origin_id
, const string16
& database_name
,
18 scoped_refptr
<DatabaseConnectionsWrapper
> obj
,
19 bool* did_task_execute
) {
20 *did_task_execute
= true;
21 obj
->RemoveOpenConnection(origin_id
, database_name
);
24 void ScheduleRemoveConnectionTask(
25 base::Thread
* thread
, const string16
& origin_id
,
26 const string16
& database_name
,
27 scoped_refptr
<DatabaseConnectionsWrapper
> obj
,
28 bool* did_task_execute
) {
29 thread
->message_loop()->PostTask(
31 base::Bind(&RemoveConnectionTask
, origin_id
, database_name
, obj
,
35 } // anonymous namespace
37 TEST(DatabaseConnectionsTest
, DatabaseConnectionsTest
) {
38 const string16
kOriginId(ASCIIToUTF16("origin_id"));
39 const string16
kName(ASCIIToUTF16("database_name"));
40 const string16
kName2(ASCIIToUTF16("database_name2"));
41 const int64 kSize
= 1000;
43 DatabaseConnections connections
;
45 EXPECT_TRUE(connections
.IsEmpty());
46 EXPECT_FALSE(connections
.IsDatabaseOpened(kOriginId
, kName
));
47 EXPECT_FALSE(connections
.IsOriginUsed(kOriginId
));
49 connections
.AddConnection(kOriginId
, kName
);
50 EXPECT_FALSE(connections
.IsEmpty());
51 EXPECT_TRUE(connections
.IsDatabaseOpened(kOriginId
, kName
));
52 EXPECT_TRUE(connections
.IsOriginUsed(kOriginId
));
53 EXPECT_EQ(0, connections
.GetOpenDatabaseSize(kOriginId
, kName
));
54 connections
.SetOpenDatabaseSize(kOriginId
, kName
, kSize
);
55 EXPECT_EQ(kSize
, connections
.GetOpenDatabaseSize(kOriginId
, kName
));
57 connections
.RemoveConnection(kOriginId
, kName
);
58 EXPECT_TRUE(connections
.IsEmpty());
59 EXPECT_FALSE(connections
.IsDatabaseOpened(kOriginId
, kName
));
60 EXPECT_FALSE(connections
.IsOriginUsed(kOriginId
));
62 connections
.AddConnection(kOriginId
, kName
);
63 connections
.SetOpenDatabaseSize(kOriginId
, kName
, kSize
);
64 EXPECT_EQ(kSize
, connections
.GetOpenDatabaseSize(kOriginId
, kName
));
65 connections
.AddConnection(kOriginId
, kName
);
66 EXPECT_EQ(kSize
, connections
.GetOpenDatabaseSize(kOriginId
, kName
));
67 EXPECT_FALSE(connections
.IsEmpty());
68 EXPECT_TRUE(connections
.IsDatabaseOpened(kOriginId
, kName
));
69 EXPECT_TRUE(connections
.IsOriginUsed(kOriginId
));
70 connections
.AddConnection(kOriginId
, kName2
);
71 EXPECT_TRUE(connections
.IsDatabaseOpened(kOriginId
, kName2
));
73 DatabaseConnections another
;
74 another
.AddConnection(kOriginId
, kName
);
75 another
.AddConnection(kOriginId
, kName2
);
77 std::vector
<std::pair
<string16
, string16
> > closed_dbs
;
78 connections
.RemoveConnections(another
, &closed_dbs
);
79 EXPECT_EQ(1u, closed_dbs
.size());
80 EXPECT_EQ(kOriginId
, closed_dbs
[0].first
);
81 EXPECT_EQ(kName2
, closed_dbs
[0].second
);
82 EXPECT_FALSE(connections
.IsDatabaseOpened(kOriginId
, kName2
));
83 EXPECT_TRUE(connections
.IsDatabaseOpened(kOriginId
, kName
));
84 EXPECT_EQ(kSize
, connections
.GetOpenDatabaseSize(kOriginId
, kName
));
85 another
.RemoveAllConnections();
86 connections
.RemoveAllConnections();
87 EXPECT_TRUE(connections
.IsEmpty());
89 // Ensure the return value properly indicates the initial
90 // addition and final removal.
91 EXPECT_TRUE(connections
.AddConnection(kOriginId
, kName
));
92 EXPECT_FALSE(connections
.AddConnection(kOriginId
, kName
));
93 EXPECT_FALSE(connections
.AddConnection(kOriginId
, kName
));
94 EXPECT_FALSE(connections
.RemoveConnection(kOriginId
, kName
));
95 EXPECT_FALSE(connections
.RemoveConnection(kOriginId
, kName
));
96 EXPECT_TRUE(connections
.RemoveConnection(kOriginId
, kName
));
99 TEST(DatabaseConnectionsTest
, DatabaseConnectionsWrapperTest
) {
100 const string16
kOriginId(ASCIIToUTF16("origin_id"));
101 const string16
kName(ASCIIToUTF16("database_name"));
103 MessageLoop message_loop
;
104 scoped_refptr
<DatabaseConnectionsWrapper
> obj(
105 new DatabaseConnectionsWrapper
);
106 EXPECT_FALSE(obj
->HasOpenConnections());
107 obj
->AddOpenConnection(kOriginId
, kName
);
108 EXPECT_TRUE(obj
->HasOpenConnections());
109 obj
->AddOpenConnection(kOriginId
, kName
);
110 EXPECT_TRUE(obj
->HasOpenConnections());
111 obj
->RemoveOpenConnection(kOriginId
, kName
);
112 EXPECT_TRUE(obj
->HasOpenConnections());
113 obj
->RemoveOpenConnection(kOriginId
, kName
);
114 EXPECT_FALSE(obj
->HasOpenConnections());
115 obj
->WaitForAllDatabasesToClose(); // should return immediately
117 // Test WaitForAllDatabasesToClose with the last connection
118 // being removed on the current thread.
119 obj
->AddOpenConnection(kOriginId
, kName
);
120 bool did_task_execute
= false;
121 MessageLoop::current()->PostTask(
123 base::Bind(&RemoveConnectionTask
, kOriginId
, kName
, obj
,
125 obj
->WaitForAllDatabasesToClose(); // should return after the task executes
126 EXPECT_TRUE(did_task_execute
);
127 EXPECT_FALSE(obj
->HasOpenConnections());
129 // Test WaitForAllDatabasesToClose with the last connection
130 // being removed on another thread.
131 obj
->AddOpenConnection(kOriginId
, kName
);
132 base::Thread
thread("WrapperTestThread");
134 did_task_execute
= false;
135 MessageLoop::current()->PostTask(
137 base::Bind(&ScheduleRemoveConnectionTask
, &thread
, kOriginId
, kName
, obj
,
139 obj
->WaitForAllDatabasesToClose(); // should return after the task executes
140 EXPECT_TRUE(did_task_execute
);
141 EXPECT_FALSE(obj
->HasOpenConnections());
144 } // namespace webkit_database