1// Copyright (c) 2011 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 CHROME_BROWSER_SYNC_TEST_PROFILE_SYNC_SERVICE_H_
6#define CHROME_BROWSER_SYNC_TEST_PROFILE_SYNC_SERVICE_H_
7#pragma once
8
9#include <string>
10
11#include "chrome/browser/sync/glue/data_type_manager_impl.h"
12#include "chrome/browser/sync/js_backend.h"
13#include "chrome/browser/sync/profile_sync_service.h"
14#include "chrome/test/profile_mock.h"
15#include "chrome/test/sync/engine/test_id_factory.h"
16#include "testing/gmock/include/gmock/gmock.h"
17
18class Profile;
19class Task;
20class TestProfileSyncService;
21
22ACTION(ReturnNewDataTypeManager) {
23  return new browser_sync::DataTypeManagerImpl(arg0, arg1);
24}
25
26namespace browser_sync {
27
28// Mocks out the SyncerThread operations (Pause/Resume) since no thread is
29// running in these tests, and allows tests to provide a task on construction
30// to set up initial nodes to mock out an actual server initial sync
31// download.
32class SyncBackendHostForProfileSyncTest
33    : public SyncBackendHost, public JsBackend {
34 public:
35  // |synchronous_init| causes initialization to block until the syncapi has
36  //     completed setting itself up and called us back.
37  SyncBackendHostForProfileSyncTest(
38      Profile* profile,
39      bool set_initial_sync_ended_on_init,
40      bool synchronous_init);
41  virtual ~SyncBackendHostForProfileSyncTest();
42
43  MOCK_METHOD1(RequestNudge, void(const tracked_objects::Location&));
44
45  virtual void ConfigureDataTypes(
46      const DataTypeController::TypeMap& data_type_controllers,
47      const syncable::ModelTypeSet& types,
48      CancelableTask* ready_task);
49
50  // Called when a nudge comes in.
51  void SimulateSyncCycleCompletedInitialSyncEnded(
52      const tracked_objects::Location&);
53
54  virtual sync_api::HttpPostProviderFactory* MakeHttpBridgeFactory(
55      net::URLRequestContextGetter* getter);
56
57  virtual void InitCore(const Core::DoInitializeOptions& options);
58
59  virtual JsBackend* GetJsBackend();
60
61  // JsBackend implementation.
62  virtual void SetParentJsEventRouter(JsEventRouter* router);
63  virtual void RemoveParentJsEventRouter();
64  virtual const JsEventRouter* GetParentJsEventRouter() const;
65  // Fires an event identical to the message unless the message has
66  // "delay" as a prefix, in which case a task to fire the identical
67  // event is posted instead.
68  virtual void ProcessMessage(const std::string& name, const JsArgList& args,
69                              const JsEventHandler* sender);
70
71  virtual void StartConfiguration(Callback0::Type* callback);
72
73  static void SetDefaultExpectationsForWorkerCreation(ProfileMock* profile);
74
75  static void SetHistoryServiceExpectations(ProfileMock* profile);
76
77 private:
78  bool synchronous_init_;
79};
80
81}  // namespace browser_sync
82
83class TestProfileSyncService : public ProfileSyncService {
84 public:
85  // |initial_condition_setup_task| can be used to populate nodes
86  // before the OnBackendInitialized callback fires.
87  TestProfileSyncService(ProfileSyncFactory* factory,
88                         Profile* profile,
89                         const std::string& test_user,
90                         bool synchronous_backend_initialization,
91                         Task* initial_condition_setup_task);
92
93  virtual ~TestProfileSyncService();
94
95  void SetInitialSyncEndedForEnabledTypes();
96
97  virtual void OnBackendInitialized();
98
99  virtual void Observe(NotificationType type,
100                       const NotificationSource& source,
101                       const NotificationDetails& details);
102
103  // If this is called, configuring data types will require a syncer
104  // nudge.
105  void dont_set_initial_sync_ended_on_init();
106  void set_synchronous_sync_configuration();
107
108  browser_sync::TestIdFactory* id_factory();
109
110  // Override of ProfileSyncService::GetBackendForTest() with a more
111  // specific return type (since C++ supports covariant return types)
112  // that is made public.
113  virtual browser_sync::SyncBackendHostForProfileSyncTest*
114      GetBackendForTest();
115
116 protected:
117  virtual void CreateBackend();
118
119 private:
120  // When testing under ChromiumOS, this method must not return an empty
121  // value value in order for the profile sync service to start.
122  virtual std::string GetLsidForAuthBootstraping();
123
124  browser_sync::TestIdFactory id_factory_;
125
126  bool synchronous_backend_initialization_;
127
128  // Set to true when a mock data type manager is being used and the configure
129  // step is performed synchronously.
130  bool synchronous_sync_configuration_;
131  bool set_expect_resume_expectations_;
132
133  Task* initial_condition_setup_task_;
134  bool set_initial_sync_ended_on_init_;
135};
136
137
138
139#endif  // CHROME_BROWSER_SYNC_TEST_PROFILE_SYNC_SERVICE_H_
140