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

Side by Side Diff: components/physical_web/data_source/physical_web_data_source_impl_unittest.cc

Issue 2349483002: Implement Physical Web listener registration (Closed)
Patch Set: Add unit tests Created 4 years, 3 months 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
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 "base/values.h"
6 #include "components/physical_web/data_source/physical_web_data_source_impl.h"
7 #include "components/physical_web/data_source/physical_web_listener.h"
8
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace physical_web {
12
13 // TestPhysicalWebDataSource --------------------------------------------------
14
15 class TestPhysicalWebDataSource : public PhysicalWebDataSourceImpl {
16 public:
17 TestPhysicalWebDataSource() {}
18 ~TestPhysicalWebDataSource() override {}
19
20 void StartDiscovery(bool network_request_enabled) override;
21 void StopDiscovery() override;
22 std::unique_ptr<base::ListValue> GetMetadata() override;
23 bool HasUnresolvedDiscoveries() override;
24 };
25 void TestPhysicalWebDataSource::StartDiscovery(bool network_request_enabled) {}
26
27 void TestPhysicalWebDataSource::StopDiscovery() {}
28
29 std::unique_ptr<base::ListValue> TestPhysicalWebDataSource::GetMetadata() {
30 return NULL;
31 }
32
33 bool TestPhysicalWebDataSource::HasUnresolvedDiscoveries() {
34 return false;
35 }
36
37 // TestPhysicalWebListener ----------------------------------------------------
38
39 class TestPhysicalWebListener : public PhysicalWebListener {
40 public:
41 TestPhysicalWebListener() {}
42 ~TestPhysicalWebListener() {}
43
44 void OnFound(const std::string& url) override;
45
46 void OnLost(const std::string& url) override;
47
48 void OnDistanceChanged(const std::string& url,
49 double distance_estimate) override;
50
51 bool OnFoundNotified() { return on_found_notified_; }
52
53 bool OnLostNotified() { return on_lost_notified_; }
54
55 bool OnDistanceChangedNotified() { return on_distance_changed_notified_; }
56
57 std::string LastEventUrl() { return last_event_url_; }
58
59 void ClearNotified();
60
61 private:
62 bool on_found_notified_;
63 bool on_lost_notified_;
64 bool on_distance_changed_notified_;
65 std::string last_event_url_;
66 };
67
68 void TestPhysicalWebListener::OnFound(const std::string& url) {
mattreynolds 2016/09/20 22:49:33 These methods are pretty short, I think it makes s
hayesjordan 2016/09/21 20:47:44 Done.
69 on_found_notified_ = true;
70 last_event_url_ = url;
71 }
72
73 void TestPhysicalWebListener::OnLost(const std::string& url) {
74 on_lost_notified_ = true;
75 last_event_url_ = url;
76 }
77
78 void TestPhysicalWebListener::OnDistanceChanged(const std::string& url,
79 double distance_estimate) {
80 on_distance_changed_notified_ = true;
81 last_event_url_ = url;
82 }
83
84 void TestPhysicalWebListener::ClearNotified() {
mattreynolds 2016/09/20 22:49:33 Why not initialize these in the TestPhysicalWebLis
hayesjordan 2016/09/21 20:47:44 Done.
85 on_found_notified_ = false;
86 on_lost_notified_ = false;
87 on_distance_changed_notified_ = false;
88 last_event_url_ = "";
mattreynolds 2016/09/20 22:49:34 last_event_url_.clear();
hayesjordan 2016/09/21 20:47:44 Done.
89 }
90
91 // PhysicalWebDataSourceImplTest ----------------------------------------------
92
93 class PhysicalWebDataSourceImplTest : public ::testing::Test {
94 public:
95 PhysicalWebDataSourceImplTest() {}
96 ~PhysicalWebDataSourceImplTest() override {}
97
98 // testing::Test
99 void SetUp() override;
100 void TearDown() override;
101
102 protected:
103 TestPhysicalWebDataSource data_source_;
104 TestPhysicalWebListener listener_;
105 };
106
107 void PhysicalWebDataSourceImplTest::SetUp() {
108 listener_.ClearNotified();
109 data_source_.RegisterListener(&listener_);
110 }
111
112 void PhysicalWebDataSourceImplTest::TearDown() {
113 data_source_.UnregisterListener(&listener_);
114 }
115
116 // Tests ----------------------------------------------------------------------
117
118 TEST_F(PhysicalWebDataSourceImplTest, OnFound) {
119 std::string url = "https://www.google.com";
mattreynolds 2016/09/20 22:49:33 Let's pull out this constant and share it between
hayesjordan 2016/09/21 20:47:44 Done.
120 data_source_.NotifyOnFound(url);
121 EXPECT_TRUE(listener_.OnFoundNotified());
122 EXPECT_FALSE(listener_.OnLostNotified());
123 EXPECT_FALSE(listener_.OnDistanceChangedNotified());
124 EXPECT_EQ(url, listener_.LastEventUrl());
125 }
126
127 TEST_F(PhysicalWebDataSourceImplTest, OnLost) {
128 std::string url = "https://www.google.com";
129 data_source_.NotifyOnLost(url);
130 EXPECT_FALSE(listener_.OnFoundNotified());
131 EXPECT_TRUE(listener_.OnLostNotified());
132 EXPECT_FALSE(listener_.OnDistanceChangedNotified());
133 EXPECT_EQ(url, listener_.LastEventUrl());
134 }
135
136 TEST_F(PhysicalWebDataSourceImplTest, OnDistanceChanged) {
137 std::string url = "https://www.google.com";
138 data_source_.NotifyOnDistanceChanged(url, 0.0);
139 EXPECT_FALSE(listener_.OnFoundNotified());
140 EXPECT_FALSE(listener_.OnLostNotified());
141 EXPECT_TRUE(listener_.OnDistanceChangedNotified());
142 EXPECT_EQ(url, listener_.LastEventUrl());
143 }
144
145 TEST_F(PhysicalWebDataSourceImplTest, OnFoundNotRegistered) {
146 std::string url = "https://www.google.com";
147 data_source_.UnregisterListener(&listener_);
148 data_source_.NotifyOnFound(url);
149 EXPECT_FALSE(listener_.OnFoundNotified());
150 EXPECT_FALSE(listener_.OnLostNotified());
151 EXPECT_FALSE(listener_.OnDistanceChangedNotified());
152 EXPECT_EQ("", listener_.LastEventUrl());
mattreynolds 2016/09/20 22:49:33 Consider changing to EXPECT_TRUE(listener_.LastEve
hayesjordan 2016/09/21 20:47:44 Done.
153 }
154
155 TEST_F(PhysicalWebDataSourceImplTest, OnLostNotRegistered) {
156 std::string url = "https://www.google.com";
157 data_source_.UnregisterListener(&listener_);
158 data_source_.NotifyOnLost(url);
159 EXPECT_FALSE(listener_.OnFoundNotified());
160 EXPECT_FALSE(listener_.OnLostNotified());
161 EXPECT_FALSE(listener_.OnDistanceChangedNotified());
162 EXPECT_EQ("", listener_.LastEventUrl());
163 }
164
165 TEST_F(PhysicalWebDataSourceImplTest, OnDistanceChangedNotregistered) {
mattreynolds 2016/09/20 22:49:33 OnDistanceChangedNotregistered -> OnDistanceChange
hayesjordan 2016/09/21 20:47:44 Done.
166 std::string url = "https://www.google.com";
167 data_source_.UnregisterListener(&listener_);
168 data_source_.NotifyOnDistanceChanged(url, 0.0);
169 EXPECT_FALSE(listener_.OnFoundNotified());
170 EXPECT_FALSE(listener_.OnLostNotified());
171 EXPECT_FALSE(listener_.OnDistanceChangedNotified());
172 EXPECT_EQ("", listener_.LastEventUrl());
173 }
174 }
mattreynolds 2016/09/20 22:49:33 Add a comment for the end of the namespace, eg: }
hayesjordan 2016/09/21 20:47:44 Done.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698