OLD | NEW |
(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 const char kUrl[] = "https://www.google.com"; |
| 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 |
| 49 ~TestPhysicalWebListener() {} |
| 50 |
| 51 void OnFound(const std::string& url) override { |
| 52 on_found_notified_ = true; |
| 53 last_event_url_ = url; |
| 54 } |
| 55 |
| 56 void OnLost(const std::string& url) override { |
| 57 on_lost_notified_ = true; |
| 58 last_event_url_ = url; |
| 59 } |
| 60 |
| 61 void OnDistanceChanged(const std::string& url, |
| 62 double distance_estimate) override { |
| 63 on_distance_changed_notified_ = true; |
| 64 last_event_url_ = url; |
| 65 } |
| 66 |
| 67 bool OnFoundNotified() { return on_found_notified_; } |
| 68 |
| 69 bool OnLostNotified() { return on_lost_notified_; } |
| 70 |
| 71 bool OnDistanceChangedNotified() { return on_distance_changed_notified_; } |
| 72 |
| 73 std::string LastEventUrl() { return last_event_url_; } |
| 74 |
| 75 private: |
| 76 bool on_found_notified_; |
| 77 bool on_lost_notified_; |
| 78 bool on_distance_changed_notified_; |
| 79 std::string last_event_url_; |
| 80 }; |
| 81 |
| 82 // PhysicalWebDataSourceImplTest ---------------------------------------------- |
| 83 |
| 84 class PhysicalWebDataSourceImplTest : public ::testing::Test { |
| 85 public: |
| 86 PhysicalWebDataSourceImplTest() {} |
| 87 ~PhysicalWebDataSourceImplTest() override {} |
| 88 |
| 89 // testing::Test |
| 90 void SetUp() override; |
| 91 void TearDown() override; |
| 92 |
| 93 protected: |
| 94 TestPhysicalWebDataSource data_source_; |
| 95 TestPhysicalWebListener listener_; |
| 96 }; |
| 97 |
| 98 void PhysicalWebDataSourceImplTest::SetUp() { |
| 99 data_source_.RegisterListener(&listener_); |
| 100 } |
| 101 |
| 102 void PhysicalWebDataSourceImplTest::TearDown() { |
| 103 data_source_.UnregisterListener(&listener_); |
| 104 } |
| 105 |
| 106 // Tests ---------------------------------------------------------------------- |
| 107 |
| 108 TEST_F(PhysicalWebDataSourceImplTest, OnFound) { |
| 109 data_source_.NotifyOnFound(kUrl); |
| 110 EXPECT_TRUE(listener_.OnFoundNotified()); |
| 111 EXPECT_FALSE(listener_.OnLostNotified()); |
| 112 EXPECT_FALSE(listener_.OnDistanceChangedNotified()); |
| 113 EXPECT_EQ(kUrl, listener_.LastEventUrl()); |
| 114 } |
| 115 |
| 116 TEST_F(PhysicalWebDataSourceImplTest, OnLost) { |
| 117 data_source_.NotifyOnLost(kUrl); |
| 118 EXPECT_FALSE(listener_.OnFoundNotified()); |
| 119 EXPECT_TRUE(listener_.OnLostNotified()); |
| 120 EXPECT_FALSE(listener_.OnDistanceChangedNotified()); |
| 121 EXPECT_EQ(kUrl, listener_.LastEventUrl()); |
| 122 } |
| 123 |
| 124 TEST_F(PhysicalWebDataSourceImplTest, OnDistanceChanged) { |
| 125 data_source_.NotifyOnDistanceChanged(kUrl, 0.0); |
| 126 EXPECT_FALSE(listener_.OnFoundNotified()); |
| 127 EXPECT_FALSE(listener_.OnLostNotified()); |
| 128 EXPECT_TRUE(listener_.OnDistanceChangedNotified()); |
| 129 EXPECT_EQ(kUrl, listener_.LastEventUrl()); |
| 130 } |
| 131 |
| 132 TEST_F(PhysicalWebDataSourceImplTest, OnFoundNotRegistered) { |
| 133 data_source_.UnregisterListener(&listener_); |
| 134 data_source_.NotifyOnFound(kUrl); |
| 135 EXPECT_FALSE(listener_.OnFoundNotified()); |
| 136 EXPECT_FALSE(listener_.OnLostNotified()); |
| 137 EXPECT_FALSE(listener_.OnDistanceChangedNotified()); |
| 138 EXPECT_TRUE(listener_.LastEventUrl().empty()); |
| 139 } |
| 140 |
| 141 TEST_F(PhysicalWebDataSourceImplTest, OnLostNotRegistered) { |
| 142 data_source_.UnregisterListener(&listener_); |
| 143 data_source_.NotifyOnLost(kUrl); |
| 144 EXPECT_FALSE(listener_.OnFoundNotified()); |
| 145 EXPECT_FALSE(listener_.OnLostNotified()); |
| 146 EXPECT_FALSE(listener_.OnDistanceChangedNotified()); |
| 147 EXPECT_TRUE(listener_.LastEventUrl().empty()); |
| 148 } |
| 149 |
| 150 TEST_F(PhysicalWebDataSourceImplTest, OnDistanceChangedNotRegistered) { |
| 151 data_source_.UnregisterListener(&listener_); |
| 152 data_source_.NotifyOnDistanceChanged(kUrl, 0.0); |
| 153 EXPECT_FALSE(listener_.OnFoundNotified()); |
| 154 EXPECT_FALSE(listener_.OnLostNotified()); |
| 155 EXPECT_FALSE(listener_.OnDistanceChangedNotified()); |
| 156 EXPECT_TRUE(listener_.LastEventUrl().empty()); |
| 157 } |
| 158 } // namespace physical_web |
OLD | NEW |