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

Side by Side Diff: components/sessions/core/session_backend_unittest.cc

Issue 2600583002: Remove ScopedVector from components/sessions. (Closed)
Patch Set: include Created 3 years, 11 months 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
OLDNEW
1 // Copyright 2011 The Chromium Authors. All rights reserved. 1 // Copyright 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/sessions/core/session_backend.h" 5 #include "components/sessions/core/session_backend.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h" 11 #include "base/files/scoped_temp_dir.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/stl_util.h" 13 #include "base/memory/ptr_util.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 16
17 namespace sessions { 17 namespace sessions {
18 namespace { 18 namespace {
19 19
20 typedef ScopedVector<sessions::SessionCommand> SessionCommands; 20 typedef std::vector<std::unique_ptr<sessions::SessionCommand>> SessionCommands;
21 21
22 struct TestData { 22 struct TestData {
23 sessions::SessionCommand::id_type command_id; 23 sessions::SessionCommand::id_type command_id;
24 std::string data; 24 std::string data;
25 }; 25 };
26 26
27 sessions::SessionCommand* CreateCommandFromData(const TestData& data) { 27 std::unique_ptr<sessions::SessionCommand> CreateCommandFromData(
28 sessions::SessionCommand* command = 28 const TestData& data) {
29 new sessions::SessionCommand( 29 std::unique_ptr<sessions::SessionCommand> command =
30 base::MakeUnique<sessions::SessionCommand>(
30 data.command_id, 31 data.command_id,
31 static_cast<sessions::SessionCommand::size_type>(data.data.size())); 32 static_cast<sessions::SessionCommand::size_type>(data.data.size()));
32 if (!data.data.empty()) 33 if (!data.data.empty())
33 memcpy(command->contents(), data.data.c_str(), data.data.size()); 34 memcpy(command->contents(), data.data.c_str(), data.data.size());
34 return command; 35 return command;
35 } 36 }
36 37
37 } // namespace 38 } // namespace
38 39
39 class SessionBackendTest : public testing::Test { 40 class SessionBackendTest : public testing::Test {
(...skipping 26 matching lines...) Expand all
66 backend->AppendCommands(std::move(commands), false); 67 backend->AppendCommands(std::move(commands), false);
67 ASSERT_TRUE(commands.empty()); 68 ASSERT_TRUE(commands.empty());
68 69
69 // Read it back in. 70 // Read it back in.
70 backend = NULL; 71 backend = NULL;
71 backend = new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE, 72 backend = new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE,
72 path_); 73 path_);
73 backend->ReadLastSessionCommandsImpl(&commands); 74 backend->ReadLastSessionCommandsImpl(&commands);
74 75
75 ASSERT_EQ(1U, commands.size()); 76 ASSERT_EQ(1U, commands.size());
76 AssertCommandEqualsData(data, commands[0]); 77 AssertCommandEqualsData(data, commands[0].get());
77 78
78 commands.clear(); 79 commands.clear();
79 80
80 backend = NULL; 81 backend = NULL;
81 backend = new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE, 82 backend = new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE,
82 path_); 83 path_);
83 backend->ReadLastSessionCommandsImpl(&commands); 84 backend->ReadLastSessionCommandsImpl(&commands);
84 85
85 ASSERT_EQ(0U, commands.size()); 86 ASSERT_EQ(0U, commands.size());
86 87
(...skipping 22 matching lines...) Expand all
109 110
110 for (size_t i = 0; i < arraysize(data); ++i) { 111 for (size_t i = 0; i < arraysize(data); ++i) {
111 scoped_refptr<SessionBackend> backend( 112 scoped_refptr<SessionBackend> backend(
112 new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE, 113 new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE,
113 path_)); 114 path_));
114 SessionCommands commands; 115 SessionCommands commands;
115 if (i != 0) { 116 if (i != 0) {
116 // Read previous data. 117 // Read previous data.
117 backend->ReadLastSessionCommandsImpl(&commands); 118 backend->ReadLastSessionCommandsImpl(&commands);
118 ASSERT_EQ(i, commands.size()); 119 ASSERT_EQ(i, commands.size());
119 for (std::vector<sessions::SessionCommand*>::iterator j = 120 for (auto j = commands.begin(); j != commands.end(); ++j)
120 commands.begin(); j != commands.end(); ++j) { 121 AssertCommandEqualsData(data[j - commands.begin()], j->get());
121 AssertCommandEqualsData(data[j - commands.begin()], *j); 122
122 }
123 backend->AppendCommands(std::move(commands), false); 123 backend->AppendCommands(std::move(commands), false);
124 } 124 }
125 commands.push_back(CreateCommandFromData(data[i])); 125 commands.push_back(CreateCommandFromData(data[i]));
126 backend->AppendCommands(std::move(commands), false); 126 backend->AppendCommands(std::move(commands), false);
127 } 127 }
128 } 128 }
129 129
130 TEST_F(SessionBackendTest, BigData) { 130 TEST_F(SessionBackendTest, BigData) {
131 struct TestData data[] = { 131 struct TestData data[] = {
132 { 1, "a" }, 132 { 1, "a" },
133 { 2, "ab" }, 133 { 2, "ab" },
134 }; 134 };
135 135
136 scoped_refptr<SessionBackend> backend( 136 scoped_refptr<SessionBackend> backend(
137 new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE, path_)); 137 new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE, path_));
138 ScopedVector<sessions::SessionCommand> commands; 138 std::vector<std::unique_ptr<sessions::SessionCommand>> commands;
139 139
140 commands.push_back(CreateCommandFromData(data[0])); 140 commands.push_back(CreateCommandFromData(data[0]));
141 const sessions::SessionCommand::size_type big_size = 141 const sessions::SessionCommand::size_type big_size =
142 SessionBackend::kFileReadBufferSize + 100; 142 SessionBackend::kFileReadBufferSize + 100;
143 const sessions::SessionCommand::id_type big_id = 50; 143 const sessions::SessionCommand::id_type big_id = 50;
144 sessions::SessionCommand* big_command = 144 std::unique_ptr<sessions::SessionCommand> big_command =
145 new sessions::SessionCommand(big_id, big_size); 145 base::MakeUnique<sessions::SessionCommand>(big_id, big_size);
146 reinterpret_cast<char*>(big_command->contents())[0] = 'a'; 146 reinterpret_cast<char*>(big_command->contents())[0] = 'a';
147 reinterpret_cast<char*>(big_command->contents())[big_size - 1] = 'z'; 147 reinterpret_cast<char*>(big_command->contents())[big_size - 1] = 'z';
148 commands.push_back(big_command); 148 commands.push_back(std::move(big_command));
149 commands.push_back(CreateCommandFromData(data[1])); 149 commands.push_back(CreateCommandFromData(data[1]));
150 backend->AppendCommands(std::move(commands), false); 150 backend->AppendCommands(std::move(commands), false);
151 151
152 backend = NULL; 152 backend = NULL;
153 backend = new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE, 153 backend = new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE,
154 path_); 154 path_);
155 155
156 backend->ReadLastSessionCommandsImpl(&commands); 156 backend->ReadLastSessionCommandsImpl(&commands);
157 ASSERT_EQ(3U, commands.size()); 157 ASSERT_EQ(3U, commands.size());
158 AssertCommandEqualsData(data[0], commands[0]); 158 AssertCommandEqualsData(data[0], commands[0].get());
159 AssertCommandEqualsData(data[1], commands[2]); 159 AssertCommandEqualsData(data[1], commands[2].get());
160 160
161 EXPECT_EQ(big_id, commands[1]->id()); 161 EXPECT_EQ(big_id, commands[1]->id());
162 ASSERT_EQ(big_size, commands[1]->size()); 162 ASSERT_EQ(big_size, commands[1]->size());
163 EXPECT_EQ('a', reinterpret_cast<char*>(commands[1]->contents())[0]); 163 EXPECT_EQ('a', reinterpret_cast<char*>(commands[1]->contents())[0]);
164 EXPECT_EQ('z', 164 EXPECT_EQ('z',
165 reinterpret_cast<char*>(commands[1]->contents())[big_size - 1]); 165 reinterpret_cast<char*>(commands[1]->contents())[big_size - 1]);
166 commands.clear(); 166 commands.clear();
167 } 167 }
168 168
169 TEST_F(SessionBackendTest, EmptyCommand) { 169 TEST_F(SessionBackendTest, EmptyCommand) {
170 TestData empty_command; 170 TestData empty_command;
171 empty_command.command_id = 1; 171 empty_command.command_id = 1;
172 scoped_refptr<SessionBackend> backend( 172 scoped_refptr<SessionBackend> backend(
173 new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE, path_)); 173 new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE, path_));
174 SessionCommands empty_commands; 174 SessionCommands empty_commands;
175 empty_commands.push_back(CreateCommandFromData(empty_command)); 175 empty_commands.push_back(CreateCommandFromData(empty_command));
176 backend->AppendCommands(std::move(empty_commands), true); 176 backend->AppendCommands(std::move(empty_commands), true);
177 backend->MoveCurrentSessionToLastSession(); 177 backend->MoveCurrentSessionToLastSession();
178 178
179 SessionCommands commands; 179 SessionCommands commands;
180 backend->ReadLastSessionCommandsImpl(&commands); 180 backend->ReadLastSessionCommandsImpl(&commands);
181 ASSERT_EQ(1U, commands.size()); 181 ASSERT_EQ(1U, commands.size());
182 AssertCommandEqualsData(empty_command, commands[0]); 182 AssertCommandEqualsData(empty_command, commands[0].get());
183 commands.clear(); 183 commands.clear();
184 } 184 }
185 185
186 // Writes a command, appends another command with reset to true, then reads 186 // Writes a command, appends another command with reset to true, then reads
187 // making sure we only get back the second command. 187 // making sure we only get back the second command.
188 TEST_F(SessionBackendTest, Truncate) { 188 TEST_F(SessionBackendTest, Truncate) {
189 scoped_refptr<SessionBackend> backend( 189 scoped_refptr<SessionBackend> backend(
190 new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE, path_)); 190 new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE, path_));
191 struct TestData first_data = { 1, "a" }; 191 struct TestData first_data = { 1, "a" };
192 SessionCommands commands; 192 SessionCommands commands;
193 commands.push_back(CreateCommandFromData(first_data)); 193 commands.push_back(CreateCommandFromData(first_data));
194 backend->AppendCommands(std::move(commands), false); 194 backend->AppendCommands(std::move(commands), false);
195 195
196 // Write another command, this time resetting the file when appending. 196 // Write another command, this time resetting the file when appending.
197 struct TestData second_data = { 2, "b" }; 197 struct TestData second_data = { 2, "b" };
198 commands.push_back(CreateCommandFromData(second_data)); 198 commands.push_back(CreateCommandFromData(second_data));
199 backend->AppendCommands(std::move(commands), true); 199 backend->AppendCommands(std::move(commands), true);
200 200
201 // Read it back in. 201 // Read it back in.
202 backend = NULL; 202 backend = NULL;
203 backend = new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE, 203 backend = new SessionBackend(sessions::BaseSessionService::SESSION_RESTORE,
204 path_); 204 path_);
205 backend->ReadLastSessionCommandsImpl(&commands); 205 backend->ReadLastSessionCommandsImpl(&commands);
206 206
207 // And make sure we get back the expected data. 207 // And make sure we get back the expected data.
208 ASSERT_EQ(1U, commands.size()); 208 ASSERT_EQ(1U, commands.size());
209 AssertCommandEqualsData(second_data, commands[0]); 209 AssertCommandEqualsData(second_data, commands[0].get());
210 210
211 commands.clear(); 211 commands.clear();
212 } 212 }
213 213
214 } // namespace sessions 214 } // namespace sessions
OLDNEW
« no previous file with comments | « components/sessions/core/session_backend.cc ('k') | components/sessions/core/session_service_commands.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698