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

Unified 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: Steven's suggestions Created 3 years, 6 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/chromeos/night_light/night_light_client_unittest.cc
diff --git a/chrome/browser/chromeos/night_light/night_light_client_unittest.cc b/chrome/browser/chromeos/night_light/night_light_client_unittest.cc
index bfe124b406301122839a8c8544133a5eaf3b8951..de6f88f677d27dff190fc11cd53e37885c0c2ac0 100644
--- a/chrome/browser/chromeos/night_light/night_light_client_unittest.cc
+++ b/chrome/browser/chromeos/night_light/night_light_client_unittest.cc
@@ -6,6 +6,8 @@
#include "ash/public/interfaces/night_light_controller.mojom.h"
#include "base/test/scoped_task_environment.h"
+#include "base/time/clock.h"
+#include "base/time/tick_clock.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -19,6 +21,8 @@ class FakeNightLightController : public ash::mojom::NightLightController {
FakeNightLightController() : binding_(this) {}
~FakeNightLightController() override = default;
+ const ash::mojom::SimpleGeopositionPtr& position() const { return position_; }
+
int position_pushes_num() const { return position_pushes_num_; }
ash::mojom::NightLightControllerPtr CreateInterfacePtrAndBind() {
@@ -56,25 +60,51 @@ class FakeNightLightController : public ash::mojom::NightLightController {
// A fake implementation of NightLightClient that doesn't perform any actual
// geoposition requests.
-class FakeNightLightClient : public NightLightClient {
+class FakeNightLightClient : public NightLightClient,
+ public base::Clock,
+ public base::TickClock {
public:
- FakeNightLightClient() : NightLightClient(nullptr /* url_context_getter */) {}
+ FakeNightLightClient() : NightLightClient(nullptr /* url_context_getter */) {
+ SetTimerForTesting(
+ base::MakeUnique<base::OneShotTimer>(this /* tick_clock */));
+ SetClockForTesting(this);
+ }
~FakeNightLightClient() override = default;
+ // base::Clock:
+ base::Time Now() override { return fake_now_; }
+
+ // base::TickClock:
+ base::TimeTicks NowTicks() override { return fake_now_ticks_; }
+
+ void set_fake_now(base::Time now) { fake_now_ = now; }
+ void set_fake_now_ticks(base::TimeTicks now_ticks) {
+ fake_now_ticks_ = now_ticks;
+ }
+
void set_position_to_send(const chromeos::Geoposition& position) {
position_to_send_ = position;
}
+ int geoposition_requests_num() const { return geoposition_requests_num_; }
+
private:
// night_light::NightLightClient:
void RequestGeoposition() override {
OnGeoposition(position_to_send_, false, base::TimeDelta());
+ ++geoposition_requests_num_;
}
+ base::Time fake_now_;
+ base::TimeTicks fake_now_ticks_;
+
// The position to send to the controller the next time OnGeoposition is
// invoked.
chromeos::Geoposition position_to_send_;
+ // The number of new geoposition requests that have been triggered.
+ int geoposition_requests_num_ = 0;
+
DISALLOW_COPY_AND_ASSIGN(FakeNightLightClient);
};
@@ -85,6 +115,10 @@ class NightLightClientTest : public testing::Test {
~NightLightClientTest() override = default;
void SetUp() override {
+ // Deterministic fake time that doesn't change for the sake of testing.
+ client_.set_fake_now(base::Time::Now());
+ client_.set_fake_now_ticks(base::TimeTicks::Now());
+
client_.SetNightLightControllerPtrForTesting(
controller_.CreateInterfacePtrAndBind());
client_.Start();
@@ -119,29 +153,71 @@ TEST_F(NightLightClientTest, TestClientRunningOnlyWhenSunsetToSunriseSchedule) {
}
// Test that client only pushes valid positions.
-TEST_F(NightLightClientTest, TestPositionPushes) {
- // Start with a valid position, and expect it to be delivered to the
- // controller.
+TEST_F(NightLightClientTest, TestInvalidPositions) {
EXPECT_EQ(0, controller_.position_pushes_num());
chromeos::Geoposition position;
position.latitude = 32.0;
position.longitude = 31.0;
- position.status = chromeos::Geoposition::STATUS_OK;
+ position.status = chromeos::Geoposition::STATUS_TIMEOUT;
position.accuracy = 10;
position.timestamp = base::Time::Now();
client_.set_position_to_send(position);
controller_.NotifyScheduleTypeChanged(ScheduleType::kSunsetToSunrise);
scoped_task_environment_.RunUntilIdle();
client_.FlushNightLightControllerForTesting();
- EXPECT_EQ(1, controller_.position_pushes_num());
+ EXPECT_EQ(1, client_.geoposition_requests_num());
+ EXPECT_EQ(0, controller_.position_pushes_num());
+}
- // Invalid positions should not be sent.
- position.status = chromeos::Geoposition::STATUS_TIMEOUT;
- client_.set_position_to_send(position);
+// Test that successive changes of the schedule type to sunset to sunrise do not
+// trigger repeated geoposition requests.
+TEST_F(NightLightClientTest, TestRepeatedScheduleTypeChanges) {
+ // Start with a valid position, and expect it to be delivered to the
+ // controller.
+ EXPECT_EQ(0, controller_.position_pushes_num());
+ chromeos::Geoposition position1;
+ position1.latitude = 32.0;
+ position1.longitude = 31.0;
+ position1.status = chromeos::Geoposition::STATUS_OK;
+ position1.accuracy = 10;
+ position1.timestamp = base::Time::Now();
+ client_.set_position_to_send(position1);
controller_.NotifyScheduleTypeChanged(ScheduleType::kSunsetToSunrise);
scoped_task_environment_.RunUntilIdle();
client_.FlushNightLightControllerForTesting();
+ EXPECT_EQ(1, client_.geoposition_requests_num());
EXPECT_EQ(1, controller_.position_pushes_num());
+ EXPECT_EQ(client_.Now(), client_.last_successful_geo_request_time());
+
+ // A new different position just for the sake of comparison with position1 to
+ // make sure that no new requests are triggered and the same old position will
+ // be resent to the controller.
+ chromeos::Geoposition position2;
+ position2.latitude = 100.0;
+ position2.longitude = 200.0;
+ position2.status = chromeos::Geoposition::STATUS_OK;
+ position2.accuracy = 10;
+ position2.timestamp = base::Time::Now();
+ client_.set_position_to_send(position2);
+ controller_.NotifyScheduleTypeChanged(ScheduleType::kSunsetToSunrise);
+ scoped_task_environment_.RunUntilIdle();
+ client_.FlushNightLightControllerForTesting();
+ // No new request has been triggered, however the same old valid position was
+ // pushed to the controller.
+ EXPECT_EQ(1, client_.geoposition_requests_num());
+ EXPECT_EQ(2, controller_.position_pushes_num());
+ EXPECT_TRUE(ash::mojom::SimpleGeoposition::New(position1.latitude,
+ position1.longitude)
+ .Equals(controller_.position()));
+
+ // The timer should be running scheduling a next request that is a
+ // kNextRequestDelayAfterSuccess from the last successful request time.
+ EXPECT_TRUE(client_.timer().IsRunning());
+ base::TimeDelta expected_delay =
+ client_.last_successful_geo_request_time() +
+ NightLightClient::GetNextRequestDelayAfterSuccessForTesting() -
+ client_.Now();
+ EXPECT_EQ(expected_delay, client_.timer().GetCurrentDelay());
}
} // namespace
« 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