nfc_client_unittest.cc revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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)#include "base/bind.h"
61e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/message_loop/message_loop.h"
71e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chromeos/dbus/nfc_adapter_client.h"
81e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chromeos/dbus/nfc_client_helpers.h"
91e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chromeos/dbus/nfc_manager_client.h"
101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "dbus/mock_bus.h"
111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "dbus/mock_object_proxy.h"
121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "testing/gmock/include/gmock/gmock.h"
131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "third_party/cros_system_api/dbus/service_constants.h"
151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)using ::testing::_;
171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)using ::testing::Invoke;
181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)using ::testing::Mock;
191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)using ::testing::Return;
201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace chromeos {
221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace {
241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// D-Bus service name used by the test.
261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const char kTestServiceName[] = "test.service.name";
271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Object paths that are used for testing.
291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const char kTestManagerPath[] = "/test/nfc/manager";
301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const char kTestAdapterPath0[] = "/test/nfc/adapter0";
311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const char kTestAdapterPath1[] = "/test/nfc/adapter1";
321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)class MockNfcManagerObserver : public NfcManagerClient::Observer {
341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public:
351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  MOCK_METHOD1(AdapterAdded, void(const dbus::ObjectPath&));
361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  MOCK_METHOD1(AdapterRemoved, void(const dbus::ObjectPath&));
371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  MOCK_METHOD1(ManagerPropertyChanged, void(const std::string&));
381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)};
391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)class MockNfcAdapterObserver : public NfcAdapterClient::Observer {
411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public:
421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  MOCK_METHOD1(AdapterAdded, void(const dbus::ObjectPath&));
431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  MOCK_METHOD1(AdapterRemoved, void(const dbus::ObjectPath&));
441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  MOCK_METHOD2(AdapterPropertyChanged, void(const dbus::ObjectPath&,
451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                            const std::string&));
461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)};
471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}  // namespace
491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)class NfcClientTest : public testing::Test {
511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public:
521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  NfcClientTest() : response_(NULL) {}
531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual ~NfcClientTest() {}
541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual void SetUp() OVERRIDE {
561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // Create the mock bus.
571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    dbus::Bus::Options options;
581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    options.bus_type = dbus::Bus::SYSTEM;
591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    mock_bus_ = new dbus::MockBus(options);
601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // Create the mock proxies.
621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    mock_manager_proxy_ = new dbus::MockObjectProxy(
631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        mock_bus_.get(),
641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        kTestServiceName,
651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        dbus::ObjectPath(kTestManagerPath));
661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    mock_adapter0_proxy_ = new dbus::MockObjectProxy(
671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        mock_bus_.get(),
681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        kTestServiceName,
691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        dbus::ObjectPath(kTestAdapterPath0));
701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    mock_adapter1_proxy_ = new dbus::MockObjectProxy(
711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        mock_bus_.get(),
721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        kTestServiceName,
731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        dbus::ObjectPath(kTestAdapterPath1));
741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // Set expectations that use NfcClientTest::OnConnectToSignal when the
761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // client connect signals on the mock proxies.
771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    EXPECT_CALL(*mock_manager_proxy_.get(), ConnectToSignal(_, _, _, _))
781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        .WillRepeatedly(Invoke(this, &NfcClientTest::OnConnectToSignal));
791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    EXPECT_CALL(*mock_adapter0_proxy_.get(), ConnectToSignal(_, _, _, _))
801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        .WillRepeatedly(Invoke(this, &NfcClientTest::OnConnectToSignal));
811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    EXPECT_CALL(*mock_adapter1_proxy_.get(), ConnectToSignal(_, _, _, _))
821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        .WillRepeatedly(Invoke(this, &NfcClientTest::OnConnectToSignal));
831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // Set expectations that return our mock proxies on demand.
851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    EXPECT_CALL(
861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        *mock_bus_.get(),
871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        GetObjectProxy(nfc_manager::kNfcManagerServiceName,
881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                       dbus::ObjectPath(nfc_manager::kNfcManagerServicePath)))
891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        .WillRepeatedly(Return(mock_manager_proxy_.get()));
901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    EXPECT_CALL(*mock_bus_.get(),
911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                GetObjectProxy(nfc_adapter::kNfcAdapterServiceName,
921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                               dbus::ObjectPath(kTestAdapterPath0)))
931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        .WillRepeatedly(Return(mock_adapter0_proxy_.get()));
941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    EXPECT_CALL(*mock_bus_.get(),
951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                GetObjectProxy(nfc_adapter::kNfcAdapterServiceName,
961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                               dbus::ObjectPath(kTestAdapterPath1)))
971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        .WillRepeatedly(Return(mock_adapter1_proxy_.get()));
981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // ShutdownAndBlock will be called in TearDown.
1001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return());
1011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // Create the clients.
1031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    manager_client_.reset(
1041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        NfcManagerClient::Create(REAL_DBUS_CLIENT_IMPLEMENTATION));
1051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    adapter_client_.reset(
1061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        NfcAdapterClient::Create(REAL_DBUS_CLIENT_IMPLEMENTATION,
1071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                 manager_client_.get()));
1081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    manager_client_->Init(mock_bus_.get());
1091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    adapter_client_->Init(mock_bus_.get());
1101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    manager_client_->AddObserver(&mock_manager_observer_);
1111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    adapter_client_->AddObserver(&mock_adapter_observer_);
1121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    message_loop_.RunUntilIdle();
1141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
1151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual void TearDown() OVERRIDE {
1171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    adapter_client_->RemoveObserver(&mock_adapter_observer_);
1181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    manager_client_->RemoveObserver(&mock_manager_observer_);
1191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    mock_bus_->ShutdownAndBlock();
1201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
1211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void SendManagerAdapterAddedSignal(const dbus::ObjectPath& object_path) {
1231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    dbus::Signal signal(nfc_manager::kNfcManagerInterface,
1241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                        nfc_manager::kAdapterAddedSignal);
1251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    dbus::MessageWriter writer(&signal);
1261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    writer.AppendObjectPath(object_path);
1271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    ASSERT_FALSE(manager_adapter_added_signal_callback_.is_null());
1281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    manager_adapter_added_signal_callback_.Run(&signal);
1291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
1301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void SendManagerAdapterRemovedSignal(const dbus::ObjectPath& object_path) {
1321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    dbus::Signal signal(nfc_manager::kNfcManagerInterface,
1331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                        nfc_manager::kAdapterRemovedSignal);
1341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    dbus::MessageWriter writer(&signal);
1351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    writer.AppendObjectPath(object_path);
1361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    ASSERT_FALSE(manager_adapter_removed_signal_callback_.is_null());
1371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    manager_adapter_removed_signal_callback_.Run(&signal);
1381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
1391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  MOCK_METHOD0(SuccessCallback, void(void));
1411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  MOCK_METHOD2(ErrorCallback, void(const std::string& error_name,
1421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                   const std::string& error_message));
1431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) protected:
1451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // The mock bus.
1461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  scoped_refptr<dbus::MockBus> mock_bus_;
1471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // A message loop to emulate asynchronous behavior.
1481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  base::MessageLoop message_loop_;
1491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Response returned by mock methods.
1501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  dbus::Response* response_;
1511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // The D-Bus client objects under test.
1521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  scoped_ptr<NfcManagerClient> manager_client_;
1531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  scoped_ptr<NfcAdapterClient> adapter_client_;
1541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Mock observers.
1551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  MockNfcManagerObserver mock_manager_observer_;
1561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  MockNfcAdapterObserver mock_adapter_observer_;
1571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // The mock object proxies.
1581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  scoped_refptr<dbus::MockObjectProxy> mock_manager_proxy_;
1591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  scoped_refptr<dbus::MockObjectProxy> mock_adapter0_proxy_;
1601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  scoped_refptr<dbus::MockObjectProxy> mock_adapter1_proxy_;
1611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // The signal callbacks used to simulate asychronous signals.
1621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  dbus::ObjectProxy::SignalCallback manager_adapter_added_signal_callback_;
1631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  dbus::ObjectProxy::SignalCallback manager_adapter_removed_signal_callback_;
1641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) private:
1661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Used to implement the mock proxy.
1671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void OnConnectToSignal(
1681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      const std::string& interface_name,
1691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      const std::string& signal_name,
1701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      const dbus::ObjectProxy::SignalCallback& signal_callback,
1711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      const dbus::ObjectProxy::OnConnectedCallback& on_connected_callback) {
1721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    if (interface_name == nfc_manager::kNfcManagerInterface) {
1731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      if (signal_name == nfc_manager::kAdapterAddedSignal)
1741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        manager_adapter_added_signal_callback_ = signal_callback;
1751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      else if (signal_name == nfc_manager::kAdapterRemovedSignal)
1761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        manager_adapter_removed_signal_callback_ = signal_callback;
1771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    }
1781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    message_loop_.PostTask(FROM_HERE, base::Bind(on_connected_callback,
1791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                 interface_name,
1801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                 signal_name,
1811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                 true));
1821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
1831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)};
1841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Tests that when adapters are added and removed through the manager, all
1861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// observers are notified and the proxies are created and removed
1871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// accordingly.
1881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)TEST_F(NfcClientTest, AdaptersAddedAndRemoved) {
1891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Invoking methods on adapters that haven't been added should fail.
1901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_CALL(*this,
1911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)              ErrorCallback(nfc_client_helpers::kUnknownObjectError, _));
1921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  adapter_client_->StartPollLoop(
1931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      dbus::ObjectPath(kTestAdapterPath0),
1941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      nfc_adapter::kModeInitiator,
1951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::Bind(&NfcClientTest::SuccessCallback, base::Unretained(this)),
1961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::Bind(&NfcClientTest::ErrorCallback, base::Unretained(this)));
1971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  Mock::VerifyAndClearExpectations(this);
1981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Add adapter 0.
2001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_CALL(mock_manager_observer_,
2011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)              AdapterAdded(dbus::ObjectPath(kTestAdapterPath0)));
2021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_CALL(mock_adapter_observer_,
2031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)              AdapterAdded(dbus::ObjectPath(kTestAdapterPath0)));
2041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  SendManagerAdapterAddedSignal(dbus::ObjectPath(kTestAdapterPath0));
2051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  Mock::VerifyAndClearExpectations(this);
2061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Invoking methods should succeed on adapter 0 but fail on adapter 1.
2081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_CALL(*mock_adapter0_proxy_, CallMethodWithErrorCallback(_, _, _, _));
2091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  adapter_client_->StartPollLoop(
2101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      dbus::ObjectPath(kTestAdapterPath0),
2111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      nfc_adapter::kModeInitiator,
2121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::Bind(&NfcClientTest::SuccessCallback, base::Unretained(this)),
2131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::Bind(&NfcClientTest::ErrorCallback, base::Unretained(this)));
2141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  Mock::VerifyAndClearExpectations(this);
2151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_CALL(*this,
2171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)              ErrorCallback(nfc_client_helpers::kUnknownObjectError, _));
2181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  adapter_client_->StartPollLoop(
2191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      dbus::ObjectPath(kTestAdapterPath1),
2201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      nfc_adapter::kModeInitiator,
2211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::Bind(&NfcClientTest::SuccessCallback, base::Unretained(this)),
2221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::Bind(&NfcClientTest::ErrorCallback, base::Unretained(this)));
2231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  Mock::VerifyAndClearExpectations(this);
2241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Add adapter 1.
2261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_CALL(mock_manager_observer_,
2271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)              AdapterAdded(dbus::ObjectPath(kTestAdapterPath1)));
2281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_CALL(mock_adapter_observer_,
2291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)              AdapterAdded(dbus::ObjectPath(kTestAdapterPath1)));
2301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  SendManagerAdapterAddedSignal(dbus::ObjectPath(kTestAdapterPath1));
2311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  Mock::VerifyAndClearExpectations(this);
2321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Invoking methods should succeed on both adapters.
2341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_CALL(*mock_adapter0_proxy_, CallMethodWithErrorCallback(_, _, _, _));
2351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_CALL(*mock_adapter1_proxy_, CallMethodWithErrorCallback(_, _, _, _));
2361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  adapter_client_->StartPollLoop(
2371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      dbus::ObjectPath(kTestAdapterPath0),
2381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      nfc_adapter::kModeInitiator,
2391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::Bind(&NfcClientTest::SuccessCallback, base::Unretained(this)),
2401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::Bind(&NfcClientTest::ErrorCallback, base::Unretained(this)));
2411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  adapter_client_->StartPollLoop(
2421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      dbus::ObjectPath(kTestAdapterPath1),
2431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      nfc_adapter::kModeInitiator,
2441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::Bind(&NfcClientTest::SuccessCallback, base::Unretained(this)),
2451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::Bind(&NfcClientTest::ErrorCallback, base::Unretained(this)));
2461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  Mock::VerifyAndClearExpectations(this);
2471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Remove adapter 0.
2491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_CALL(mock_manager_observer_,
2501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)              AdapterRemoved(dbus::ObjectPath(kTestAdapterPath0)));
2511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_CALL(mock_adapter_observer_,
2521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)              AdapterRemoved(dbus::ObjectPath(kTestAdapterPath0)));
2531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  SendManagerAdapterRemovedSignal(dbus::ObjectPath(kTestAdapterPath0));
2541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  Mock::VerifyAndClearExpectations(this);
2551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Invoking methods should succeed on adapter 1 but fail on adapter 0.
2571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_CALL(*this,
2581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)              ErrorCallback(nfc_client_helpers::kUnknownObjectError, _));
2591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  adapter_client_->StartPollLoop(
2601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      dbus::ObjectPath(kTestAdapterPath0),
2611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      nfc_adapter::kModeInitiator,
2621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::Bind(&NfcClientTest::SuccessCallback, base::Unretained(this)),
2631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::Bind(&NfcClientTest::ErrorCallback, base::Unretained(this)));
2641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  Mock::VerifyAndClearExpectations(this);
2651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_CALL(*mock_adapter1_proxy_, CallMethodWithErrorCallback(_, _, _, _));
2671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  adapter_client_->StartPollLoop(
2681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      dbus::ObjectPath(kTestAdapterPath1),
2691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      nfc_adapter::kModeInitiator,
2701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::Bind(&NfcClientTest::SuccessCallback, base::Unretained(this)),
2711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::Bind(&NfcClientTest::ErrorCallback, base::Unretained(this)));
2721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  Mock::VerifyAndClearExpectations(this);
2731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Remove adapter 1.
2751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_CALL(mock_manager_observer_,
2761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)              AdapterRemoved(dbus::ObjectPath(kTestAdapterPath1)));
2771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_CALL(mock_adapter_observer_,
2781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)              AdapterRemoved(dbus::ObjectPath(kTestAdapterPath1)));
2791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  SendManagerAdapterRemovedSignal(dbus::ObjectPath(kTestAdapterPath1));
2801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  Mock::VerifyAndClearExpectations(this);
2811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Invoking methods should fail on both adapters.
2831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  EXPECT_CALL(*this,
2841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)              ErrorCallback(nfc_client_helpers::kUnknownObjectError, _))
2851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      .Times(2);
2861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  adapter_client_->StartPollLoop(
2871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      dbus::ObjectPath(kTestAdapterPath0),
2881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      nfc_adapter::kModeInitiator,
2891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::Bind(&NfcClientTest::SuccessCallback, base::Unretained(this)),
2901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::Bind(&NfcClientTest::ErrorCallback, base::Unretained(this)));
2911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  adapter_client_->StartPollLoop(
2921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      dbus::ObjectPath(kTestAdapterPath1),
2931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      nfc_adapter::kModeInitiator,
2941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::Bind(&NfcClientTest::SuccessCallback, base::Unretained(this)),
2951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::Bind(&NfcClientTest::ErrorCallback, base::Unretained(this)));
2961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}  // namespace chromeos
299