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_NFC_ADAPTER_CLIENT_H_ 6 #define CHROMEOS_DBUS_FAKE_NFC_ADAPTER_CLIENT_H_ 7 8 #include <string> 9 10 #include "base/memory/scoped_ptr.h" 11 #include "base/observer_list.h" 12 #include "chromeos/chromeos_export.h" 13 #include "chromeos/dbus/nfc_adapter_client.h" 14 #include "chromeos/dbus/nfc_client_helpers.h" 15 16 namespace chromeos { 17 18 // FakeNfcAdapterClient simulates the behavior of the NFC adapter objects 19 // and is used both in test cases in place of a mock and on the Linux desktop. 20 class CHROMEOS_EXPORT FakeNfcAdapterClient : public NfcAdapterClient { 21 public: 22 // The object paths for the adapters that are being emulated. 23 static const char kAdapterPath0[]; 24 static const char kAdapterPath1[]; 25 26 // Properties structure that provides fake behavior for D-Bus calls. 27 struct Properties : public NfcAdapterClient::Properties { 28 explicit Properties(const PropertyChangedCallback& callback); 29 virtual ~Properties(); 30 31 // dbus::PropertySet overrides. 32 virtual void Get(dbus::PropertyBase* property, 33 dbus::PropertySet::GetCallback callback) OVERRIDE; 34 virtual void GetAll() OVERRIDE; 35 virtual void Set(dbus::PropertyBase* property, 36 dbus::PropertySet::SetCallback callback) OVERRIDE; 37 }; 38 39 FakeNfcAdapterClient(); 40 virtual ~FakeNfcAdapterClient(); 41 42 // NfcAdapterClient overrides. 43 virtual void Init(dbus::Bus* bus) OVERRIDE; 44 virtual void AddObserver(Observer* observer) OVERRIDE; 45 virtual void RemoveObserver(Observer* observer) OVERRIDE; 46 virtual std::vector<dbus::ObjectPath> GetAdapters() OVERRIDE; 47 virtual Properties* GetProperties( 48 const dbus::ObjectPath& object_path) OVERRIDE; 49 virtual void StartPollLoop( 50 const dbus::ObjectPath& object_path, 51 const std::string& mode, 52 const base::Closure& callback, 53 const nfc_client_helpers::ErrorCallback& error_callback) OVERRIDE; 54 virtual void StopPollLoop( 55 const dbus::ObjectPath& object_path, 56 const base::Closure& callback, 57 const nfc_client_helpers::ErrorCallback& error_callback) OVERRIDE; 58 59 // Sets the adapter as |present|. Used for testing. 60 void SetAdapterPresent(bool present); 61 void SetSecondAdapterPresent(bool present); 62 63 // Tells the FakeNfcAdapterClient to add the device or tag with the given path 64 // to its corresponding list for |kAdapterPath0|, if it is not already in 65 // the list and promptly triggers a property changed signal. This method will 66 // also fail, if the polling property of the adapter is false and will set it 67 // to false on success. 68 void SetDevice(const dbus::ObjectPath& device_path); 69 void SetTag(const dbus::ObjectPath& tag_path); 70 71 // Tells the FakeNfcAdapterClient to remove the device or tag with the given 72 // path from its corresponding list exposed for |kAdapterPath0|, if it 73 // is in the list. On success, this method will mark the polling property of 74 // the adapter to true. 75 void UnsetDevice(const dbus::ObjectPath& device_path); 76 void UnsetTag(const dbus::ObjectPath& tag_path); 77 78 // Sets a flag that determines whether FakeNfcAdapterClient should notify 79 // FakeNfcDeviceClient or FakeNfcTagClient to start a pairing simulation as a 80 // result of a call to StartPollLoop(). This is enabled by default. If 81 // enabled, the first call to StartPollLoop, will initiate a tag pairing 82 // simulation. The simulation will alternate between device and tag pairing on 83 // each successive call to StartPollLoop. This behavior, which is meant for 84 // feature development based on fake classes, can be disabled to allow manual 85 // control for unit tests. 86 void EnablePairingOnPoll(bool enabled); 87 88 private: 89 // Property changed callback passed when we create Properties* structures. 90 void OnPropertyChanged(const dbus::ObjectPath& object_path, 91 const std::string& property_name); 92 93 // List of observers interested in event notifications from us. 94 ObserverList<Observer> observers_; 95 96 // Fake properties that are returned for the emulated adapters. 97 scoped_ptr<Properties> properties_; 98 scoped_ptr<Properties> second_properties_; 99 100 // Whether the adapter and second adapter are present or not. 101 bool present_; 102 bool second_present_; 103 104 // If true, a pairing simulation is initiated on a successful call to 105 // StartPollLoop(). 106 bool start_pairing_on_poll_; 107 108 // If true, device pairing will be simulated on the next call to 109 // StartPollLoop. Otherwise, tag pairing will be simulated. 110 bool device_pairing_; 111 112 DISALLOW_COPY_AND_ASSIGN(FakeNfcAdapterClient); 113 }; 114 115 } // namespace chromeos 116 117 #endif // CHROMEOS_DBUS_FAKE_NFC_ADAPTER_CLIENT_H_ 118