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

media / audio / power_observer_helper_unittest.cc [blame]

// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>

#include "base/functional/bind.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/task_environment.h"
#include "base/threading/thread.h"
#include "media/audio/power_observer_helper.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media {

class PowerObserverHelperTest : public testing::Test {
 public:
  PowerObserverHelperTest()
      : power_observer_helper_thread_("AliveCheckerThread"),
        suspend_event_(base::WaitableEvent::ResetPolicy::MANUAL,
                       base::WaitableEvent::InitialState::NOT_SIGNALED),
        resume_event_(base::WaitableEvent::ResetPolicy::MANUAL,
                      base::WaitableEvent::InitialState::NOT_SIGNALED) {
    power_observer_helper_thread_.StartAndWaitForTesting();
  }

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

  void OnSuspend() {
    EXPECT_TRUE(
        power_observer_helper_thread_.task_runner()->BelongsToCurrentThread());
    suspend_event_.Signal();
  }

  void OnResume() {
    EXPECT_TRUE(
        power_observer_helper_thread_.task_runner()->BelongsToCurrentThread());
    resume_event_.Signal();
  }

 protected:
  ~PowerObserverHelperTest() override {
    base::WaitableEvent done(base::WaitableEvent::ResetPolicy::MANUAL,
                             base::WaitableEvent::InitialState::NOT_SIGNALED);
    power_observer_helper_thread_.task_runner()->PostTask(
        FROM_HERE,
        base::BindOnce(&PowerObserverHelperTest::
                           ResetPowerObserverHelperOnPowerObserverHelperThread,
                       base::Unretained(this), &done));
    done.Wait();
  }

  void CreatePowerObserverHelper() {
    DCHECK(!power_observer_helper_);
    power_observer_helper_ = std::make_unique<PowerObserverHelper>(
        power_observer_helper_thread_.task_runner(),
        base::BindRepeating(&PowerObserverHelperTest::OnSuspend,
                            base::Unretained(this)),
        base::BindRepeating(&PowerObserverHelperTest::OnResume,
                            base::Unretained(this)));
  }

  void WaitUntilSuspendNotification() {
    suspend_event_.Wait();
    suspend_event_.Reset();
  }

  void WaitUntilResumeNotification() {
    resume_event_.Wait();
    resume_event_.Reset();
  }

  bool IsSuspending() {
    bool is_suspending = false;
    base::WaitableEvent did_check(
        base::WaitableEvent::ResetPolicy::MANUAL,
        base::WaitableEvent::InitialState::NOT_SIGNALED);
    power_observer_helper_thread_.task_runner()->PostTask(
        FROM_HERE,
        base::BindOnce(&PowerObserverHelperTest::CheckIfSuspending,
                       base::Unretained(this), &is_suspending, &did_check));
    did_check.Wait();
    return is_suspending;
  }

  PowerObserverHelper* power_observer_helper() const {
    return power_observer_helper_.get();
  }

 private:
  void CheckIfSuspending(bool* is_suspending, base::WaitableEvent* done) {
    EXPECT_TRUE(
        power_observer_helper_thread_.task_runner()->BelongsToCurrentThread());
    *is_suspending = power_observer_helper_->IsSuspending();
    done->Signal();
  }

  void ResetPowerObserverHelperOnPowerObserverHelperThread(
      base::WaitableEvent* done) {
    EXPECT_TRUE(
        power_observer_helper_thread_.task_runner()->BelongsToCurrentThread());
    power_observer_helper_.reset();
    done->Signal();
  }

  // The test task environment.
  base::test::TaskEnvironment task_environment_;

  // The thread the helper is run on.
  base::Thread power_observer_helper_thread_;

  // PowerObserverHelper under test.
  std::unique_ptr<PowerObserverHelper> power_observer_helper_;

  // Events to signal a notifications.
  base::WaitableEvent suspend_event_;
  base::WaitableEvent resume_event_;
};

// Suspend and resume notifications.
TEST_F(PowerObserverHelperTest, SuspendAndResumeNotificationsTwice) {
  CreatePowerObserverHelper();
  EXPECT_FALSE(IsSuspending());

  power_observer_helper()->OnSuspend();
  WaitUntilSuspendNotification();
  EXPECT_TRUE(IsSuspending());

  power_observer_helper()->OnResume();
  WaitUntilResumeNotification();
  EXPECT_FALSE(IsSuspending());

  power_observer_helper()->OnSuspend();
  WaitUntilSuspendNotification();
  EXPECT_TRUE(IsSuspending());

  power_observer_helper()->OnResume();
  WaitUntilResumeNotification();
  EXPECT_FALSE(IsSuspending());
}

// Two suspend and two resume notifications.
TEST_F(PowerObserverHelperTest, TwoSuspendAndTwoResumeNotifications) {
  CreatePowerObserverHelper();
  EXPECT_FALSE(IsSuspending());

  power_observer_helper()->OnSuspend();
  WaitUntilSuspendNotification();
  EXPECT_TRUE(IsSuspending());

  power_observer_helper()->OnSuspend();
  WaitUntilSuspendNotification();
  EXPECT_TRUE(IsSuspending());

  power_observer_helper()->OnResume();
  WaitUntilResumeNotification();
  EXPECT_FALSE(IsSuspending());

  power_observer_helper()->OnResume();
  WaitUntilResumeNotification();
  EXPECT_FALSE(IsSuspending());
}

}  // namespace media