1 // Copyright (c) 2011 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/file_util.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/stl_util.h"
8 #include "base/strings/string_util.h"
9 #include "chrome/browser/sessions/session_backend.h"
10 #include "testing/gtest/include/gtest/gtest.h"
14 typedef std::vector
<SessionCommand
*> SessionCommands
;
17 SessionCommand::id_type command_id
;
21 SessionCommand
* CreateCommandFromData(const TestData
& data
) {
22 SessionCommand
* command
=
25 static_cast<SessionCommand::size_type
>(data
.data
.size()));
26 if (!data
.data
.empty())
27 memcpy(command
->contents(), data
.data
.c_str(), data
.data
.size());
33 class SessionBackendTest
: public testing::Test
{
35 virtual void SetUp() {
36 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
37 path_
= temp_dir_
.path().Append(FILE_PATH_LITERAL("SessionTestDirs"));
38 base::CreateDirectory(path_
);
41 void AssertCommandEqualsData(const TestData
& data
, SessionCommand
* command
) {
42 EXPECT_EQ(data
.command_id
, command
->id());
43 EXPECT_EQ(data
.data
.size(), command
->size());
45 memcmp(command
->contents(), data
.data
.c_str(), command
->size()) == 0);
48 // Path used in testing.
50 base::ScopedTempDir temp_dir_
;
53 TEST_F(SessionBackendTest
, SimpleReadWrite
) {
54 scoped_refptr
<SessionBackend
> backend(
55 new SessionBackend(BaseSessionService::SESSION_RESTORE
, path_
));
56 struct TestData data
= { 1, "a" };
57 std::vector
<SessionCommand
*> commands
;
58 commands
.push_back(CreateCommandFromData(data
));
59 backend
->AppendCommands(new SessionCommands(commands
), false);
64 backend
= new SessionBackend(BaseSessionService::SESSION_RESTORE
, path_
);
65 backend
->ReadLastSessionCommandsImpl(&commands
);
67 ASSERT_EQ(1U, commands
.size());
68 AssertCommandEqualsData(data
, commands
[0]);
70 STLDeleteElements(&commands
);
73 backend
= new SessionBackend(BaseSessionService::SESSION_RESTORE
, path_
);
74 backend
->ReadLastSessionCommandsImpl(&commands
);
76 ASSERT_EQ(0U, commands
.size());
78 // Make sure we can delete.
79 backend
->DeleteLastSession();
80 backend
->ReadLastSessionCommandsImpl(&commands
);
81 ASSERT_EQ(0U, commands
.size());
84 TEST_F(SessionBackendTest
, RandomData
) {
85 struct TestData data
[] = {
96 { 11, "abcdefghijk" },
97 { 12, "abcdefghijkl" },
98 { 13, "abcdefghijklm" },
101 for (size_t i
= 0; i
< arraysize(data
); ++i
) {
102 scoped_refptr
<SessionBackend
> backend(
103 new SessionBackend(BaseSessionService::SESSION_RESTORE
, path_
));
104 std::vector
<SessionCommand
*> commands
;
106 // Read previous data.
107 backend
->ReadLastSessionCommandsImpl(&commands
);
108 ASSERT_EQ(i
, commands
.size());
109 for (std::vector
<SessionCommand
*>::iterator j
= commands
.begin();
110 j
!= commands
.end(); ++j
) {
111 AssertCommandEqualsData(data
[j
- commands
.begin()], *j
);
113 backend
->AppendCommands(new SessionCommands(commands
), false);
116 commands
.push_back(CreateCommandFromData(data
[i
]));
117 backend
->AppendCommands(new SessionCommands(commands
), false);
121 TEST_F(SessionBackendTest
, BigData
) {
122 struct TestData data
[] = {
127 scoped_refptr
<SessionBackend
> backend(
128 new SessionBackend(BaseSessionService::SESSION_RESTORE
, path_
));
129 std::vector
<SessionCommand
*> commands
;
130 commands
.push_back(CreateCommandFromData(data
[0]));
131 const SessionCommand::size_type big_size
=
132 SessionBackend::kFileReadBufferSize
+ 100;
133 const SessionCommand::id_type big_id
= 50;
134 SessionCommand
* big_command
= new SessionCommand(big_id
, big_size
);
135 reinterpret_cast<char*>(big_command
->contents())[0] = 'a';
136 reinterpret_cast<char*>(big_command
->contents())[big_size
- 1] = 'z';
137 commands
.push_back(big_command
);
138 commands
.push_back(CreateCommandFromData(data
[1]));
139 backend
->AppendCommands(new SessionCommands(commands
), false);
143 backend
= new SessionBackend(BaseSessionService::SESSION_RESTORE
, path_
);
145 backend
->ReadLastSessionCommandsImpl(&commands
);
146 ASSERT_EQ(3U, commands
.size());
147 AssertCommandEqualsData(data
[0], commands
[0]);
148 AssertCommandEqualsData(data
[1], commands
[2]);
150 EXPECT_EQ(big_id
, commands
[1]->id());
151 ASSERT_EQ(big_size
, commands
[1]->size());
152 EXPECT_EQ('a', reinterpret_cast<char*>(commands
[1]->contents())[0]);
154 reinterpret_cast<char*>(commands
[1]->contents())[big_size
- 1]);
155 STLDeleteElements(&commands
);
158 TEST_F(SessionBackendTest
, EmptyCommand
) {
159 TestData empty_command
;
160 empty_command
.command_id
= 1;
161 scoped_refptr
<SessionBackend
> backend(
162 new SessionBackend(BaseSessionService::SESSION_RESTORE
, path_
));
163 std::vector
<SessionCommand
*>* empty_commands
=
164 new std::vector
<SessionCommand
*>();
165 empty_commands
->push_back(CreateCommandFromData(empty_command
));
166 backend
->AppendCommands(empty_commands
, true);
167 backend
->MoveCurrentSessionToLastSession();
169 std::vector
<SessionCommand
*> commands
;
170 backend
->ReadLastSessionCommandsImpl(&commands
);
171 ASSERT_EQ(1U, commands
.size());
172 AssertCommandEqualsData(empty_command
, commands
[0]);
173 STLDeleteElements(&commands
);
176 // Writes a command, appends another command with reset to true, then reads
177 // making sure we only get back the second command.
178 TEST_F(SessionBackendTest
, Truncate
) {
179 scoped_refptr
<SessionBackend
> backend(
180 new SessionBackend(BaseSessionService::SESSION_RESTORE
, path_
));
181 struct TestData first_data
= { 1, "a" };
182 std::vector
<SessionCommand
*> commands
;
183 commands
.push_back(CreateCommandFromData(first_data
));
184 backend
->AppendCommands(new SessionCommands(commands
), false);
187 // Write another command, this time resetting the file when appending.
188 struct TestData second_data
= { 2, "b" };
189 commands
.push_back(CreateCommandFromData(second_data
));
190 backend
->AppendCommands(new SessionCommands(commands
), true);
195 backend
= new SessionBackend(BaseSessionService::SESSION_RESTORE
, path_
);
196 backend
->ReadLastSessionCommandsImpl(&commands
);
198 // And make sure we get back the expected data.
199 ASSERT_EQ(1U, commands
.size());
200 AssertCommandEqualsData(second_data
, commands
[0]);
202 STLDeleteElements(&commands
);