summaryrefslogtreecommitdiffstats
path: root/content/renderer/media/video_capture_impl_manager_unittest.cc
blob: 98f2b5645e773fa0c6263384c6189343d6062156 (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
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
// Copyright 2014 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/bind.h"
#include "base/callback.h"
#include "base/memory/ref_counted.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "content/child/child_process.h"
#include "content/renderer/media/video_capture_impl.h"
#include "content/renderer/media/video_capture_impl_manager.h"
#include "content/renderer/media/video_capture_message_filter.h"
#include "media/base/bind_to_current_loop.h"
#include "media/video/capture/mock_video_capture_event_handler.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using ::testing::_;
using ::testing::DoAll;
using ::testing::SaveArg;
using media::BindToCurrentLoop;
using media::MockVideoCaptureEventHandler;

namespace content {

ACTION_P(RunClosure, closure) {
  closure.Run();
}

class MockVideoCaptureImpl : public VideoCaptureImpl {
 public:
  MockVideoCaptureImpl(media::VideoCaptureSessionId session_id,
                       VideoCaptureMessageFilter* filter,
                       base::Closure destruct_callback)
      : VideoCaptureImpl(session_id, filter),
        destruct_callback_(destruct_callback) {
  }

  virtual ~MockVideoCaptureImpl() {
    destruct_callback_.Run();
  }

 private:
  base::Closure destruct_callback_;

  DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImpl);
};

class MockVideoCaptureImplManager : public VideoCaptureImplManager {
 public:
  explicit MockVideoCaptureImplManager(
      base::Closure destruct_video_capture_callback)
      : destruct_video_capture_callback_(
          destruct_video_capture_callback) {}

 protected:
  virtual VideoCaptureImpl* CreateVideoCaptureImpl(
      media::VideoCaptureSessionId id,
      VideoCaptureMessageFilter* filter) const OVERRIDE {
    return new MockVideoCaptureImpl(id,
                                    filter,
                                    destruct_video_capture_callback_);
  }

 private:
  base::Closure destruct_video_capture_callback_;

  DISALLOW_COPY_AND_ASSIGN(MockVideoCaptureImplManager);
};

class VideoCaptureImplManagerTest : public ::testing::Test {
 public:
  VideoCaptureImplManagerTest()
      : manager_(BindToCurrentLoop(cleanup_run_loop_.QuitClosure())) {
    params_.requested_format = media::VideoCaptureFormat(
        gfx::Size(176, 144), 30, media::PIXEL_FORMAT_I420);
    child_process_.reset(new ChildProcess());
  }

  void FakeChannelSetup() {
    scoped_refptr<base::MessageLoopProxy> loop =
        child_process_->io_message_loop_proxy();
    if (!loop->BelongsToCurrentThread()) {
      loop->PostTask(
          FROM_HERE,
          base::Bind(
              &VideoCaptureImplManagerTest::FakeChannelSetup,
              base::Unretained(this)));
      return;
    }
    manager_.video_capture_message_filter()->OnFilterAdded(NULL);
  }

 protected:
  base::MessageLoop message_loop_;
  scoped_ptr<ChildProcess> child_process_;
  media::VideoCaptureParams params_;
  base::RunLoop cleanup_run_loop_;
  MockVideoCaptureImplManager manager_;

 private:
  DISALLOW_COPY_AND_ASSIGN(VideoCaptureImplManagerTest);
};

// Multiple clients with the same session id. There is only one
// media::VideoCapture object.
TEST_F(VideoCaptureImplManagerTest, MultipleClients) {
  scoped_ptr<MockVideoCaptureEventHandler> client1(
      new MockVideoCaptureEventHandler);
  scoped_ptr<MockVideoCaptureEventHandler> client2(
      new MockVideoCaptureEventHandler);

  media::VideoCapture* device1 = NULL;
  media::VideoCapture* device2 = NULL;

  scoped_ptr<VideoCaptureHandle> handle1;
  scoped_ptr<VideoCaptureHandle> handle2;
  {
    base::RunLoop run_loop;
    base::Closure quit_closure = BindToCurrentLoop(
        run_loop.QuitClosure());

    EXPECT_CALL(*client1, OnStarted(_)).WillOnce(SaveArg<0>(&device1));
    EXPECT_CALL(*client2, OnStarted(_)).WillOnce(
        DoAll(
            SaveArg<0>(&device2),
            RunClosure(quit_closure)));
    handle1 = manager_.UseDevice(1);
    handle2 = manager_.UseDevice(1);
    handle1->StartCapture(client1.get(), params_);
    handle2->StartCapture(client2.get(), params_);
    FakeChannelSetup();
    run_loop.Run();
  }

  {
    base::RunLoop run_loop;
    base::Closure quit_closure = BindToCurrentLoop(
        run_loop.QuitClosure());

    EXPECT_CALL(*client1, OnStopped(_));
    EXPECT_CALL(*client1, OnRemoved(_));
    EXPECT_CALL(*client2, OnStopped(_));
    EXPECT_CALL(*client2, OnRemoved(_)).WillOnce(
        RunClosure(quit_closure));
    handle1->StopCapture(client1.get());
    handle2->StopCapture(client2.get());
    run_loop.Run();
  }
  EXPECT_TRUE(device1 == device2);

  handle1.reset();
  handle2.reset();
  cleanup_run_loop_.Run();
}

}  // namespace content