Commit 7b30c99f authored by hayesjordan's avatar hayesjordan Committed by Commit bot

Implement Physical Web listener registration

Implement the RegisterListener, UnregisterListener, and notify methods
in an abstract class so that all Physical Web data sources can have an
the ability to push data to subscribers.

BUG=636490

Review-Url: https://codereview.chromium.org/2349483002
Cr-Commit-Position: refs/heads/master@{#420682}
parent 6a823415
...@@ -109,6 +109,7 @@ test("components_unittests") { ...@@ -109,6 +109,7 @@ test("components_unittests") {
"//components/password_manager/core/browser:unit_tests", "//components/password_manager/core/browser:unit_tests",
"//components/password_manager/core/common:unit_tests", "//components/password_manager/core/common:unit_tests",
"//components/password_manager/sync/browser:unit_tests", "//components/password_manager/sync/browser:unit_tests",
"//components/physical_web/data_source:unit_tests",
"//components/precache/core:unit_tests", "//components/precache/core:unit_tests",
"//components/prefs:unit_tests", "//components/prefs:unit_tests",
"//components/previews/core:unit_tests", "//components/previews/core:unit_tests",
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
source_set("data_source") { source_set("data_source") {
sources = [ sources = [
"physical_web_data_source.h", "physical_web_data_source.h",
"physical_web_data_source_impl.cc",
"physical_web_data_source_impl.h",
"physical_web_listener.h", "physical_web_listener.h",
] ]
...@@ -12,3 +14,16 @@ source_set("data_source") { ...@@ -12,3 +14,16 @@ source_set("data_source") {
"//base", "//base",
] ]
} }
source_set("unit_tests") {
testonly = true
sources = [
"physical_web_data_source_impl_unittest.cc",
]
deps = [
":data_source",
"//base",
"//testing/gtest",
]
}
// 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 "base/observer_list.h"
#include "components/physical_web/data_source/physical_web_data_source.h"
#include "components/physical_web/data_source/physical_web_data_source_impl.h"
#include "components/physical_web/data_source/physical_web_listener.h"
PhysicalWebDataSourceImpl::PhysicalWebDataSourceImpl() {}
PhysicalWebDataSourceImpl::~PhysicalWebDataSourceImpl() {}
void PhysicalWebDataSourceImpl::RegisterListener(
PhysicalWebListener* physical_web_listener) {
observer_list_.AddObserver(physical_web_listener);
}
void PhysicalWebDataSourceImpl::UnregisterListener(
PhysicalWebListener* physical_web_listener) {
observer_list_.RemoveObserver(physical_web_listener);
}
void PhysicalWebDataSourceImpl::NotifyOnFound(const std::string& url) {
FOR_EACH_OBSERVER(PhysicalWebListener, observer_list_, OnFound(url));
}
void PhysicalWebDataSourceImpl::NotifyOnLost(const std::string& url) {
FOR_EACH_OBSERVER(PhysicalWebListener, observer_list_, OnLost(url));
}
void PhysicalWebDataSourceImpl::NotifyOnDistanceChanged(
const std::string& url,
double distance_estimate) {
FOR_EACH_OBSERVER(PhysicalWebListener, observer_list_,
OnDistanceChanged(url, distance_estimate));
}
// 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 COMPONENTS_PHYSICAL_WEB_DATA_SOURCE_PHYSICAL_WEB_DATA_SOURCE_IMPL_H_
#define COMPONENTS_PHYSICAL_WEB_DATA_SOURCE_PHYSICAL_WEB_DATA_SOURCE_IMPL_H_
#include "base/observer_list.h"
#include "components/physical_web/data_source/physical_web_data_source.h"
class PhysicalWebListener;
class PhysicalWebDataSourceImpl : public PhysicalWebDataSource {
public:
PhysicalWebDataSourceImpl();
~PhysicalWebDataSourceImpl() override;
// Register for changes to Physical Web URLs and associated page metadata.
void RegisterListener(PhysicalWebListener* physical_web_listener) override;
// Unregister for changes to Physical Web URLs and associated page metadata.
void UnregisterListener(PhysicalWebListener* physical_web_listener) override;
// Notify all registered listeners that a URL has been found.
void NotifyOnFound(const std::string& url);
// Notify all registered listeners that a URL has been lost.
void NotifyOnLost(const std::string& url);
// Notify all registered listeners that a distance has changed for a URL.
void NotifyOnDistanceChanged(const std::string& url,
double distance_estimate);
private:
base::ObserverList<PhysicalWebListener> observer_list_;
};
#endif // COMPONENTS_PHYSICAL_WEB_DATA_SOURCE_PHYSICAL_WEB_DATA_SOURCE_IMPL_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 "base/values.h"
#include "components/physical_web/data_source/physical_web_data_source_impl.h"
#include "components/physical_web/data_source/physical_web_listener.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace physical_web {
// Test Values ----------------------------------------------------------------
const char kUrl[] = "https://www.google.com";
// TestPhysicalWebDataSource --------------------------------------------------
class TestPhysicalWebDataSource : public PhysicalWebDataSourceImpl {
public:
TestPhysicalWebDataSource() {}
~TestPhysicalWebDataSource() override {}
void StartDiscovery(bool network_request_enabled) override;
void StopDiscovery() override;
std::unique_ptr<base::ListValue> GetMetadata() override;
bool HasUnresolvedDiscoveries() override;
};
void TestPhysicalWebDataSource::StartDiscovery(bool network_request_enabled) {}
void TestPhysicalWebDataSource::StopDiscovery() {}
std::unique_ptr<base::ListValue> TestPhysicalWebDataSource::GetMetadata() {
return NULL;
}
bool TestPhysicalWebDataSource::HasUnresolvedDiscoveries() {
return false;
}
// TestPhysicalWebListener ----------------------------------------------------
class TestPhysicalWebListener : public PhysicalWebListener {
public:
TestPhysicalWebListener()
: on_found_notified_(false),
on_lost_notified_(false),
on_distance_changed_notified_(false) {}
~TestPhysicalWebListener() {}
void OnFound(const std::string& url) override {
on_found_notified_ = true;
last_event_url_ = url;
}
void OnLost(const std::string& url) override {
on_lost_notified_ = true;
last_event_url_ = url;
}
void OnDistanceChanged(const std::string& url,
double distance_estimate) override {
on_distance_changed_notified_ = true;
last_event_url_ = url;
}
bool OnFoundNotified() { return on_found_notified_; }
bool OnLostNotified() { return on_lost_notified_; }
bool OnDistanceChangedNotified() { return on_distance_changed_notified_; }
std::string LastEventUrl() { return last_event_url_; }
private:
bool on_found_notified_;
bool on_lost_notified_;
bool on_distance_changed_notified_;
std::string last_event_url_;
};
// PhysicalWebDataSourceImplTest ----------------------------------------------
class PhysicalWebDataSourceImplTest : public ::testing::Test {
public:
PhysicalWebDataSourceImplTest() {}
~PhysicalWebDataSourceImplTest() override {}
// testing::Test
void SetUp() override;
void TearDown() override;
protected:
TestPhysicalWebDataSource data_source_;
TestPhysicalWebListener listener_;
};
void PhysicalWebDataSourceImplTest::SetUp() {
data_source_.RegisterListener(&listener_);
}
void PhysicalWebDataSourceImplTest::TearDown() {
data_source_.UnregisterListener(&listener_);
}
// Tests ----------------------------------------------------------------------
TEST_F(PhysicalWebDataSourceImplTest, OnFound) {
data_source_.NotifyOnFound(kUrl);
EXPECT_TRUE(listener_.OnFoundNotified());
EXPECT_FALSE(listener_.OnLostNotified());
EXPECT_FALSE(listener_.OnDistanceChangedNotified());
EXPECT_EQ(kUrl, listener_.LastEventUrl());
}
TEST_F(PhysicalWebDataSourceImplTest, OnLost) {
data_source_.NotifyOnLost(kUrl);
EXPECT_FALSE(listener_.OnFoundNotified());
EXPECT_TRUE(listener_.OnLostNotified());
EXPECT_FALSE(listener_.OnDistanceChangedNotified());
EXPECT_EQ(kUrl, listener_.LastEventUrl());
}
TEST_F(PhysicalWebDataSourceImplTest, OnDistanceChanged) {
data_source_.NotifyOnDistanceChanged(kUrl, 0.0);
EXPECT_FALSE(listener_.OnFoundNotified());
EXPECT_FALSE(listener_.OnLostNotified());
EXPECT_TRUE(listener_.OnDistanceChangedNotified());
EXPECT_EQ(kUrl, listener_.LastEventUrl());
}
TEST_F(PhysicalWebDataSourceImplTest, OnFoundNotRegistered) {
data_source_.UnregisterListener(&listener_);
data_source_.NotifyOnFound(kUrl);
EXPECT_FALSE(listener_.OnFoundNotified());
EXPECT_FALSE(listener_.OnLostNotified());
EXPECT_FALSE(listener_.OnDistanceChangedNotified());
EXPECT_TRUE(listener_.LastEventUrl().empty());
}
TEST_F(PhysicalWebDataSourceImplTest, OnLostNotRegistered) {
data_source_.UnregisterListener(&listener_);
data_source_.NotifyOnLost(kUrl);
EXPECT_FALSE(listener_.OnFoundNotified());
EXPECT_FALSE(listener_.OnLostNotified());
EXPECT_FALSE(listener_.OnDistanceChangedNotified());
EXPECT_TRUE(listener_.LastEventUrl().empty());
}
TEST_F(PhysicalWebDataSourceImplTest, OnDistanceChangedNotRegistered) {
data_source_.UnregisterListener(&listener_);
data_source_.NotifyOnDistanceChanged(kUrl, 0.0);
EXPECT_FALSE(listener_.OnFoundNotified());
EXPECT_FALSE(listener_.OnLostNotified());
EXPECT_FALSE(listener_.OnDistanceChangedNotified());
EXPECT_TRUE(listener_.LastEventUrl().empty());
}
} // namespace physical_web
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
#include "base/macros.h" #include "base/macros.h"
#import "base/mac/scoped_nsobject.h" #import "base/mac/scoped_nsobject.h"
#include "components/physical_web/data_source/physical_web_data_source.h" #include "components/physical_web/data_source/physical_web_data_source_impl.h"
namespace base { namespace base {
class ListValue; class ListValue;
...@@ -17,7 +17,7 @@ class PhysicalWebListener; ...@@ -17,7 +17,7 @@ class PhysicalWebListener;
@class PhysicalWebScanner; @class PhysicalWebScanner;
// iOS implementation of PhysicalWebDataSource // iOS implementation of PhysicalWebDataSource
class IOSChromePhysicalWebDataSource : public PhysicalWebDataSource { class IOSChromePhysicalWebDataSource : public PhysicalWebDataSourceImpl {
public: public:
IOSChromePhysicalWebDataSource(); IOSChromePhysicalWebDataSource();
~IOSChromePhysicalWebDataSource() override; ~IOSChromePhysicalWebDataSource() override;
...@@ -37,12 +37,6 @@ class IOSChromePhysicalWebDataSource : public PhysicalWebDataSource { ...@@ -37,12 +37,6 @@ class IOSChromePhysicalWebDataSource : public PhysicalWebDataSource {
// or more discovered URLs that have not been sent to the resolution service. // or more discovered URLs that have not been sent to the resolution service.
bool HasUnresolvedDiscoveries() override; bool HasUnresolvedDiscoveries() override;
// Register for changes to Physical Web URLs and associated page metadata.
void RegisterListener(PhysicalWebListener* physical_web_listener) override;
// Unregister for changes to Physical Web URLs and associated page metadata.
void UnregisterListener(PhysicalWebListener* physical_web_listener) override;
private: private:
// Scanner for nearby Physical Web URL devices. // Scanner for nearby Physical Web URL devices.
base::scoped_nsobject<PhysicalWebScanner> scanner_; base::scoped_nsobject<PhysicalWebScanner> scanner_;
......
...@@ -45,9 +45,3 @@ std::unique_ptr<base::ListValue> IOSChromePhysicalWebDataSource::GetMetadata() { ...@@ -45,9 +45,3 @@ std::unique_ptr<base::ListValue> IOSChromePhysicalWebDataSource::GetMetadata() {
bool IOSChromePhysicalWebDataSource::HasUnresolvedDiscoveries() { bool IOSChromePhysicalWebDataSource::HasUnresolvedDiscoveries() {
return [scanner_ unresolvedBeaconsCount] > 0; return [scanner_ unresolvedBeaconsCount] > 0;
} }
void IOSChromePhysicalWebDataSource::RegisterListener(
PhysicalWebListener* physical_web_listener) {}
void IOSChromePhysicalWebDataSource::UnregisterListener(
PhysicalWebListener* physical_web_listener) {}
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