summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/blink/renderer/modules/webaudio/audio_basic_processor_handler_test.cc
blob: 11f5fe8d19d08ae706fcc3db584ac0a63b29fac7 (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
// Copyright 2015 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 "third_party/blink/renderer/modules/webaudio/audio_basic_processor_handler.h"
#include <memory>
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/testing/dummy_page_holder.h"
#include "third_party/blink/renderer/modules/webaudio/offline_audio_context.h"
#include "third_party/blink/renderer/platform/audio/audio_processor.h"

namespace blink {

class MockAudioProcessor final : public AudioProcessor {
 public:
  MockAudioProcessor() : AudioProcessor(48000, 2) {}
  void Initialize() override { initialized_ = true; }
  void Uninitialize() override { initialized_ = false; }
  void Process(const AudioBus*, AudioBus*, uint32_t) override {}
  void Reset() override {}
  void SetNumberOfChannels(unsigned) override {}
  unsigned NumberOfChannels() const override { return number_of_channels_; }
  bool RequiresTailProcessing() const override { return true; }
  double TailTime() const override { return 0; }
  double LatencyTime() const override { return 0; }
};

class MockProcessorHandler final : public AudioBasicProcessorHandler {
 public:
  static scoped_refptr<MockProcessorHandler> Create(AudioNode& node,
                                                    float sample_rate) {
    return base::AdoptRef(new MockProcessorHandler(node, sample_rate));
  }

 private:
  MockProcessorHandler(AudioNode& node, float sample_rate)
      : AudioBasicProcessorHandler(AudioHandler::kNodeTypeWaveShaper,
                                   node,
                                   sample_rate,
                                   std::make_unique<MockAudioProcessor>()) {
    Initialize();
  }
};

class MockProcessorNode final : public AudioNode {
 public:
  MockProcessorNode(BaseAudioContext& context) : AudioNode(context) {
    SetHandler(MockProcessorHandler::Create(*this, 48000));
  }
  void ReportDidCreate() final {}
  void ReportWillBeDestroyed() final {}
};

TEST(AudioBasicProcessorHandlerTest, ProcessorFinalization) {
  auto page = std::make_unique<DummyPageHolder>();
  OfflineAudioContext* context = OfflineAudioContext::Create(
      page->GetFrame().DomWindow(), 2, 1, 48000, ASSERT_NO_EXCEPTION);
  MockProcessorNode* node = MakeGarbageCollected<MockProcessorNode>(*context);
  AudioBasicProcessorHandler& handler =
      static_cast<AudioBasicProcessorHandler&>(node->Handler());
  EXPECT_TRUE(handler.Processor());
  EXPECT_TRUE(handler.Processor()->IsInitialized());
  BaseAudioContext::GraphAutoLocker locker(context);
  handler.Dispose();
  // The AudioProcessor should live after dispose() and should not be
  // finalized because an audio thread is using it.
  EXPECT_TRUE(handler.Processor());
  EXPECT_TRUE(handler.Processor()->IsInitialized());
}

}  // namespace blink