File: audio_mirroring_manager_unittest.cc

package info (click to toggle)
chromium-browser 37.0.2062.120-1~deb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,707,260 kB
  • sloc: cpp: 8,976,677; ansic: 3,473,199; python: 586,578; asm: 449,013; xml: 184,195; java: 142,924; sh: 118,496; perl: 81,467; makefile: 27,557; yacc: 10,506; objc: 8,886; tcl: 3,186; cs: 2,252; lex: 2,213; sql: 1,198; pascal: 1,170; lisp: 790; awk: 407; ruby: 155; php: 83; sed: 52; exp: 11
file content (233 lines) | stat: -rw-r--r-- 8,931 bytes parent folder | download
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright (c) 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 "content/browser/media/capture/audio_mirroring_manager.h"

#include <map>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/message_loop/message_loop.h"
#include "base/synchronization/waitable_event.h"
#include "content/browser/browser_thread_impl.h"
#include "media/audio/audio_parameters.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using media::AudioOutputStream;
using media::AudioParameters;
using testing::_;
using testing::NotNull;
using testing::Ref;
using testing::Return;
using testing::ReturnRef;

namespace content {

namespace {

class MockDiverter : public AudioMirroringManager::Diverter {
 public:
  MOCK_METHOD0(GetAudioParameters, const AudioParameters&());
  MOCK_METHOD1(StartDiverting, void(AudioOutputStream*));
  MOCK_METHOD0(StopDiverting, void());
};

class MockMirroringDestination
    : public AudioMirroringManager::MirroringDestination {
 public:
  MOCK_METHOD1(AddInput,
               media::AudioOutputStream*(const media::AudioParameters& params));
};

}  // namespace

class AudioMirroringManagerTest : public testing::Test {
 public:
  AudioMirroringManagerTest()
      : io_thread_(BrowserThread::IO, &message_loop_),
        params_(AudioParameters::AUDIO_FAKE, media::CHANNEL_LAYOUT_STEREO,
                AudioParameters::kAudioCDSampleRate, 16,
                AudioParameters::kAudioCDSampleRate / 10) {}

  MockDiverter* CreateStream(
      int render_process_id, int render_view_id, int expected_times_diverted) {
    MockDiverter* const diverter = new MockDiverter();
    if (expected_times_diverted > 0) {
      EXPECT_CALL(*diverter, GetAudioParameters())
          .Times(expected_times_diverted)
          .WillRepeatedly(ReturnRef(params_));
      EXPECT_CALL(*diverter, StartDiverting(NotNull()))
          .Times(expected_times_diverted);
      EXPECT_CALL(*diverter, StopDiverting())
          .Times(expected_times_diverted);
    }

    mirroring_manager_.AddDiverter(render_process_id, render_view_id, diverter);

    return diverter;
  }

  void KillStream(
      int render_process_id, int render_view_id, MockDiverter* diverter) {
    mirroring_manager_.RemoveDiverter(
        render_process_id, render_view_id, diverter);

    delete diverter;
  }

  MockMirroringDestination* StartMirroringTo(
      int render_process_id, int render_view_id, int expected_inputs_added) {
    MockMirroringDestination* const dest = new MockMirroringDestination();
    if (expected_inputs_added > 0) {
      static AudioOutputStream* const kNonNullPointer =
          reinterpret_cast<AudioOutputStream*>(0x11111110);
      EXPECT_CALL(*dest, AddInput(Ref(params_)))
          .Times(expected_inputs_added)
          .WillRepeatedly(Return(kNonNullPointer));
    }

    mirroring_manager_.StartMirroring(render_process_id, render_view_id, dest);

    return dest;
  }

  void StopMirroringTo(int render_process_id, int render_view_id,
                       MockMirroringDestination* dest) {
    mirroring_manager_.StopMirroring(render_process_id, render_view_id, dest);

    delete dest;
}

 private:
  base::MessageLoopForIO message_loop_;
  BrowserThreadImpl io_thread_;
  AudioParameters params_;
  AudioMirroringManager mirroring_manager_;

  DISALLOW_COPY_AND_ASSIGN(AudioMirroringManagerTest);
};

namespace {
const int kRenderProcessId = 123;
const int kRenderViewId = 456;
const int kAnotherRenderProcessId = 789;
const int kAnotherRenderViewId = 1234;
const int kYetAnotherRenderProcessId = 4560;
const int kYetAnotherRenderViewId = 7890;
}

TEST_F(AudioMirroringManagerTest, MirroringSessionOfNothing) {
  MockMirroringDestination* const destination =
      StartMirroringTo(kRenderProcessId, kRenderViewId, 0);
  StopMirroringTo(kRenderProcessId, kRenderViewId, destination);
}

TEST_F(AudioMirroringManagerTest, TwoMirroringSessionsOfNothing) {
  MockMirroringDestination* const destination =
      StartMirroringTo(kRenderProcessId, kRenderViewId, 0);
  StopMirroringTo(kRenderProcessId, kRenderViewId, destination);

  MockMirroringDestination* const another_destination =
      StartMirroringTo(kAnotherRenderProcessId, kAnotherRenderViewId, 0);
  StopMirroringTo(kAnotherRenderProcessId, kAnotherRenderViewId,
                  another_destination);
}

TEST_F(AudioMirroringManagerTest, SwitchMirroringDestinationNoStreams) {
  MockMirroringDestination* const destination =
      StartMirroringTo(kRenderProcessId, kRenderViewId, 0);
  MockMirroringDestination* const new_destination =
      StartMirroringTo(kRenderProcessId, kRenderViewId, 0);
  StopMirroringTo(kRenderProcessId, kRenderViewId, destination);
  StopMirroringTo(kRenderProcessId, kRenderViewId, new_destination);
}

TEST_F(AudioMirroringManagerTest, StreamLifetimeAroundMirroringSession) {
  MockDiverter* const stream = CreateStream(kRenderProcessId, kRenderViewId, 1);
  MockMirroringDestination* const destination =
      StartMirroringTo(kRenderProcessId, kRenderViewId, 1);
  StopMirroringTo(kRenderProcessId, kRenderViewId, destination);
  KillStream(kRenderProcessId, kRenderViewId, stream);
}

TEST_F(AudioMirroringManagerTest, StreamLifetimeWithinMirroringSession) {
  MockMirroringDestination* const destination =
      StartMirroringTo(kRenderProcessId, kRenderViewId, 1);
  MockDiverter* const stream = CreateStream(kRenderProcessId, kRenderViewId, 1);
  KillStream(kRenderProcessId, kRenderViewId, stream);
  StopMirroringTo(kRenderProcessId, kRenderViewId, destination);
}

TEST_F(AudioMirroringManagerTest, StreamLifetimeAroundTwoMirroringSessions) {
  MockDiverter* const stream = CreateStream(kRenderProcessId, kRenderViewId, 2);
  MockMirroringDestination* const destination =
      StartMirroringTo(kRenderProcessId, kRenderViewId, 1);
  StopMirroringTo(kRenderProcessId, kRenderViewId, destination);
  MockMirroringDestination* const new_destination =
      StartMirroringTo(kRenderProcessId, kRenderViewId, 1);
  StopMirroringTo(kRenderProcessId, kRenderViewId, new_destination);
  KillStream(kRenderProcessId, kRenderViewId, stream);
}

TEST_F(AudioMirroringManagerTest, StreamLifetimeWithinTwoMirroringSessions) {
  MockMirroringDestination* const destination =
      StartMirroringTo(kRenderProcessId, kRenderViewId, 1);
  MockDiverter* const stream = CreateStream(kRenderProcessId, kRenderViewId, 2);
  StopMirroringTo(kRenderProcessId, kRenderViewId, destination);
  MockMirroringDestination* const new_destination =
      StartMirroringTo(kRenderProcessId, kRenderViewId, 1);
  KillStream(kRenderProcessId, kRenderViewId, stream);
  StopMirroringTo(kRenderProcessId, kRenderViewId, new_destination);
}

TEST_F(AudioMirroringManagerTest, MultipleStreamsInOneMirroringSession) {
  MockDiverter* const stream1 =
      CreateStream(kRenderProcessId, kRenderViewId, 1);
  MockMirroringDestination* const destination =
      StartMirroringTo(kRenderProcessId, kRenderViewId, 3);
  MockDiverter* const stream2 =
      CreateStream(kRenderProcessId, kRenderViewId, 1);
  MockDiverter* const stream3 =
      CreateStream(kRenderProcessId, kRenderViewId, 1);
  KillStream(kRenderProcessId, kRenderViewId, stream2);
  StopMirroringTo(kRenderProcessId, kRenderViewId, destination);
  KillStream(kRenderProcessId, kRenderViewId, stream3);
  KillStream(kRenderProcessId, kRenderViewId, stream1);
}

// A random interleaving of operations for three separate targets, each of which
// has one stream mirrored to one destination.
TEST_F(AudioMirroringManagerTest, ThreeSeparateMirroringSessions) {
  MockDiverter* const stream =
      CreateStream(kRenderProcessId, kRenderViewId, 1);
  MockMirroringDestination* const destination =
      StartMirroringTo(kRenderProcessId, kRenderViewId, 1);

  MockMirroringDestination* const another_destination =
      StartMirroringTo(kAnotherRenderProcessId, kAnotherRenderViewId, 1);
  MockDiverter* const another_stream =
      CreateStream(kAnotherRenderProcessId, kAnotherRenderViewId, 1);

  KillStream(kRenderProcessId, kRenderViewId, stream);

  MockDiverter* const yet_another_stream =
      CreateStream(kYetAnotherRenderProcessId, kYetAnotherRenderViewId, 1);
  MockMirroringDestination* const yet_another_destination =
      StartMirroringTo(kYetAnotherRenderProcessId, kYetAnotherRenderViewId, 1);

  StopMirroringTo(kAnotherRenderProcessId, kAnotherRenderViewId,
                  another_destination);

  StopMirroringTo(kYetAnotherRenderProcessId, kYetAnotherRenderViewId,
                  yet_another_destination);

  StopMirroringTo(kRenderProcessId, kRenderViewId, destination);

  KillStream(kAnotherRenderProcessId, kAnotherRenderViewId, another_stream);
  KillStream(kYetAnotherRenderProcessId, kYetAnotherRenderViewId,
             yet_another_stream);
}

}  // namespace content