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#include "sync/test/engine/test_directory_setter_upper.h"
6
7#include "base/compiler_specific.h"
8#include "base/files/file_util.h"
9#include "base/location.h"
10#include "base/strings/string_util.h"
11#include "sync/syncable/directory.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/test_transaction_observer.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
19namespace syncer {
20
21TestDirectorySetterUpper::TestDirectorySetterUpper() : name_("Test") {}
22
23TestDirectorySetterUpper::~TestDirectorySetterUpper() {}
24
25void TestDirectorySetterUpper::SetUp() {
26  test_transaction_observer_.reset(new syncable::TestTransactionObserver());
27  WeakHandle<syncable::TransactionObserver> transaction_observer =
28      MakeWeakHandle(test_transaction_observer_->AsWeakPtr());
29
30  directory_.reset(
31      new syncable::Directory(
32          new syncable::InMemoryDirectoryBackingStore(name_),
33          &handler_,
34          NULL,
35          &encryption_handler_,
36          encryption_handler_.cryptographer()));
37  ASSERT_EQ(syncable::OPENED, directory_->Open(
38          name_, &delegate_, transaction_observer));
39}
40
41void TestDirectorySetterUpper::SetUpWith(
42    syncer::syncable::DirectoryBackingStore* directory_store) {
43  CHECK(directory_store);
44  test_transaction_observer_.reset(new syncable::TestTransactionObserver());
45  WeakHandle<syncable::TransactionObserver> transaction_observer =
46      MakeWeakHandle(test_transaction_observer_->AsWeakPtr());
47
48  directory_.reset(
49      new syncable::Directory(directory_store,
50                              &handler_,
51                              NULL,
52                              &encryption_handler_,
53                              encryption_handler_.cryptographer()));
54    ASSERT_EQ(syncable::OPENED, directory_->Open(
55          name_, &delegate_, transaction_observer));
56}
57
58void TestDirectorySetterUpper::TearDown() {
59  if (!directory()->good())
60    return;
61
62  RunInvariantCheck();
63  directory()->SaveChanges();
64  RunInvariantCheck();
65  directory()->SaveChanges();
66
67  directory_.reset();
68}
69
70void TestDirectorySetterUpper::RunInvariantCheck() {
71  // Check invariants for all items.
72  syncable::ReadTransaction trans(FROM_HERE, directory());
73
74  // The TestUnrecoverableErrorHandler that this directory was constructed with
75  // will handle error reporting, so we can safely ignore the return value.
76  directory()->FullyCheckTreeInvariants(&trans);
77}
78
79}  // namespace syncer
80