summaryrefslogtreecommitdiffstats
path: root/media/audio/sounds/audio_stream_handler_unittest.cc
blob: 234ac9d16a919abe4b66b7b1651303599098e047 (plain)
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
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "media/audio/audio_io.h"
#include "media/audio/audio_manager.h"
#include "media/audio/simple_sources.h"
#include "media/audio/sounds/audio_stream_handler.h"
#include "media/audio/sounds/test_data.h"
#include "media/base/channel_layout.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media {

class AudioStreamHandlerTest : public testing::Test {
 public:
  AudioStreamHandlerTest() {}
  ~AudioStreamHandlerTest() override {}

  void SetUp() override {
    audio_manager_.reset(AudioManager::CreateForTesting());

    base::StringPiece data(kTestAudioData, arraysize(kTestAudioData));
    audio_stream_handler_.reset(new AudioStreamHandler(data));
  }

  void TearDown() override {
    audio_stream_handler_.reset();
    audio_manager_.reset();
  }

  AudioStreamHandler* audio_stream_handler() {
    return audio_stream_handler_.get();
  }

  void SetObserverForTesting(AudioStreamHandler::TestObserver* observer) {
    AudioStreamHandler::SetObserverForTesting(observer);
  }

  void SetAudioSourceForTesting(
      AudioOutputStream::AudioSourceCallback* source) {
    AudioStreamHandler::SetAudioSourceForTesting(source);
  }

 private:
  scoped_ptr<AudioManager> audio_manager_;
  scoped_ptr<AudioStreamHandler> audio_stream_handler_;

  base::MessageLoop message_loop_;
};

TEST_F(AudioStreamHandlerTest, Play) {
  base::RunLoop run_loop;
  TestObserver observer(run_loop.QuitClosure());

  SetObserverForTesting(&observer);

  ASSERT_TRUE(audio_stream_handler()->IsInitialized());
  ASSERT_TRUE(audio_stream_handler()->Play());

  run_loop.Run();

  SetObserverForTesting(NULL);

  ASSERT_EQ(1, observer.num_play_requests());
  ASSERT_EQ(1, observer.num_stop_requests());
  ASSERT_EQ(4, observer.cursor());
}

TEST_F(AudioStreamHandlerTest, ConsecutivePlayRequests) {
  base::RunLoop run_loop;
  TestObserver observer(run_loop.QuitClosure());
  SineWaveAudioSource source(CHANNEL_LAYOUT_STEREO, 200.0, 8000);

  SetObserverForTesting(&observer);
  SetAudioSourceForTesting(&source);

  ASSERT_TRUE(audio_stream_handler()->IsInitialized());

  ASSERT_TRUE(audio_stream_handler()->Play());
  base::MessageLoop::current()->PostDelayedTask(
      FROM_HERE,
      base::Bind(base::IgnoreResult(&AudioStreamHandler::Play),
                 base::Unretained(audio_stream_handler())),
      base::TimeDelta::FromSeconds(1));
  base::MessageLoop::current()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&AudioStreamHandler::Stop,
                 base::Unretained(audio_stream_handler())),
      base::TimeDelta::FromSeconds(2));

  run_loop.Run();

  SetObserverForTesting(NULL);
  SetAudioSourceForTesting(NULL);

  ASSERT_EQ(1, observer.num_play_requests());
  ASSERT_EQ(1, observer.num_stop_requests());
}

}  // namespace media