summaryrefslogtreecommitdiffstats
path: root/sync/internal_api/test/test_entry_factory.cc
blob: 512fe55b9e1a288360b637318fad5fc4fdd2d10f (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
// Copyright (c) 2012 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 "sync/internal_api/public/test/test_entry_factory.h"

#include "sync/syncable/directory.h"
#include "sync/syncable/mutable_entry.h"
#include "sync/syncable/syncable_id.h"
#include "sync/syncable/write_transaction.h"
#include "sync/test/engine/test_id_factory.h"

using std::string;

namespace syncer {

using syncable::Id;
using syncable::MutableEntry;
using syncable::UNITTEST;
using syncable::WriteTransaction;

TestEntryFactory::TestEntryFactory(syncable::Directory *dir)
    : directory_(dir), next_revision_(1) {
}

TestEntryFactory::~TestEntryFactory() { }

void TestEntryFactory::CreateUnappliedNewItemWithParent(
    const string& item_id,
    const sync_pb::EntitySpecifics& specifics,
    const string& parent_id) {
  WriteTransaction trans(FROM_HERE, UNITTEST, directory_);
  MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM,
      Id::CreateFromServerId(item_id));
  DCHECK(entry.good());
  entry.Put(syncable::SERVER_VERSION, GetNextRevision());
  entry.Put(syncable::IS_UNAPPLIED_UPDATE, true);

  entry.Put(syncable::SERVER_NON_UNIQUE_NAME, item_id);
  entry.Put(syncable::SERVER_PARENT_ID, Id::CreateFromServerId(parent_id));
  entry.Put(syncable::SERVER_IS_DIR, true);
  entry.Put(syncable::SERVER_SPECIFICS, specifics);
}

void TestEntryFactory::CreateUnappliedNewItem(
                            const string& item_id,
                            const sync_pb::EntitySpecifics& specifics,
                            bool is_unique) {
  WriteTransaction trans(FROM_HERE, UNITTEST, directory_);
  MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM,
      Id::CreateFromServerId(item_id));
  DCHECK(entry.good());
  entry.Put(syncable::SERVER_VERSION, GetNextRevision());
  entry.Put(syncable::IS_UNAPPLIED_UPDATE, true);
  entry.Put(syncable::SERVER_NON_UNIQUE_NAME, item_id);
  entry.Put(syncable::SERVER_PARENT_ID, syncable::GetNullId());
  entry.Put(syncable::SERVER_IS_DIR, false);
  entry.Put(syncable::SERVER_SPECIFICS, specifics);
  if (is_unique)  // For top-level nodes.
    entry.Put(syncable::UNIQUE_SERVER_TAG, item_id);
}

void TestEntryFactory::CreateUnsyncedItem(
    const Id& item_id,
    const Id& parent_id,
    const string& name,
    bool is_folder,
    ModelType model_type,
    int64* metahandle_out) {
  WriteTransaction trans(FROM_HERE, UNITTEST, directory_);
  Id predecessor_id;
  DCHECK(
      directory_->GetLastChildIdForTest(&trans, parent_id, &predecessor_id));
  MutableEntry entry(&trans, syncable::CREATE, parent_id, name);
  DCHECK(entry.good());
  entry.Put(syncable::ID, item_id);
  entry.Put(syncable::BASE_VERSION,
      item_id.ServerKnows() ? GetNextRevision() : 0);
  entry.Put(syncable::IS_UNSYNCED, true);
  entry.Put(syncable::IS_DIR, is_folder);
  entry.Put(syncable::IS_DEL, false);
  entry.Put(syncable::PARENT_ID, parent_id);
  CHECK(entry.PutPredecessor(predecessor_id));
  sync_pb::EntitySpecifics default_specifics;
  AddDefaultFieldValue(model_type, &default_specifics);
  entry.Put(syncable::SPECIFICS, default_specifics);
  if (item_id.ServerKnows()) {
    entry.Put(syncable::SERVER_SPECIFICS, default_specifics);
    entry.Put(syncable::SERVER_IS_DIR, is_folder);
    entry.Put(syncable::SERVER_PARENT_ID, parent_id);
    entry.Put(syncable::SERVER_IS_DEL, false);
  }
  if (metahandle_out)
    *metahandle_out = entry.Get(syncable::META_HANDLE);
}

int64 TestEntryFactory::CreateUnappliedAndUnsyncedItem(
    const string& name,
    ModelType model_type) {
  int64 metahandle = 0;
  CreateUnsyncedItem(
      TestIdFactory::MakeServer(name), TestIdFactory::root(),
      name, false, model_type, &metahandle);

  WriteTransaction trans(FROM_HERE, UNITTEST, directory_);
  MutableEntry entry(&trans, syncable::GET_BY_HANDLE, metahandle);
  if (!entry.good()) {
    NOTREACHED();
    return syncable::kInvalidMetaHandle;
  }

  entry.Put(syncable::IS_UNAPPLIED_UPDATE, true);
  entry.Put(syncable::SERVER_VERSION, GetNextRevision());

  return metahandle;
}

int64 TestEntryFactory::CreateSyncedItem(
    const std::string& name, ModelType
    model_type, bool is_folder) {
  WriteTransaction trans(FROM_HERE, UNITTEST, directory_);

  syncable::Id parent_id(TestIdFactory::root());
  syncable::Id item_id(TestIdFactory::MakeServer(name));
  int64 version = GetNextRevision();

  sync_pb::EntitySpecifics default_specifics;
  AddDefaultFieldValue(model_type, &default_specifics);

  MutableEntry entry(&trans, syncable::CREATE, parent_id, name);
  if (!entry.good()) {
    NOTREACHED();
    return syncable::kInvalidMetaHandle;
  }

  entry.Put(syncable::ID, item_id);
  entry.Put(syncable::BASE_VERSION, version);
  entry.Put(syncable::IS_UNSYNCED, false);
  entry.Put(syncable::NON_UNIQUE_NAME, name);
  entry.Put(syncable::IS_DIR, is_folder);
  entry.Put(syncable::IS_DEL, false);
  entry.Put(syncable::PARENT_ID, parent_id);

  if (!entry.PutPredecessor(TestIdFactory::root())) {
    NOTREACHED();
    return syncable::kInvalidMetaHandle;
  }
  entry.Put(syncable::SPECIFICS, default_specifics);

  entry.Put(syncable::SERVER_VERSION, GetNextRevision());
  entry.Put(syncable::IS_UNAPPLIED_UPDATE, true);
  entry.Put(syncable::SERVER_NON_UNIQUE_NAME, "X");
  entry.Put(syncable::SERVER_PARENT_ID, TestIdFactory::MakeServer("Y"));
  entry.Put(syncable::SERVER_IS_DIR, is_folder);
  entry.Put(syncable::SERVER_IS_DEL, false);
  entry.Put(syncable::SERVER_SPECIFICS, default_specifics);
  entry.Put(syncable::SERVER_PARENT_ID, parent_id);

  return entry.Get(syncable::META_HANDLE);
}

int64 TestEntryFactory::GetNextRevision() {
  return next_revision_++;
}

}  // namespace syncer