sync_file_system_test_util.cc revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
1// Copyright (c) 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 "chrome/browser/sync_file_system/sync_file_system_test_util.h"
6
7#include "base/bind.h"
8#include "base/message_loop/message_loop_proxy.h"
9#include "base/run_loop.h"
10#include "base/single_thread_task_runner.h"
11#include "base/threading/thread.h"
12#include "chrome/browser/google_apis/gdata_errorcode.h"
13#include "chrome/browser/sync_file_system/drive_backend/metadata_database.h"
14#include "chrome/browser/sync_file_system/sync_status_code.h"
15#include "content/public/browser/browser_thread.h"
16#include "content/public/test/test_utils.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
19using content::BrowserThread;
20
21namespace sync_file_system {
22
23namespace drive_backend {
24class MetadataDatabase;
25}  // drive_backend
26
27template <typename R>
28void AssignAndQuit(base::RunLoop* run_loop, R* result_out, R result) {
29  DCHECK(result_out);
30  DCHECK(run_loop);
31  *result_out = result;
32  run_loop->Quit();
33}
34
35template <typename R> base::Callback<void(R)>
36AssignAndQuitCallback(base::RunLoop* run_loop, R* result) {
37  return base::Bind(&AssignAndQuit<R>, run_loop, base::Unretained(result));
38}
39
40template <typename Arg>
41void ReceiveResult1(bool* done, Arg* arg_out, Arg arg) {
42  EXPECT_FALSE(*done);
43  *done = true;
44  *arg_out = base::internal::CallbackForward(arg);
45}
46
47template <typename Arg1, typename Arg2>
48void ReceiveResult2(bool* done,
49                    Arg1* arg1_out,
50                    Arg2* arg2_out,
51                    Arg1 arg1,
52                    Arg2 arg2) {
53  EXPECT_FALSE(*done);
54  *done = true;
55  *arg1_out = base::internal::CallbackForward(arg1);
56  *arg2_out = base::internal::CallbackForward(arg2);
57}
58
59template <typename Arg>
60base::Callback<void(Arg)> CreateResultReceiver(Arg* arg_out) {
61  return base::Bind(&ReceiveResult1<Arg>,
62                    base::Owned(new bool(false)), arg_out);
63}
64
65template <typename Arg1, typename Arg2>
66base::Callback<void(Arg1, Arg2)> CreateResultReceiver(Arg1* arg1_out,
67                                                      Arg2* arg2_out) {
68  return base::Bind(&ReceiveResult2<Arg1, Arg2>,
69                    base::Owned(new bool(false)),
70                    arg1_out, arg2_out);
71}
72
73// Instantiate versions we know callers will need.
74template base::Callback<void(SyncStatusCode)>
75AssignAndQuitCallback(base::RunLoop*, SyncStatusCode*);
76
77#define INSTANTIATE_RECEIVER(type)                                  \
78  template base::Callback<void(type)> CreateResultReceiver(type*);
79INSTANTIATE_RECEIVER(SyncStatusCode);
80INSTANTIATE_RECEIVER(google_apis::GDataErrorCode);
81#undef INSTANTIATE_RECEIVER
82
83#define INSTANTIATE_RECEIVER(type1, type2)                \
84  template base::Callback<void(type1, scoped_ptr<type2>)> \
85  CreateResultReceiver(type1*, scoped_ptr<type2>*)
86INSTANTIATE_RECEIVER(SyncStatusCode, drive_backend::MetadataDatabase);
87#undef INSTANTIATE_RECEIVER
88
89}  // namespace sync_file_system
90