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