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

Side by Side Diff: chrome/browser/chromeos/night_light/night_light_client_unittest.cc

Issue 2966873002: [Night Light] CL12: String changes and fix frequent requests. (Closed)
Patch Set: James comments Created 3 years, 5 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
« no previous file with comments | « chrome/browser/chromeos/night_light/night_light_client.cc ('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
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/night_light/night_light_client.h" 5 #include "chrome/browser/chromeos/night_light/night_light_client.h"
6 6
7 #include "ash/public/interfaces/night_light_controller.mojom.h" 7 #include "ash/public/interfaces/night_light_controller.mojom.h"
8 #include "base/test/scoped_task_environment.h" 8 #include "base/test/scoped_task_environment.h"
9 #include "mojo/public/cpp/bindings/binding.h" 9 #include "mojo/public/cpp/bindings/binding.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 11
12 namespace { 12 namespace {
13 13
14 using ScheduleType = ash::mojom::NightLightController::ScheduleType; 14 using ScheduleType = ash::mojom::NightLightController::ScheduleType;
15 15
16 // A fake implementation of NightLightController for testing. 16 // A fake implementation of NightLightController for testing.
17 class FakeNightLightController : public ash::mojom::NightLightController { 17 class FakeNightLightController : public ash::mojom::NightLightController {
18 public: 18 public:
19 FakeNightLightController() : binding_(this) {} 19 FakeNightLightController() : binding_(this) {}
20 ~FakeNightLightController() override = default; 20 ~FakeNightLightController() override = default;
21 21
22 const ash::mojom::SimpleGeopositionPtr& position() const { return position_; }
23
22 int position_pushes_num() const { return position_pushes_num_; } 24 int position_pushes_num() const { return position_pushes_num_; }
23 25
24 ash::mojom::NightLightControllerPtr CreateInterfacePtrAndBind() { 26 ash::mojom::NightLightControllerPtr CreateInterfacePtrAndBind() {
25 ash::mojom::NightLightControllerPtr ptr; 27 ash::mojom::NightLightControllerPtr ptr;
26 binding_.Bind(mojo::MakeRequest(&ptr)); 28 binding_.Bind(mojo::MakeRequest(&ptr));
27 return ptr; 29 return ptr;
28 } 30 }
29 31
30 // ash::mojom::NightLightController: 32 // ash::mojom::NightLightController:
31 void SetCurrentGeoposition( 33 void SetCurrentGeoposition(
(...skipping 26 matching lines...) Expand all
58 // geoposition requests. 60 // geoposition requests.
59 class FakeNightLightClient : public NightLightClient { 61 class FakeNightLightClient : public NightLightClient {
60 public: 62 public:
61 FakeNightLightClient() : NightLightClient(nullptr /* url_context_getter */) {} 63 FakeNightLightClient() : NightLightClient(nullptr /* url_context_getter */) {}
62 ~FakeNightLightClient() override = default; 64 ~FakeNightLightClient() override = default;
63 65
64 void set_position_to_send(const chromeos::Geoposition& position) { 66 void set_position_to_send(const chromeos::Geoposition& position) {
65 position_to_send_ = position; 67 position_to_send_ = position;
66 } 68 }
67 69
70 int geoposition_requests_num() const { return geoposition_requests_num_; }
71
68 private: 72 private:
69 // night_light::NightLightClient: 73 // night_light::NightLightClient:
70 void RequestGeoposition() override { 74 void RequestGeoposition() override {
71 OnGeoposition(position_to_send_, false, base::TimeDelta()); 75 OnGeoposition(position_to_send_, false, base::TimeDelta());
76 ++geoposition_requests_num_;
72 } 77 }
73 78
74 // The position to send to the controller the next time OnGeoposition is 79 // The position to send to the controller the next time OnGeoposition is
75 // invoked. 80 // invoked.
76 chromeos::Geoposition position_to_send_; 81 chromeos::Geoposition position_to_send_;
77 82
83 // The number of new geoposition requests that have been triggered.
84 int geoposition_requests_num_ = 0;
85
78 DISALLOW_COPY_AND_ASSIGN(FakeNightLightClient); 86 DISALLOW_COPY_AND_ASSIGN(FakeNightLightClient);
79 }; 87 };
80 88
81 // Base test fixture. 89 // Base test fixture.
82 class NightLightClientTest : public testing::Test { 90 class NightLightClientTest : public testing::Test {
83 public: 91 public:
84 NightLightClientTest() = default; 92 NightLightClientTest() = default;
85 ~NightLightClientTest() override = default; 93 ~NightLightClientTest() override = default;
86 94
87 void SetUp() override { 95 void SetUp() override {
(...skipping 24 matching lines...) Expand all
112 client_.FlushNightLightControllerForTesting(); 120 client_.FlushNightLightControllerForTesting();
113 EXPECT_TRUE(client_.using_geoposition()); 121 EXPECT_TRUE(client_.using_geoposition());
114 122
115 // Client should stop retrieving geopositions when schedule type changes to 123 // Client should stop retrieving geopositions when schedule type changes to
116 // something else. 124 // something else.
117 controller_.NotifyScheduleTypeChanged(ScheduleType::kNone); 125 controller_.NotifyScheduleTypeChanged(ScheduleType::kNone);
118 EXPECT_FALSE(client_.using_geoposition()); 126 EXPECT_FALSE(client_.using_geoposition());
119 } 127 }
120 128
121 // Test that client only pushes valid positions. 129 // Test that client only pushes valid positions.
122 TEST_F(NightLightClientTest, TestPositionPushes) { 130 TEST_F(NightLightClientTest, TestInavlidPositions) {
James Cook 2017/06/30 22:50:06 nit: invalid
afakhry 2017/06/30 23:41:45 Done.
123 // Start with a valid position, and expect it to be delivered to the
124 // controller.
125 EXPECT_EQ(0, controller_.position_pushes_num()); 131 EXPECT_EQ(0, controller_.position_pushes_num());
126 chromeos::Geoposition position; 132 chromeos::Geoposition position;
127 position.latitude = 32.0; 133 position.latitude = 32.0;
128 position.longitude = 31.0; 134 position.longitude = 31.0;
129 position.status = chromeos::Geoposition::STATUS_OK; 135 position.status = chromeos::Geoposition::STATUS_TIMEOUT;
130 position.accuracy = 10; 136 position.accuracy = 10;
131 position.timestamp = base::Time::Now(); 137 position.timestamp = base::Time::Now();
132 client_.set_position_to_send(position); 138 client_.set_position_to_send(position);
133 controller_.NotifyScheduleTypeChanged(ScheduleType::kSunsetToSunrise); 139 controller_.NotifyScheduleTypeChanged(ScheduleType::kSunsetToSunrise);
134 scoped_task_environment_.RunUntilIdle(); 140 scoped_task_environment_.RunUntilIdle();
135 client_.FlushNightLightControllerForTesting(); 141 client_.FlushNightLightControllerForTesting();
136 EXPECT_EQ(1, controller_.position_pushes_num()); 142 EXPECT_EQ(1, client_.geoposition_requests_num());
143 EXPECT_EQ(0, controller_.position_pushes_num());
144 }
137 145
138 // Invalid positions should not be sent. 146 // Test that successive changes of the schedule type to sunset to sunrise do not
139 position.status = chromeos::Geoposition::STATUS_TIMEOUT; 147 // trigger repeated geoposition requests.
140 client_.set_position_to_send(position); 148 TEST_F(NightLightClientTest, TestRepeatedScheduleTypeChanges) {
149 // Start with a valid position, and expect it to be delivered to the
150 // controller.
151 EXPECT_EQ(0, controller_.position_pushes_num());
152 chromeos::Geoposition position1;
153 position1.latitude = 32.0;
154 position1.longitude = 31.0;
155 position1.status = chromeos::Geoposition::STATUS_OK;
156 position1.accuracy = 10;
157 position1.timestamp = base::Time::Now();
158 client_.set_position_to_send(position1);
141 controller_.NotifyScheduleTypeChanged(ScheduleType::kSunsetToSunrise); 159 controller_.NotifyScheduleTypeChanged(ScheduleType::kSunsetToSunrise);
142 scoped_task_environment_.RunUntilIdle(); 160 scoped_task_environment_.RunUntilIdle();
143 client_.FlushNightLightControllerForTesting(); 161 client_.FlushNightLightControllerForTesting();
162 EXPECT_EQ(1, client_.geoposition_requests_num());
144 EXPECT_EQ(1, controller_.position_pushes_num()); 163 EXPECT_EQ(1, controller_.position_pushes_num());
164
165 // A new different position just for the sake of comparison with position1 to
166 // make sure that no new requests are triggered and the same old position will
167 // be resent to the controller.
168 chromeos::Geoposition position2;
169 position2.latitude = 100.0;
170 position2.longitude = 200.0;
171 position2.status = chromeos::Geoposition::STATUS_OK;
172 position2.accuracy = 10;
173 position2.timestamp = base::Time::Now();
174 client_.set_position_to_send(position2);
175 controller_.NotifyScheduleTypeChanged(ScheduleType::kSunsetToSunrise);
176 scoped_task_environment_.RunUntilIdle();
177 client_.FlushNightLightControllerForTesting();
178 // No new request has been triggered, however the same old valid position was
179 // pushed to the controller.
180 EXPECT_EQ(1, client_.geoposition_requests_num());
181 EXPECT_EQ(2, controller_.position_pushes_num());
182 EXPECT_TRUE(ash::mojom::SimpleGeoposition::New(position1.latitude,
183 position1.longitude)
184 .Equals(controller_.position()));
185
186 // The timer should be running scheduling a next request that is a
187 // kNextRequestDelayAfterSuccess from the last successful request time.
188 EXPECT_TRUE(client_.timer().IsRunning());
189 base::TimeDelta expected_delay =
190 client_.last_successful_geo_request_time() +
191 NightLightClient::GetNextRequestDelayAfterSuccessForTesting() -
192 base::Time::Now();
193 // To avoid flakiness, we get the absolute value of difference between the
James Cook 2017/06/30 22:50:06 This makes me a little nervous. As you mention, te
afakhry 2017/06/30 23:41:46 I opted for the first suggestion. Please take a lo
194 // expected delay and the actual delay of the timer, and make sure it's less
195 // than an arbitrary epsilon TimeDelta value; say 1 minute.
196 base::TimeDelta delay_delta =
197 (expected_delay - client_.timer().GetCurrentDelay()).magnitude();
198 EXPECT_LT(delay_delta, base::TimeDelta::FromMinutes(1));
145 } 199 }
146 200
147 } // namespace 201 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/night_light/night_light_client.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698