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 @@
#include "base/strings/string_util.h"
#include "base/task/post_task.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/time/default_clock.h"
#include "base/values.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
......@@ -252,7 +252,9 @@ std::string UpdateStatusToString(VersionUpdater::Status status) {
namespace settings {
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);
}
......@@ -630,7 +632,7 @@ void AboutHandler::OnGetEndOfLifeInfo(
chromeos::UpdateEngineClient::EolInfo eol_info) {
base::Value response(base::Value::Type::DICTIONARY);
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);
int eol_string_id =
has_eol_passed ? IDS_SETTINGS_ABOUT_PAGE_END_OF_LIFE_MESSAGE_PAST
......
......@@ -28,6 +28,7 @@ namespace base {
class DictionaryValue;
class FilePath;
class ListValue;
class Clock;
} // namespace base
class Profile;
......@@ -52,6 +53,10 @@ class AboutHandler : public settings::SettingsPageUIHandler,
// Returns the browser version as a string.
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:
void OnDeviceAutoUpdatePolicyChanged(const base::Value* previous_policy,
const base::Value* current_policy);
......@@ -182,6 +187,9 @@ class AboutHandler : public settings::SettingsPageUIHandler,
// If true changes to UpgradeObserver are applied, if false they are ignored.
bool apply_changes_from_upgrade_observer_;
// Override to test the EOL string displayed in the About details page.
base::Clock* clock_;
// Used for callbacks.
base::WeakPtrFactory<AboutHandler> weak_factory_{this};
......
......@@ -3,6 +3,7 @@
// found in the LICENSE file.
#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_profile.h"
#include "chromeos/dbus/dbus_thread_manager.h"
......@@ -21,6 +22,9 @@ class TestAboutHandler : public ::settings::AboutHandler {
explicit TestAboutHandler(Profile* profile) : AboutHandler(profile) {}
~TestAboutHandler() override = default;
// Make public for testing.
using AboutHandler::set_clock;
// Make public for testing.
using AboutHandler::set_web_ui;
};
......@@ -42,6 +46,9 @@ class AboutHandlerTest : public testing::Test {
handler_->set_web_ui(&web_ui_);
handler_->RegisterMessages();
handler_->AllowJavascriptForTesting();
clock_ = std::make_unique<base::SimpleTestClock>();
handler_->set_clock(clock_.get());
}
void TearDown() override {
......@@ -72,35 +79,38 @@ class AboutHandlerTest : public testing::Test {
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:
content::BrowserTaskEnvironment task_environment_;
TestingProfile profile_;
content::TestWebUI web_ui_;
std::unique_ptr<TestAboutHandler> handler_;
FakeUpdateEngineClient* fake_update_engine_client_;
std::unique_ptr<base::SimpleTestClock> clock_;
};
// Deterministic fail on CHROMEOS, crbug.com/1122584.
#if defined(OS_CHROMEOS)
#define MAYBE_EndOfLifeMessageInAboutDetailsSubpage \
DISABLED_EndOfLifeMessageInAboutDetailsSubpage
#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);
TEST_F(AboutHandlerTest, EndOfLifeMessageInAboutDetailsSubpage) {
SetCurrentTimeToUtc("15 March 2020");
SetEolDateUtc("15 November 2017");
EXPECT_EQ(
"This device stopped getting automatic software and security "
"updates in November 2017. <a target=\"_blank\" href=\"https:"
"//www.google.com/chromebook/older/\">Learn more</a>",
CallGetEndOfLifeInfoAndReturnString(true /*=has_eol_passed*/));
const base::Time eol_future_date =
base::Time::Now() + base::TimeDelta::FromDays(1000);
fake_update_engine_client_->set_eol_date(eol_future_date);
SetEolDateUtc("15 May 2023");
EXPECT_EQ(
"This device will get automatic software and security updates "
"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