1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/performance_manager/scenario_api/performance_scenario_observer.h"
#include <atomic>
#include <memory>
#include "base/barrier_closure.h"
#include "base/containers/enum_set.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/scoped_multi_source_observation.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/task_environment.h"
#include "components/performance_manager/scenario_api/performance_scenario_memory.h"
#include "components/performance_manager/scenario_api/performance_scenario_test_support.h"
#include "components/performance_manager/scenario_api/performance_scenarios.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace performance_scenarios {
namespace {
using ::testing::_;
class MockPerformanceScenarioObserver : public PerformanceScenarioObserver {
public:
MOCK_METHOD(void,
OnLoadingScenarioChanged,
(ScenarioScope scope,
LoadingScenario old_scenario,
LoadingScenario new_scenario),
(override));
MOCK_METHOD(void,
OnInputScenarioChanged,
(ScenarioScope scope,
InputScenario old_scenario,
InputScenario new_scenario),
(override));
};
using StrictMockPerformanceScenarioObserver =
::testing::StrictMock<MockPerformanceScenarioObserver>;
class MockMatchingScenarioObserver : public MatchingScenarioObserver {
public:
using MatchingScenarioObserver::MatchingScenarioObserver;
MOCK_METHOD(void,
OnScenarioMatchChanged,
(ScenarioScope scope, bool matches_pattern),
(override));
};
using StrictMockMatchingScenarioObserver =
::testing::StrictMock<MockMatchingScenarioObserver>;
class PerformanceScenarioObserverTest : public ::testing::Test {
public:
void SetUp() override {
test_helper_ = PerformanceScenarioTestHelper::CreateWithoutMapping();
ASSERT_TRUE(test_helper_);
}
protected:
std::unique_ptr<PerformanceScenarioTestHelper> test_helper_;
base::test::TaskEnvironment task_env_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
};
TEST_F(PerformanceScenarioObserverTest, GetForScope) {
EXPECT_FALSE(PerformanceScenarioObserverList::GetForScope(
ScenarioScope::kCurrentProcess));
EXPECT_FALSE(
PerformanceScenarioObserverList::GetForScope(ScenarioScope::kGlobal));
{
ScopedReadOnlyScenarioMemory scoped_process_memory(
ScenarioScope::kCurrentProcess, test_helper_->GetReadOnlyScenarioRegion(
ScenarioScope::kCurrentProcess));
EXPECT_TRUE(PerformanceScenarioObserverList::GetForScope(
ScenarioScope::kCurrentProcess));
EXPECT_FALSE(
PerformanceScenarioObserverList::GetForScope(ScenarioScope::kGlobal));
{
ScopedReadOnlyScenarioMemory scoped_global_memory(
ScenarioScope::kGlobal,
test_helper_->GetReadOnlyScenarioRegion(ScenarioScope::kGlobal));
EXPECT_TRUE(PerformanceScenarioObserverList::GetForScope(
ScenarioScope::kCurrentProcess));
EXPECT_TRUE(
PerformanceScenarioObserverList::GetForScope(ScenarioScope::kGlobal));
}
EXPECT_TRUE(PerformanceScenarioObserverList::GetForScope(
ScenarioScope::kCurrentProcess));
EXPECT_FALSE(
PerformanceScenarioObserverList::GetForScope(ScenarioScope::kGlobal));
}
EXPECT_FALSE(PerformanceScenarioObserverList::GetForScope(
ScenarioScope::kCurrentProcess));
EXPECT_FALSE(
PerformanceScenarioObserverList::GetForScope(ScenarioScope::kGlobal));
}
TEST_F(PerformanceScenarioObserverTest, NotifyOnChange) {
// Update the process scenario state before creating the ObserverList, to
// make sure the state tracking doesn't depend on the state starting at
// kNoPageLoading.
test_helper_->SetLoadingScenario(ScenarioScope::kCurrentProcess,
LoadingScenario::kFocusedPageLoading);
// Map in scenario memory.
ScopedReadOnlyScenarioMemory scoped_process_memory(
ScenarioScope::kCurrentProcess,
test_helper_->GetReadOnlyScenarioRegion(ScenarioScope::kCurrentProcess));
ScopedReadOnlyScenarioMemory scoped_global_memory(
ScenarioScope::kGlobal,
test_helper_->GetReadOnlyScenarioRegion(ScenarioScope::kGlobal));
EXPECT_FALSE(CurrentScenariosMatch(ScenarioScope::kCurrentProcess,
kDefaultIdleScenarios));
EXPECT_TRUE(
CurrentScenariosMatch(ScenarioScope::kGlobal, kDefaultIdleScenarios));
// Create a PerformanceScenarioObserver and two MatchingScenarioObservers with
// different patterns, and have them observe both scopes.
StrictMockPerformanceScenarioObserver mock_observer;
base::ScopedMultiSourceObservation<PerformanceScenarioObserverList,
PerformanceScenarioObserver>
scoped_observation(&mock_observer);
StrictMockMatchingScenarioObserver mock_idle_observer(kDefaultIdleScenarios);
base::ScopedMultiSourceObservation<PerformanceScenarioObserverList,
MatchingScenarioObserver>
idle_observation(&mock_idle_observer);
// This observer won't be notified on LoadingScenario changes because it only
// watches the InputScenario.
StrictMockMatchingScenarioObserver mock_input_only_observer(
ScenarioPattern{.input = {InputScenario::kNoInput}});
base::ScopedMultiSourceObservation<PerformanceScenarioObserverList,
MatchingScenarioObserver>
input_only_observation(&mock_input_only_observer);
for (ScenarioScope scope : ScenarioScopes::All()) {
auto observer_list = PerformanceScenarioObserverList::GetForScope(scope);
scoped_observation.AddObservation(observer_list.get());
idle_observation.AddObservation(observer_list.get());
input_only_observation.AddObservation(observer_list.get());
}
// Utility function that waits for all mock expectations to be filled. The
// test should invoke `task_env_.QuitClosure()` when all expected observer
// methods are called.
auto wait_for_expectations = [&] {
using ::testing::Mock;
task_env_.RunUntilQuit();
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_observer));
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_idle_observer));
EXPECT_TRUE(Mock::VerifyAndClearExpectations(&mock_input_only_observer));
};
// Toggle process loading scenario, then global loading scenario. 2 observers
// will fire for each of 2 scopes.
auto quit_closure = base::BarrierClosure(4, task_env_.QuitClosure());
// kCurrentProcess scope transitions from kFocusedPageLoading (non-idle) ->
// kBackgroundPageLoading (idle).
EXPECT_CALL(mock_observer,
OnLoadingScenarioChanged(ScenarioScope::kCurrentProcess,
LoadingScenario::kFocusedPageLoading,
LoadingScenario::kBackgroundPageLoading))
.WillOnce(base::test::RunClosure(quit_closure));
EXPECT_CALL(mock_idle_observer,
OnScenarioMatchChanged(ScenarioScope::kCurrentProcess, true))
.WillOnce(base::test::RunClosure(quit_closure));
// kGlobal scope transitions from kNoPageLoading (idle) -> kVisiblePageLoading
// (non-idle).
EXPECT_CALL(mock_observer,
OnLoadingScenarioChanged(ScenarioScope::kGlobal,
LoadingScenario::kNoPageLoading,
LoadingScenario::kVisiblePageLoading))
.WillOnce(base::test::RunClosure(quit_closure));
EXPECT_CALL(mock_idle_observer,
OnScenarioMatchChanged(ScenarioScope::kGlobal, false))
.WillOnce(base::test::RunClosure(quit_closure));
test_helper_->SetLoadingScenario(ScenarioScope::kCurrentProcess,
LoadingScenario::kBackgroundPageLoading);
test_helper_->SetLoadingScenario(ScenarioScope::kGlobal,
LoadingScenario::kVisiblePageLoading);
wait_for_expectations();
// Toggle process scenario again without changing global scenario.
// kBackgroundPageLoading (idle) -> kFocusedPageLoading (non-idle).
quit_closure = base::BarrierClosure(2, task_env_.QuitClosure());
EXPECT_CALL(mock_observer,
OnLoadingScenarioChanged(ScenarioScope::kCurrentProcess,
LoadingScenario::kBackgroundPageLoading,
LoadingScenario::kFocusedPageLoading))
.WillOnce(base::test::RunClosure(quit_closure));
EXPECT_CALL(mock_idle_observer,
OnScenarioMatchChanged(ScenarioScope::kCurrentProcess, false))
.WillOnce(base::test::RunClosure(quit_closure));
test_helper_->SetLoadingScenario(ScenarioScope::kCurrentProcess,
LoadingScenario::kFocusedPageLoading);
wait_for_expectations();
// Stop observing the process scenario, then toggle both scenarios again.
//
// kCurrentProcess scope transitions from kFocusedPageLoading (non-idle) ->
// kBackgroundPageLoading (idle), but shouldn't notify any observers.
//
// kGlobal scope transitions from kVisiblePageLoading (non-idle) ->
// kNoPageLoading (idle).
quit_closure = base::BarrierClosure(2, task_env_.QuitClosure());
EXPECT_CALL(mock_observer,
OnLoadingScenarioChanged(ScenarioScope::kGlobal,
LoadingScenario::kVisiblePageLoading,
LoadingScenario::kNoPageLoading))
.WillOnce(base::test::RunClosure(quit_closure));
EXPECT_CALL(mock_idle_observer,
OnScenarioMatchChanged(ScenarioScope::kGlobal, true))
.WillOnce(base::test::RunClosure(quit_closure));
scoped_observation.RemoveObservation(
PerformanceScenarioObserverList::GetForScope(
ScenarioScope::kCurrentProcess)
.get());
idle_observation.RemoveObservation(
PerformanceScenarioObserverList::GetForScope(
ScenarioScope::kCurrentProcess)
.get());
input_only_observation.RemoveObservation(
PerformanceScenarioObserverList::GetForScope(
ScenarioScope::kCurrentProcess)
.get());
test_helper_->SetLoadingScenario(ScenarioScope::kCurrentProcess,
LoadingScenario::kBackgroundPageLoading);
test_helper_->SetLoadingScenario(ScenarioScope::kGlobal,
LoadingScenario::kNoPageLoading);
wait_for_expectations();
// Update global scenario from kNoPageLoading to kBackgroundPageLoading. The
// idle observer shouldn't be notified because the new scenario is still idle.
EXPECT_CALL(mock_observer,
OnLoadingScenarioChanged(ScenarioScope::kGlobal,
LoadingScenario::kNoPageLoading,
LoadingScenario::kBackgroundPageLoading))
.WillOnce(base::test::RunClosure(task_env_.QuitClosure()));
test_helper_->SetLoadingScenario(ScenarioScope::kGlobal,
LoadingScenario::kBackgroundPageLoading);
wait_for_expectations();
// Update the global input scenario. All 3 observers will now be notified.
quit_closure = base::BarrierClosure(3, task_env_.QuitClosure());
EXPECT_CALL(mock_observer, OnInputScenarioChanged(ScenarioScope::kGlobal,
InputScenario::kNoInput,
InputScenario::kTyping))
.WillOnce(base::test::RunClosure(quit_closure));
EXPECT_CALL(mock_idle_observer,
OnScenarioMatchChanged(ScenarioScope::kGlobal, false))
.WillOnce(base::test::RunClosure(quit_closure));
EXPECT_CALL(mock_input_only_observer,
OnScenarioMatchChanged(ScenarioScope::kGlobal, false))
.WillOnce(base::test::RunClosure(quit_closure));
test_helper_->SetInputScenario(ScenarioScope::kGlobal,
InputScenario::kTyping);
wait_for_expectations();
}
} // namespace
} // namespace performance_scenarios
|