File: arc_power_throttle_observer_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (236 lines) | stat: -rw-r--r-- 7,912 bytes parent folder | download | duplicates (6)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/arc/instance_throttle/arc_power_throttle_observer.h"

#include "base/command_line.h"
#include "base/functional/bind.h"
#include "chrome/browser/ash/arc/session/arc_session_manager.h"
#include "chrome/browser/ash/arc/test/test_arc_session_manager.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/ash/components/dbus/concierge/concierge_client.h"
#include "chromeos/ash/experiences/arc/mojom/anr.mojom.h"
#include "chromeos/ash/experiences/arc/session/arc_bridge_service.h"
#include "chromeos/ash/experiences/arc/session/arc_service_manager.h"
#include "chromeos/ash/experiences/arc/test/arc_util_test_support.h"
#include "chromeos/ash/experiences/arc/test/fake_arc_session.h"
#include "chromeos/dbus/power/power_manager_client.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace arc {
namespace {

void TestCallback(int* counter,
                  int* active_counter,
                  const ash::ThrottleObserver* self) {
  (*counter)++;
  if (self->active())
    (*active_counter)++;
}

}  // namespace

class ArcPowerThrottleObserverTest : public testing::Test {
 public:
  ArcPowerThrottleObserverTest() = default;
  ~ArcPowerThrottleObserverTest() override = default;

  ArcPowerThrottleObserverTest(const ArcPowerThrottleObserverTest&) = delete;
  ArcPowerThrottleObserverTest& operator=(const ArcPowerThrottleObserverTest&) =
      delete;

  void SetUp() override {
    chromeos::PowerManagerClient::InitializeFake();
    ash::ConciergeClient::InitializeFake(/*fake_cicerone_client=*/nullptr);
    service_manager_ = std::make_unique<ArcServiceManager>();
    session_manager_ =
        CreateTestArcSessionManager(std::make_unique<ArcSessionRunner>(
            base::BindRepeating(FakeArcSession::Create)));
    testing_profile_ = std::make_unique<TestingProfile>();

    SetArcAvailableCommandLineForTesting(
        base::CommandLine::ForCurrentProcess());

    ArcPowerBridge* const power_bridge =
        ArcPowerBridge::GetForBrowserContextForTesting(testing_profile_.get());
    DCHECK(power_bridge);
  }

  void TearDown() override {
    testing_profile_.reset();
    session_manager_.reset();
    service_manager_.reset();
    chromeos::PowerManagerClient::Shutdown();
  }

 protected:
  content::BrowserTaskEnvironment& task_environment() {
    return task_environment_;
  }
  TestingProfile* profile() { return testing_profile_.get(); }

 private:
  content::BrowserTaskEnvironment task_environment_{
      base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  std::unique_ptr<ArcServiceManager> service_manager_;
  std::unique_ptr<ArcSessionManager> session_manager_;
  std::unique_ptr<TestingProfile> testing_profile_;
};

TEST_F(ArcPowerThrottleObserverTest, Default) {
  ArcPowerThrottleObserver observer;
  int call_count = 0;
  int active_count = 0;
  observer.StartObserving(
      profile(),
      base::BindRepeating(&TestCallback, &call_count, &active_count));
  EXPECT_EQ(0, call_count);
  EXPECT_EQ(0, active_count);
  EXPECT_FALSE(observer.active());

  observer.OnPreAnr(mojom::AnrType::CONTENT_PROVIDER);
  EXPECT_EQ(1, call_count);
  EXPECT_EQ(1, active_count);
  EXPECT_TRUE(observer.active());

  // One more notification does not change state.
  observer.OnPreAnr(mojom::AnrType::PROCESS);
  EXPECT_EQ(2, call_count);
  EXPECT_EQ(2, active_count);
  EXPECT_TRUE(observer.active());

  // Duration of temporary lifting CPU restrictions is not yet over.
  task_environment().FastForwardBy(base::Milliseconds(9000));
  EXPECT_EQ(2, call_count);
  EXPECT_EQ(2, active_count);
  EXPECT_TRUE(observer.active());

  // Wait a bit more, duration of temporary lifting CPU restrictions is over
  // now.
  task_environment().FastForwardBy(base::Milliseconds(1000));
  EXPECT_EQ(3, call_count);
  EXPECT_EQ(2, active_count);
  EXPECT_FALSE(observer.active());

  // Wait much longer, the last state should not change.
  task_environment().FastForwardBy(base::Milliseconds(10000));
  EXPECT_EQ(3, call_count);
  EXPECT_EQ(2, active_count);
  EXPECT_FALSE(observer.active());

  // Timer is not fired after stopping observing.
  observer.OnPreAnr(mojom::AnrType::CONTENT_PROVIDER);
  EXPECT_EQ(4, call_count);
  EXPECT_EQ(3, active_count);
  EXPECT_TRUE(observer.active());

  observer.StopObserving();
  task_environment().FastForwardBy(base::Milliseconds(11000));
  EXPECT_EQ(4, call_count);
  EXPECT_EQ(3, active_count);
  EXPECT_TRUE(observer.active());
}

// Test verifies that new preANR extends active time of the lock in the case
// new preANR type requires more time for handling.
TEST_F(ArcPowerThrottleObserverTest, ActiveTimeExtended) {
  ArcPowerThrottleObserver observer;
  int call_count = 0;
  int active_count = 0;
  observer.StartObserving(
      profile(),
      base::BindRepeating(&TestCallback, &call_count, &active_count));

  observer.OnPreAnr(mojom::AnrType::PROCESS);
  EXPECT_EQ(1, call_count);
  EXPECT_EQ(1, active_count);
  EXPECT_TRUE(observer.active());

  task_environment().FastForwardBy(base::Milliseconds(5000));
  // Timer not yet fired
  EXPECT_TRUE(observer.active());

  observer.OnPreAnr(mojom::AnrType::FOREGROUND_SERVICE);
  EXPECT_EQ(2, call_count);
  EXPECT_EQ(2, active_count);
  EXPECT_TRUE(observer.active());

  task_environment().FastForwardBy(base::Milliseconds(19000));
  // Without FOREGROUND_SERVICE preANR timer would be already fired.
  // So it is still active now.
  EXPECT_EQ(2, call_count);
  EXPECT_EQ(2, active_count);
  EXPECT_TRUE(observer.active());

  task_environment().FastForwardBy(base::Milliseconds(1000));
  EXPECT_EQ(3, call_count);
  EXPECT_EQ(2, active_count);
  EXPECT_FALSE(observer.active());
}

// Test verifies that new preANR does not change active time of the lock in
// the case new preANR type requires less time that currently remained from the
// previous preANR handling.
TEST_F(ArcPowerThrottleObserverTest, ActiveTimePreserved) {
  ArcPowerThrottleObserver observer;
  int call_count = 0;
  int active_count = 0;
  observer.StartObserving(
      profile(),
      base::BindRepeating(&TestCallback, &call_count, &active_count));

  observer.OnPreAnr(mojom::AnrType::FOREGROUND_SERVICE);
  EXPECT_EQ(1, call_count);
  EXPECT_EQ(1, active_count);
  EXPECT_TRUE(observer.active());

  task_environment().FastForwardBy(base::Milliseconds(9000));
  // Timer not yet fired
  EXPECT_TRUE(observer.active());

  // Process has shorter active time. It should not change the previous timeout.
  observer.OnPreAnr(mojom::AnrType::PROCESS);
  EXPECT_EQ(2, call_count);
  EXPECT_EQ(2, active_count);
  EXPECT_TRUE(observer.active());

  task_environment().FastForwardBy(base::Milliseconds(10000));
  EXPECT_EQ(2, call_count);
  EXPECT_EQ(2, active_count);
  EXPECT_TRUE(observer.active());

  // Only now the lock becomes inactive.
  task_environment().FastForwardBy(base::Milliseconds(1000));
  EXPECT_EQ(3, call_count);
  EXPECT_EQ(2, active_count);
  EXPECT_FALSE(observer.active());
}

TEST_F(ArcPowerThrottleObserverTest, Broadcast) {
  ArcPowerThrottleObserver observer;
  int call_count = 0;
  int active_count = 0;
  observer.StartObserving(
      profile(),
      base::BindRepeating(&TestCallback, &call_count, &active_count));

  observer.OnPreAnr(mojom::AnrType::BROADCAST);
  EXPECT_EQ(1, call_count);
  EXPECT_EQ(1, active_count);
  EXPECT_TRUE(observer.active());

  task_environment().FastForwardBy(base::Milliseconds(9999));
  EXPECT_EQ(1, call_count);
  EXPECT_EQ(1, active_count);
  EXPECT_TRUE(observer.active());

  // Only now the lock becomes inactive.
  task_environment().FastForwardBy(base::Milliseconds(1));
  EXPECT_EQ(2, call_count);
  EXPECT_EQ(1, active_count);
  EXPECT_FALSE(observer.active());
}

}  // namespace arc