1 // Copyright 2014 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/message_loop.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/thread_task_runner_handle.h"
9 #include "base/threading/thread.h"
10 #include "storage/common/database/database_connections.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using base::ASCIIToUTF16
;
14 using storage::DatabaseConnections
;
15 using storage::DatabaseConnectionsWrapper
;
21 void RemoveConnectionTask(
22 const std::string
& origin_id
, const base::string16
& database_name
,
23 scoped_refptr
<DatabaseConnectionsWrapper
> obj
,
24 bool* did_task_execute
) {
25 *did_task_execute
= true;
26 obj
->RemoveOpenConnection(origin_id
, database_name
);
29 void ScheduleRemoveConnectionTask(
30 base::Thread
* thread
, const std::string
& origin_id
,
31 const base::string16
& database_name
,
32 scoped_refptr
<DatabaseConnectionsWrapper
> obj
,
33 bool* did_task_execute
) {
34 thread
->task_runner()->PostTask(
35 FROM_HERE
, base::Bind(&RemoveConnectionTask
, origin_id
, database_name
,
36 obj
, did_task_execute
));
39 } // anonymous namespace
41 TEST(DatabaseConnectionsTest
, DatabaseConnectionsTest
) {
42 const std::string
kOriginId("origin_id");
43 const base::string16
kName(ASCIIToUTF16("database_name"));
44 const base::string16
kName2(ASCIIToUTF16("database_name2"));
45 const int64 kSize
= 1000;
47 DatabaseConnections connections
;
49 EXPECT_TRUE(connections
.IsEmpty());
50 EXPECT_FALSE(connections
.IsDatabaseOpened(kOriginId
, kName
));
51 EXPECT_FALSE(connections
.IsOriginUsed(kOriginId
));
53 connections
.AddConnection(kOriginId
, kName
);
54 EXPECT_FALSE(connections
.IsEmpty());
55 EXPECT_TRUE(connections
.IsDatabaseOpened(kOriginId
, kName
));
56 EXPECT_TRUE(connections
.IsOriginUsed(kOriginId
));
57 EXPECT_EQ(0, connections
.GetOpenDatabaseSize(kOriginId
, kName
));
58 connections
.SetOpenDatabaseSize(kOriginId
, kName
, kSize
);
59 EXPECT_EQ(kSize
, connections
.GetOpenDatabaseSize(kOriginId
, kName
));
61 connections
.RemoveConnection(kOriginId
, kName
);
62 EXPECT_TRUE(connections
.IsEmpty());
63 EXPECT_FALSE(connections
.IsDatabaseOpened(kOriginId
, kName
));
64 EXPECT_FALSE(connections
.IsOriginUsed(kOriginId
));
66 connections
.AddConnection(kOriginId
, kName
);
67 connections
.SetOpenDatabaseSize(kOriginId
, kName
, kSize
);
68 EXPECT_EQ(kSize
, connections
.GetOpenDatabaseSize(kOriginId
, kName
));
69 connections
.AddConnection(kOriginId
, kName
);
70 EXPECT_EQ(kSize
, connections
.GetOpenDatabaseSize(kOriginId
, kName
));
71 EXPECT_FALSE(connections
.IsEmpty());
72 EXPECT_TRUE(connections
.IsDatabaseOpened(kOriginId
, kName
));
73 EXPECT_TRUE(connections
.IsOriginUsed(kOriginId
));
74 connections
.AddConnection(kOriginId
, kName2
);
75 EXPECT_TRUE(connections
.IsDatabaseOpened(kOriginId
, kName2
));
77 DatabaseConnections another
;
78 another
.AddConnection(kOriginId
, kName
);
79 another
.AddConnection(kOriginId
, kName2
);
81 std::vector
<std::pair
<std::string
, base::string16
> > closed_dbs
;
82 connections
.RemoveConnections(another
, &closed_dbs
);
83 EXPECT_EQ(1u, closed_dbs
.size());
84 EXPECT_EQ(kOriginId
, closed_dbs
[0].first
);
85 EXPECT_EQ(kName2
, closed_dbs
[0].second
);
86 EXPECT_FALSE(connections
.IsDatabaseOpened(kOriginId
, kName2
));
87 EXPECT_TRUE(connections
.IsDatabaseOpened(kOriginId
, kName
));
88 EXPECT_EQ(kSize
, connections
.GetOpenDatabaseSize(kOriginId
, kName
));
89 another
.RemoveAllConnections();
90 connections
.RemoveAllConnections();
91 EXPECT_TRUE(connections
.IsEmpty());
93 // Ensure the return value properly indicates the initial
94 // addition and final removal.
95 EXPECT_TRUE(connections
.AddConnection(kOriginId
, kName
));
96 EXPECT_FALSE(connections
.AddConnection(kOriginId
, kName
));
97 EXPECT_FALSE(connections
.AddConnection(kOriginId
, kName
));
98 EXPECT_FALSE(connections
.RemoveConnection(kOriginId
, kName
));
99 EXPECT_FALSE(connections
.RemoveConnection(kOriginId
, kName
));
100 EXPECT_TRUE(connections
.RemoveConnection(kOriginId
, kName
));
103 TEST(DatabaseConnectionsTest
, DatabaseConnectionsWrapperTest
) {
104 const std::string
kOriginId("origin_id");
105 const base::string16
kName(ASCIIToUTF16("database_name"));
107 base::MessageLoop message_loop
;
108 scoped_refptr
<DatabaseConnectionsWrapper
> obj(new DatabaseConnectionsWrapper
);
109 EXPECT_FALSE(obj
->HasOpenConnections());
110 obj
->AddOpenConnection(kOriginId
, kName
);
111 EXPECT_TRUE(obj
->HasOpenConnections());
112 obj
->AddOpenConnection(kOriginId
, kName
);
113 EXPECT_TRUE(obj
->HasOpenConnections());
114 obj
->RemoveOpenConnection(kOriginId
, kName
);
115 EXPECT_TRUE(obj
->HasOpenConnections());
116 obj
->RemoveOpenConnection(kOriginId
, kName
);
117 EXPECT_FALSE(obj
->HasOpenConnections());
118 obj
->WaitForAllDatabasesToClose(); // should return immediately
120 // Test WaitForAllDatabasesToClose with the last connection
121 // being removed on the current thread.
122 obj
->AddOpenConnection(kOriginId
, kName
);
123 bool did_task_execute
= false;
124 base::ThreadTaskRunnerHandle::Get()->PostTask(
125 FROM_HERE
, base::Bind(&RemoveConnectionTask
, kOriginId
, kName
, obj
,
127 obj
->WaitForAllDatabasesToClose(); // should return after the task executes
128 EXPECT_TRUE(did_task_execute
);
129 EXPECT_FALSE(obj
->HasOpenConnections());
131 // Test WaitForAllDatabasesToClose with the last connection
132 // being removed on another thread.
133 obj
->AddOpenConnection(kOriginId
, kName
);
134 base::Thread
thread("WrapperTestThread");
136 did_task_execute
= false;
137 base::ThreadTaskRunnerHandle::Get()->PostTask(
138 FROM_HERE
, base::Bind(&ScheduleRemoveConnectionTask
, &thread
, kOriginId
,
139 kName
, obj
, &did_task_execute
));
140 obj
->WaitForAllDatabasesToClose(); // should return after the task executes
141 EXPECT_TRUE(did_task_execute
);
142 EXPECT_FALSE(obj
->HasOpenConnections());
145 } // namespace content