| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/supervised_user/supervised_user_bookmarks_handler.h" |
| 6 |
| 5 #include <stddef.h> | 7 #include <stddef.h> |
| 6 | 8 |
| 7 #include <iostream> | 9 #include <iostream> |
| 8 #include <map> | 10 #include <map> |
| 11 #include <memory> |
| 9 #include <string> | 12 #include <string> |
| 10 #include <vector> | 13 #include <vector> |
| 11 | 14 |
| 12 #include "base/json/json_reader.h" | 15 #include "base/json/json_reader.h" |
| 13 #include "base/json/json_writer.h" | 16 #include "base/json/json_writer.h" |
| 14 #include "base/macros.h" | 17 #include "base/macros.h" |
| 15 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/ptr_util.h" |
| 16 #include "base/values.h" | 19 #include "base/values.h" |
| 17 #include "chrome/browser/supervised_user/supervised_user_bookmarks_handler.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 21 |
| 20 using Folder = SupervisedUserBookmarksHandler::Folder; | 22 using Folder = SupervisedUserBookmarksHandler::Folder; |
| 21 using Link = SupervisedUserBookmarksHandler::Link; | 23 using Link = SupervisedUserBookmarksHandler::Link; |
| 22 | 24 |
| 23 namespace { | 25 namespace { |
| 24 | 26 |
| 25 typedef std::pair<std::string, std::string> Setting; | 27 typedef std::pair<std::string, std::string> Setting; |
| 26 | 28 |
| 27 // Settings representing the following tree: | 29 // Settings representing the following tree: |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 " \"name\":\"\"," | 149 " \"name\":\"\"," |
| 148 " \"url\":\"http://www.theoatmeal.com/\"" | 150 " \"url\":\"http://www.theoatmeal.com/\"" |
| 149 " }" | 151 " }" |
| 150 " ]" | 152 " ]" |
| 151 " }" | 153 " }" |
| 152 " ]" | 154 " ]" |
| 153 " }" | 155 " }" |
| 154 "]"; | 156 "]"; |
| 155 | 157 |
| 156 // Builds the base::Values tree from a json string above. | 158 // Builds the base::Values tree from a json string above. |
| 157 scoped_ptr<base::ListValue> CreateTree(const char* json) { | 159 std::unique_ptr<base::ListValue> CreateTree(const char* json) { |
| 158 scoped_ptr<base::Value> value = base::JSONReader::Read(json); | 160 std::unique_ptr<base::Value> value = base::JSONReader::Read(json); |
| 159 EXPECT_NE(value.get(), nullptr); | 161 EXPECT_NE(value.get(), nullptr); |
| 160 base::ListValue* list; | 162 base::ListValue* list; |
| 161 EXPECT_TRUE(value->GetAsList(&list)); | 163 EXPECT_TRUE(value->GetAsList(&list)); |
| 162 ignore_result(value.release()); | 164 ignore_result(value.release()); |
| 163 return make_scoped_ptr(list); | 165 return base::WrapUnique(list); |
| 164 } | 166 } |
| 165 | 167 |
| 166 scoped_ptr<base::ListValue> CreateBookmarksTree() { | 168 std::unique_ptr<base::ListValue> CreateBookmarksTree() { |
| 167 return CreateTree(BOOKMARKS_TREE_JSON); | 169 return CreateTree(BOOKMARKS_TREE_JSON); |
| 168 } | 170 } |
| 169 | 171 |
| 170 scoped_ptr<base::ListValue> CreateBookmarksTreeWithInvalidParents() { | 172 std::unique_ptr<base::ListValue> CreateBookmarksTreeWithInvalidParents() { |
| 171 return CreateTree(BOOKMARKS_TREE_INVALID_PARENTS_JSON); | 173 return CreateTree(BOOKMARKS_TREE_INVALID_PARENTS_JSON); |
| 172 } | 174 } |
| 173 | 175 |
| 174 } // namespace | 176 } // namespace |
| 175 | 177 |
| 176 static bool operator==(const Folder& f1, const Folder& f2) { | 178 static bool operator==(const Folder& f1, const Folder& f2) { |
| 177 return f1.id == f2.id && f1.name == f2.name && f1.parent_id == f2.parent_id; | 179 return f1.id == f2.id && f1.name == f2.name && f1.parent_id == f2.parent_id; |
| 178 } | 180 } |
| 179 | 181 |
| 180 static bool operator==(const Link& l1, const Link& l2) { | 182 static bool operator==(const Link& l1, const Link& l2) { |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 | 257 |
| 256 const std::vector<Link>& GetLinks() const { | 258 const std::vector<Link>& GetLinks() const { |
| 257 return deserializer_.links_for_testing(); | 259 return deserializer_.links_for_testing(); |
| 258 } | 260 } |
| 259 | 261 |
| 260 private: | 262 private: |
| 261 SupervisedUserBookmarksHandler deserializer_; | 263 SupervisedUserBookmarksHandler deserializer_; |
| 262 }; | 264 }; |
| 263 | 265 |
| 264 TEST_F(SupervisedUserBookmarksHandlerTest, ParseSettings) { | 266 TEST_F(SupervisedUserBookmarksHandlerTest, ParseSettings) { |
| 265 scoped_ptr<base::DictionaryValue> link_dictionary(CreateLinkDictionary()); | 267 std::unique_ptr<base::DictionaryValue> link_dictionary( |
| 266 scoped_ptr<base::DictionaryValue> folder_dictionary(CreateFolderDictionary()); | 268 CreateLinkDictionary()); |
| 269 std::unique_ptr<base::DictionaryValue> folder_dictionary( |
| 270 CreateFolderDictionary()); |
| 267 | 271 |
| 268 ParseLinks(*link_dictionary.get()); | 272 ParseLinks(*link_dictionary.get()); |
| 269 ParseFolders(*folder_dictionary.get()); | 273 ParseFolders(*folder_dictionary.get()); |
| 270 | 274 |
| 271 const std::vector<Link>& links = GetLinks(); | 275 const std::vector<Link>& links = GetLinks(); |
| 272 EXPECT_EQ(arraysize(PARSED_LINKS), links.size()); | 276 EXPECT_EQ(arraysize(PARSED_LINKS), links.size()); |
| 273 for (size_t i = 0; i < links.size(); ++i) | 277 for (size_t i = 0; i < links.size(); ++i) |
| 274 EXPECT_EQ(PARSED_LINKS[i], links[i]); | 278 EXPECT_EQ(PARSED_LINKS[i], links[i]); |
| 275 | 279 |
| 276 const std::vector<Folder>& folders = GetFolders(); | 280 const std::vector<Folder>& folders = GetFolders(); |
| 277 EXPECT_EQ(arraysize(PARSED_FOLDERS), folders.size()); | 281 EXPECT_EQ(arraysize(PARSED_FOLDERS), folders.size()); |
| 278 for (size_t i = 0; i < folders.size(); ++i) | 282 for (size_t i = 0; i < folders.size(); ++i) |
| 279 EXPECT_EQ(PARSED_FOLDERS[i], folders[i]); | 283 EXPECT_EQ(PARSED_FOLDERS[i], folders[i]); |
| 280 } | 284 } |
| 281 | 285 |
| 282 TEST_F(SupervisedUserBookmarksHandlerTest, BuildBookmarksTree) { | 286 TEST_F(SupervisedUserBookmarksHandlerTest, BuildBookmarksTree) { |
| 283 // Make some fake settings. | 287 // Make some fake settings. |
| 284 scoped_ptr<base::DictionaryValue> settings( | 288 std::unique_ptr<base::DictionaryValue> settings( |
| 285 CreateSettings(CreateLinkDictionary(), CreateFolderDictionary())); | 289 CreateSettings(CreateLinkDictionary(), CreateFolderDictionary())); |
| 286 // Parse the settings into a bookmarks tree. | 290 // Parse the settings into a bookmarks tree. |
| 287 scoped_ptr<base::ListValue> bookmarks( | 291 std::unique_ptr<base::ListValue> bookmarks( |
| 288 SupervisedUserBookmarksHandler::BuildBookmarksTree(*settings.get())); | 292 SupervisedUserBookmarksHandler::BuildBookmarksTree(*settings.get())); |
| 289 | 293 |
| 290 // Check that the parsed tree matches the expected tree constructed directly | 294 // Check that the parsed tree matches the expected tree constructed directly |
| 291 // from the hardcoded json above. | 295 // from the hardcoded json above. |
| 292 scoped_ptr<base::ListValue> expected_bookmarks(CreateBookmarksTree()); | 296 std::unique_ptr<base::ListValue> expected_bookmarks(CreateBookmarksTree()); |
| 293 EXPECT_TRUE(bookmarks->Equals(expected_bookmarks.get())); | 297 EXPECT_TRUE(bookmarks->Equals(expected_bookmarks.get())); |
| 294 } | 298 } |
| 295 | 299 |
| 296 TEST_F(SupervisedUserBookmarksHandlerTest, | 300 TEST_F(SupervisedUserBookmarksHandlerTest, |
| 297 BuildBookmarksTreeWithInvalidParents) { | 301 BuildBookmarksTreeWithInvalidParents) { |
| 298 // Make some fake settings, including some entries with invalid parent | 302 // Make some fake settings, including some entries with invalid parent |
| 299 // references. | 303 // references. |
| 300 scoped_ptr<base::DictionaryValue> settings( | 304 std::unique_ptr<base::DictionaryValue> settings( |
| 301 CreateSettings(CreateLinkDictionaryWithInvalidParents(), | 305 CreateSettings(CreateLinkDictionaryWithInvalidParents(), |
| 302 CreateFolderDictionaryWithInvalidParents())); | 306 CreateFolderDictionaryWithInvalidParents())); |
| 303 // Parse the settings into a bookmarks tree. | 307 // Parse the settings into a bookmarks tree. |
| 304 scoped_ptr<base::ListValue> bookmarks( | 308 std::unique_ptr<base::ListValue> bookmarks( |
| 305 SupervisedUserBookmarksHandler::BuildBookmarksTree(*settings.get())); | 309 SupervisedUserBookmarksHandler::BuildBookmarksTree(*settings.get())); |
| 306 | 310 |
| 307 // Check that the parsed tree matches the expected tree constructed directly | 311 // Check that the parsed tree matches the expected tree constructed directly |
| 308 // from the hardcoded json above (which does not contain the entries with | 312 // from the hardcoded json above (which does not contain the entries with |
| 309 // invalid parents!). | 313 // invalid parents!). |
| 310 scoped_ptr<base::ListValue> expected_bookmarks( | 314 std::unique_ptr<base::ListValue> expected_bookmarks( |
| 311 CreateBookmarksTreeWithInvalidParents()); | 315 CreateBookmarksTreeWithInvalidParents()); |
| 312 EXPECT_TRUE(bookmarks->Equals(expected_bookmarks.get())); | 316 EXPECT_TRUE(bookmarks->Equals(expected_bookmarks.get())); |
| 313 } | 317 } |
| 314 | 318 |
| 315 TEST_F(SupervisedUserBookmarksHandlerTest, Circle) { | 319 TEST_F(SupervisedUserBookmarksHandlerTest, Circle) { |
| 316 // Make some fake settings which include a circular reference in the folders. | 320 // Make some fake settings which include a circular reference in the folders. |
| 317 scoped_ptr<base::DictionaryValue> settings( | 321 std::unique_ptr<base::DictionaryValue> settings(CreateSettings( |
| 318 CreateSettings(CreateLinkDictionary(), | 322 CreateLinkDictionary(), CreateFolderDictionaryWithCircle())); |
| 319 CreateFolderDictionaryWithCircle())); | 323 std::unique_ptr<base::ListValue> bookmarks( |
| 320 scoped_ptr<base::ListValue> bookmarks( | |
| 321 SupervisedUserBookmarksHandler::BuildBookmarksTree(*settings.get())); | 324 SupervisedUserBookmarksHandler::BuildBookmarksTree(*settings.get())); |
| 322 // Don't care what exactly the result looks like, just that we don't run into | 325 // Don't care what exactly the result looks like, just that we don't run into |
| 323 // an endless loop. | 326 // an endless loop. |
| 324 } | 327 } |
| OLD | NEW |