Commit aa616dc5 authored by Regan Hsu's avatar Regan Hsu Committed by Commit Bot

[CrOS Settings] Fix AboutHandler tests.

The test was previously failing because it was using Time::Now()
as a reference, instead of a fake clock as it does now.

Fixed: 1122584
Change-Id: I04cbb723dabc38fa1aad391d1a601150ec43c0cb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2392984
Commit-Queue: Regan Hsu <hsuregan@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#804523}
parent b3d05379
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/task/post_task.h" #include "base/task/post_task.h"
#include "base/task/thread_pool.h" #include "base/task/thread_pool.h"
#include "base/time/time.h" #include "base/time/default_clock.h"
#include "base/values.h" #include "base/values.h"
#include "build/branding_buildflags.h" #include "build/branding_buildflags.h"
#include "build/build_config.h" #include "build/build_config.h"
...@@ -252,7 +252,9 @@ std::string UpdateStatusToString(VersionUpdater::Status status) { ...@@ -252,7 +252,9 @@ std::string UpdateStatusToString(VersionUpdater::Status status) {
namespace settings { namespace settings {
AboutHandler::AboutHandler(Profile* profile) AboutHandler::AboutHandler(Profile* profile)
: profile_(profile), apply_changes_from_upgrade_observer_(false) { : profile_(profile),
apply_changes_from_upgrade_observer_(false),
clock_(base::DefaultClock::GetInstance()) {
UpgradeDetector::GetInstance()->AddObserver(this); UpgradeDetector::GetInstance()->AddObserver(this);
} }
...@@ -630,7 +632,7 @@ void AboutHandler::OnGetEndOfLifeInfo( ...@@ -630,7 +632,7 @@ void AboutHandler::OnGetEndOfLifeInfo(
chromeos::UpdateEngineClient::EolInfo eol_info) { chromeos::UpdateEngineClient::EolInfo eol_info) {
base::Value response(base::Value::Type::DICTIONARY); base::Value response(base::Value::Type::DICTIONARY);
if (!eol_info.eol_date.is_null()) { if (!eol_info.eol_date.is_null()) {
bool has_eol_passed = eol_info.eol_date <= base::Time::Now(); bool has_eol_passed = eol_info.eol_date <= clock_->Now();
response.SetBoolKey("hasEndOfLife", has_eol_passed); response.SetBoolKey("hasEndOfLife", has_eol_passed);
int eol_string_id = int eol_string_id =
has_eol_passed ? IDS_SETTINGS_ABOUT_PAGE_END_OF_LIFE_MESSAGE_PAST has_eol_passed ? IDS_SETTINGS_ABOUT_PAGE_END_OF_LIFE_MESSAGE_PAST
......
...@@ -28,6 +28,7 @@ namespace base { ...@@ -28,6 +28,7 @@ namespace base {
class DictionaryValue; class DictionaryValue;
class FilePath; class FilePath;
class ListValue; class ListValue;
class Clock;
} // namespace base } // namespace base
class Profile; class Profile;
...@@ -52,6 +53,10 @@ class AboutHandler : public settings::SettingsPageUIHandler, ...@@ -52,6 +53,10 @@ class AboutHandler : public settings::SettingsPageUIHandler,
// Returns the browser version as a string. // Returns the browser version as a string.
static base::string16 BuildBrowserVersionString(); static base::string16 BuildBrowserVersionString();
protected:
// Used to test the EOL string displayed in the About details page.
void set_clock(base::Clock* clock) { clock_ = clock; }
private: private:
void OnDeviceAutoUpdatePolicyChanged(const base::Value* previous_policy, void OnDeviceAutoUpdatePolicyChanged(const base::Value* previous_policy,
const base::Value* current_policy); const base::Value* current_policy);
...@@ -182,6 +187,9 @@ class AboutHandler : public settings::SettingsPageUIHandler, ...@@ -182,6 +187,9 @@ class AboutHandler : public settings::SettingsPageUIHandler,
// If true changes to UpgradeObserver are applied, if false they are ignored. // If true changes to UpgradeObserver are applied, if false they are ignored.
bool apply_changes_from_upgrade_observer_; bool apply_changes_from_upgrade_observer_;
// Override to test the EOL string displayed in the About details page.
base::Clock* clock_;
// Used for callbacks. // Used for callbacks.
base::WeakPtrFactory<AboutHandler> weak_factory_{this}; base::WeakPtrFactory<AboutHandler> weak_factory_{this};
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "chrome/browser/ui/webui/settings/about_handler.h" #include "chrome/browser/ui/webui/settings/about_handler.h"
#include "base/test/simple_test_clock.h"
#include "chrome/test/base/testing_browser_process.h" #include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile.h"
#include "chromeos/dbus/dbus_thread_manager.h" #include "chromeos/dbus/dbus_thread_manager.h"
...@@ -21,6 +22,9 @@ class TestAboutHandler : public ::settings::AboutHandler { ...@@ -21,6 +22,9 @@ class TestAboutHandler : public ::settings::AboutHandler {
explicit TestAboutHandler(Profile* profile) : AboutHandler(profile) {} explicit TestAboutHandler(Profile* profile) : AboutHandler(profile) {}
~TestAboutHandler() override = default; ~TestAboutHandler() override = default;
// Make public for testing.
using AboutHandler::set_clock;
// Make public for testing. // Make public for testing.
using AboutHandler::set_web_ui; using AboutHandler::set_web_ui;
}; };
...@@ -42,6 +46,9 @@ class AboutHandlerTest : public testing::Test { ...@@ -42,6 +46,9 @@ class AboutHandlerTest : public testing::Test {
handler_->set_web_ui(&web_ui_); handler_->set_web_ui(&web_ui_);
handler_->RegisterMessages(); handler_->RegisterMessages();
handler_->AllowJavascriptForTesting(); handler_->AllowJavascriptForTesting();
clock_ = std::make_unique<base::SimpleTestClock>();
handler_->set_clock(clock_.get());
} }
void TearDown() override { void TearDown() override {
...@@ -72,35 +79,38 @@ class AboutHandlerTest : public testing::Test { ...@@ -72,35 +79,38 @@ class AboutHandlerTest : public testing::Test {
return call_data.arg3()->FindKey("aboutPageEndOfLifeMessage")->GetString(); return call_data.arg3()->FindKey("aboutPageEndOfLifeMessage")->GetString();
} }
void SetCurrentTimeToUtc(const char* utc_date_string) {
base::Time utc_time;
ASSERT_TRUE(base::Time::FromUTCString(utc_date_string, &utc_time));
clock_->SetNow(utc_time);
}
void SetEolDateUtc(const char* utc_date_string) {
base::Time utc_date;
ASSERT_TRUE(base::Time::FromUTCString(utc_date_string, &utc_date));
fake_update_engine_client_->set_eol_date(utc_date);
}
protected: protected:
content::BrowserTaskEnvironment task_environment_; content::BrowserTaskEnvironment task_environment_;
TestingProfile profile_; TestingProfile profile_;
content::TestWebUI web_ui_; content::TestWebUI web_ui_;
std::unique_ptr<TestAboutHandler> handler_; std::unique_ptr<TestAboutHandler> handler_;
FakeUpdateEngineClient* fake_update_engine_client_; FakeUpdateEngineClient* fake_update_engine_client_;
std::unique_ptr<base::SimpleTestClock> clock_;
}; };
// Deterministic fail on CHROMEOS, crbug.com/1122584. TEST_F(AboutHandlerTest, EndOfLifeMessageInAboutDetailsSubpage) {
#if defined(OS_CHROMEOS) SetCurrentTimeToUtc("15 March 2020");
#define MAYBE_EndOfLifeMessageInAboutDetailsSubpage \
DISABLED_EndOfLifeMessageInAboutDetailsSubpage SetEolDateUtc("15 November 2017");
#else
#define MAYBE_EndOfLifeMessageInAboutDetailsSubpage \
EndOfLifeMessageInAboutDetailsSubpage
#endif
TEST_F(AboutHandlerTest, MAYBE_EndOfLifeMessageInAboutDetailsSubpage) {
const base::Time eol_passed_date =
base::Time::Now() - base::TimeDelta::FromDays(1000);
fake_update_engine_client_->set_eol_date(eol_passed_date);
EXPECT_EQ( EXPECT_EQ(
"This device stopped getting automatic software and security " "This device stopped getting automatic software and security "
"updates in November 2017. <a target=\"_blank\" href=\"https:" "updates in November 2017. <a target=\"_blank\" href=\"https:"
"//www.google.com/chromebook/older/\">Learn more</a>", "//www.google.com/chromebook/older/\">Learn more</a>",
CallGetEndOfLifeInfoAndReturnString(true /*=has_eol_passed*/)); CallGetEndOfLifeInfoAndReturnString(true /*=has_eol_passed*/));
const base::Time eol_future_date = SetEolDateUtc("15 May 2023");
base::Time::Now() + base::TimeDelta::FromDays(1000);
fake_update_engine_client_->set_eol_date(eol_future_date);
EXPECT_EQ( EXPECT_EQ(
"This device will get automatic software and security updates " "This device will get automatic software and security updates "
"until May 2023. <a target=\"_blank\" href=\"http://support.google" "until May 2023. <a target=\"_blank\" href=\"http://support.google"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment