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"
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"
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(
36 const string
& originator_cache_guid
,
37 const string
& originator_client_item_id
)
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(
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()
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
,
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
,
98 originator_cache_guid_
,
99 originator_client_item_id_
,
108 } // namespace fake_server