Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(454)

Unified Diff: components/physical_web/data_source/fake_physical_web_data_source.cc

Issue 2529303002: [PhysicalWeb] Add fake data source and helpers for testing. (Closed)
Patch Set: trybot fix. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/physical_web/data_source/fake_physical_web_data_source.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/physical_web/data_source/fake_physical_web_data_source.cc
diff --git a/components/physical_web/data_source/fake_physical_web_data_source.cc b/components/physical_web/data_source/fake_physical_web_data_source.cc
new file mode 100644
index 0000000000000000000000000000000000000000..adc64566cf97f142e07b420a7c28d97a6883472c
--- /dev/null
+++ b/components/physical_web/data_source/fake_physical_web_data_source.cc
@@ -0,0 +1,111 @@
+// 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 "components/physical_web/data_source/fake_physical_web_data_source.h"
+
+#include "base/strings/string_number_conversions.h"
+#include "base/values.h"
+#include "components/physical_web/data_source/physical_web_listener.h"
+
+using base::ListValue;
+using base::DictionaryValue;
+
+namespace physical_web {
+
+std::unique_ptr<DictionaryValue> CreatePhysicalWebPage(
+ const std::string& resolved_url,
+ double distance_estimate,
+ int scan_timestamp,
+ const std::string& title,
+ const std::string& description,
+ const std::string& scanned_url) {
+ auto page = base::MakeUnique<DictionaryValue>();
+ page->SetString(kScannedUrlKey, scanned_url);
+ page->SetDouble(kDistanceEstimateKey, distance_estimate);
+ // TODO(crbug.com/667722): Remove this integer workaround once timestamp is
+ // fixed.
+ page->SetInteger(kScanTimestampKey, scan_timestamp);
+ page->SetString(kResolvedUrlKey, resolved_url);
+ page->SetString(kTitleKey, title);
+ page->SetString(kDescriptionKey, description);
+ return page;
+}
+
+std::unique_ptr<DictionaryValue> CreateDummyPhysicalWebPage(int id,
+ double distance,
+ int timestamp) {
+ const std::string id_string = base::IntToString(id);
+ return CreatePhysicalWebPage("https://resolved_url.com/" + id_string,
+ distance, timestamp, "title " + id_string,
+ "description " + id_string,
+ "https://scanned_url.com/" + id_string);
+}
+
+std::unique_ptr<ListValue> CreateDummyPhysicalWebPages(
+ const std::vector<int>& ids) {
+ int distance = 1;
+ int timestamp = static_cast<int>(ids.size());
+ auto list = base::MakeUnique<ListValue>();
+ for (int id : ids) {
+ list->Append(CreateDummyPhysicalWebPage(id, distance, timestamp));
+ ++distance;
+ --timestamp;
+ }
+ return list;
+}
+
+FakePhysicalWebDataSource::FakePhysicalWebDataSource()
+ : metadata_(base::MakeUnique<ListValue>()) {}
+
+FakePhysicalWebDataSource::~FakePhysicalWebDataSource() = default;
+
+void FakePhysicalWebDataSource::StartDiscovery(bool network_request_enabled) {
+ // Ignored.
+}
+
+void FakePhysicalWebDataSource::StopDiscovery() {
+ // Ignored.
+}
+
+std::unique_ptr<base::ListValue> FakePhysicalWebDataSource::GetMetadata() {
+ return metadata_->CreateDeepCopy();
+}
+
+bool FakePhysicalWebDataSource::HasUnresolvedDiscoveries() {
+ return false;
+}
+
+void FakePhysicalWebDataSource::RegisterListener(
+ PhysicalWebListener* physical_web_listener) {
+ observer_list_.AddObserver(physical_web_listener);
+}
+
+void FakePhysicalWebDataSource::UnregisterListener(
+ PhysicalWebListener* physical_web_listener) {
+ observer_list_.RemoveObserver(physical_web_listener);
+}
+
+void FakePhysicalWebDataSource::SetMetadata(
+ std::unique_ptr<ListValue> metadata) {
+ metadata_ = std::move(metadata);
+}
+
+void FakePhysicalWebDataSource::NotifyOnFound(const std::string& url) {
+ for (PhysicalWebListener& observer : observer_list_)
+ observer.OnFound(url);
+}
+
+void FakePhysicalWebDataSource::NotifyOnLost(const std::string& url) {
+ for (PhysicalWebListener& observer : observer_list_)
+ observer.OnLost(url);
+}
+
+void FakePhysicalWebDataSource::NotifyOnDistanceChanged(
+ const std::string& url,
+ double distance_estimate) {
+ for (PhysicalWebListener& observer : observer_list_)
+ observer.OnDistanceChanged(url, distance_estimate);
+}
+
+} // namespace physical_web
« no previous file with comments | « components/physical_web/data_source/fake_physical_web_data_source.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698