- add sources.
[platform/framework/web/crosswalk.git] / src / sync / test / engine / test_syncable_utils.cc
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.
4
5 // Utilities to verify the state of items in unit tests.
6
7 #include "sync/test/engine/test_syncable_utils.h"
8
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"
15
16 using std::string;
17
18 namespace syncer {
19 namespace syncable {
20
21 int CountEntriesWithName(BaseTransaction* rtrans,
22                          const syncable::Id& parent_id,
23                          const string& name) {
24   Directory::Metahandles child_handles;
25   rtrans->directory()->GetChildHandlesById(rtrans, parent_id, &child_handles);
26   if (child_handles.size() <= 0) {
27     return 0;
28   }
29
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);
34     CHECK(e.good());
35     if (e.GetNonUniqueName()== name) {
36       ++number_of_entries_with_name;
37     }
38   }
39   return number_of_entries_with_name;
40 }
41
42 Id GetFirstEntryWithName(BaseTransaction* rtrans,
43                          const syncable::Id& parent_id,
44                          const string& name) {
45   Directory::Metahandles child_handles;
46   rtrans->directory()->GetChildHandlesById(rtrans, parent_id, &child_handles);
47
48   for (Directory::Metahandles::iterator i = child_handles.begin();
49        i != child_handles.end(); ++i) {
50     Entry e(rtrans, GET_BY_HANDLE, *i);
51     CHECK(e.good());
52     if (e.GetNonUniqueName()== name) {
53       return e.GetId();
54     }
55   }
56
57   CHECK(false);
58   return Id();
59 }
60
61 Id GetOnlyEntryWithName(BaseTransaction* rtrans,
62                         const syncable::Id& parent_id,
63                         const string& name) {
64   CHECK(1 == CountEntriesWithName(rtrans, parent_id, name));
65   return GetFirstEntryWithName(rtrans, parent_id, name);
66 }
67
68 void CreateTypeRoot(WriteTransaction* trans,
69                     syncable::Directory *dir,
70                     ModelType type) {
71   std::string tag_name = syncer::ModelTypeToRootTag(type);
72   syncable::MutableEntry node(trans,
73                               syncable::CREATE,
74                               type,
75                               TestIdFactory::root(),
76                               tag_name);
77   DCHECK(node.good());
78   node.PutUniqueServerTag(tag_name);
79   node.PutIsDir(true);
80   node.PutServerIsDir(false);
81   node.PutIsUnsynced(false);
82   node.PutIsUnappliedUpdate(false);
83   node.PutServerVersion(20);
84   node.PutBaseVersion(20);
85   node.PutIsDel(false);
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);
91 }
92
93 }  // namespace syncable
94 }  // namespace syncer