Add a multi-process time zone scoper for testing.
The base::test::ScopedRestoreDefaultTimezone class works fine for
single-process tests, and for multi-process tests on Linux. On Linux,
where zygotes are normally enabled, zygotes will pass the browser time
zone to newly spawned processes, but this is an implementation detail.
To make scoped time zone changes work across platform, add a new
content::ScopedTimeZone class that:
1) Wraps base::test::ScopedRestoreDefaultTimezone.
2) Changes the command line within its scope to have
--time-zone-for-testing=new_zoneid.
Then change child processes to read the value of this command line
switch and apply it. Use it in pdf_extension_test.cc to make a test case
more reliably pass independent of the host machine's time zone.
Note that the scoper only affects the running process and any child
processes it creates. Other existing processes are not affected.
Bug: 1199021
Change-Id: I18f82007598e37a873126cb0716c8321d159ef8f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2795988
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Arthur Sonzogni <arthursonzogni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#872683}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
95564c8889
commit
8e6e2d5f64
chrome/browser/pdf
content
browser
ppapi_plugin
public
renderer
test
utility
@ -82,6 +82,7 @@
|
||||
#include "content/public/test/browser_test_utils.h"
|
||||
#include "content/public/test/dump_accessibility_test_helper.h"
|
||||
#include "content/public/test/hit_test_region_observer.h"
|
||||
#include "content/public/test/scoped_time_zone.h"
|
||||
#include "content/public/test/test_navigation_observer.h"
|
||||
#include "content/public/test/url_loader_interceptor.h"
|
||||
#include "extensions/browser/api/extensions_api_client.h"
|
||||
@ -894,8 +895,9 @@ IN_PROC_BROWSER_TEST_F(PDFExtensionDocumentPropertiesEnabledTest,
|
||||
ViewerPropertiesDialog) {
|
||||
// The properties dialog formats some values based on locale.
|
||||
base::test::ScopedRestoreICUDefaultLocale scoped_locale{"en_US"};
|
||||
base::test::ScopedRestoreDefaultTimezone
|
||||
scoped_timezone{"America/Los_Angeles"};
|
||||
// This will apply to the new processes spawned within RunTestsInJsModule(),
|
||||
// thus consistently running the test in a well known time zone.
|
||||
content::ScopedTimeZone scoped_time_zone{"America/Los_Angeles"};
|
||||
RunTestsInJsModule("viewer_properties_dialog_test.js", "document_info.pdf");
|
||||
}
|
||||
|
||||
|
@ -340,6 +340,7 @@ bool PpapiPluginProcessHost::Init(const PepperPluginInfo& info) {
|
||||
sandbox::policy::switches::kEnableSandboxLogging,
|
||||
#endif
|
||||
switches::kPpapiStartupDialog,
|
||||
switches::kTimeZoneForTesting,
|
||||
};
|
||||
cmd_line->CopySwitchesFrom(browser_command_line, kPluginForwardSwitches,
|
||||
base::size(kPluginForwardSwitches));
|
||||
|
@ -3266,6 +3266,7 @@ void RenderProcessHostImpl::PropagateBrowserCommandLineToRenderer(
|
||||
switches::kSkiaFontCacheLimitMb,
|
||||
switches::kSkiaResourceCacheLimitMb,
|
||||
switches::kTestType,
|
||||
switches::kTimeZoneForTesting,
|
||||
switches::kTouchEventFeatureDetection,
|
||||
switches::kTraceToConsole,
|
||||
switches::kUseFakeCodecForPeerConnection,
|
||||
|
@ -265,6 +265,7 @@ bool UtilityProcessHost::StartProcess() {
|
||||
switches::kUseFileForFakeVideoCapture,
|
||||
switches::kUseMockCertVerifierForTesting,
|
||||
switches::kMockCertVerifierDefaultResultForTesting,
|
||||
switches::kTimeZoneForTesting,
|
||||
switches::kUtilityStartupDialog,
|
||||
switches::kUseANGLE,
|
||||
switches::kUseGL,
|
||||
|
@ -25,6 +25,8 @@
|
||||
#include "ipc/ipc_sender.h"
|
||||
#include "ppapi/proxy/plugin_globals.h"
|
||||
#include "services/tracing/public/cpp/trace_startup.h"
|
||||
#include "third_party/icu/source/common/unicode/unistr.h"
|
||||
#include "third_party/icu/source/i18n/unicode/timezone.h"
|
||||
#include "ui/base/ui_base_switches.h"
|
||||
|
||||
#if defined(OS_WIN)
|
||||
@ -107,6 +109,13 @@ int PpapiPluginMain(const MainFunctionParams& parameters) {
|
||||
#endif
|
||||
}
|
||||
|
||||
if (command_line.HasSwitch(switches::kTimeZoneForTesting)) {
|
||||
std::string time_zone =
|
||||
command_line.GetSwitchValueASCII(switches::kTimeZoneForTesting);
|
||||
icu::TimeZone::adoptDefault(
|
||||
icu::TimeZone::createTimeZone(icu::UnicodeString(time_zone.c_str())));
|
||||
}
|
||||
|
||||
#if BUILDFLAG(IS_CHROMEOS_ASH)
|
||||
// Specifies $HOME explicitly because some plugins rely on $HOME but
|
||||
// no other part of Chrome OS uses that. See crbug.com/335290.
|
||||
|
@ -766,6 +766,9 @@ const char kSkiaResourceCacheLimitMb[] = "skia-resource-cache-limit-mb";
|
||||
// Type of the current test harness ("browser" or "ui").
|
||||
const char kTestType[] = "test-type";
|
||||
|
||||
// The time zone to use for testing. Passed to renderers and plugins on startup.
|
||||
const char kTimeZoneForTesting[] = "time-zone-for-testing";
|
||||
|
||||
// Enable support for touch event feature detection.
|
||||
const char kTouchEventFeatureDetection[] = "touch-events";
|
||||
|
||||
|
@ -204,6 +204,7 @@ CONTENT_EXPORT extern const char kStatsCollectionController[];
|
||||
extern const char kSkiaFontCacheLimitMb[];
|
||||
extern const char kSkiaResourceCacheLimitMb[];
|
||||
CONTENT_EXPORT extern const char kTestType[];
|
||||
CONTENT_EXPORT extern const char kTimeZoneForTesting[];
|
||||
CONTENT_EXPORT extern const char kTouchEventFeatureDetection[];
|
||||
CONTENT_EXPORT extern const char kTouchEventFeatureDetectionAuto[];
|
||||
CONTENT_EXPORT extern const char kTouchEventFeatureDetectionEnabled[];
|
||||
|
20
content/public/test/scoped_time_zone.cc
Normal file
20
content/public/test/scoped_time_zone.cc
Normal file
@ -0,0 +1,20 @@
|
||||
// Copyright 2021 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "content/public/test/scoped_time_zone.h"
|
||||
|
||||
#include "base/command_line.h"
|
||||
#include "content/public/common/content_switches.h"
|
||||
|
||||
namespace content {
|
||||
|
||||
ScopedTimeZone::ScopedTimeZone(const char* new_zoneid)
|
||||
: icu_time_zone_(new_zoneid) {
|
||||
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
|
||||
switches::kTimeZoneForTesting, new_zoneid);
|
||||
}
|
||||
|
||||
ScopedTimeZone::~ScopedTimeZone() = default;
|
||||
|
||||
} // namespace content
|
29
content/public/test/scoped_time_zone.h
Normal file
29
content/public/test/scoped_time_zone.h
Normal file
@ -0,0 +1,29 @@
|
||||
// Copyright 2021 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef CONTENT_PUBLIC_TEST_SCOPED_TIME_ZONE_H_
|
||||
#define CONTENT_PUBLIC_TEST_SCOPED_TIME_ZONE_H_
|
||||
|
||||
#include "base/test/icu_test_util.h"
|
||||
#include "base/test/scoped_command_line.h"
|
||||
|
||||
namespace content {
|
||||
|
||||
// Helper class to temporarily set the time zone of the browser process and
|
||||
// propagate it to newly spawned child processes.
|
||||
class ScopedTimeZone {
|
||||
public:
|
||||
explicit ScopedTimeZone(const char* new_zoneid);
|
||||
ScopedTimeZone(const ScopedTimeZone&) = delete;
|
||||
ScopedTimeZone& operator=(const ScopedTimeZone&) = delete;
|
||||
~ScopedTimeZone();
|
||||
|
||||
private:
|
||||
base::test::ScopedCommandLine command_line_;
|
||||
base::test::ScopedRestoreDefaultTimezone icu_time_zone_;
|
||||
};
|
||||
|
||||
} // namespace content
|
||||
|
||||
#endif // CONTENT_PUBLIC_TEST_SCOPED_TIME_ZONE_H_
|
@ -42,6 +42,8 @@
|
||||
#include "services/tracing/public/cpp/trace_startup.h"
|
||||
#include "third_party/blink/public/platform/platform.h"
|
||||
#include "third_party/blink/public/platform/scheduler/web_thread_scheduler.h"
|
||||
#include "third_party/icu/source/common/unicode/unistr.h"
|
||||
#include "third_party/icu/source/i18n/unicode/timezone.h"
|
||||
#include "third_party/webrtc_overrides/init_webrtc.h" // nogncheck
|
||||
#include "ui/base/ui_base_switches.h"
|
||||
|
||||
@ -143,6 +145,13 @@ int RendererMain(const MainFunctionParams& parameters) {
|
||||
}
|
||||
#endif
|
||||
|
||||
if (command_line.HasSwitch(switches::kTimeZoneForTesting)) {
|
||||
std::string time_zone =
|
||||
command_line.GetSwitchValueASCII(switches::kTimeZoneForTesting);
|
||||
icu::TimeZone::adoptDefault(
|
||||
icu::TimeZone::createTimeZone(icu::UnicodeString(time_zone.c_str())));
|
||||
}
|
||||
|
||||
InitializeSkia();
|
||||
|
||||
// This function allows pausing execution using the --renderer-startup-dialog
|
||||
|
@ -201,6 +201,8 @@ static_library("test_support") {
|
||||
"../public/test/scoped_overscroll_modes.h",
|
||||
"../public/test/scoped_page_focus_override.cc",
|
||||
"../public/test/scoped_page_focus_override.h",
|
||||
"../public/test/scoped_time_zone.cc",
|
||||
"../public/test/scoped_time_zone.h",
|
||||
"../public/test/service_worker_host_interceptor.cc",
|
||||
"../public/test/service_worker_host_interceptor.h",
|
||||
"../public/test/service_worker_test_helpers.cc",
|
||||
|
@ -25,6 +25,8 @@
|
||||
#include "content/utility/utility_thread_impl.h"
|
||||
#include "sandbox/policy/sandbox.h"
|
||||
#include "services/tracing/public/cpp/trace_startup.h"
|
||||
#include "third_party/icu/source/common/unicode/unistr.h"
|
||||
#include "third_party/icu/source/i18n/unicode/timezone.h"
|
||||
|
||||
#if defined(OS_LINUX) || defined(OS_CHROMEOS)
|
||||
#include "content/utility/speech/speech_recognition_sandbox_hook_linux.h"
|
||||
@ -82,6 +84,13 @@ int UtilityMain(const MainFunctionParams& parameters) {
|
||||
message_pump_type = base::MessagePumpType::IO;
|
||||
#endif // defined(OS_FUCHSIA)
|
||||
|
||||
if (parameters.command_line.HasSwitch(switches::kTimeZoneForTesting)) {
|
||||
std::string time_zone = parameters.command_line.GetSwitchValueASCII(
|
||||
switches::kTimeZoneForTesting);
|
||||
icu::TimeZone::adoptDefault(
|
||||
icu::TimeZone::createTimeZone(icu::UnicodeString(time_zone.c_str())));
|
||||
}
|
||||
|
||||
// The main task executor of the utility process.
|
||||
base::SingleThreadTaskExecutor main_thread_task_executor(message_pump_type);
|
||||
base::PlatformThread::SetName("CrUtilityMain");
|
||||
|
Reference in New Issue
Block a user