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/internal_api/public/test/test_user_share.h"
6
7#include "base/compiler_specific.h"
8#include "sync/syncable/directory.h"
9#include "sync/syncable/mutable_entry.h"
10#include "sync/syncable/syncable_read_transaction.h"
11#include "sync/syncable/syncable_write_transaction.h"
12#include "sync/test/engine/test_directory_setter_upper.h"
13#include "sync/test/engine/test_id_factory.h"
14#include "sync/test/engine/test_syncable_utils.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17namespace syncer {
18
19TestUserShare::TestUserShare() : dir_maker_(new TestDirectorySetterUpper()) {}
20
21TestUserShare::~TestUserShare() {
22  if (user_share_)
23    ADD_FAILURE() << "Should have called TestUserShare::TearDown()";
24}
25
26void TestUserShare::SetUp() {
27  user_share_.reset(new UserShare());
28  dir_maker_->SetUp();
29
30  // The pointer is owned by dir_maker_, we should not be storing it in a
31  // scoped_ptr.  We must be careful to ensure the scoped_ptr never deletes it.
32  user_share_->directory.reset(dir_maker_->directory());
33}
34
35void TestUserShare::TearDown() {
36  // Ensure the scoped_ptr doesn't delete the memory we don't own.
37  ignore_result(user_share_->directory.release());
38
39  user_share_.reset();
40  dir_maker_->TearDown();
41}
42
43bool TestUserShare::Reload() {
44  if (!user_share_->directory->SaveChanges())
45    return false;
46
47  syncer::syncable::DirectoryBackingStore* saved_store =
48      user_share_->directory->store_.release();
49
50  // Ensure the scoped_ptr doesn't delete the memory we don't own.
51  ignore_result(user_share_->directory.release());
52  user_share_.reset(new UserShare());
53  dir_maker_->SetUpWith(saved_store);
54  user_share_->directory.reset(dir_maker_->directory());
55  return true;
56}
57
58UserShare* TestUserShare::user_share() {
59  return user_share_.get();
60}
61
62SyncEncryptionHandler* TestUserShare::encryption_handler() {
63  return dir_maker_->encryption_handler();
64}
65
66syncable::TestTransactionObserver* TestUserShare::transaction_observer() {
67  return dir_maker_->transaction_observer();
68}
69
70/* static */
71bool TestUserShare::CreateRoot(ModelType model_type, UserShare* user_share) {
72  syncer::syncable::Directory* directory = user_share->directory.get();
73  syncable::WriteTransaction wtrans(FROM_HERE, syncable::UNITTEST, directory);
74  CreateTypeRoot(&wtrans, directory, model_type);
75  return true;
76}
77
78size_t TestUserShare::GetDeleteJournalSize() const {
79  syncable::ReadTransaction trans(FROM_HERE, user_share_->directory.get());
80  return user_share_->directory->delete_journal()->GetDeleteJournalSize(&trans);
81}
82
83}  // namespace syncer
84