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.
4
5#ifndef SYNC_SYNCABLE_DIRECTORY_UNITTEST_H_
6#define SYNC_SYNCABLE_DIRECTORY_UNITTEST_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/message_loop/message_loop.h"
12#include "sync/syncable/in_memory_directory_backing_store.h"
13#include "sync/syncable/mutable_entry.h"
14#include "sync/syncable/syncable_read_transaction.h"
15#include "sync/syncable/syncable_write_transaction.h"
16#include "sync/test/engine/test_id_factory.h"
17#include "sync/test/fake_encryptor.h"
18#include "sync/test/null_directory_change_delegate.h"
19#include "sync/test/null_transaction_observer.h"
20#include "sync/util/test_unrecoverable_error_handler.h"
21#include "testing/gtest/include/gtest/gtest.h"
22
23namespace syncer {
24
25namespace syncable {
26
27class BaseTransaction;
28
29// A test fixture for syncable::Directory.  Uses an in-memory database to keep
30// the unit tests fast.
31//
32// Serves as base class for several other test fixtures.
33class SyncableDirectoryTest : public testing::Test {
34 protected:
35  static const char kDirectoryName[];
36
37  SyncableDirectoryTest();
38  virtual ~SyncableDirectoryTest();
39
40  virtual void SetUp();
41  virtual void TearDown();
42
43  // Destroys any currently opened directory, creates and opens a new one.
44  //
45  // Returns result of the Open call.
46  DirOpenResult ReopenDirectory();
47
48  // Creates an empty entry and sets the ID field to a default one.
49  void CreateEntry(const ModelType& model_type, const std::string& entryname);
50
51  // Creates an empty entry and sets the ID field to id.
52  void CreateEntry(const ModelType& model_type,
53                   const std::string& entryname,
54                   const int id);
55
56  void CreateEntry(const ModelType& model_type,
57                   const std::string& entryname,
58                   const Id& id);
59
60  void CreateEntryWithAttachmentMetadata(
61      const ModelType& model_type,
62      const std::string& entryname,
63      const Id& id,
64      const sync_pb::AttachmentMetadata& attachment_metadata);
65
66  void DeleteEntry(const Id& id);
67
68  // When a directory is saved then loaded from disk, it will pass through
69  // DropDeletedEntries().  This will remove some entries from the directory.
70  // This function is intended to simulate that process.
71  //
72  // WARNING: The directory will be deleted by this operation.  You should
73  // not have any pointers to the directory (open transactions included)
74  // when you call this.
75  DirOpenResult SimulateSaveAndReloadDir();
76
77  // This function will close and re-open the directory without saving any
78  // pending changes.  This is intended to simulate the recovery from a crash
79  // scenario.  The same warnings for SimulateSaveAndReloadDir apply here.
80  DirOpenResult SimulateCrashAndReloadDir();
81
82  void GetAllMetaHandles(BaseTransaction* trans, MetahandleSet* result);
83  void CheckPurgeEntriesWithTypeInSucceeded(ModelTypeSet types_to_purge,
84                                            bool before_reload);
85  bool IsInDirtyMetahandles(int64 metahandle);
86  bool IsInMetahandlesToPurge(int64 metahandle);
87
88  scoped_ptr<Directory>& dir();
89  DirectoryChangeDelegate* directory_change_delegate();
90  Encryptor* encryptor();
91  UnrecoverableErrorHandler* unrecoverable_error_handler();
92
93 private:
94  void ValidateEntry(BaseTransaction* trans,
95                     int64 id,
96                     bool check_name,
97                     const std::string& name,
98                     int64 base_version,
99                     int64 server_version,
100                     bool is_del);
101
102  base::MessageLoop message_loop_;
103  scoped_ptr<Directory> dir_;
104  NullDirectoryChangeDelegate delegate_;
105  FakeEncryptor encryptor_;
106  TestUnrecoverableErrorHandler handler_;
107  sql::Connection connection_;
108};
109
110}  // namespace syncable
111
112}  // namespace syncer
113
114#endif  // SYNC_SYNCABLE_DIRECTORY_UNITTEST_H_
115