fake_shill_service_client.h revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright 2013 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 CHROMEOS_DBUS_FAKE_SHILL_SERVICE_CLIENT_H_
6#define CHROMEOS_DBUS_FAKE_SHILL_SERVICE_CLIENT_H_
7
8#include <map>
9#include <string>
10
11#include "base/basictypes.h"
12#include "base/callback.h"
13#include "base/memory/weak_ptr.h"
14#include "chromeos/chromeos_export.h"
15#include "chromeos/dbus/shill_service_client.h"
16
17namespace chromeos {
18
19// A fake implementation of ShillServiceClient. This works in close coordination
20// with FakeShillManagerClient and is not intended to be used independently.
21class CHROMEOS_EXPORT FakeShillServiceClient
22    : public ShillServiceClient,
23      public ShillServiceClient::TestInterface {
24 public:
25  FakeShillServiceClient();
26  virtual ~FakeShillServiceClient();
27
28  // ShillServiceClient overrides
29  virtual void Init(dbus::Bus* bus) OVERRIDE;
30  virtual void AddPropertyChangedObserver(
31      const dbus::ObjectPath& service_path,
32      ShillPropertyChangedObserver* observer) OVERRIDE;
33  virtual void RemovePropertyChangedObserver(
34      const dbus::ObjectPath& service_path,
35      ShillPropertyChangedObserver* observer) OVERRIDE;
36  virtual void GetProperties(const dbus::ObjectPath& service_path,
37                             const DictionaryValueCallback& callback) OVERRIDE;
38  virtual void SetProperty(const dbus::ObjectPath& service_path,
39                           const std::string& name,
40                           const base::Value& value,
41                           const base::Closure& callback,
42                           const ErrorCallback& error_callback) OVERRIDE;
43  virtual void SetProperties(const dbus::ObjectPath& service_path,
44                             const base::DictionaryValue& properties,
45                             const base::Closure& callback,
46                             const ErrorCallback& error_callback) OVERRIDE;
47  virtual void ClearProperty(const dbus::ObjectPath& service_path,
48                             const std::string& name,
49                             const base::Closure& callback,
50                             const ErrorCallback& error_callback) OVERRIDE;
51  virtual void ClearProperties(const dbus::ObjectPath& service_path,
52                               const std::vector<std::string>& names,
53                               const ListValueCallback& callback,
54                               const ErrorCallback& error_callback) OVERRIDE;
55  virtual void Connect(const dbus::ObjectPath& service_path,
56                       const base::Closure& callback,
57                       const ErrorCallback& error_callback) OVERRIDE;
58  virtual void Disconnect(const dbus::ObjectPath& service_path,
59                          const base::Closure& callback,
60                          const ErrorCallback& error_callback) OVERRIDE;
61  virtual void Remove(const dbus::ObjectPath& service_path,
62                      const base::Closure& callback,
63                      const ErrorCallback& error_callback) OVERRIDE;
64  virtual void ActivateCellularModem(
65      const dbus::ObjectPath& service_path,
66      const std::string& carrier,
67      const base::Closure& callback,
68      const ErrorCallback& error_callback) OVERRIDE;
69  virtual void CompleteCellularActivation(
70      const dbus::ObjectPath& service_path,
71      const base::Closure& callback,
72      const ErrorCallback& error_callback) OVERRIDE;
73  virtual void GetLoadableProfileEntries(
74      const dbus::ObjectPath& service_path,
75      const DictionaryValueCallback& callback) OVERRIDE;
76  virtual ShillServiceClient::TestInterface* GetTestInterface() OVERRIDE;
77
78  // ShillServiceClient::TestInterface overrides.
79  virtual void AddService(const std::string& service_path,
80                          const std::string& guid,
81                          const std::string& name,
82                          const std::string& type,
83                          const std::string& state,
84                          bool visible) OVERRIDE;
85  virtual void AddServiceWithIPConfig(const std::string& service_path,
86                                      const std::string& guid,
87                                      const std::string& name,
88                                      const std::string& type,
89                                      const std::string& state,
90                                      const std::string& ipconfig_path,
91                                      bool visible) OVERRIDE;
92  virtual base::DictionaryValue* SetServiceProperties(
93      const std::string& service_path,
94      const std::string& guid,
95      const std::string& name,
96      const std::string& type,
97      const std::string& state,
98      bool visible) OVERRIDE;
99  virtual void RemoveService(const std::string& service_path) OVERRIDE;
100  virtual bool SetServiceProperty(const std::string& service_path,
101                                  const std::string& property,
102                                  const base::Value& value) OVERRIDE;
103  virtual const base::DictionaryValue* GetServiceProperties(
104      const std::string& service_path) const OVERRIDE;
105  virtual void ClearServices() OVERRIDE;
106  virtual void SetConnectBehavior(const std::string& service_path,
107                                  const base::Closure& behavior) OVERRIDE;
108
109 private:
110  typedef ObserverList<ShillPropertyChangedObserver> PropertyObserverList;
111
112  void NotifyObserversPropertyChanged(const dbus::ObjectPath& service_path,
113                                      const std::string& property);
114  base::DictionaryValue* GetModifiableServiceProperties(
115      const std::string& service_path,
116      bool create_if_missing);
117  PropertyObserverList& GetObserverList(const dbus::ObjectPath& device_path);
118  void SetOtherServicesOffline(const std::string& service_path);
119  void SetCellularActivated(const dbus::ObjectPath& service_path,
120                            const ErrorCallback& error_callback);
121  void ContinueConnect(const std::string& service_path);
122
123  base::DictionaryValue stub_services_;
124
125  // Per network service, stores a closure that is executed on each connection
126  // attempt. The callback can for example modify the services properties in
127  // order to simulate a connection failure.
128  std::map<std::string, base::Closure> connect_behavior_;
129
130  // Observer list for each service.
131  std::map<dbus::ObjectPath, PropertyObserverList*> observer_list_;
132
133  // Note: This should remain the last member so it'll be destroyed and
134  // invalidate its weak pointers before any other members are destroyed.
135  base::WeakPtrFactory<FakeShillServiceClient> weak_ptr_factory_;
136
137  DISALLOW_COPY_AND_ASSIGN(FakeShillServiceClient);
138};
139
140}  // namespace chromeos
141
142#endif  // CHROMEOS_DBUS_FAKE_SHILL_SERVICE_CLIENT_H_
143