summaryrefslogtreecommitdiffstats
path: root/chrome/browser/supervised_user/supervised_user_bookmarks_handler_unittest.cc
blob: d259cf7766b0151a1bf260e663fd5c5af3548721 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <iostream>
#include <map>
#include <string>
#include <vector>

#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "chrome/browser/supervised_user/supervised_user_bookmarks_handler.h"
#include "testing/gtest/include/gtest/gtest.h"

using Folder = SupervisedUserBookmarksHandler::Folder;
using Link = SupervisedUserBookmarksHandler::Link;

namespace {

typedef std::pair<std::string, std::string> Setting;

// Settings representing the following tree:
// |--Folder1
//    |--SubFolder
//       |--Empty SubSubFolder
//       |--Google(google.com)
//       |--(www.theoatmeal.com)
//    |--Test(www.test.de)
// |--Empty Folder
// |--XKCD(m.xkcd.com)

// Folder setting format: "<ID>", "<ParentID>:<Name>"
const Setting FOLDER_SETTINGS[] = {
  Setting("5", "0:Folder1"),
  Setting("9", "0:Empty Folder"),
  Setting("3", "5:SubFolder"),
  Setting("4", "3:Empty SubSubFolder"),
};

const Setting FOLDER_SETTINGS_INVALID_PARENT[] = {
    Setting("5", "0:Folder1"),
    Setting("9", "7:Empty Folder"),  // Invalid parent id.
    Setting("3", "5:SubFolder"),
    Setting("4", "3:Empty SubSubFolder"),
};

const Setting FOLDER_SETTINGS_CIRCLE[] = {
  Setting("5", "3:Folder1"),
  Setting("9", "5:Empty Folder"),
  Setting("3", "9:SubFolder"),
};

// Link setting format: "<ID>:<URL>", "<ParentID>:<Name>"
const Setting LINK_SETTINGS[] = {
  Setting("4:www.theoatmeal.com", "3:"),
  Setting("1:m.xkcd.com", "0:XKCD"),
  Setting("2:www.test.de", "5:Test"),
  Setting("3:google.com", "3:Google"),
};

const Setting LINK_SETTINGS_INVALID_PARENT[] = {
  Setting("4:www.theoatmeal.com", "3:"),
  Setting("1:m.xkcd.com", "7:XKCD"),  // Invalid parent id.
  Setting("2:www.test.de", "8:Test"),  // Invalid parent id.
};

// Parsed data is sorted by ID (even though the parsed links don't actually
// contain their IDs!)

const Folder PARSED_FOLDERS[] = {
  Folder(3, "SubFolder", 5),  // Note: Forward reference.
  Folder(4, "Empty SubSubFolder", 3),
  Folder(5, "Folder1", 0),
  Folder(9, "Empty Folder", 0),
};

const Link PARSED_LINKS[] = {
  Link("m.xkcd.com", "XKCD", 0),
  Link("www.test.de", "Test", 5),
  Link("google.com", "Google", 3),
  Link("www.theoatmeal.com", std::string(), 3),
};

const char BOOKMARKS_TREE_JSON[] =
    "["
    "  {"
    "    \"id\":5,"
    "    \"name\":\"Folder1\","
    "    \"children\":["
    "      {"
    "        \"id\":3,"
    "        \"name\":\"SubFolder\","
    "        \"children\":["
    "          {"
    "            \"id\":4,"
    "            \"name\":\"Empty SubSubFolder\","
    "            \"children\":[]"
    "          },"
    "          {"
    "            \"name\":\"Google\","
    "            \"url\":\"http://google.com/\""
    "          },"
    "          {"
    "            \"name\":\"\","
    "            \"url\":\"http://www.theoatmeal.com/\""
    "          }"
    "        ]"
    "      },"
    "      {"
    "        \"name\":\"Test\","
    "        \"url\":\"http://www.test.de/\""
    "      }"
    "    ]"
    "  },"
    "  {"
    "    \"id\":9,"
    "    \"name\":\"Empty Folder\","
    "    \"children\":[]"
    "  },"
    "  {"
    "    \"name\":\"XKCD\","
    "    \"url\":\"http://m.xkcd.com/\""
    "  }"
    "]";

const char BOOKMARKS_TREE_INVALID_PARENTS_JSON[] =
    "["
    "  {"
    "    \"id\":5,"
    "    \"name\":\"Folder1\","
    "    \"children\":["
    "      {"
    "        \"id\":3,"
    "        \"name\":\"SubFolder\","
    "        \"children\":["
    "          {"
    "            \"id\":4,"
    "            \"name\":\"Empty SubSubFolder\","
    "            \"children\":[]"
    "          },"
    "          {"
    "            \"name\":\"\","
    "            \"url\":\"http://www.theoatmeal.com/\""
    "          }"
    "        ]"
    "      }"
    "    ]"
    "  }"
    "]";

// Builds the base::Values tree from a json string above.
scoped_ptr<base::ListValue> CreateTree(const char* json) {
  base::Value* value = base::JSONReader::DeprecatedRead(json);
  EXPECT_NE(value, nullptr);
  base::ListValue* list;
  EXPECT_TRUE(value->GetAsList(&list));
  return make_scoped_ptr(list);
}

scoped_ptr<base::ListValue> CreateBookmarksTree() {
  return CreateTree(BOOKMARKS_TREE_JSON);
}

scoped_ptr<base::ListValue> CreateBookmarksTreeWithInvalidParents() {
  return CreateTree(BOOKMARKS_TREE_INVALID_PARENTS_JSON);
}

}  // namespace

static bool operator==(const Folder& f1, const Folder& f2) {
  return f1.id == f2.id && f1.name == f2.name && f1.parent_id == f2.parent_id;
}

static bool operator==(const Link& l1, const Link& l2) {
  return l1.url == l2.url && l1.name == l2.name && l1.parent_id == l2.parent_id;
}

std::ostream& operator<<(std::ostream& str, const Folder& folder) {
  str << folder.id << " " << folder.name << " " << folder.parent_id;
  return str;
}

std::ostream& operator<<(std::ostream& str, const Link& link) {
  str << link.url << " " << link.name << " " << link.parent_id;
  return str;
}

class SupervisedUserBookmarksHandlerTest : public ::testing::Test {
 protected:
  static base::DictionaryValue* CreateSettings(base::DictionaryValue* links,
                                               base::DictionaryValue* folders) {
    base::DictionaryValue* settings = new base::DictionaryValue;
    settings->SetStringWithoutPathExpansion("some_setting", "bleh");
    settings->SetWithoutPathExpansion("SupervisedBookmarkLink", links);
    settings->SetStringWithoutPathExpansion("some_other_setting", "foo");
    settings->SetWithoutPathExpansion("SupervisedBookmarkFolder", folders);
    settings->SetStringWithoutPathExpansion("another_one", "blurb");
    return settings;
  }

  static base::DictionaryValue* CreateDictionary(const Setting* begin,
                                                 const Setting* end) {
    base::DictionaryValue* dict = new base::DictionaryValue;
    for (const Setting* setting = begin; setting != end; ++setting)
      dict->SetStringWithoutPathExpansion(setting->first, setting->second);
    return dict;
  }

  static base::DictionaryValue* CreateLinkDictionary() {
    return CreateDictionary(LINK_SETTINGS,
                            LINK_SETTINGS + arraysize(LINK_SETTINGS));
  }

  static base::DictionaryValue* CreateLinkDictionaryWithInvalidParents() {
    return CreateDictionary(
        LINK_SETTINGS_INVALID_PARENT,
        LINK_SETTINGS_INVALID_PARENT + arraysize(LINK_SETTINGS_INVALID_PARENT));
  }

  static base::DictionaryValue* CreateFolderDictionary() {
    return CreateDictionary(FOLDER_SETTINGS,
                            FOLDER_SETTINGS + arraysize(FOLDER_SETTINGS));
  }

  static base::DictionaryValue* CreateFolderDictionaryWithInvalidParents() {
    return CreateDictionary(
        FOLDER_SETTINGS_INVALID_PARENT,
        FOLDER_SETTINGS_INVALID_PARENT +
            arraysize(FOLDER_SETTINGS_INVALID_PARENT));
  }

  static base::DictionaryValue* CreateFolderDictionaryWithCircle() {
    return CreateDictionary(
        FOLDER_SETTINGS_CIRCLE,
        FOLDER_SETTINGS_CIRCLE + arraysize(FOLDER_SETTINGS_CIRCLE));
  }

  void ParseFolders(const base::DictionaryValue& folders) {
    deserializer_.ParseFolders(folders);
  }

  void ParseLinks(const base::DictionaryValue& links) {
    deserializer_.ParseLinks(links);
  }

  const std::vector<Folder>& GetFolders() const {
    return deserializer_.folders_for_testing();
  }

  const std::vector<Link>& GetLinks() const {
    return deserializer_.links_for_testing();
  }

 private:
  SupervisedUserBookmarksHandler deserializer_;
};

TEST_F(SupervisedUserBookmarksHandlerTest, ParseSettings) {
  scoped_ptr<base::DictionaryValue> link_dictionary(CreateLinkDictionary());
  scoped_ptr<base::DictionaryValue> folder_dictionary(CreateFolderDictionary());

  ParseLinks(*link_dictionary.get());
  ParseFolders(*folder_dictionary.get());

  const std::vector<Link>& links = GetLinks();
  EXPECT_EQ(arraysize(PARSED_LINKS), links.size());
  for (size_t i = 0; i < links.size(); ++i)
    EXPECT_EQ(PARSED_LINKS[i], links[i]);

  const std::vector<Folder>& folders = GetFolders();
  EXPECT_EQ(arraysize(PARSED_FOLDERS), folders.size());
  for (size_t i = 0; i < folders.size(); ++i)
    EXPECT_EQ(PARSED_FOLDERS[i], folders[i]);
}

TEST_F(SupervisedUserBookmarksHandlerTest, BuildBookmarksTree) {
  // Make some fake settings.
  scoped_ptr<base::DictionaryValue> settings(
      CreateSettings(CreateLinkDictionary(), CreateFolderDictionary()));
  // Parse the settings into a bookmarks tree.
  scoped_ptr<base::ListValue> bookmarks(
      SupervisedUserBookmarksHandler::BuildBookmarksTree(*settings.get()));

  // Check that the parsed tree matches the expected tree constructed directly
  // from the hardcoded json above.
  scoped_ptr<base::ListValue> expected_bookmarks(CreateBookmarksTree());
  EXPECT_TRUE(bookmarks->Equals(expected_bookmarks.get()));
}

TEST_F(SupervisedUserBookmarksHandlerTest,
       BuildBookmarksTreeWithInvalidParents) {
  // Make some fake settings, including some entries with invalid parent
  // references.
  scoped_ptr<base::DictionaryValue> settings(
      CreateSettings(CreateLinkDictionaryWithInvalidParents(),
                     CreateFolderDictionaryWithInvalidParents()));
  // Parse the settings into a bookmarks tree.
  scoped_ptr<base::ListValue> bookmarks(
      SupervisedUserBookmarksHandler::BuildBookmarksTree(*settings.get()));

  // Check that the parsed tree matches the expected tree constructed directly
  // from the hardcoded json above (which does not contain the entries with
  // invalid parents!).
  scoped_ptr<base::ListValue> expected_bookmarks(
      CreateBookmarksTreeWithInvalidParents());
  EXPECT_TRUE(bookmarks->Equals(expected_bookmarks.get()));
}

TEST_F(SupervisedUserBookmarksHandlerTest, Circle) {
  // Make some fake settings which include a circular reference in the folders.
  scoped_ptr<base::DictionaryValue> settings(
      CreateSettings(CreateLinkDictionary(),
                     CreateFolderDictionaryWithCircle()));
  scoped_ptr<base::ListValue> bookmarks(
      SupervisedUserBookmarksHandler::BuildBookmarksTree(*settings.get()));
  // Don't care what exactly the result looks like, just that we don't run into
  // an endless loop.
}