Reland "Add MOCK_TIME mode to ScopedTaskEnvironment :)"

Reland follow-up nits CL along with it this time

Initial CL made ProfileShortcutManagerTest flaky.

Running them locally it turns out they mostly pass but they spew out
a whole lot of "ScopedTaskEnvironment::RunUntilIdle() appears to be
stuck in infinite loop" warning logs (which were added in this CL).
This is because the ongoing tasks that prevent DisallowRunTasks()
are slow and the main thread goes into a busy loop trying to
DisallowRunTasks(). These tests being already slow, these prints
pushed them over the edge (I think).

DisallowRunTasks() was tweaked in this reland to not return until
either one task completes or a short timeout expires (see code
comments for detailed reasoning).

This also allows removing the YieldCurrentThread() logic in the original
CL as busy-looping is avoided altogether.

The LOG statement trying to catch infinite RunUntilIdle() was also
removed as it wasn't really the nice place for it (it should a counter
in the RunOrSkipTask() override if we want it later).

This is a reland of 5e2df665dfce3a3aceb4852d90ad55d31fc36f20
Original change's description:
> Add MOCK_TIME mode to ScopedTaskEnvironment :)
> Taking advantage of the new kBoundToThread mode on
> TestMockTimeTaskRunner.
> This change also required tweaking the
> ScopedTaskEnvironment::RunUntilIdle() logic as RunLoop().Run() on
> TestMockTimeTaskRunner results in advancing time when there's no
> request to quit-when-idle which is undesired here. New logic gets rid
> of need for |on_queue_empty_closure_| and I think is simpler overall.
> As of patch set 20, this new RunUntilIdle() logic also avoids using
> TaskScheduler::FlushForTesting() as that can result in hangs should a
> TaskScheduler task synchronously block on the main thread.
> (IWYU fixes)
> Bug: 708584
> Change-Id: I76ba55ec64d398151420379d3fcdcd5186fbceb8
> Reviewed-on:
> Commit-Queue: Gabriel Charette <>
> Reviewed-by: Gabriel Charette <>
> Reviewed-by: Kyle Horimoto <>
> Reviewed-by: Fran├žois Doray <>
> Cr-Commit-Position: refs/heads/master@{#518433}

TEST=Repro'ed locally and fixed. (IWYU fixes)

Bug: 708584, 787683
Change-Id: I511c159ccbb608aa54394a4f35c40ad14697196b
Commit-Queue: Gabriel Charette <>
Reviewed-by: Fran├žois Doray <>
Reviewed-by: Gabriel Charette <>
Cr-Commit-Position: refs/heads/master@{#519032}
8 files changed