Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(610)

Side by Side Diff: media/remoting/remoting_controller_unittest.cc

Issue 2389473002: Media Remoting: Add RemotingController. (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "media/remoting/remoting_controller.h"
6
7 #include "base/callback.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "media/base/audio_decoder_config.h"
11 #include "media/base/limits.h"
12 #include "media/base/media_util.h"
13 #include "media/base/video_decoder_config.h"
14 #include "mojo/public/cpp/bindings/strong_binding.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace media {
18 namespace {
19
20 constexpr gfx::Size kCodedSize(320, 240);
21 constexpr gfx::Rect kVisibleRect(320, 240);
22 constexpr gfx::Size kNaturalSize(320, 240);
23
24 AudioDecoderConfig defaultAudioConfig() {
25 AudioDecoderConfig config(kCodecOpus, SampleFormat::kSampleFormatU8,
26 ChannelLayout::CHANNEL_LAYOUT_MONO,
27 limits::kMinSampleRate, EmptyExtraData(),
28 Unencrypted());
29 return config;
30 }
31
32 VideoDecoderConfig defaultVideoConfig() {
33 VideoDecoderConfig config(
34 kCodecVP8, VP8PROFILE_ANY, VideoPixelFormat::PIXEL_FORMAT_I420,
35 ColorSpace::COLOR_SPACE_SD_REC601, kCodedSize, kVisibleRect, kNaturalSize,
36 EmptyExtraData(), Unencrypted());
37 return config;
38 }
39
40 class FakeRemoter final : public mojom::Remoter {
41 public:
42 // |start_will_fail| indicates whether starting remoting will fail.
43 FakeRemoter(mojom::RemotingSourcePtr source, bool start_will_fail)
44 : source_(std::move(source)), start_will_fail_(start_will_fail) {}
45 ~FakeRemoter() override {}
46
47 // mojom::Remoter implementations.
48 void Start() override {
49 if (start_will_fail_) {
50 base::ThreadTaskRunnerHandle::Get()->PostTask(
51 FROM_HERE,
52 base::Bind(&FakeRemoter::StartFailed, base::Unretained(this)));
53 } else {
54 base::ThreadTaskRunnerHandle::Get()->PostTask(
55 FROM_HERE, base::Bind(&FakeRemoter::Started, base::Unretained(this)));
56 }
57 }
58
59 void StartDataStreams(
60 mojo::ScopedDataPipeConsumerHandle audio_pipe,
61 mojo::ScopedDataPipeConsumerHandle video_pipe,
62 mojom::RemotingDataStreamSenderRequest audio_sender_request,
63 mojom::RemotingDataStreamSenderRequest video_sender_request) override {}
64
65 void Stop(mojom::RemotingStopReason reason) override {
66 base::ThreadTaskRunnerHandle::Get()->PostTask(
67 FROM_HERE,
68 base::Bind(&FakeRemoter::Stopped, base::Unretained(this), reason));
69 }
70
71 void SendMessageToSink(const std::vector<uint8_t>& message) override {}
72
73 private:
74 void Started() { source_->OnStarted(); }
75 void StartFailed() {
76 source_->OnStartFailed(mojom::RemotingStartFailReason::ROUTE_TERMINATED);
77 }
78 void Stopped(mojom::RemotingStopReason reason) { source_->OnStopped(reason); }
79
80 mojom::RemotingSourcePtr source_;
81 bool start_will_fail_;
82
83 DISALLOW_COPY_AND_ASSIGN(FakeRemoter);
84 };
85
86 class FakeRemoterFactory final : public mojom::RemoterFactory {
87 public:
88 // |start_will_fail| indicates whether starting remoting will fail.
89 explicit FakeRemoterFactory(bool start_will_fail)
90 : start_will_fail_(start_will_fail) {}
91 ~FakeRemoterFactory() override {}
92
93 void Create(mojom::RemotingSourcePtr source,
94 mojom::RemoterRequest request) override {
95 mojo::MakeStrongBinding(
96 base::MakeUnique<FakeRemoter>(std::move(source), start_will_fail_),
97 std::move(request));
98 }
99
100 private:
101 bool start_will_fail_;
102
103 DISALLOW_COPY_AND_ASSIGN(FakeRemoterFactory);
104 };
105
106 } // namespace
107
108 class RemotingControllerTest : public ::testing::Test {
109 public:
110 RemotingControllerTest()
111 : remoting_controller_(
112 new RemotingController(new FakeRemoterFactory(false))),
113 is_remoting_(false) {
114 remoting_controller_->SetSwitchRenderCallback(base::Bind(
115 &RemotingControllerTest::ToggleRenderer, base::Unretained(this)));
116 }
117 ~RemotingControllerTest() override {}
118
119 void TearDown() final { RunUntilIdle(); }
120
121 static void RunUntilIdle() { base::RunLoop().RunUntilIdle(); }
122
123 void ToggleRenderer() { is_remoting_ = remoting_controller_->is_remoting(); }
124
125 base::MessageLoop message_loop_;
126
127 protected:
128 std::unique_ptr<RemotingController> remoting_controller_;
129 bool is_remoting_;
130
131 private:
132 DISALLOW_COPY_AND_ASSIGN(RemotingControllerTest);
133 };
134
135 TEST_F(RemotingControllerTest, ToggleRenderer) {
136 EXPECT_FALSE(is_remoting_);
137 remoting_controller_->OnSinkAvailable();
138 remoting_controller_->OnEnteredFullscreen();
139 EXPECT_FALSE(is_remoting_);
140 remoting_controller_->OnDecoderConfigChanged(defaultAudioConfig(),
141 defaultVideoConfig());
142 RunUntilIdle();
143 EXPECT_TRUE(is_remoting_);
144 remoting_controller_->OnExitedFullscreen();
145 RunUntilIdle();
146 EXPECT_FALSE(is_remoting_);
147 }
148
149 TEST_F(RemotingControllerTest, StartFailed) {
150 EXPECT_FALSE(is_remoting_);
151 remoting_controller_.reset(
152 new RemotingController(new FakeRemoterFactory(true)));
153 remoting_controller_->OnSinkAvailable();
154 remoting_controller_->OnEnteredFullscreen();
155 remoting_controller_->OnDecoderConfigChanged(defaultAudioConfig(),
156 defaultVideoConfig());
157 RunUntilIdle();
158 EXPECT_FALSE(is_remoting_);
159 }
160
161 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698