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

Side by Side 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 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/physical_web/data_source/fake_physical_web_data_source.h"
6
7 #include "base/strings/string_number_conversions.h"
8 #include "base/values.h"
9 #include "components/physical_web/data_source/physical_web_listener.h"
10
11 using base::ListValue;
12 using base::DictionaryValue;
13
14 namespace physical_web {
15
16 std::unique_ptr<DictionaryValue> CreatePhysicalWebPage(
17 const std::string& resolved_url,
18 double distance_estimate,
19 int scan_timestamp,
20 const std::string& title,
21 const std::string& description,
22 const std::string& scanned_url) {
23 auto page = base::MakeUnique<DictionaryValue>();
24 page->SetString(kScannedUrlKey, scanned_url);
25 page->SetDouble(kDistanceEstimateKey, distance_estimate);
26 // TODO(crbug.com/667722): Remove this integer workaround once timestamp is
27 // fixed.
28 page->SetInteger(kScanTimestampKey, scan_timestamp);
29 page->SetString(kResolvedUrlKey, resolved_url);
30 page->SetString(kTitleKey, title);
31 page->SetString(kDescriptionKey, description);
32 return page;
33 }
34
35 std::unique_ptr<DictionaryValue> CreateDummyPhysicalWebPage(int id,
36 double distance,
37 int timestamp) {
38 const std::string id_string = base::IntToString(id);
39 return CreatePhysicalWebPage("https://resolved_url.com/" + id_string,
40 distance, timestamp, "title " + id_string,
41 "description " + id_string,
42 "https://scanned_url.com/" + id_string);
43 }
44
45 std::unique_ptr<ListValue> CreateDummyPhysicalWebPages(
46 const std::vector<int>& ids) {
47 int distance = 1;
48 int timestamp = static_cast<int>(ids.size());
49 auto list = base::MakeUnique<ListValue>();
50 for (int id : ids) {
51 list->Append(CreateDummyPhysicalWebPage(id, distance, timestamp));
52 ++distance;
53 --timestamp;
54 }
55 return list;
56 }
57
58 FakePhysicalWebDataSource::FakePhysicalWebDataSource()
59 : metadata_(base::MakeUnique<ListValue>()) {}
60
61 FakePhysicalWebDataSource::~FakePhysicalWebDataSource() = default;
62
63 void FakePhysicalWebDataSource::StartDiscovery(bool network_request_enabled) {
64 // Ignored.
65 }
66
67 void FakePhysicalWebDataSource::StopDiscovery() {
68 // Ignored.
69 }
70
71 std::unique_ptr<base::ListValue> FakePhysicalWebDataSource::GetMetadata() {
72 return metadata_->CreateDeepCopy();
73 }
74
75 bool FakePhysicalWebDataSource::HasUnresolvedDiscoveries() {
76 return false;
77 }
78
79 void FakePhysicalWebDataSource::RegisterListener(
80 PhysicalWebListener* physical_web_listener) {
81 observer_list_.AddObserver(physical_web_listener);
82 }
83
84 void FakePhysicalWebDataSource::UnregisterListener(
85 PhysicalWebListener* physical_web_listener) {
86 observer_list_.RemoveObserver(physical_web_listener);
87 }
88
89 void FakePhysicalWebDataSource::SetMetadata(
90 std::unique_ptr<ListValue> metadata) {
91 metadata_ = std::move(metadata);
92 }
93
94 void FakePhysicalWebDataSource::NotifyOnFound(const std::string& url) {
95 for (PhysicalWebListener& observer : observer_list_)
96 observer.OnFound(url);
97 }
98
99 void FakePhysicalWebDataSource::NotifyOnLost(const std::string& url) {
100 for (PhysicalWebListener& observer : observer_list_)
101 observer.OnLost(url);
102 }
103
104 void FakePhysicalWebDataSource::NotifyOnDistanceChanged(
105 const std::string& url,
106 double distance_estimate) {
107 for (PhysicalWebListener& observer : observer_list_)
108 observer.OnDistanceChanged(url, distance_estimate);
109 }
110
111 } // namespace physical_web
OLDNEW
« 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