Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / sync / test / fake_server / bookmark_entity_builder.cc
blobc73fb08bb4d3b25d87e92a939fab459f6017b640
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.
5 #include "sync/test/fake_server/bookmark_entity_builder.h"
7 #include <string>
9 #include "base/guid.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/time/time.h"
12 #include "sync/internal_api/public/base/model_type.h"
13 #include "sync/internal_api/public/base/unique_position.h"
14 #include "sync/protocol/sync.pb.h"
15 #include "sync/syncable/syncable_util.h"
16 #include "sync/test/fake_server/bookmark_entity.h"
17 #include "sync/test/fake_server/fake_server_entity.h"
18 #include "sync/util/time.h"
19 #include "url/gurl.h"
21 using std::string;
23 using syncer::syncable::GenerateSyncableBookmarkHash;
25 // A version must be passed when creating a FakeServerEntity, but this value
26 // is overrideen immediately when saving the entity in FakeServer.
27 const int64 kUnusedVersion = 0L;
29 // Default time (creation and last modified) used when creating entities.
30 const int64 kDefaultTime = 1234L;
32 namespace fake_server {
34 BookmarkEntityBuilder::BookmarkEntityBuilder(
35 const string& title,
36 const string& originator_cache_guid,
37 const string& originator_client_item_id)
38 : title_(title),
39 originator_cache_guid_(originator_cache_guid),
40 originator_client_item_id_(originator_client_item_id) {
43 BookmarkEntityBuilder::~BookmarkEntityBuilder() {
46 void BookmarkEntityBuilder::SetParentId(const std::string& parent_id) {
47 parent_id_ = parent_id;
50 scoped_ptr<FakeServerEntity> BookmarkEntityBuilder::BuildBookmark(
51 const GURL& url) {
52 if (!url.is_valid()) {
53 return make_scoped_ptr<FakeServerEntity>(NULL);
56 sync_pb::EntitySpecifics entity_specifics = CreateBaseEntitySpecifics();
57 entity_specifics.mutable_bookmark()->set_url(url.spec());
58 const bool kIsNotFolder = false;
59 return Build(entity_specifics, kIsNotFolder);
62 scoped_ptr<FakeServerEntity> BookmarkEntityBuilder::BuildFolder() {
63 const bool kIsFolder = true;
64 return Build(CreateBaseEntitySpecifics(), kIsFolder);
67 sync_pb::EntitySpecifics BookmarkEntityBuilder::CreateBaseEntitySpecifics()
68 const {
69 sync_pb::EntitySpecifics entity_specifics;
70 sync_pb::BookmarkSpecifics* bookmark_specifics =
71 entity_specifics.mutable_bookmark();
72 bookmark_specifics->set_title(title_);
74 return entity_specifics;
77 scoped_ptr<FakeServerEntity> BookmarkEntityBuilder::Build(
78 const sync_pb::EntitySpecifics& entity_specifics,
79 bool is_folder) {
80 sync_pb::UniquePosition unique_position;
81 // TODO(pvalenzuela): Allow caller customization of the position integer.
82 const string suffix = GenerateSyncableBookmarkHash(
83 originator_cache_guid_,
84 originator_client_item_id_);
85 syncer::UniquePosition::FromInt64(0, suffix).ToProto(&unique_position);
87 if (parent_id_.empty()) {
88 parent_id_ = FakeServerEntity::CreateId(syncer::BOOKMARKS, "bookmark_bar");
91 const string id = FakeServerEntity::CreateId(syncer::BOOKMARKS,
92 base::GenerateGUID());
94 return make_scoped_ptr<FakeServerEntity>(
95 new BookmarkEntity(id,
96 kUnusedVersion,
97 title_,
98 originator_cache_guid_,
99 originator_client_item_id_,
100 unique_position,
101 entity_specifics,
102 is_folder,
103 parent_id_,
104 kDefaultTime,
105 kDefaultTime));
108 } // namespace fake_server