11e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 21e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 31e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// found in the LICENSE file. 41e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 51e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#ifndef CHROMEOS_DBUS_FAKE_NFC_ADAPTER_CLIENT_H_ 61e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#define CHROMEOS_DBUS_FAKE_NFC_ADAPTER_CLIENT_H_ 71e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 8a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include <string> 9a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 10a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "base/memory/scoped_ptr.h" 11a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "base/observer_list.h" 121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chromeos/chromeos_export.h" 131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chromeos/dbus/nfc_adapter_client.h" 141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chromeos/dbus/nfc_client_helpers.h" 151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace chromeos { 171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// FakeNfcAdapterClient simulates the behavior of the NFC adapter objects 191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// and is used both in test cases in place of a mock and on the Linux desktop. 201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)class CHROMEOS_EXPORT FakeNfcAdapterClient : public NfcAdapterClient { 211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public: 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // The object paths for the adapters that are being emulated. 235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) static const char kAdapterPath0[]; 245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) static const char kAdapterPath1[]; 255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Properties structure that provides fake behavior for D-Bus calls. 271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) struct Properties : public NfcAdapterClient::Properties { 281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) explicit Properties(const PropertyChangedCallback& callback); 291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) virtual ~Properties(); 301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // dbus::PropertySet overrides. 321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) virtual void Get(dbus::PropertyBase* property, 331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) dbus::PropertySet::GetCallback callback) OVERRIDE; 341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) virtual void GetAll() OVERRIDE; 351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) virtual void Set(dbus::PropertyBase* property, 361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) dbus::PropertySet::SetCallback callback) OVERRIDE; 371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) }; 381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) FakeNfcAdapterClient(); 401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) virtual ~FakeNfcAdapterClient(); 411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) // NfcAdapterClient overrides. 431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) virtual void Init(dbus::Bus* bus) OVERRIDE; 441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) virtual void AddObserver(Observer* observer) OVERRIDE; 451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) virtual void RemoveObserver(Observer* observer) OVERRIDE; 46a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) virtual std::vector<dbus::ObjectPath> GetAdapters() OVERRIDE; 470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) virtual Properties* GetProperties( 480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const dbus::ObjectPath& object_path) OVERRIDE; 491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) virtual void StartPollLoop( 501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const dbus::ObjectPath& object_path, 511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const std::string& mode, 521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const base::Closure& callback, 531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const nfc_client_helpers::ErrorCallback& error_callback) OVERRIDE; 541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) virtual void StopPollLoop( 551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const dbus::ObjectPath& object_path, 561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const base::Closure& callback, 571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) const nfc_client_helpers::ErrorCallback& error_callback) OVERRIDE; 581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Sets the adapter as |present|. Used for testing. 60a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) void SetAdapterPresent(bool present); 61a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) void SetSecondAdapterPresent(bool present); 62a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Tells the FakeNfcAdapterClient to add the device or tag with the given path 645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // to its corresponding list for |kAdapterPath0|, if it is not already in 655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // the list and promptly triggers a property changed signal. This method will 665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // also fail, if the polling property of the adapter is false and will set it 675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // to false on success. 685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void SetDevice(const dbus::ObjectPath& device_path); 695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void SetTag(const dbus::ObjectPath& tag_path); 705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Tells the FakeNfcAdapterClient to remove the device or tag with the given 725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // path from its corresponding list exposed for |kAdapterPath0|, if it 735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // is in the list. On success, this method will mark the polling property of 745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // the adapter to true. 755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void UnsetDevice(const dbus::ObjectPath& device_path); 765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void UnsetTag(const dbus::ObjectPath& tag_path); 775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Sets a flag that determines whether FakeNfcAdapterClient should notify 795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // FakeNfcDeviceClient or FakeNfcTagClient to start a pairing simulation as a 805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // result of a call to StartPollLoop(). This is enabled by default. If 815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // enabled, the first call to StartPollLoop, will initiate a tag pairing 825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // simulation. The simulation will alternate between device and tag pairing on 835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // each successive call to StartPollLoop. This behavior, which is meant for 845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // feature development based on fake classes, can be disabled to allow manual 855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // control for unit tests. 865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void EnablePairingOnPoll(bool enabled); 87a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) private: 89a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // Property changed callback passed when we create Properties* structures. 90a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) void OnPropertyChanged(const dbus::ObjectPath& object_path, 91a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const std::string& property_name); 92a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 93a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // List of observers interested in event notifications from us. 94a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) ObserverList<Observer> observers_; 95a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 96a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // Fake properties that are returned for the emulated adapters. 97a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) scoped_ptr<Properties> properties_; 98a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) scoped_ptr<Properties> second_properties_; 99a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Whether the adapter and second adapter are present or not. 101a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) bool present_; 102a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) bool second_present_; 103a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // If true, a pairing simulation is initiated on a successful call to 1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // StartPollLoop(). 1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool start_pairing_on_poll_; 1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // If true, device pairing will be simulated on the next call to 1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // StartPollLoop. Otherwise, tag pairing will be simulated. 1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool device_pairing_; 1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(FakeNfcAdapterClient); 1131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}; 1141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 1151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)} // namespace chromeos 1161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 1171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#endif // CHROMEOS_DBUS_FAKE_NFC_ADAPTER_CLIENT_H_ 118