Commit 59e8a565 authored by Patrick Monette's avatar Patrick Monette Committed by Commit Bot

Make IntensiveWakeUpThrottlingTest resilient to other console messages

Bug: 1075553
Change-Id: I538dc87123f87bc1688cb12eba8d3b9922815a8a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2357089Reviewed-by: default avatarAlexander Timin <altimin@chromium.org>
Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Commit-Queue: Patrick Monette <pmonette@chromium.org>
Cr-Commit-Position: refs/heads/master@{#799298}
parent 6ca90d12
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
// found in LICENSE file. // found in LICENSE file.
#include "base/numerics/safe_conversions.h" #include "base/numerics/safe_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_feature_list.h" #include "base/test/scoped_feature_list.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -28,6 +27,11 @@ namespace blink { ...@@ -28,6 +27,11 @@ namespace blink {
constexpr auto kDefaultThrottledWakeUpInterval = constexpr auto kDefaultThrottledWakeUpInterval =
base::TimeDelta::FromSeconds(1); base::TimeDelta::FromSeconds(1);
// This test suite relies on messages being posted to the console. In order to
// be resilient against messages not posted by this specific test suite, a small
// prefix is used to allowed filtering.
constexpr char kTestConsoleMessagePrefix[] = "[ThrottlingTest]";
// A SimTest with mock time. // A SimTest with mock time.
class ThrottlingTestBase : public SimTest { class ThrottlingTestBase : public SimTest {
public: public:
...@@ -41,6 +45,32 @@ class ThrottlingTestBase : public SimTest { ...@@ -41,6 +45,32 @@ class ThrottlingTestBase : public SimTest {
platform_->NowTicks()); platform_->NowTicks());
} }
String BuildTimerConsoleMessage(String suffix = String()) {
String message(kTestConsoleMessagePrefix);
message = message + " Timer called";
if (!suffix.IsNull())
message + " " + suffix;
return message;
}
// Returns a filtered copy of console messages where items not prefixed with
// |kTestConsoleMessagePrefix| are removed.
Vector<String> FilteredConsoleMessages() {
Vector<String> result = ConsoleMessages();
result.erase(
std::remove_if(result.begin(), result.end(),
[](const String& element) {
return !element.StartsWith(kTestConsoleMessagePrefix);
}),
result.end());
return result;
}
ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler> ScopedTestingPlatformSupport<TestingPlatformSupportWithMockScheduler>
platform_; platform_;
}; };
...@@ -59,15 +89,17 @@ TEST_F(DisableBackgroundThrottlingIsRespectedTest, ...@@ -59,15 +89,17 @@ TEST_F(DisableBackgroundThrottlingIsRespectedTest,
LoadURL("https://example.com/"); LoadURL("https://example.com/");
const String console_message = BuildTimerConsoleMessage();
main_resource.Complete( main_resource.Complete(
"(<script>" String::Format("(<script>"
" function f(repetitions) {" " function f(repetitions) {"
" if (repetitions == 0) return;" " if (repetitions == 0) return;"
" console.log('called f');" " console.log('%s');"
" setTimeout(f, 10, repetitions - 1);" " setTimeout(f, 10, repetitions - 1);"
" }" " }"
" f(5);" " f(5);"
"</script>)"); "</script>)",
console_message.Utf8().c_str()));
GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false); GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false);
...@@ -75,8 +107,9 @@ TEST_F(DisableBackgroundThrottlingIsRespectedTest, ...@@ -75,8 +107,9 @@ TEST_F(DisableBackgroundThrottlingIsRespectedTest,
// with throttling disabled. // with throttling disabled.
platform_->RunForPeriod(base::TimeDelta::FromSeconds(1)); platform_->RunForPeriod(base::TimeDelta::FromSeconds(1));
EXPECT_THAT(ConsoleMessages(), ElementsAre("called f", "called f", "called f", EXPECT_THAT(FilteredConsoleMessages(),
"called f", "called f")); ElementsAre(console_message, console_message, console_message,
console_message, console_message));
} }
class BackgroundPageThrottlingTest : public ThrottlingTestBase {}; class BackgroundPageThrottlingTest : public ThrottlingTestBase {};
...@@ -86,22 +119,24 @@ TEST_F(BackgroundPageThrottlingTest, TimersThrottledInBackgroundPage) { ...@@ -86,22 +119,24 @@ TEST_F(BackgroundPageThrottlingTest, TimersThrottledInBackgroundPage) {
LoadURL("https://example.com/"); LoadURL("https://example.com/");
const String console_message = BuildTimerConsoleMessage();
main_resource.Complete( main_resource.Complete(
"(<script>" String::Format("(<script>"
" function f(repetitions) {" " function f(repetitions) {"
" if (repetitions == 0) return;" " if (repetitions == 0) return;"
" console.log('called f');" " console.log('%s');"
" setTimeout(f, 10, repetitions - 1);" " setTimeout(f, 10, repetitions - 1);"
" }" " }"
" setTimeout(f, 10, 50);" " setTimeout(f, 10, 50);"
"</script>)"); "</script>)",
console_message.Utf8().c_str()));
GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false); GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false);
// Make sure that we run no more than one task a second. // Make sure that we run no more than one task a second.
platform_->RunForPeriod(base::TimeDelta::FromSeconds(3)); platform_->RunForPeriod(base::TimeDelta::FromSeconds(3));
EXPECT_THAT(ConsoleMessages(), EXPECT_THAT(FilteredConsoleMessages(),
ElementsAre("called f", "called f", "called f")); ElementsAre(console_message, console_message, console_message));
} }
// Same test as above, but using timeout=0. // Same test as above, but using timeout=0.
...@@ -111,15 +146,17 @@ TEST_F(BackgroundPageThrottlingTest, ...@@ -111,15 +146,17 @@ TEST_F(BackgroundPageThrottlingTest,
LoadURL("https://example.com/"); LoadURL("https://example.com/");
const String console_message = BuildTimerConsoleMessage();
main_resource.Complete( main_resource.Complete(
"(<script>" String::Format("(<script>"
" function f(repetitions) {" " function f(repetitions) {"
" if (repetitions == 0) return;" " if (repetitions == 0) return;"
" console.log('called f');" " console.log('%s');"
" setTimeout(f, 0, repetitions - 1);" " setTimeout(f, 0, repetitions - 1);"
" }" " }"
" setTimeout(f, 0, 50);" " setTimeout(f, 0, 50);"
"</script>)"); "</script>)",
console_message.Utf8().c_str()));
GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false); GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false);
...@@ -128,8 +165,9 @@ TEST_F(BackgroundPageThrottlingTest, ...@@ -128,8 +165,9 @@ TEST_F(BackgroundPageThrottlingTest,
// throttled wake up is 3ms. Therefore, at the 2 first wake ups, the timer // throttled wake up is 3ms. Therefore, at the 2 first wake ups, the timer
// runs twice. At the third wake up, it runs once. // runs twice. At the third wake up, it runs once.
platform_->RunForPeriod(base::TimeDelta::FromSeconds(3)); platform_->RunForPeriod(base::TimeDelta::FromSeconds(3));
EXPECT_THAT(ConsoleMessages(), ElementsAre("called f", "called f", "called f", EXPECT_THAT(FilteredConsoleMessages(),
"called f", "called f")); ElementsAre(console_message, console_message, console_message,
console_message, console_message));
} }
namespace { namespace {
...@@ -155,25 +193,34 @@ class OptOutZeroTimeoutFromThrottlingTest : public ThrottlingTestBase { ...@@ -155,25 +193,34 @@ class OptOutZeroTimeoutFromThrottlingTest : public ThrottlingTestBase {
TEST_F(OptOutZeroTimeoutFromThrottlingTest, WithoutNesting) { TEST_F(OptOutZeroTimeoutFromThrottlingTest, WithoutNesting) {
SimRequest main_resource("https://example.com/", "text/html"); SimRequest main_resource("https://example.com/", "text/html");
LoadURL("https://example.com/"); LoadURL("https://example.com/");
main_resource.Complete(
String timeout_0_message = BuildTimerConsoleMessage("0");
String timeout_minus_1_message = BuildTimerConsoleMessage("-1");
String timeout_5_message = BuildTimerConsoleMessage("5");
main_resource.Complete(String::Format(
"<script>" "<script>"
" setTimeout(function() {" " setTimeout(function() {"
" setTimeout(function() { console.log('setTimeout 0'); }, 0);" " setTimeout(function() { console.log('%s'); }, 0);"
" setTimeout(function() { console.log('setTimeout -1'); }, -1);" " setTimeout(function() { console.log('%s'); }, -1);"
" setTimeout(function() { console.log('setTimeout 5'); }, 5);" " setTimeout(function() { console.log('%s'); }, 5);"
" }, 1000);" " }, 1000);"
"</script>"); "</script>",
timeout_0_message.Utf8().c_str(), timeout_minus_1_message.Utf8().c_str(),
timeout_5_message.Utf8().c_str()));
GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false); GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false);
platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1001)); platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1001));
EXPECT_THAT(ConsoleMessages(), ElementsAre("setTimeout 0", "setTimeout -1")); EXPECT_THAT(FilteredConsoleMessages(),
ElementsAre(timeout_0_message, timeout_minus_1_message));
platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(998)); platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(998));
EXPECT_THAT(ConsoleMessages(), ElementsAre("setTimeout 0", "setTimeout -1")); EXPECT_THAT(FilteredConsoleMessages(),
ElementsAre(timeout_0_message, timeout_minus_1_message));
platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1)); platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1));
EXPECT_THAT(ConsoleMessages(), EXPECT_THAT(FilteredConsoleMessages(),
ElementsAre("setTimeout 0", "setTimeout -1", "setTimeout 5")); ElementsAre(timeout_0_message, timeout_minus_1_message,
timeout_5_message));
} }
// Verify that in a hidden page, when the kOptOutZeroTimeoutTimersFromThrottling // Verify that in a hidden page, when the kOptOutZeroTimeoutTimersFromThrottling
...@@ -182,29 +229,32 @@ TEST_F(OptOutZeroTimeoutFromThrottlingTest, WithoutNesting) { ...@@ -182,29 +229,32 @@ TEST_F(OptOutZeroTimeoutFromThrottlingTest, WithoutNesting) {
TEST_F(OptOutZeroTimeoutFromThrottlingTest, SetTimeoutNesting) { TEST_F(OptOutZeroTimeoutFromThrottlingTest, SetTimeoutNesting) {
SimRequest main_resource("https://example.com/", "text/html"); SimRequest main_resource("https://example.com/", "text/html");
LoadURL("https://example.com/"); LoadURL("https://example.com/");
const String console_message = BuildTimerConsoleMessage();
main_resource.Complete( main_resource.Complete(
"<script>" String::Format("<script>"
" function f(repetitions) {" " function f(repetitions) {"
" if (repetitions == 0) return;" " if (repetitions == 0) return;"
" console.log('called f');" " console.log('%s');"
" setTimeout(f, 0, repetitions - 1);" " setTimeout(f, 0, repetitions - 1);"
" }" " }"
" setTimeout(f, 0, 50);" " setTimeout(f, 0, 50);"
"</script>"); "</script>",
console_message.Utf8().c_str()));
GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false); GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false);
platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1)); platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1));
EXPECT_THAT(ConsoleMessages(), Vector<String>(1, "called f")); EXPECT_THAT(FilteredConsoleMessages(), Vector<String>(1, console_message));
platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1)); platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1));
EXPECT_THAT(ConsoleMessages(), Vector<String>(2, "called f")); EXPECT_THAT(FilteredConsoleMessages(), Vector<String>(2, console_message));
platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1)); platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1));
EXPECT_THAT(ConsoleMessages(), Vector<String>(3, "called f")); EXPECT_THAT(FilteredConsoleMessages(), Vector<String>(3, console_message));
platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1)); platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1));
EXPECT_THAT(ConsoleMessages(), Vector<String>(4, "called f")); EXPECT_THAT(FilteredConsoleMessages(), Vector<String>(4, console_message));
platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(995)); platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(995));
EXPECT_THAT(ConsoleMessages(), Vector<String>(4, "called f")); EXPECT_THAT(FilteredConsoleMessages(), Vector<String>(4, console_message));
platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1)); platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1));
EXPECT_THAT(ConsoleMessages(), Vector<String>(5, "called f")); EXPECT_THAT(FilteredConsoleMessages(), Vector<String>(5, console_message));
} }
// Verify that in a hidden page, when the kOptOutZeroTimeoutTimersFromThrottling // Verify that in a hidden page, when the kOptOutZeroTimeoutTimersFromThrottling
...@@ -213,30 +263,33 @@ TEST_F(OptOutZeroTimeoutFromThrottlingTest, SetTimeoutNesting) { ...@@ -213,30 +263,33 @@ TEST_F(OptOutZeroTimeoutFromThrottlingTest, SetTimeoutNesting) {
TEST_F(OptOutZeroTimeoutFromThrottlingTest, SetIntervalNesting) { TEST_F(OptOutZeroTimeoutFromThrottlingTest, SetIntervalNesting) {
SimRequest main_resource("https://example.com/", "text/html"); SimRequest main_resource("https://example.com/", "text/html");
LoadURL("https://example.com/"); LoadURL("https://example.com/");
const String console_message = BuildTimerConsoleMessage();
main_resource.Complete( main_resource.Complete(
"<script>" String::Format("<script>"
" function f() {" " function f() {"
" if (repetitions == 0) clearInterval(interval_id);" " if (repetitions == 0) clearInterval(interval_id);"
" console.log('called f');" " console.log('%s');"
" repetitions = repetitions - 1;" " repetitions = repetitions - 1;"
" }" " }"
" var repetitions = 50;" " var repetitions = 50;"
" var interval_id = setInterval(f, 0);" " var interval_id = setInterval(f, 0);"
"</script>"); "</script>",
console_message.Utf8().c_str()));
GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false); GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false);
platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1)); platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1));
EXPECT_THAT(ConsoleMessages(), Vector<String>(1, "called f")); EXPECT_THAT(FilteredConsoleMessages(), Vector<String>(1, console_message));
platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1)); platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1));
EXPECT_THAT(ConsoleMessages(), Vector<String>(2, "called f")); EXPECT_THAT(FilteredConsoleMessages(), Vector<String>(2, console_message));
platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1)); platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1));
EXPECT_THAT(ConsoleMessages(), Vector<String>(3, "called f")); EXPECT_THAT(FilteredConsoleMessages(), Vector<String>(3, console_message));
platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1)); platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1));
EXPECT_THAT(ConsoleMessages(), Vector<String>(4, "called f")); EXPECT_THAT(FilteredConsoleMessages(), Vector<String>(4, console_message));
platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(995)); platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(995));
EXPECT_THAT(ConsoleMessages(), Vector<String>(4, "called f")); EXPECT_THAT(FilteredConsoleMessages(), Vector<String>(4, console_message));
platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1)); platform_->RunForPeriod(base::TimeDelta::FromMilliseconds(1));
EXPECT_THAT(ConsoleMessages(), Vector<String>(5, "called f")); EXPECT_THAT(FilteredConsoleMessages(), Vector<String>(5, console_message));
} }
namespace { namespace {
...@@ -250,16 +303,17 @@ class IntensiveWakeUpThrottlingTest : public ThrottlingTestBase { ...@@ -250,16 +303,17 @@ class IntensiveWakeUpThrottlingTest : public ThrottlingTestBase {
{features::kStopInBackground}); {features::kStopInBackground});
} }
void TestNoIntensiveThrotlingOnTitleOrFaviconUpdate() { void TestNoIntensiveThrotlingOnTitleOrFaviconUpdate(
const String& console_message) {
// The page does not attempt to run onTimer in the first 5 minutes. // The page does not attempt to run onTimer in the first 5 minutes.
platform_->RunForPeriod(base::TimeDelta::FromMinutes(5)); platform_->RunForPeriod(base::TimeDelta::FromMinutes(5));
EXPECT_THAT(ConsoleMessages(), ElementsAre()); EXPECT_THAT(FilteredConsoleMessages(), ElementsAre());
// At 5 minutes, a timer fires to run the afterFiveMinutes() function. // At 5 minutes, a timer fires to run the afterFiveMinutes() function.
// This function does not communicate in the background, so the intensive // This function does not communicate in the background, so the intensive
// throttling policy applies and onTimer() can only run after 1 minute. // throttling policy applies and onTimer() can only run after 1 minute.
platform_->RunForPeriod(base::TimeDelta::FromMinutes(1)); platform_->RunForPeriod(base::TimeDelta::FromMinutes(1));
EXPECT_THAT(ConsoleMessages(), ElementsAre("called onTimer")); EXPECT_THAT(FilteredConsoleMessages(), ElementsAre(console_message));
ConsoleMessages().clear(); ConsoleMessages().clear();
...@@ -274,8 +328,8 @@ class IntensiveWakeUpThrottlingTest : public ThrottlingTestBase { ...@@ -274,8 +328,8 @@ class IntensiveWakeUpThrottlingTest : public ThrottlingTestBase {
Vector<String> expected_ouput( Vector<String> expected_ouput(
base::ClampFloor<wtf_size_t>(kTimeUntilNextCheck / base::ClampFloor<wtf_size_t>(kTimeUntilNextCheck /
kDefaultThrottledWakeUpInterval), kDefaultThrottledWakeUpInterval),
"called onTimer"); console_message);
EXPECT_THAT(ConsoleMessages(), expected_ouput); EXPECT_THAT(FilteredConsoleMessages(), expected_ouput);
} }
private: private:
...@@ -310,10 +364,10 @@ constexpr char kCommunicateThroughFavisonScript[] = ...@@ -310,10 +364,10 @@ constexpr char kCommunicateThroughFavisonScript[] =
// A script that schedules a timer with a long delay that is not aligned on the // A script that schedules a timer with a long delay that is not aligned on the
// intensive throttling wake up interval. // intensive throttling wake up interval.
constexpr char kLongUnalignedTimerScript[] = constexpr char kLongUnalignedTimerScriptTemplate[] =
"<script>" "<script>"
" function onTimer() {" " function onTimer() {"
" console.log('called onTimer');" " console.log('%s');"
" }" " }"
" setTimeout(onTimer, 342 * 1000);" " setTimeout(onTimer, 342 * 1000);"
"</script>"; "</script>";
...@@ -325,7 +379,8 @@ constexpr base::TimeDelta kLongUnalignedTimerDelay = ...@@ -325,7 +379,8 @@ constexpr base::TimeDelta kLongUnalignedTimerDelay =
// Use to build a web-page ready to test intensive javascript throttling. // Use to build a web-page ready to test intensive javascript throttling.
// The page will differ in its definition of the maybeCommunicateInBackground() // The page will differ in its definition of the maybeCommunicateInBackground()
// function which has to be defined in a script passed in |communicate_script|. // function which has to be defined in a script passed in |communicate_script|.
String BuildRepeatingTimerPage(const char* communicate_script) { String BuildRepeatingTimerPage(const char* console_message,
const char* communicate_script) {
// A template for a page that waits 5 minutes on load then creates a timer // A template for a page that waits 5 minutes on load then creates a timer
// that reschedules itself 50 times with 10 ms delay. Contains the minimimal // that reschedules itself 50 times with 10 ms delay. Contains the minimimal
// page structure to simulate background communication with the user via title // page structure to simulate background communication with the user via title
...@@ -340,7 +395,7 @@ String BuildRepeatingTimerPage(const char* communicate_script) { ...@@ -340,7 +395,7 @@ String BuildRepeatingTimerPage(const char* communicate_script) {
"<script>" "<script>"
" function onTimer(repetitions) {" " function onTimer(repetitions) {"
" if (repetitions == 0) return;" " if (repetitions == 0) return;"
" console.log('called onTimer');" " console.log('%s');"
" maybeCommunicateInBackground();" " maybeCommunicateInBackground();"
" setTimeout(onTimer, 10, repetitions - 1);" " setTimeout(onTimer, 10, repetitions - 1);"
" }" " }"
...@@ -353,10 +408,8 @@ String BuildRepeatingTimerPage(const char* communicate_script) { ...@@ -353,10 +408,8 @@ String BuildRepeatingTimerPage(const char* communicate_script) {
"</body>" "</body>"
"</html>"; "</html>";
std::string page = return String::Format(kRepeatingTimerPageTemplate, console_message,
base::StringPrintf(kRepeatingTimerPageTemplate, communicate_script); communicate_script);
return {page.data(), page.size()};
} }
} // namespace } // namespace
...@@ -367,29 +420,32 @@ TEST_F(IntensiveWakeUpThrottlingTest, MainFrameTimer_ShortTimeout) { ...@@ -367,29 +420,32 @@ TEST_F(IntensiveWakeUpThrottlingTest, MainFrameTimer_ShortTimeout) {
SimRequest main_resource("https://example.com/", "text/html"); SimRequest main_resource("https://example.com/", "text/html");
LoadURL("https://example.com/"); LoadURL("https://example.com/");
const String console_message = BuildTimerConsoleMessage();
// Page does not communicate with the user. Normal intensive throttling // Page does not communicate with the user. Normal intensive throttling
// applies. // applies.
main_resource.Complete(BuildRepeatingTimerPage(kCommunicationNop)); main_resource.Complete(BuildRepeatingTimerPage(console_message.Utf8().c_str(),
kCommunicationNop));
GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false); GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false);
// No timer is scheduled in the 5 first minutes. // No timer is scheduled in the 5 first minutes.
platform_->RunForPeriod(base::TimeDelta::FromMinutes(5)); platform_->RunForPeriod(base::TimeDelta::FromMinutes(5));
EXPECT_THAT(ConsoleMessages(), ElementsAre()); EXPECT_THAT(FilteredConsoleMessages(), ElementsAre());
// After that, intensive throttling starts and there should be 1 wake up per // After that, intensive throttling starts and there should be 1 wake up per
// minute. // minute.
platform_->RunForPeriod(base::TimeDelta::FromMinutes(1)); platform_->RunForPeriod(base::TimeDelta::FromMinutes(1));
EXPECT_THAT(ConsoleMessages(), ElementsAre("called onTimer")); EXPECT_THAT(FilteredConsoleMessages(), ElementsAre(console_message));
// No tasks execute early. // No tasks execute early.
platform_->RunForPeriod(base::TimeDelta::FromSeconds(30)); platform_->RunForPeriod(base::TimeDelta::FromSeconds(30));
EXPECT_THAT(ConsoleMessages(), ElementsAre("called onTimer")); EXPECT_THAT(FilteredConsoleMessages(), ElementsAre(console_message));
// A minute after the last timer. // A minute after the last timer.
platform_->RunForPeriod(base::TimeDelta::FromSeconds(30)); platform_->RunForPeriod(base::TimeDelta::FromSeconds(30));
EXPECT_THAT(ConsoleMessages(), EXPECT_THAT(FilteredConsoleMessages(),
ElementsAre("called onTimer", "called onTimer")); ElementsAre(console_message, console_message));
} }
// Verify that a main frame timer that reposts itself with a 10 ms timeout runs // Verify that a main frame timer that reposts itself with a 10 ms timeout runs
...@@ -398,12 +454,14 @@ TEST_F(IntensiveWakeUpThrottlingTest, MainFrameTimer_ShortTimeout) { ...@@ -398,12 +454,14 @@ TEST_F(IntensiveWakeUpThrottlingTest, MainFrameTimer_ShortTimeout) {
TEST_F(IntensiveWakeUpThrottlingTest, MainFrameTimer_ShortTimeout_TitleUpdate) { TEST_F(IntensiveWakeUpThrottlingTest, MainFrameTimer_ShortTimeout_TitleUpdate) {
SimRequest main_resource("https://example.com/", "text/html"); SimRequest main_resource("https://example.com/", "text/html");
LoadURL("https://example.com/"); LoadURL("https://example.com/");
main_resource.Complete(
BuildRepeatingTimerPage(kCommunicateThroughTitleScript)); const String console_message = BuildTimerConsoleMessage();
main_resource.Complete(BuildRepeatingTimerPage(
console_message.Utf8().c_str(), kCommunicateThroughTitleScript));
GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false); GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false);
TestNoIntensiveThrotlingOnTitleOrFaviconUpdate(); TestNoIntensiveThrotlingOnTitleOrFaviconUpdate(console_message);
} }
// Verify that a main frame timer that reposts itself with a 10 ms timeout runs // Verify that a main frame timer that reposts itself with a 10 ms timeout runs
...@@ -413,12 +471,14 @@ TEST_F(IntensiveWakeUpThrottlingTest, ...@@ -413,12 +471,14 @@ TEST_F(IntensiveWakeUpThrottlingTest,
MainFrameTimer_ShortTimeout_FaviconUpdate) { MainFrameTimer_ShortTimeout_FaviconUpdate) {
SimRequest main_resource("https://example.com/", "text/html"); SimRequest main_resource("https://example.com/", "text/html");
LoadURL("https://example.com/"); LoadURL("https://example.com/");
main_resource.Complete(
BuildRepeatingTimerPage(kCommunicateThroughFavisonScript)); const String console_message = BuildTimerConsoleMessage();
main_resource.Complete(BuildRepeatingTimerPage(
console_message.Utf8().c_str(), kCommunicateThroughFavisonScript));
GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false); GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false);
TestNoIntensiveThrotlingOnTitleOrFaviconUpdate(); TestNoIntensiveThrotlingOnTitleOrFaviconUpdate(console_message);
} }
// Verify that a same-origin subframe timer that reposts itself with a 10 ms // Verify that a same-origin subframe timer that reposts itself with a 10 ms
...@@ -431,21 +491,24 @@ TEST_F(IntensiveWakeUpThrottlingTest, SameOriginSubFrameTimer_ShortTimeout) { ...@@ -431,21 +491,24 @@ TEST_F(IntensiveWakeUpThrottlingTest, SameOriginSubFrameTimer_ShortTimeout) {
// Run tasks to let the main frame request the iframe resource. It is not // Run tasks to let the main frame request the iframe resource. It is not
// possible to complete the iframe resource request before that. // possible to complete the iframe resource request before that.
platform_->RunUntilIdle(); platform_->RunUntilIdle();
subframe_resource.Complete(BuildRepeatingTimerPage(kCommunicationNop));
const String console_message = BuildTimerConsoleMessage();
subframe_resource.Complete(BuildRepeatingTimerPage(
console_message.Utf8().c_str(), kCommunicationNop));
GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false); GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false);
// No timer is scheduled in the 5 first minutes. // No timer is scheduled in the 5 first minutes.
platform_->RunForPeriod(base::TimeDelta::FromMinutes(5)); platform_->RunForPeriod(base::TimeDelta::FromMinutes(5));
EXPECT_THAT(ConsoleMessages(), ElementsAre()); EXPECT_THAT(FilteredConsoleMessages(), ElementsAre());
// After that, intensive throttling starts and there should be 1 wake up per // After that, intensive throttling starts and there should be 1 wake up per
// minute. // minute.
platform_->RunForPeriod(base::TimeDelta::FromMinutes(1)); platform_->RunForPeriod(base::TimeDelta::FromMinutes(1));
EXPECT_THAT(ConsoleMessages(), ElementsAre("called onTimer")); EXPECT_THAT(FilteredConsoleMessages(), ElementsAre(console_message));
platform_->RunForPeriod(base::TimeDelta::FromMinutes(1)); platform_->RunForPeriod(base::TimeDelta::FromMinutes(1));
EXPECT_THAT(ConsoleMessages(), EXPECT_THAT(FilteredConsoleMessages(),
ElementsAre("called onTimer", "called onTimer")); ElementsAre(console_message, console_message));
} }
// Verify that a cross-origin subframe timer that reposts itself with a 10 ms // Verify that a cross-origin subframe timer that reposts itself with a 10 ms
...@@ -460,21 +523,24 @@ TEST_F(IntensiveWakeUpThrottlingTest, CrossOriginSubFrameTimer_ShortTimeout) { ...@@ -460,21 +523,24 @@ TEST_F(IntensiveWakeUpThrottlingTest, CrossOriginSubFrameTimer_ShortTimeout) {
// Run tasks to let the main frame request the iframe resource. It is not // Run tasks to let the main frame request the iframe resource. It is not
// possible to complete the iframe resource request before that. // possible to complete the iframe resource request before that.
platform_->RunUntilIdle(); platform_->RunUntilIdle();
subframe_resource.Complete(BuildRepeatingTimerPage(kCommunicationNop));
const String console_message = BuildTimerConsoleMessage();
subframe_resource.Complete(BuildRepeatingTimerPage(
console_message.Utf8().c_str(), kCommunicationNop));
GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false); GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false);
// No timer is scheduled in the 5 first minutes. // No timer is scheduled in the 5 first minutes.
platform_->RunForPeriod(base::TimeDelta::FromMinutes(5)); platform_->RunForPeriod(base::TimeDelta::FromMinutes(5));
EXPECT_THAT(ConsoleMessages(), ElementsAre()); EXPECT_THAT(FilteredConsoleMessages(), ElementsAre());
// After that, intensive throttling starts and there should be 1 wake up per // After that, intensive throttling starts and there should be 1 wake up per
// minute. // minute.
platform_->RunForPeriod(base::TimeDelta::FromMinutes(1)); platform_->RunForPeriod(base::TimeDelta::FromMinutes(1));
EXPECT_THAT(ConsoleMessages(), ElementsAre("called onTimer")); EXPECT_THAT(FilteredConsoleMessages(), ElementsAre(console_message));
platform_->RunForPeriod(base::TimeDelta::FromMinutes(1)); platform_->RunForPeriod(base::TimeDelta::FromMinutes(1));
EXPECT_THAT(ConsoleMessages(), EXPECT_THAT(FilteredConsoleMessages(),
ElementsAre("called onTimer", "called onTimer")); ElementsAre(console_message, console_message));
} }
// Verify that a main frame timer with a long timeout runs at the desired run // Verify that a main frame timer with a long timeout runs at the desired run
...@@ -482,16 +548,19 @@ TEST_F(IntensiveWakeUpThrottlingTest, CrossOriginSubFrameTimer_ShortTimeout) { ...@@ -482,16 +548,19 @@ TEST_F(IntensiveWakeUpThrottlingTest, CrossOriginSubFrameTimer_ShortTimeout) {
TEST_F(IntensiveWakeUpThrottlingTest, MainFrameTimer_LongUnalignedTimeout) { TEST_F(IntensiveWakeUpThrottlingTest, MainFrameTimer_LongUnalignedTimeout) {
SimRequest main_resource("https://example.com/", "text/html"); SimRequest main_resource("https://example.com/", "text/html");
LoadURL("https://example.com/"); LoadURL("https://example.com/");
main_resource.Complete(kLongUnalignedTimerScript);
const String console_message = BuildTimerConsoleMessage();
main_resource.Complete(String::Format(kLongUnalignedTimerScriptTemplate,
console_message.Utf8().c_str()));
GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false); GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false);
platform_->RunForPeriod(kLongUnalignedTimerDelay - platform_->RunForPeriod(kLongUnalignedTimerDelay -
base::TimeDelta::FromSeconds(1)); base::TimeDelta::FromSeconds(1));
EXPECT_THAT(ConsoleMessages(), ElementsAre()); EXPECT_THAT(FilteredConsoleMessages(), ElementsAre());
platform_->RunForPeriod(base::TimeDelta::FromSeconds(1)); platform_->RunForPeriod(base::TimeDelta::FromSeconds(1));
EXPECT_THAT(ConsoleMessages(), ElementsAre("called onTimer")); EXPECT_THAT(FilteredConsoleMessages(), ElementsAre(console_message));
} }
// Verify that a same-origin subframe timer with a long timeout runs at the // Verify that a same-origin subframe timer with a long timeout runs at the
...@@ -505,16 +574,19 @@ TEST_F(IntensiveWakeUpThrottlingTest, ...@@ -505,16 +574,19 @@ TEST_F(IntensiveWakeUpThrottlingTest,
// Run tasks to let the main frame request the iframe resource. It is not // Run tasks to let the main frame request the iframe resource. It is not
// possible to complete the iframe resource request before that. // possible to complete the iframe resource request before that.
platform_->RunUntilIdle(); platform_->RunUntilIdle();
subframe_resource.Complete(kLongUnalignedTimerScript);
const String console_message = BuildTimerConsoleMessage();
subframe_resource.Complete(String::Format(kLongUnalignedTimerScriptTemplate,
console_message.Utf8().c_str()));
GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false); GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false);
platform_->RunForPeriod(kLongUnalignedTimerDelay - platform_->RunForPeriod(kLongUnalignedTimerDelay -
base::TimeDelta::FromSeconds(1)); base::TimeDelta::FromSeconds(1));
EXPECT_THAT(ConsoleMessages(), ElementsAre()); EXPECT_THAT(FilteredConsoleMessages(), ElementsAre());
platform_->RunForPeriod(base::TimeDelta::FromSeconds(1)); platform_->RunForPeriod(base::TimeDelta::FromSeconds(1));
EXPECT_THAT(ConsoleMessages(), ElementsAre("called onTimer")); EXPECT_THAT(FilteredConsoleMessages(), ElementsAre(console_message));
} }
// Verify that a cross-origin subframe timer with a long timeout runs at an // Verify that a cross-origin subframe timer with a long timeout runs at an
...@@ -531,16 +603,19 @@ TEST_F(IntensiveWakeUpThrottlingTest, ...@@ -531,16 +603,19 @@ TEST_F(IntensiveWakeUpThrottlingTest,
// Run tasks to let the main frame request the iframe resource. It is not // Run tasks to let the main frame request the iframe resource. It is not
// possible to complete the iframe resource request before that. // possible to complete the iframe resource request before that.
platform_->RunUntilIdle(); platform_->RunUntilIdle();
subframe_resource.Complete(kLongUnalignedTimerScript);
const String console_message = BuildTimerConsoleMessage();
subframe_resource.Complete(String::Format(kLongUnalignedTimerScriptTemplate,
console_message.Utf8().c_str()));
GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false); GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false);
platform_->RunForPeriod(base::TimeDelta::FromSeconds(342)); platform_->RunForPeriod(base::TimeDelta::FromSeconds(342));
EXPECT_THAT(ConsoleMessages(), ElementsAre()); EXPECT_THAT(FilteredConsoleMessages(), ElementsAre());
// Fast-forward to the next aligned time. // Fast-forward to the next aligned time.
platform_->RunForPeriod(base::TimeDelta::FromSeconds(18)); platform_->RunForPeriod(base::TimeDelta::FromSeconds(18));
EXPECT_THAT(ConsoleMessages(), ElementsAre("called onTimer")); EXPECT_THAT(FilteredConsoleMessages(), ElementsAre(console_message));
} }
// Verify that if both the main frame and a cross-origin frame schedule a timer // Verify that if both the main frame and a cross-origin frame schedule a timer
...@@ -553,23 +628,28 @@ TEST_F(IntensiveWakeUpThrottlingTest, ...@@ -553,23 +628,28 @@ TEST_F(IntensiveWakeUpThrottlingTest,
SimRequest subframe_resource("https://cross-origin.example.com/iframe.html", SimRequest subframe_resource("https://cross-origin.example.com/iframe.html",
"text/html"); "text/html");
LoadURL("https://example.com/"); LoadURL("https://example.com/");
const String console_message = BuildTimerConsoleMessage();
const String script = String::Format(kLongUnalignedTimerScriptTemplate,
console_message.Utf8().c_str());
main_resource.Complete( main_resource.Complete(
WTF::String(kLongUnalignedTimerScript) + script +
"<iframe src=\"https://cross-origin.example.com/iframe.html\" />"); "<iframe src=\"https://cross-origin.example.com/iframe.html\" />");
// Run tasks to let the main frame request the iframe resource. It is not // Run tasks to let the main frame request the iframe resource. It is not
// possible to complete the iframe resource request before that. // possible to complete the iframe resource request before that.
platform_->RunUntilIdle(); platform_->RunUntilIdle();
subframe_resource.Complete(kLongUnalignedTimerScript); subframe_resource.Complete(script);
GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false); GetDocument().GetPage()->GetPageScheduler()->SetPageVisible(false);
platform_->RunForPeriod(base::TimeDelta::FromSeconds(342)); platform_->RunForPeriod(base::TimeDelta::FromSeconds(342));
EXPECT_THAT(ConsoleMessages(), ElementsAre("called onTimer")); EXPECT_THAT(FilteredConsoleMessages(), ElementsAre(console_message));
// Fast-forward to the next aligned time. // Fast-forward to the next aligned time.
platform_->RunForPeriod(base::TimeDelta::FromSeconds(18)); platform_->RunForPeriod(base::TimeDelta::FromSeconds(18));
EXPECT_THAT(ConsoleMessages(), EXPECT_THAT(FilteredConsoleMessages(),
ElementsAre("called onTimer", "called onTimer")); ElementsAre(console_message, console_message));
} }
} // namespace blink } // namespace blink
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