1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ash/system/chromeos/power/power_event_observer.h"
6
7#include "ash/test/ash_test_base.h"
8#include "base/memory/scoped_ptr.h"
9#include "base/time/time.h"
10#include "chromeos/dbus/dbus_thread_manager.h"
11#include "chromeos/dbus/power_manager_client.h"
12
13namespace ash {
14namespace internal {
15
16class PowerEventObserverTest : public test::AshTestBase {
17 public:
18  PowerEventObserverTest() {}
19  virtual ~PowerEventObserverTest() {}
20
21  // test::AshTestBase::SetUp() overrides:
22  virtual void SetUp() OVERRIDE {
23    test::AshTestBase::SetUp();
24    observer_.reset(new PowerEventObserver());
25  }
26
27  virtual void TearDown() OVERRIDE {
28    observer_.reset();
29    test::AshTestBase::TearDown();
30  }
31
32 protected:
33  scoped_ptr<PowerEventObserver> observer_;
34
35 private:
36  DISALLOW_COPY_AND_ASSIGN(PowerEventObserverTest);
37};
38
39TEST_F(PowerEventObserverTest, LockBeforeSuspend) {
40  chromeos::PowerManagerClient* client =
41      chromeos::DBusThreadManager::Get()->GetPowerManagerClient();
42  ASSERT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks());
43
44  // Check that the observer requests a suspend-readiness callback when it hears
45  // that the system is about to suspend.
46  SetCanLockScreen(true);
47  SetShouldLockScreenBeforeSuspending(true);
48  observer_->SuspendImminent();
49  EXPECT_EQ(1, client->GetNumPendingSuspendReadinessCallbacks());
50
51  // It should run the callback when it hears that the screen is locked.
52  observer_->ScreenIsLocked();
53  RunAllPendingInMessageLoop();
54  EXPECT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks());
55
56  // If the system is already locked, no callback should be requested.
57  observer_->SystemResumed(base::TimeDelta());
58  observer_->ScreenIsUnlocked();
59  observer_->ScreenIsLocked();
60  observer_->SuspendImminent();
61  EXPECT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks());
62
63  // It also shouldn't request a callback if it isn't instructed to lock the
64  // screen.
65  observer_->SystemResumed(base::TimeDelta());
66  SetShouldLockScreenBeforeSuspending(false);
67  observer_->SuspendImminent();
68  EXPECT_EQ(0, client->GetNumPendingSuspendReadinessCallbacks());
69}
70
71}  // namespace internal
72}  // namespace ash
73