1 // Copyright 2012 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 // Utilities to verify the state of items in unit tests.
7 #include "sync/test/engine/test_syncable_utils.h"
9 #include "sync/syncable/directory.h"
10 #include "sync/syncable/entry.h"
11 #include "sync/syncable/mutable_entry.h"
12 #include "sync/syncable/syncable_base_transaction.h"
13 #include "sync/syncable/syncable_write_transaction.h"
14 #include "sync/test/engine/test_id_factory.h"
21 int CountEntriesWithName(BaseTransaction
* rtrans
,
22 const syncable::Id
& parent_id
,
24 Directory::Metahandles child_handles
;
25 rtrans
->directory()->GetChildHandlesById(rtrans
, parent_id
, &child_handles
);
26 if (child_handles
.size() <= 0) {
30 int number_of_entries_with_name
= 0;
31 for (Directory::Metahandles::iterator i
= child_handles
.begin();
32 i
!= child_handles
.end(); ++i
) {
33 Entry
e(rtrans
, GET_BY_HANDLE
, *i
);
35 if (e
.GetNonUniqueName()== name
) {
36 ++number_of_entries_with_name
;
39 return number_of_entries_with_name
;
42 Id
GetFirstEntryWithName(BaseTransaction
* rtrans
,
43 const syncable::Id
& parent_id
,
45 Directory::Metahandles child_handles
;
46 rtrans
->directory()->GetChildHandlesById(rtrans
, parent_id
, &child_handles
);
48 for (Directory::Metahandles::iterator i
= child_handles
.begin();
49 i
!= child_handles
.end(); ++i
) {
50 Entry
e(rtrans
, GET_BY_HANDLE
, *i
);
52 if (e
.GetNonUniqueName()== name
) {
61 Id
GetOnlyEntryWithName(BaseTransaction
* rtrans
,
62 const syncable::Id
& parent_id
,
64 CHECK(1 == CountEntriesWithName(rtrans
, parent_id
, name
));
65 return GetFirstEntryWithName(rtrans
, parent_id
, name
);
68 void CreateTypeRoot(WriteTransaction
* trans
,
69 syncable::Directory
*dir
,
71 std::string tag_name
= syncer::ModelTypeToRootTag(type
);
72 syncable::MutableEntry
node(trans
,
75 TestIdFactory::root(),
78 node
.PutUniqueServerTag(tag_name
);
80 node
.PutServerIsDir(false);
81 node
.PutIsUnsynced(false);
82 node
.PutIsUnappliedUpdate(false);
83 node
.PutServerVersion(20);
84 node
.PutBaseVersion(20);
86 node
.PutId(syncer::TestIdFactory::MakeServer(tag_name
));
87 sync_pb::EntitySpecifics specifics
;
88 syncer::AddDefaultFieldValue(type
, &specifics
);
89 node
.PutServerSpecifics(specifics
);
90 node
.PutSpecifics(specifics
);
93 sync_pb::DataTypeProgressMarker
BuildProgress(ModelType type
) {
94 sync_pb::DataTypeProgressMarker progress
;
95 progress
.set_token("token");
96 progress
.set_data_type_id(GetSpecificsFieldNumberFromModelType(type
));
100 } // namespace syncable
101 } // namespace syncer