File: dark_resume_controller_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (190 lines) | stat: -rw-r--r-- 7,438 bytes parent folder | download | duplicates (9)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/power/dark_resume_controller.h"

#include <memory>
#include <utility>

#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "chromeos/dbus/power/fake_power_manager_client.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/device/public/cpp/test/test_wake_lock_provider.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash::system {

namespace {

using ::device::mojom::WakeLockType;

constexpr char kWakeLockDescription[] = "DarkResumeTest";

}  // namespace

class DarkResumeControllerTest : public testing::Test {
 public:
  DarkResumeControllerTest()
      : task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}

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

  ~DarkResumeControllerTest() override = default;

  void SetUp() override {
    // Create wake lock that will be acquired and released in tests.
    wake_lock_provider_.GetWakeLockWithoutContext(
        WakeLockType::kPreventAppSuspension,
        device::mojom::WakeLockReason::kOther, kWakeLockDescription,
        wake_lock_.BindNewPipeAndPassReceiver());

    chromeos::PowerManagerClient::InitializeFake();

    mojo::PendingRemote<device::mojom::WakeLockProvider> provider_remote;
    wake_lock_provider_.BindReceiver(
        provider_remote.InitWithNewPipeAndPassReceiver());
    dark_resume_controller_ =
        std::make_unique<DarkResumeController>(std::move(provider_remote));
  }

  void TearDown() override {
    dark_resume_controller_.reset();
    chromeos::PowerManagerClient::Shutdown();
  }

 protected:
  // Returns the number of active wake locks of type |type|.
  int32_t GetActiveWakeLocks(WakeLockType type) {
    base::RunLoop run_loop;
    int32_t result_count = -1;
    wake_lock_provider_.GetActiveWakeLocksForTests(
        type,
        base::BindOnce(
            [](base::RunLoop* run_loop, int32_t* result_count, int32_t count) {
              *result_count = count;
              run_loop->Quit();
            },
            &run_loop, &result_count));
    run_loop.Run();
    return result_count;
  }

  chromeos::FakePowerManagerClient* fake_power_manager_client() {
    return chromeos::FakePowerManagerClient::Get();
  }

  base::test::TaskEnvironment task_environment_;
  mojo::Remote<device::mojom::WakeLock> wake_lock_;
  std::unique_ptr<DarkResumeController> dark_resume_controller_;

 private:
  device::TestWakeLockProvider wake_lock_provider_;
};

TEST_F(DarkResumeControllerTest, CheckSuspendAfterDarkResumeNoWakeLocksHeld) {
  // Trigger a dark resume event, move time forward to trigger a wake lock check
  // and check if a re-suspend happened if no wake locks were acquired.
  fake_power_manager_client()->SendDarkSuspendImminent();
  task_environment_.FastForwardBy(
      DarkResumeController::kDarkResumeWakeLockCheckTimeout);
  base::RunLoop run_loop;
  run_loop.RunUntilIdle();
  EXPECT_TRUE(dark_resume_controller_->IsDarkResumeStateClearedForTesting());
  EXPECT_EQ(
      0,
      fake_power_manager_client()->num_pending_suspend_readiness_callbacks());

  // Trigger a dark resume event, acquire and release a wake lock and move time
  // forward to trigger a wake lock check. The device should re-suspend in this
  // case since no wake locks were held at the time of the wake lock check.
  fake_power_manager_client()->SendDarkSuspendImminent();
  wake_lock_->RequestWakeLock();
  wake_lock_->CancelWakeLock();
  task_environment_.FastForwardBy(
      DarkResumeController::kDarkResumeWakeLockCheckTimeout);
  base::RunLoop run_loop2;
  run_loop2.RunUntilIdle();
  EXPECT_TRUE(dark_resume_controller_->IsDarkResumeStateClearedForTesting());
  EXPECT_EQ(
      0,
      fake_power_manager_client()->num_pending_suspend_readiness_callbacks());
}

TEST_F(DarkResumeControllerTest, CheckSuspendAfterDarkResumeWakeLocksHeld) {
  // Trigger a dark resume event, acquire a wake lock and move time forward to a
  // wake lock check. At this point the system shouldn't re-suspend i.e. the
  // suspend readiness callback should be set and wake lock release should have
  // observers.
  fake_power_manager_client()->SendDarkSuspendImminent();
  wake_lock_->RequestWakeLock();
  task_environment_.FastForwardBy(
      DarkResumeController::kDarkResumeWakeLockCheckTimeout);
  base::RunLoop run_loop;
  run_loop.RunUntilIdle();
  EXPECT_TRUE(dark_resume_controller_->IsDarkResumeStateSetForTesting());

  // Move time forward by < |dark_resume_hard_timeout_| and release the
  // partial wake lock. This should instantaneously re-suspend the device.
  base::TimeDelta small_delay = base::Seconds(1);
  ASSERT_GT(dark_resume_controller_->GetHardTimeoutForTesting(), small_delay);
  task_environment_.FastForwardBy(
      dark_resume_controller_->GetHardTimeoutForTesting() - small_delay);
  wake_lock_->CancelWakeLock();
  base::RunLoop run_loop2;
  run_loop2.RunUntilIdle();
  EXPECT_TRUE(dark_resume_controller_->IsDarkResumeStateClearedForTesting());
  EXPECT_EQ(
      0,
      fake_power_manager_client()->num_pending_suspend_readiness_callbacks());
}

TEST_F(DarkResumeControllerTest, CheckSuspendAfterDarkResumeHardTimeout) {
  // Trigger a dark resume event, acquire a wake lock and move time forward to a
  // wake lock check. At this point the system shouldn't re-suspend i.e. the
  // suspend readiness callback should be set and wake lock release should have
  // observers.
  fake_power_manager_client()->SendDarkSuspendImminent();
  wake_lock_->RequestWakeLock();
  task_environment_.FastForwardBy(
      DarkResumeController::kDarkResumeWakeLockCheckTimeout);
  base::RunLoop run_loop;
  run_loop.RunUntilIdle();
  EXPECT_TRUE(dark_resume_controller_->IsDarkResumeStateSetForTesting());

  // Move time forward by |dark_resume_hard_timeout_|. At this point the
  // device should re-suspend even though the wake lock is acquired.
  task_environment_.FastForwardBy(
      dark_resume_controller_->GetHardTimeoutForTesting());
  EXPECT_EQ(1, GetActiveWakeLocks(WakeLockType::kPreventAppSuspension));
  base::RunLoop run_loop2;
  run_loop2.RunUntilIdle();
  EXPECT_TRUE(dark_resume_controller_->IsDarkResumeStateClearedForTesting());
  EXPECT_EQ(
      0,
      fake_power_manager_client()->num_pending_suspend_readiness_callbacks());
}

TEST_F(DarkResumeControllerTest, CheckStateResetAfterSuspendDone) {
  // Trigger a dark resume event, acquire a wake lock and move time forward to a
  // wake lock check. At this point the system shouldn't re-suspend i.e. the
  // suspend readiness callback should be set and wake lock release should have
  // observers.
  fake_power_manager_client()->SendDarkSuspendImminent();
  wake_lock_->RequestWakeLock();
  task_environment_.FastForwardBy(
      DarkResumeController::kDarkResumeWakeLockCheckTimeout);
  base::RunLoop run_loop;
  run_loop.RunUntilIdle();
  EXPECT_TRUE(dark_resume_controller_->IsDarkResumeStateSetForTesting());

  // Trigger suspend done event. Check if state is reset as dark resume would be
  // exited.
  fake_power_manager_client()->SendSuspendDone();
  EXPECT_TRUE(dark_resume_controller_->IsDarkResumeStateClearedForTesting());
}

}  // namespace ash::system