Commit 7a376a1a authored by lethalantidote's avatar lethalantidote Committed by Commit bot

Adds GeolocationFeature for Blimp Geolocation project.

This change provides the client-side classes needed to receive and send
geolocation messages. GeolocationFeature sends messages from the client
that either contain a geolocation coordinate or error message. The
GeolocationFeature class handles incoming GeoloationSetInterestLevelMessages
and RequestRefreshMessages.

Other items included:
* Changes device/geolocation/mock_location_provider to use fake_location_provider name
* Moves blimp/client/feature/mock_location_provider into device/geolocation.

BUG=614486

Review-Url: https://codereview.chromium.org/2161223003
Cr-Commit-Position: refs/heads/master@{#414797}
parent f8bc5669
...@@ -37,6 +37,7 @@ group("core") { ...@@ -37,6 +37,7 @@ group("core") {
":switches", ":switches",
"//blimp/client/core/compositor", "//blimp/client/core/compositor",
"//blimp/client/core/contents", "//blimp/client/core/contents",
"//blimp/client/core/geolocation",
"//blimp/client/core/session", "//blimp/client/core/session",
] ]
} }
...@@ -78,6 +79,7 @@ source_set("unit_tests") { ...@@ -78,6 +79,7 @@ source_set("unit_tests") {
"//blimp/client:feature", "//blimp/client:feature",
"//blimp/client/core/compositor:unit_tests", "//blimp/client/core/compositor:unit_tests",
"//blimp/client/core/contents:unit_tests", "//blimp/client/core/contents:unit_tests",
"//blimp/client/core/geolocation:unit_tests",
"//blimp/client/core/session:unit_tests", "//blimp/client/core/session:unit_tests",
"//blimp/client/public:public_headers", "//blimp/client/public:public_headers",
"//blimp/client/test", "//blimp/client/test",
......
# Copyright 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
if (is_android) {
import("//build/config/android/config.gni")
import("//build/config/android/rules.gni")
}
source_set("geolocation") {
visibility = [
"//blimp/client/core/*",
"//blimp/engine:browser_tests",
]
sources = [
"geolocation_feature.cc",
"geolocation_feature.h",
]
deps = [
"//base:base",
"//blimp/common",
"//cc",
"//cc/proto",
"//net:net",
]
public_deps = [
"//blimp/common/proto",
"//blimp/net",
"//device/geolocation:device_geolocation",
]
}
source_set("unit_tests") {
visibility = [ "//blimp/client/core:unit_tests" ]
testonly = true
sources = [
"geolocation_feature_unittest.cc",
]
deps = [
":geolocation",
"//base",
"//base/test:test_support",
"//blimp/common",
"//blimp/common/proto",
"//blimp/net",
"//blimp/net:test_support",
"//blimp/test:support",
"//cc/proto",
"//device/geolocation:device_geolocation",
"//device/geolocation:test_support",
"//net",
"//net:test_support",
"//testing/gmock",
"//testing/gtest",
]
}
include_rules = [
"+device/geolocation",
"+google_apis",
]
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "blimp/client/core/geolocation/geolocation_feature.h"
#include <memory>
#include <string>
#include <utility>
#include "blimp/common/create_blimp_message.h"
#include "blimp/common/proto/blimp_message.pb.h"
#include "device/geolocation/geoposition.h"
#include "device/geolocation/location_provider.h"
#include "net/base/net_errors.h"
namespace blimp {
namespace client {
GeolocationFeature::GeolocationFeature(
std::unique_ptr<device::LocationProvider> location_provider)
: location_provider_(std::move(location_provider)) {
location_provider_->SetUpdateCallback(base::Bind(
&GeolocationFeature::OnLocationUpdate, base::Unretained(this)));
}
GeolocationFeature::~GeolocationFeature() {}
void GeolocationFeature::set_outgoing_message_processor(
std::unique_ptr<BlimpMessageProcessor> processor) {
outgoing_message_processor_ = std::move(processor);
am_sending_message_ = false;
}
void GeolocationFeature::ProcessMessage(
std::unique_ptr<BlimpMessage> message,
const net::CompletionCallback& callback) {
DCHECK_EQ(BlimpMessage::kGeolocation, message->feature_case());
int result = net::OK;
const GeolocationMessage& geolocation_message = message->geolocation();
switch (geolocation_message.type_case()) {
case GeolocationMessage::kSetInterestLevel:
SetInterestLevel(geolocation_message.set_interest_level().level());
break;
case GeolocationMessage::kRequestRefresh:
location_provider_->OnPermissionGranted();
break;
case GeolocationMessage::kCoordinates:
case GeolocationMessage::kError:
case GeolocationMessage::TYPE_NOT_SET:
result = net::ERR_UNEXPECTED;
break;
}
if (!callback.is_null()) {
callback.Run(result);
}
}
void GeolocationFeature::OnLocationUpdate(
const device::LocationProvider* location_provider,
const device::Geoposition& position) {
DCHECK_EQ(location_provider_.get(), location_provider);
if (!am_sending_message_) {
switch (position.error_code) {
case device::Geoposition::ERROR_CODE_NONE:
SendGeolocationPositionMessage(position);
break;
case device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED:
SendGeolocationErrorMessage(GeolocationErrorMessage::PERMISSION_DENIED,
position.error_message);
break;
case device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE:
SendGeolocationErrorMessage(
GeolocationErrorMessage::POSITION_UNAVAILABLE,
position.error_message);
break;
case device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT:
SendGeolocationErrorMessage(GeolocationErrorMessage::TIMEOUT,
position.error_message);
break;
}
} else {
need_to_send_message_ = true;
}
}
void GeolocationFeature::SetInterestLevel(
GeolocationSetInterestLevelMessage::Level level) {
switch (level) {
case GeolocationSetInterestLevelMessage::HIGH_ACCURACY:
location_provider_->StartProvider(true);
break;
case GeolocationSetInterestLevelMessage::LOW_ACCURACY:
location_provider_->StartProvider(false);
break;
case GeolocationSetInterestLevelMessage::NO_INTEREST:
location_provider_->StopProvider();
break;
}
}
void GeolocationFeature::SendGeolocationPositionMessage(
const device::Geoposition& position) {
GeolocationMessage* geolocation_message = nullptr;
std::unique_ptr<BlimpMessage> blimp_message =
CreateBlimpMessage(&geolocation_message);
GeolocationCoordinatesMessage* coordinates =
geolocation_message->mutable_coordinates();
coordinates->set_latitude(position.latitude);
coordinates->set_longitude(position.longitude);
coordinates->set_altitude(position.altitude);
coordinates->set_accuracy(position.accuracy);
coordinates->set_altitude_accuracy(position.altitude_accuracy);
coordinates->set_heading(position.heading);
coordinates->set_speed(position.speed);
am_sending_message_ = true;
outgoing_message_processor_->ProcessMessage(
std::move(blimp_message),
base::Bind(&GeolocationFeature::OnSendComplete, base::Unretained(this)));
}
void GeolocationFeature::SendGeolocationErrorMessage(
GeolocationErrorMessage::ErrorCode error_code,
const std::string& error_message) {
GeolocationMessage* geolocation_message = nullptr;
std::unique_ptr<BlimpMessage> blimp_message =
CreateBlimpMessage(&geolocation_message);
GeolocationErrorMessage* error = geolocation_message->mutable_error();
error->set_error_code(error_code);
error->set_error_message(error_message);
am_sending_message_ = true;
outgoing_message_processor_->ProcessMessage(
std::move(blimp_message),
base::Bind(&GeolocationFeature::OnSendComplete, base::Unretained(this)));
}
void GeolocationFeature::OnSendComplete(int result) {
am_sending_message_ = false;
if (need_to_send_message_) {
need_to_send_message_ = false;
OnLocationUpdate(location_provider_.get(),
location_provider_->GetPosition());
}
}
} // namespace client
} // namespace blimp
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BLIMP_CLIENT_CORE_GEOLOCATION_GEOLOCATION_FEATURE_H_
#define BLIMP_CLIENT_CORE_GEOLOCATION_GEOLOCATION_FEATURE_H_
#include <memory>
#include <string>
#include "blimp/common/proto/geolocation.pb.h"
#include "blimp/net/blimp_message_processor.h"
#include "device/geolocation/geoposition.h"
#include "device/geolocation/location_provider.h"
namespace blimp {
namespace client {
// Client-side feature handling geolocation messages.
class GeolocationFeature : public BlimpMessageProcessor {
public:
explicit GeolocationFeature(
std::unique_ptr<device::LocationProvider> location_provider);
~GeolocationFeature() override;
// Sets the BlimpMessageProcessor that will be used to send
// BlimpMessage::GEOLOCATION messages to the engine.
void set_outgoing_message_processor(
std::unique_ptr<BlimpMessageProcessor> processor);
// BlimpMessageProcessor implementation.
void ProcessMessage(std::unique_ptr<BlimpMessage> message,
const net::CompletionCallback& callback) override;
private:
// Sends engine an update of the client's geoposition.
void OnLocationUpdate(const device::LocationProvider* location_provider,
const device::Geoposition& position);
// Handles a request from the Engine to change the accuracy level of the
// Geolocation information reported to it.
void SetInterestLevel(GeolocationSetInterestLevelMessage::Level level);
// Sends a GeolocationPositionMessage that reflects the given
// geoposition.
void SendGeolocationPositionMessage(const device::Geoposition& position);
// Sends a GeolocationErrorMessage.
void SendGeolocationErrorMessage(
GeolocationErrorMessage::ErrorCode error_code,
const std::string& error_message);
// Called when a message send completes.
void OnSendComplete(int result);
// Used to obtain the client's location.
std::unique_ptr<device::LocationProvider> location_provider_;
// Used to send BlimpMessage::GEOLOCATION message to the engine.
std::unique_ptr<BlimpMessageProcessor> outgoing_message_processor_;
// True if a message is in the process of being sent.
bool am_sending_message_ = false;
// True if location has been updated but a message cannot be sent at the
// moment.
bool need_to_send_message_ = false;
DISALLOW_COPY_AND_ASSIGN(GeolocationFeature);
};
} // namespace client
} // namespace blimp
#endif // BLIMP_CLIENT_CORE_GEOLOCATION_GEOLOCATION_FEATURE_H_
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "blimp/client/core/geolocation/geolocation_feature.h"
#include <memory>
#include <utility>
#include "base/memory/ptr_util.h"
#include "blimp/common/create_blimp_message.h"
#include "blimp/common/proto/blimp_message.pb.h"
#include "blimp/net/test_common.h"
#include "device/geolocation/geoposition.h"
#include "device/geolocation/location_provider.h"
#include "device/geolocation/mock_location_provider.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/test/gtest_util.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::InSequence;
using testing::Invoke;
using testing::SaveArg;
using testing::StrictMock;
using testing::_;
namespace blimp {
namespace client {
const double kLatitude = -42.0;
const double kLongitude = 17.3;
const double kAltitude = 123.4;
const double kAccuracy = 73.7;
MATCHER(EqualsDefaultGeoposition, "") {
return arg.feature_case() == BlimpMessage::kGeolocation &&
arg.geolocation().type_case() == GeolocationMessage::kCoordinates &&
arg.geolocation().coordinates().latitude() == kLatitude &&
arg.geolocation().coordinates().longitude() == kLongitude &&
arg.geolocation().coordinates().altitude() == kAltitude &&
arg.geolocation().coordinates().accuracy() == kAccuracy;
}
MATCHER_P4(EqualGeoposition, lat, lon, alt, acc, "") {
return arg.feature_case() == BlimpMessage::kGeolocation &&
arg.geolocation().type_case() == GeolocationMessage::kCoordinates &&
arg.geolocation().coordinates().latitude() == lat &&
arg.geolocation().coordinates().longitude() == lon &&
arg.geolocation().coordinates().altitude() == alt &&
arg.geolocation().coordinates().accuracy() == acc;
}
MATCHER_P(EqualsError, error_code, "") {
return arg.feature_case() == BlimpMessage::kGeolocation &&
arg.geolocation().type_case() == GeolocationMessage::kError &&
arg.geolocation().error().error_code() == error_code;
}
class GeolocationFeatureTest : public testing::Test {
public:
GeolocationFeatureTest() {}
void SetUp() override {
auto location_provider =
base::MakeUnique<StrictMock<device::MockLocationProvider>>();
location_provider_ = location_provider.get();
EXPECT_CALL(*location_provider_, SetUpdateCallback(_))
.WillOnce(SaveArg<0>(&callback_));
feature_ =
base::MakeUnique<GeolocationFeature>(std::move(location_provider));
auto out_processor =
base::MakeUnique<StrictMock<MockBlimpMessageProcessor>>();
out_processor_ = out_processor.get();
feature_->set_outgoing_message_processor(std::move(out_processor));
position_.latitude = kLatitude;
position_.longitude = kLongitude;
position_.altitude = kAltitude;
position_.accuracy = kAccuracy;
}
protected:
void SendMockSetInterestLevelMessage(
GeolocationSetInterestLevelMessage::Level level) {
GeolocationMessage* geolocation_message;
std::unique_ptr<BlimpMessage> message =
CreateBlimpMessage(&geolocation_message);
GeolocationSetInterestLevelMessage* interest_message =
geolocation_message->mutable_set_interest_level();
interest_message->set_level(level);
net::TestCompletionCallback cb;
feature_->ProcessMessage(std::move(message), cb.callback());
EXPECT_EQ(net::OK, cb.WaitForResult());
}
void ReportProcessMessageSuccess(const BlimpMessage& blimp_message,
const net::CompletionCallback& callback) {
callback.Run(net::OK);
}
// These are raw pointers to classes that are owned by the
// GeolocationFeature.
StrictMock<MockBlimpMessageProcessor>* out_processor_;
StrictMock<device::MockLocationProvider>* location_provider_;
std::unique_ptr<GeolocationFeature> feature_;
device::LocationProvider::LocationProviderUpdateCallback callback_;
device::Geoposition position_;
private:
DISALLOW_COPY_AND_ASSIGN(GeolocationFeatureTest);
};
TEST_F(GeolocationFeatureTest, UpdateInterestLevelReceived) {
InSequence s;
EXPECT_CALL(*location_provider_, StartProvider(true));
EXPECT_CALL(*location_provider_, StopProvider());
EXPECT_CALL(*location_provider_, StartProvider(false));
SendMockSetInterestLevelMessage(
GeolocationSetInterestLevelMessage::HIGH_ACCURACY);
SendMockSetInterestLevelMessage(
GeolocationSetInterestLevelMessage::NO_INTEREST);
SendMockSetInterestLevelMessage(
GeolocationSetInterestLevelMessage::LOW_ACCURACY);
}
TEST_F(GeolocationFeatureTest, UnexpectedMessageReceived) {
GeolocationMessage* geolocation_message;
std::unique_ptr<BlimpMessage> message =
CreateBlimpMessage(&geolocation_message);
GeolocationCoordinatesMessage* coordinates_message =
geolocation_message->mutable_coordinates();
coordinates_message->set_latitude(1.0);
net::TestCompletionCallback cb;
feature_->ProcessMessage(std::move(message), cb.callback());
EXPECT_EQ(net::ERR_UNEXPECTED, cb.WaitForResult());
}
TEST_F(GeolocationFeatureTest, RequestRefreshReceived) {
EXPECT_CALL(*location_provider_, OnPermissionGranted());
GeolocationMessage* geolocation_message;
std::unique_ptr<BlimpMessage> message =
CreateBlimpMessage(&geolocation_message);
geolocation_message->mutable_request_refresh();
net::TestCompletionCallback cb;
feature_->ProcessMessage(std::move(message), cb.callback());
EXPECT_EQ(net::OK, cb.WaitForResult());
}
TEST_F(GeolocationFeatureTest, LocationUpdateSendsCorrectMessage) {
EXPECT_CALL(*out_processor_,
MockableProcessMessage(EqualsDefaultGeoposition(), _));
callback_.Run(location_provider_, position_);
}
TEST_F(GeolocationFeatureTest, ErrorUpdateSendsCorrectMessage) {
ON_CALL(*out_processor_, MockableProcessMessage(_, _))
.WillByDefault(Invoke(
this, &GeolocationFeatureTest_ErrorUpdateSendsCorrectMessage_Test::
ReportProcessMessageSuccess));
EXPECT_CALL(
*out_processor_,
MockableProcessMessage(
EqualsError(GeolocationErrorMessage::POSITION_UNAVAILABLE), _));
EXPECT_CALL(*out_processor_,
MockableProcessMessage(
EqualsError(GeolocationErrorMessage::PERMISSION_DENIED), _));
EXPECT_CALL(
*out_processor_,
MockableProcessMessage(EqualsError(GeolocationErrorMessage::TIMEOUT), _));
device::Geoposition err_position;
err_position.error_code =
device::Geoposition::ErrorCode::ERROR_CODE_POSITION_UNAVAILABLE;
callback_.Run(location_provider_, err_position);
err_position.error_code =
device::Geoposition::ErrorCode::ERROR_CODE_PERMISSION_DENIED;
callback_.Run(location_provider_, err_position);
err_position.error_code = device::Geoposition::ErrorCode::ERROR_CODE_TIMEOUT;
callback_.Run(location_provider_, err_position);
}
TEST_F(GeolocationFeatureTest, NoRepeatSendsWithMessagePending) {
EXPECT_CALL(*out_processor_,
MockableProcessMessage(EqualsDefaultGeoposition(), _));
callback_.Run(location_provider_, position_);
callback_.Run(location_provider_, position_);
callback_.Run(location_provider_, position_);
}
TEST_F(GeolocationFeatureTest, MessageSendsAfterAcknowledgement) {
EXPECT_CALL(*out_processor_,
MockableProcessMessage(EqualsDefaultGeoposition(), _))
.WillOnce(Invoke(
this, &GeolocationFeatureTest_MessageSendsAfterAcknowledgement_Test::
ReportProcessMessageSuccess));
device::Geoposition position;
position.latitude = 1.0;
position.longitude = 1.0;
position.altitude = 1.0;
position.accuracy = 1.0;
EXPECT_CALL(*out_processor_,
MockableProcessMessage(EqualGeoposition(1.0, 1.0, 1.0, 1.0), _));
callback_.Run(location_provider_, position_);
callback_.Run(location_provider_, position);
}
TEST_F(GeolocationFeatureTest, ProcessMessageHandlesNullCallback) {
EXPECT_CALL(*location_provider_, OnPermissionGranted());
GeolocationMessage* geolocation_message;
std::unique_ptr<BlimpMessage> message =
CreateBlimpMessage(&geolocation_message);
geolocation_message->mutable_request_refresh();
feature_->ProcessMessage(std::move(message), net::CompletionCallback());
}
} // namespace client
} // namespace blimp
...@@ -51,7 +51,7 @@ const device::Geoposition& BlimpLocationProvider::GetPosition() { ...@@ -51,7 +51,7 @@ const device::Geoposition& BlimpLocationProvider::GetPosition() {
void BlimpLocationProvider::OnPermissionGranted() { void BlimpLocationProvider::OnPermissionGranted() {
DCHECK(is_started_); DCHECK(is_started_);
if (delegate_) { if (delegate_) {
delegate_->RequestRefresh(); delegate_->OnPermissionGranted();
} }
} }
......
...@@ -26,7 +26,7 @@ class BlimpLocationProvider : public device::LocationProvider { ...@@ -26,7 +26,7 @@ class BlimpLocationProvider : public device::LocationProvider {
virtual void RequestAccuracy( virtual void RequestAccuracy(
GeolocationSetInterestLevelMessage::Level level) = 0; GeolocationSetInterestLevelMessage::Level level) = 0;
virtual void RequestRefresh() = 0; virtual void OnPermissionGranted() = 0;
virtual void SetUpdateCallback( virtual void SetUpdateCallback(
const GeopositionReceivedCallback& callback) = 0; const GeopositionReceivedCallback& callback) = 0;
}; };
......
...@@ -107,14 +107,14 @@ TEST_F(BlimpLocationProviderTest, LocationProviderDeleted) { ...@@ -107,14 +107,14 @@ TEST_F(BlimpLocationProviderTest, LocationProviderDeleted) {
} }
TEST_F(BlimpLocationProviderTest, OnPermissionGranted) { TEST_F(BlimpLocationProviderTest, OnPermissionGranted) {
EXPECT_CALL(*delegate_, RequestRefresh()).Times(1); EXPECT_CALL(*delegate_, OnPermissionGranted()).Times(1);
location_provider_->StartProvider(true); location_provider_->StartProvider(true);
location_provider_->OnPermissionGranted(); location_provider_->OnPermissionGranted();
} }
TEST_F(BlimpLocationProviderTest, OnPermissionGrantedHandlesNullDelegate) { TEST_F(BlimpLocationProviderTest, OnPermissionGrantedHandlesNullDelegate) {
EXPECT_CALL(*delegate_, RequestRefresh()).Times(0); EXPECT_CALL(*delegate_, OnPermissionGranted()).Times(0);
location_provider_->StartProvider(true); location_provider_->StartProvider(true);
delegate_.reset(); delegate_.reset();
......
...@@ -137,7 +137,7 @@ void EngineGeolocationFeature::RequestAccuracy( ...@@ -137,7 +137,7 @@ void EngineGeolocationFeature::RequestAccuracy(
net::CompletionCallback()); net::CompletionCallback());
} }
void EngineGeolocationFeature::RequestRefresh() { void EngineGeolocationFeature::OnPermissionGranted() {
GeolocationMessage* geolocation_message = nullptr; GeolocationMessage* geolocation_message = nullptr;
std::unique_ptr<BlimpMessage> blimp_message = std::unique_ptr<BlimpMessage> blimp_message =
CreateBlimpMessage(&geolocation_message); CreateBlimpMessage(&geolocation_message);
......
...@@ -42,7 +42,7 @@ class EngineGeolocationFeature : public BlimpMessageProcessor, ...@@ -42,7 +42,7 @@ class EngineGeolocationFeature : public BlimpMessageProcessor,
// BlimpLocationProvider::Delegate implementation. // BlimpLocationProvider::Delegate implementation.
void RequestAccuracy( void RequestAccuracy(
GeolocationSetInterestLevelMessage::Level level) override; GeolocationSetInterestLevelMessage::Level level) override;
void RequestRefresh() override; void OnPermissionGranted() override;
void SetUpdateCallback(const GeopositionReceivedCallback& callback) override; void SetUpdateCallback(const GeopositionReceivedCallback& callback) override;
std::unique_ptr<BlimpMessageProcessor> outgoing_message_processor_; std::unique_ptr<BlimpMessageProcessor> outgoing_message_processor_;
......
...@@ -23,7 +23,7 @@ class MockBlimpLocationProviderDelegate ...@@ -23,7 +23,7 @@ class MockBlimpLocationProviderDelegate
MOCK_METHOD1(RequestAccuracy, MOCK_METHOD1(RequestAccuracy,
void(GeolocationSetInterestLevelMessage::Level level)); void(GeolocationSetInterestLevelMessage::Level level));
MOCK_METHOD0(RequestRefresh, void()); MOCK_METHOD0(OnPermissionGranted, void());
MOCK_METHOD1( MOCK_METHOD1(
SetUpdateCallback, SetUpdateCallback,
void(const base::Callback<void(const device::Geoposition&)>& callback)); void(const base::Callback<void(const device::Geoposition&)>& callback));
......
...@@ -146,16 +146,32 @@ if (is_android) { ...@@ -146,16 +146,32 @@ if (is_android) {
} }
} }
source_set("unittests") { source_set("test_support") {
testonly = true testonly = true
sources = [ sources = [
"fake_access_token_store.cc", "fake_access_token_store.cc",
"fake_access_token_store.h", "fake_access_token_store.h",
"geolocation_provider_impl_unittest.cc", "fake_location_provider.cc",
"location_arbitrator_impl_unittest.cc", "fake_location_provider.h",
"mock_location_provider.cc", "mock_location_provider.cc",
"mock_location_provider.h", "mock_location_provider.h",
]
public_deps = [
":device_geolocation",
]
deps = [
"//testing/gmock",
"//testing/gtest",
]
}
source_set("unittests") {
testonly = true
sources = [
"geolocation_provider_impl_unittest.cc",
"location_arbitrator_impl_unittest.cc",
"network_location_provider_unittest.cc", "network_location_provider_unittest.cc",
"wifi_data_provider_chromeos_unittest.cc", "wifi_data_provider_chromeos_unittest.cc",
"wifi_data_provider_common_unittest.cc", "wifi_data_provider_common_unittest.cc",
...@@ -166,6 +182,7 @@ source_set("unittests") { ...@@ -166,6 +182,7 @@ source_set("unittests") {
":device_geolocation", ":device_geolocation",
] ]
deps = [ deps = [
":test_support",
"//base", "//base",
"//base/third_party/dynamic_annotations", "//base/third_party/dynamic_annotations",
"//net:test_support", "//net:test_support",
......
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file implements a fake location provider and the factory functions for
// various ways of creating it.
// TODO(lethalantidote): Convert location_arbitrator_impl to use actual mock
// instead of FakeLocationProvider.
#include "device/geolocation/fake_location_provider.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/compiler_specific.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_task_runner_handle.h"
namespace device {
FakeLocationProvider::FakeLocationProvider()
: provider_task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
FakeLocationProvider::~FakeLocationProvider() {}
void FakeLocationProvider::HandlePositionChanged(const Geoposition& position) {
if (provider_task_runner_->BelongsToCurrentThread()) {
// The location arbitrator unit tests rely on this method running
// synchronously.
position_ = position;
NotifyCallback(position_);
} else {
provider_task_runner_->PostTask(
FROM_HERE, base::Bind(&FakeLocationProvider::HandlePositionChanged,
base::Unretained(this), position));
}
}
bool FakeLocationProvider::StartProvider(bool high_accuracy) {
state_ = high_accuracy ? HIGH_ACCURACY : LOW_ACCURACY;
return true;
}
void FakeLocationProvider::StopProvider() {
state_ = STOPPED;
}
const Geoposition& FakeLocationProvider::GetPosition() {
return position_;
}
void FakeLocationProvider::OnPermissionGranted() {
is_permission_granted_ = true;
}
} // namespace device
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef DEVICE_GEOLOCATION_FAKE_LOCATION_PROVIDER_H_
#define DEVICE_GEOLOCATION_FAKE_LOCATION_PROVIDER_H_
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread.h"
#include "device/geolocation/geoposition.h"
#include "device/geolocation/location_provider_base.h"
namespace device {
// Fake implementation of a location provider for testing.
class FakeLocationProvider : public LocationProviderBase {
public:
enum State { STOPPED, LOW_ACCURACY, HIGH_ACCURACY } state_ = STOPPED;
FakeLocationProvider();
~FakeLocationProvider() override;
// Updates listeners with the new position.
void HandlePositionChanged(const Geoposition& position);
State state() const { return state_; }
bool is_permission_granted() const { return is_permission_granted_; }
// LocationProvider implementation.
bool StartProvider(bool high_accuracy) override;
void StopProvider() override;
const Geoposition& GetPosition() override;
void OnPermissionGranted() override;
scoped_refptr<base::SingleThreadTaskRunner> provider_task_runner_;
private:
bool is_permission_granted_ = false;
Geoposition position_;
DISALLOW_COPY_AND_ASSIGN(FakeLocationProvider);
};
} // namespace device
#endif // DEVICE_GEOLOCATION_FAKE_LOCATION_PROVIDER_H_
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "device/geolocation/access_token_store.h" #include "device/geolocation/access_token_store.h"
#include "device/geolocation/mock_location_provider.h" #include "device/geolocation/fake_location_provider.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -98,7 +98,7 @@ void DummyFunction(const LocationProvider* provider, ...@@ -98,7 +98,7 @@ void DummyFunction(const LocationProvider* provider,
class GeolocationProviderTest : public testing::Test { class GeolocationProviderTest : public testing::Test {
protected: protected:
GeolocationProviderTest() : arbitrator_(new MockLocationProvider) { GeolocationProviderTest() : arbitrator_(new FakeLocationProvider) {
provider()->SetArbitratorForTesting(base::WrapUnique(arbitrator_)); provider()->SetArbitratorForTesting(base::WrapUnique(arbitrator_));
} }
...@@ -108,7 +108,7 @@ class GeolocationProviderTest : public testing::Test { ...@@ -108,7 +108,7 @@ class GeolocationProviderTest : public testing::Test {
return GeolocationProviderImpl::GetInstance(); return GeolocationProviderImpl::GetInstance();
} }
MockLocationProvider* arbitrator() { return arbitrator_; } FakeLocationProvider* arbitrator() { return arbitrator_; }
// Called on test thread. // Called on test thread.
bool ProvidersStarted(); bool ProvidersStarted();
...@@ -116,7 +116,7 @@ class GeolocationProviderTest : public testing::Test { ...@@ -116,7 +116,7 @@ class GeolocationProviderTest : public testing::Test {
private: private:
// Called on provider thread. // Called on provider thread.
void GetProvidersStarted(bool* started); void GetProvidersStarted();
// |at_exit| must be initialized before all other variables so that it is // |at_exit| must be initialized before all other variables so that it is
// available to register with Singletons and can handle tear down when the // available to register with Singletons and can handle tear down when the
...@@ -126,7 +126,10 @@ class GeolocationProviderTest : public testing::Test { ...@@ -126,7 +126,10 @@ class GeolocationProviderTest : public testing::Test {
base::MessageLoopForUI message_loop_; base::MessageLoopForUI message_loop_;
// Owned by the GeolocationProviderImpl class. // Owned by the GeolocationProviderImpl class.
MockLocationProvider* arbitrator_; FakeLocationProvider* arbitrator_;
// True if |arbitrator_| is started.
bool is_started_;
DISALLOW_COPY_AND_ASSIGN(GeolocationProviderTest); DISALLOW_COPY_AND_ASSIGN(GeolocationProviderTest);
}; };
...@@ -135,18 +138,17 @@ bool GeolocationProviderTest::ProvidersStarted() { ...@@ -135,18 +138,17 @@ bool GeolocationProviderTest::ProvidersStarted() {
DCHECK(provider()->IsRunning()); DCHECK(provider()->IsRunning());
DCHECK(base::MessageLoop::current() == &message_loop_); DCHECK(base::MessageLoop::current() == &message_loop_);
bool started;
provider()->task_runner()->PostTaskAndReply( provider()->task_runner()->PostTaskAndReply(
FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted, FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted,
base::Unretained(this), &started), base::Unretained(this)),
base::MessageLoop::QuitWhenIdleClosure()); base::MessageLoop::QuitWhenIdleClosure());
base::RunLoop().Run(); base::RunLoop().Run();
return started; return is_started_;
} }
void GeolocationProviderTest::GetProvidersStarted(bool* started) { void GeolocationProviderTest::GetProvidersStarted() {
DCHECK(provider()->task_runner()->BelongsToCurrentThread()); DCHECK(provider()->task_runner()->BelongsToCurrentThread());
*started = arbitrator()->is_started(); is_started_ = arbitrator()->state() != FakeLocationProvider::STOPPED;
} }
void GeolocationProviderTest::SendMockLocation(const Geoposition& position) { void GeolocationProviderTest::SendMockLocation(const Geoposition& position) {
...@@ -169,11 +171,9 @@ TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { ...@@ -169,11 +171,9 @@ TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) {
TEST_F(GeolocationProviderTest, StartStop) { TEST_F(GeolocationProviderTest, StartStop) {
EXPECT_FALSE(provider()->IsRunning()); EXPECT_FALSE(provider()->IsRunning());
LocationProvider::LocationProviderUpdateCallback callback =
base::Bind(&DummyFunction);
std::unique_ptr<GeolocationProvider::Subscription> subscription = std::unique_ptr<GeolocationProvider::Subscription> subscription =
provider()->AddLocationUpdateCallback(base::Bind(callback, arbitrator()), provider()->AddLocationUpdateCallback(
false); base::Bind(&DummyFunction, arbitrator()), false);
EXPECT_TRUE(provider()->IsRunning()); EXPECT_TRUE(provider()->IsRunning());
EXPECT_TRUE(ProvidersStarted()); EXPECT_TRUE(ProvidersStarted());
......
...@@ -10,9 +10,9 @@ ...@@ -10,9 +10,9 @@
#include "base/bind.h" #include "base/bind.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "device/geolocation/fake_access_token_store.h" #include "device/geolocation/fake_access_token_store.h"
#include "device/geolocation/fake_location_provider.h"
#include "device/geolocation/geolocation_delegate.h" #include "device/geolocation/geolocation_delegate.h"
#include "device/geolocation/geoposition.h" #include "device/geolocation/geoposition.h"
#include "device/geolocation/mock_location_provider.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -47,7 +47,7 @@ void AdvanceTimeNow(const base::TimeDelta& delta) { ...@@ -47,7 +47,7 @@ void AdvanceTimeNow(const base::TimeDelta& delta) {
g_fake_time_now_secs += delta.InSecondsF(); g_fake_time_now_secs += delta.InSecondsF();
} }
void SetPositionFix(MockLocationProvider* provider, void SetPositionFix(FakeLocationProvider* provider,
double latitude, double latitude,
double longitude, double longitude,
double accuracy) { double accuracy) {
...@@ -61,7 +61,9 @@ void SetPositionFix(MockLocationProvider* provider, ...@@ -61,7 +61,9 @@ void SetPositionFix(MockLocationProvider* provider,
provider->HandlePositionChanged(position); provider->HandlePositionChanged(position);
} }
void SetReferencePosition(MockLocationProvider* provider) { // TODO(lethalantidote): Populate a Geoposition in the class from kConstants
// and then just copy that with "=" versus using a helper function.
void SetReferencePosition(FakeLocationProvider* provider) {
SetPositionFix(provider, 51.0, -0.1, 400); SetPositionFix(provider, 51.0, -0.1, 400);
} }
...@@ -76,17 +78,17 @@ class FakeGeolocationDelegate : public GeolocationDelegate { ...@@ -76,17 +78,17 @@ class FakeGeolocationDelegate : public GeolocationDelegate {
std::unique_ptr<LocationProvider> OverrideSystemLocationProvider() override { std::unique_ptr<LocationProvider> OverrideSystemLocationProvider() override {
DCHECK(!mock_location_provider_); DCHECK(!mock_location_provider_);
mock_location_provider_ = new MockLocationProvider; mock_location_provider_ = new FakeLocationProvider;
return base::WrapUnique(mock_location_provider_); return base::WrapUnique(mock_location_provider_);
} }
MockLocationProvider* mock_location_provider() const { FakeLocationProvider* mock_location_provider() const {
return mock_location_provider_; return mock_location_provider_;
} }
private: private:
bool use_network_ = true; bool use_network_ = true;
MockLocationProvider* mock_location_provider_ = nullptr; FakeLocationProvider* mock_location_provider_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(FakeGeolocationDelegate); DISALLOW_COPY_AND_ASSIGN(FakeGeolocationDelegate);
}; };
...@@ -117,12 +119,12 @@ class TestingLocationArbitrator : public LocationArbitratorImpl { ...@@ -117,12 +119,12 @@ class TestingLocationArbitrator : public LocationArbitratorImpl {
const scoped_refptr<net::URLRequestContextGetter>& context, const scoped_refptr<net::URLRequestContextGetter>& context,
const GURL& url, const GURL& url,
const base::string16& access_token) override { const base::string16& access_token) override {
cell_ = new MockLocationProvider; cell_ = new FakeLocationProvider;
return base::WrapUnique(cell_); return base::WrapUnique(cell_);
} }
std::unique_ptr<LocationProvider> NewSystemLocationProvider() override { std::unique_ptr<LocationProvider> NewSystemLocationProvider() override {
gps_ = new MockLocationProvider; gps_ = new FakeLocationProvider;
return base::WrapUnique(gps_); return base::WrapUnique(gps_);
} }
...@@ -132,8 +134,8 @@ class TestingLocationArbitrator : public LocationArbitratorImpl { ...@@ -132,8 +134,8 @@ class TestingLocationArbitrator : public LocationArbitratorImpl {
// type). // type).
// TODO(mvanouwerkerk): rename |cell_| to |network_location_provider_| and // TODO(mvanouwerkerk): rename |cell_| to |network_location_provider_| and
// |gps_| to |gps_location_provider_| // |gps_| to |gps_location_provider_|
MockLocationProvider* cell_; FakeLocationProvider* cell_;
MockLocationProvider* gps_; FakeLocationProvider* gps_;
const scoped_refptr<AccessTokenStore> access_token_store_; const scoped_refptr<AccessTokenStore> access_token_store_;
}; };
...@@ -175,9 +177,9 @@ class GeolocationLocationArbitratorTest : public testing::Test { ...@@ -175,9 +177,9 @@ class GeolocationLocationArbitratorTest : public testing::Test {
LocationArbitratorImpl::kFixStaleTimeoutMilliseconds + 1); LocationArbitratorImpl::kFixStaleTimeoutMilliseconds + 1);
} }
MockLocationProvider* cell() { return arbitrator_->cell_; } FakeLocationProvider* cell() { return arbitrator_->cell_; }
MockLocationProvider* gps() { return arbitrator_->gps_; } FakeLocationProvider* gps() { return arbitrator_->gps_; }
const scoped_refptr<FakeAccessTokenStore> access_token_store_; const scoped_refptr<FakeAccessTokenStore> access_token_store_;
const std::unique_ptr<MockLocationObserver> observer_; const std::unique_ptr<MockLocationObserver> observer_;
...@@ -219,8 +221,8 @@ TEST_F(GeolocationLocationArbitratorTest, NormalUsage) { ...@@ -219,8 +221,8 @@ TEST_F(GeolocationLocationArbitratorTest, NormalUsage) {
access_token_store_->NotifyDelegateTokensLoaded(); access_token_store_->NotifyDelegateTokensLoaded();
ASSERT_TRUE(cell()); ASSERT_TRUE(cell());
EXPECT_TRUE(gps()); EXPECT_TRUE(gps());
EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, cell()->state_);
EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, gps()->state_);
EXPECT_FALSE(observer_->last_position_.Validate()); EXPECT_FALSE(observer_->last_position_.Validate());
EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code); EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code);
...@@ -229,7 +231,7 @@ TEST_F(GeolocationLocationArbitratorTest, NormalUsage) { ...@@ -229,7 +231,7 @@ TEST_F(GeolocationLocationArbitratorTest, NormalUsage) {
EXPECT_TRUE(observer_->last_position_.Validate() || EXPECT_TRUE(observer_->last_position_.Validate() ||
observer_->last_position_.error_code != observer_->last_position_.error_code !=
Geoposition::ERROR_CODE_NONE); Geoposition::ERROR_CODE_NONE);
EXPECT_EQ(cell()->position().latitude, observer_->last_position_.latitude); EXPECT_EQ(cell()->GetPosition().latitude, observer_->last_position_.latitude);
EXPECT_FALSE(cell()->is_permission_granted()); EXPECT_FALSE(cell()->is_permission_granted());
EXPECT_FALSE(arbitrator_->HasPermissionBeenGrantedForTest()); EXPECT_FALSE(arbitrator_->HasPermissionBeenGrantedForTest());
...@@ -253,7 +255,7 @@ TEST_F(GeolocationLocationArbitratorTest, CustomSystemProviderOnly) { ...@@ -253,7 +255,7 @@ TEST_F(GeolocationLocationArbitratorTest, CustomSystemProviderOnly) {
ASSERT_FALSE(cell()); ASSERT_FALSE(cell());
EXPECT_FALSE(gps()); EXPECT_FALSE(gps());
ASSERT_TRUE(fake_delegate->mock_location_provider()); ASSERT_TRUE(fake_delegate->mock_location_provider());
EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY,
fake_delegate->mock_location_provider()->state_); fake_delegate->mock_location_provider()->state_);
EXPECT_FALSE(observer_->last_position_.Validate()); EXPECT_FALSE(observer_->last_position_.Validate());
EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code); EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code);
...@@ -263,7 +265,7 @@ TEST_F(GeolocationLocationArbitratorTest, CustomSystemProviderOnly) { ...@@ -263,7 +265,7 @@ TEST_F(GeolocationLocationArbitratorTest, CustomSystemProviderOnly) {
EXPECT_TRUE(observer_->last_position_.Validate() || EXPECT_TRUE(observer_->last_position_.Validate() ||
observer_->last_position_.error_code != observer_->last_position_.error_code !=
Geoposition::ERROR_CODE_NONE); Geoposition::ERROR_CODE_NONE);
EXPECT_EQ(fake_delegate->mock_location_provider()->position().latitude, EXPECT_EQ(fake_delegate->mock_location_provider()->GetPosition().latitude,
observer_->last_position_.latitude); observer_->last_position_.latitude);
EXPECT_FALSE( EXPECT_FALSE(
...@@ -294,9 +296,9 @@ TEST_F(GeolocationLocationArbitratorTest, ...@@ -294,9 +296,9 @@ TEST_F(GeolocationLocationArbitratorTest,
ASSERT_TRUE(cell()); ASSERT_TRUE(cell());
EXPECT_FALSE(gps()); EXPECT_FALSE(gps());
ASSERT_TRUE(fake_delegate->mock_location_provider()); ASSERT_TRUE(fake_delegate->mock_location_provider());
EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY,
fake_delegate->mock_location_provider()->state_); fake_delegate->mock_location_provider()->state_);
EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, cell()->state_);
EXPECT_FALSE(observer_->last_position_.Validate()); EXPECT_FALSE(observer_->last_position_.Validate());
EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code); EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code);
...@@ -305,7 +307,7 @@ TEST_F(GeolocationLocationArbitratorTest, ...@@ -305,7 +307,7 @@ TEST_F(GeolocationLocationArbitratorTest,
EXPECT_TRUE(observer_->last_position_.Validate() || EXPECT_TRUE(observer_->last_position_.Validate() ||
observer_->last_position_.error_code != observer_->last_position_.error_code !=
Geoposition::ERROR_CODE_NONE); Geoposition::ERROR_CODE_NONE);
EXPECT_EQ(cell()->position().latitude, observer_->last_position_.latitude); EXPECT_EQ(cell()->GetPosition().latitude, observer_->last_position_.latitude);
EXPECT_FALSE(cell()->is_permission_granted()); EXPECT_FALSE(cell()->is_permission_granted());
EXPECT_FALSE(arbitrator_->HasPermissionBeenGrantedForTest()); EXPECT_FALSE(arbitrator_->HasPermissionBeenGrantedForTest());
...@@ -320,14 +322,14 @@ TEST_F(GeolocationLocationArbitratorTest, SetObserverOptions) { ...@@ -320,14 +322,14 @@ TEST_F(GeolocationLocationArbitratorTest, SetObserverOptions) {
access_token_store_->NotifyDelegateTokensLoaded(); access_token_store_->NotifyDelegateTokensLoaded();
ASSERT_TRUE(cell()); ASSERT_TRUE(cell());
ASSERT_TRUE(gps()); ASSERT_TRUE(gps());
EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, cell()->state_);
EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, gps()->state_);
SetReferencePosition(cell()); SetReferencePosition(cell());
EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, cell()->state_);
EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, gps()->state_);
arbitrator_->StartProvider(true); arbitrator_->StartProvider(true);
EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, cell()->state_); EXPECT_EQ(FakeLocationProvider::HIGH_ACCURACY, cell()->state_);
EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, gps()->state_); EXPECT_EQ(FakeLocationProvider::HIGH_ACCURACY, gps()->state_);
} }
TEST_F(GeolocationLocationArbitratorTest, Arbitration) { TEST_F(GeolocationLocationArbitratorTest, Arbitration) {
...@@ -355,7 +357,7 @@ TEST_F(GeolocationLocationArbitratorTest, Arbitration) { ...@@ -355,7 +357,7 @@ TEST_F(GeolocationLocationArbitratorTest, Arbitration) {
// Advance time, and notify once again // Advance time, and notify once again
AdvanceTimeNow(SwitchOnFreshnessCliff()); AdvanceTimeNow(SwitchOnFreshnessCliff());
cell()->HandlePositionChanged(cell()->position()); cell()->HandlePositionChanged(cell()->GetPosition());
// New fix is available, less accurate but fresher // New fix is available, less accurate but fresher
CheckLastPositionInfo(5, 6, 150); CheckLastPositionInfo(5, 6, 150);
...@@ -423,8 +425,8 @@ TEST_F(GeolocationLocationArbitratorTest, TwoOneShotsIsNewPositionBetter) { ...@@ -423,8 +425,8 @@ TEST_F(GeolocationLocationArbitratorTest, TwoOneShotsIsNewPositionBetter) {
// To test 240956, perform a throwaway alloc. // To test 240956, perform a throwaway alloc.
// This convinces the allocator to put the providers in a new memory location. // This convinces the allocator to put the providers in a new memory location.
std::unique_ptr<MockLocationProvider> dummy_provider( std::unique_ptr<FakeLocationProvider> dummy_provider(
new MockLocationProvider); new FakeLocationProvider);
arbitrator_->StartProvider(false); arbitrator_->StartProvider(false);
access_token_store_->NotifyDelegateTokensLoaded(); access_token_store_->NotifyDelegateTokensLoaded();
......
// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// This file implements a mock location provider and the factory functions for
// various ways of creating it.
#include "device/geolocation/mock_location_provider.h" #include "device/geolocation/mock_location_provider.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/compiler_specific.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/thread_task_runner_handle.h"
namespace device { namespace device {
MockLocationProvider::MockLocationProvider() MockLocationProvider::MockLocationProvider() {}
: state_(STOPPED),
is_permission_granted_(false),
provider_task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
MockLocationProvider::~MockLocationProvider() {} MockLocationProvider::~MockLocationProvider() {}
void MockLocationProvider::HandlePositionChanged(const Geoposition& position) {
if (provider_task_runner_->BelongsToCurrentThread()) {
// The location arbitrator unit tests rely on this method running
// synchronously.
position_ = position;
NotifyCallback(position_);
} else {
provider_task_runner_->PostTask(
FROM_HERE, base::Bind(&MockLocationProvider::HandlePositionChanged,
base::Unretained(this), position));
}
}
bool MockLocationProvider::StartProvider(bool high_accuracy) {
state_ = high_accuracy ? HIGH_ACCURACY : LOW_ACCURACY;
return true;
}
void MockLocationProvider::StopProvider() {
state_ = STOPPED;
}
const Geoposition& MockLocationProvider::GetPosition() {
return position_;
}
void MockLocationProvider::OnPermissionGranted() {
is_permission_granted_ = true;
}
} // namespace device } // namespace device
// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef DEVICE_GEOLOCATION_MOCK_LOCATION_PROVIDER_H_ #ifndef DEVICE_GEOLOCATION_MOCK_LOCATION_PROVIDER_H_
#define DEVICE_GEOLOCATION_MOCK_LOCATION_PROVIDER_H_ #define DEVICE_GEOLOCATION_MOCK_LOCATION_PROVIDER_H_
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread.h"
#include "device/geolocation/geoposition.h" #include "device/geolocation/geoposition.h"
#include "device/geolocation/location_provider_base.h" #include "device/geolocation/location_provider.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace device { namespace device {
// Mock implementation of a location provider for testing. class MockLocationProvider : public device::LocationProvider {
class MockLocationProvider : public LocationProviderBase {
public: public:
enum State { STOPPED, LOW_ACCURACY, HIGH_ACCURACY } state_;
MockLocationProvider(); MockLocationProvider();
~MockLocationProvider() override; ~MockLocationProvider() override;
bool is_started() const { return state_ != STOPPED; } MOCK_METHOD1(SetUpdateCallback,
bool is_permission_granted() const { return is_permission_granted_; } void(const LocationProviderUpdateCallback& callback));
const Geoposition& position() const { return position_; } MOCK_METHOD1(StartProvider, bool(bool high_accuracy));
MOCK_METHOD0(StopProvider, void());
// Updates listeners with the new position. MOCK_METHOD0(GetPosition, const device::Geoposition&());
void HandlePositionChanged(const Geoposition& position); MOCK_METHOD0(OnPermissionGranted, void());
// LocationProvider implementation.
bool StartProvider(bool high_accuracy) override;
void StopProvider() override;
const Geoposition& GetPosition() override;
void OnPermissionGranted() override;
private: private:
bool is_permission_granted_;
Geoposition position_;
scoped_refptr<base::SingleThreadTaskRunner> provider_task_runner_;
DISALLOW_COPY_AND_ASSIGN(MockLocationProvider); DISALLOW_COPY_AND_ASSIGN(MockLocationProvider);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment