/*
 * Copyright © 2012-2013 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 */
#include <com/lomiri/location/provider.h>
#include <com/lomiri/location/session.h>

#include <gmock/gmock.h>
#include <gtest/gtest.h>

namespace
{
struct MockProvider : public com::lomiri::location::Provider
{
    MOCK_METHOD1(subscribe_to_position_updates,
                 com::lomiri::location::ChannelConnection(std::function<void(const com::lomiri::location::Update<com::lomiri::location::Position>&)>));

    MOCK_METHOD1(subscribe_to_heading_updates,
                 com::lomiri::location::ChannelConnection(std::function<void(const com::lomiri::location::Update<com::lomiri::location::Heading>&)>));

    MOCK_METHOD1(subscribe_to_velocity_updates,
                 com::lomiri::location::ChannelConnection(std::function<void(const com::lomiri::location::Update<com::lomiri::location::Velocity>&)>));
};
}

TEST(LocationSession, constructing_with_a_null_provider_throws)
{
    EXPECT_ANY_THROW(com::lomiri::location::Session session(com::lomiri::location::Provider::Ptr {}));
}

TEST(LocationSession, changing_provider_association_of_session_results_in_connections_to_new_provider)
{
    using namespace ::testing;
    NiceMock<MockProvider> mock_provider1;
    ON_CALL(mock_provider1, subscribe_to_position_updates(_))
    .WillByDefault(Return(com::lomiri::location::ChannelConnection()));
    ON_CALL(mock_provider1, subscribe_to_heading_updates(_))
    .WillByDefault(Return(com::lomiri::location::ChannelConnection()));
    ON_CALL(mock_provider1, subscribe_to_velocity_updates(_))
    .WillByDefault(Return(com::lomiri::location::ChannelConnection()));

    NiceMock<MockProvider> mock_provider2;
    EXPECT_CALL(mock_provider2, subscribe_to_position_updates(_))
    .WillRepeatedly(Return(com::lomiri::location::ChannelConnection()));
    EXPECT_CALL(mock_provider2, subscribe_to_heading_updates(_))
    .WillRepeatedly(Return(com::lomiri::location::ChannelConnection()));
    EXPECT_CALL(mock_provider2, subscribe_to_velocity_updates(_))
    .WillRepeatedly(Return(com::lomiri::location::ChannelConnection()));

    com::lomiri::location::Session session(com::lomiri::location::Provider::Ptr {&mock_provider1, [](com::lomiri::location::Provider*) {}});
    session.change_provider_assocation_to(com::lomiri::location::Provider::Ptr {&mock_provider2, [](com::lomiri::location::Provider*) {}});
}

TEST(LocationSession, installing_updates_handlers_connects_them_to_the_provider_associated_to_the_session)
{
    using namespace ::testing;

    NiceMock<MockProvider> mock_provider;

    EXPECT_CALL(mock_provider, subscribe_to_position_updates(_))
    .Times(1)
    .WillRepeatedly(Return(com::lomiri::location::ChannelConnection()));
    EXPECT_CALL(mock_provider, subscribe_to_heading_updates(_))
    .Times(1)
    .WillRepeatedly(Return(com::lomiri::location::ChannelConnection()));
    EXPECT_CALL(mock_provider, subscribe_to_velocity_updates(_))
    .Times(1)
    .WillRepeatedly(Return(com::lomiri::location::ChannelConnection()));

    com::lomiri::location::Session session(com::lomiri::location::Provider::Ptr {&mock_provider, [](com::lomiri::location::Provider*) {}});

    session.install_position_updates_handler([](const com::lomiri::location::Update<com::lomiri::location::Position>&)
    {
    });
    session.install_heading_updates_handler([](const com::lomiri::location::Update<com::lomiri::location::Heading>&)
    {
    });
    session.install_velocity_updates_handler([](const com::lomiri::location::Update<com::lomiri::location::Velocity>&)
    {
    });
}
