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

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: Address mattreynolds comments Created 4 years, 2 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 // Test Values ----------------------------------------------------------------
14 std::string url = "https://www.google.com";
mattreynolds 2016/09/21 22:01:34 Declare this as a char array, make it const, and r
hayesjordan 2016/09/21 22:25:07 Done.
15
16 // TestPhysicalWebDataSource --------------------------------------------------
17
18 class TestPhysicalWebDataSource : public PhysicalWebDataSourceImpl {
19 public:
20 TestPhysicalWebDataSource() {}
21 ~TestPhysicalWebDataSource() override {}
22
23 void StartDiscovery(bool network_request_enabled) override;
24 void StopDiscovery() override;
25 std::unique_ptr<base::ListValue> GetMetadata() override;
26 bool HasUnresolvedDiscoveries() override;
27 };
28 void TestPhysicalWebDataSource::StartDiscovery(bool network_request_enabled) {}
29
30 void TestPhysicalWebDataSource::StopDiscovery() {}
31
32 std::unique_ptr<base::ListValue> TestPhysicalWebDataSource::GetMetadata() {
33 return NULL;
34 }
35
36 bool TestPhysicalWebDataSource::HasUnresolvedDiscoveries() {
37 return false;
38 }
39
40 // TestPhysicalWebListener ----------------------------------------------------
41
42 class TestPhysicalWebListener : public PhysicalWebListener {
43 public:
44 TestPhysicalWebListener() {
45 on_found_notified_ = false;
46 on_lost_notified_ = false;
47 on_distance_changed_notified_ = false;
48 last_event_url_.clear();
mattreynolds 2016/09/21 22:01:34 You can use member initialization here TestPhysic
hayesjordan 2016/09/21 22:25:07 Done.
49 }
50 ~TestPhysicalWebListener() {}
51
52 void OnFound(const std::string& url) override {
53 on_found_notified_ = true;
54 last_event_url_ = url;
55 }
56
57 void OnLost(const std::string& url) override {
58 on_lost_notified_ = true;
59 last_event_url_ = url;
60 }
61
62 void OnDistanceChanged(const std::string& url,
63 double distance_estimate) override {
64 on_distance_changed_notified_ = true;
65 last_event_url_ = url;
66 }
67
68 bool OnFoundNotified() { return on_found_notified_; }
69
70 bool OnLostNotified() { return on_lost_notified_; }
71
72 bool OnDistanceChangedNotified() { return on_distance_changed_notified_; }
73
74 std::string LastEventUrl() { return last_event_url_; }
75
76 private:
77 bool on_found_notified_;
78 bool on_lost_notified_;
79 bool on_distance_changed_notified_;
80 std::string last_event_url_;
81 };
82
83 // PhysicalWebDataSourceImplTest ----------------------------------------------
84
85 class PhysicalWebDataSourceImplTest : public ::testing::Test {
86 public:
87 PhysicalWebDataSourceImplTest() {}
88 ~PhysicalWebDataSourceImplTest() override {}
89
90 // testing::Test
91 void SetUp() override;
92 void TearDown() override;
93
94 protected:
95 TestPhysicalWebDataSource data_source_;
96 TestPhysicalWebListener listener_;
97 };
98
99 void PhysicalWebDataSourceImplTest::SetUp() {
100 data_source_.RegisterListener(&listener_);
101 }
102
103 void PhysicalWebDataSourceImplTest::TearDown() {
104 data_source_.UnregisterListener(&listener_);
105 }
106
107 // Tests ----------------------------------------------------------------------
108
109 TEST_F(PhysicalWebDataSourceImplTest, OnFound) {
110 data_source_.NotifyOnFound(url);
111 EXPECT_TRUE(listener_.OnFoundNotified());
112 EXPECT_FALSE(listener_.OnLostNotified());
113 EXPECT_FALSE(listener_.OnDistanceChangedNotified());
114 EXPECT_EQ(url, listener_.LastEventUrl());
115 }
116
117 TEST_F(PhysicalWebDataSourceImplTest, OnLost) {
118 data_source_.NotifyOnLost(url);
119 EXPECT_FALSE(listener_.OnFoundNotified());
120 EXPECT_TRUE(listener_.OnLostNotified());
121 EXPECT_FALSE(listener_.OnDistanceChangedNotified());
122 EXPECT_EQ(url, listener_.LastEventUrl());
123 }
124
125 TEST_F(PhysicalWebDataSourceImplTest, OnDistanceChanged) {
126 data_source_.NotifyOnDistanceChanged(url, 0.0);
127 EXPECT_FALSE(listener_.OnFoundNotified());
128 EXPECT_FALSE(listener_.OnLostNotified());
129 EXPECT_TRUE(listener_.OnDistanceChangedNotified());
130 EXPECT_EQ(url, listener_.LastEventUrl());
131 }
132
133 TEST_F(PhysicalWebDataSourceImplTest, OnFoundNotRegistered) {
134 data_source_.UnregisterListener(&listener_);
135 data_source_.NotifyOnFound(url);
136 EXPECT_FALSE(listener_.OnFoundNotified());
137 EXPECT_FALSE(listener_.OnLostNotified());
138 EXPECT_FALSE(listener_.OnDistanceChangedNotified());
139 EXPECT_TRUE(listener_.LastEventUrl().empty());
140 }
141
142 TEST_F(PhysicalWebDataSourceImplTest, OnLostNotRegistered) {
143 data_source_.UnregisterListener(&listener_);
144 data_source_.NotifyOnLost(url);
145 EXPECT_FALSE(listener_.OnFoundNotified());
146 EXPECT_FALSE(listener_.OnLostNotified());
147 EXPECT_FALSE(listener_.OnDistanceChangedNotified());
148 EXPECT_TRUE(listener_.LastEventUrl().empty());
149 }
150
151 TEST_F(PhysicalWebDataSourceImplTest, OnDistanceChangedNotRegistered) {
152 data_source_.UnregisterListener(&listener_);
153 data_source_.NotifyOnDistanceChanged(url, 0.0);
154 EXPECT_FALSE(listener_.OnFoundNotified());
155 EXPECT_FALSE(listener_.OnLostNotified());
156 EXPECT_FALSE(listener_.OnDistanceChangedNotified());
157 EXPECT_TRUE(listener_.LastEventUrl().empty());
158 }
159 } // namespace physical_web
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698