| OLD | NEW | 
| (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 PipelineMetadata defaultMetadata() { | 
 |   25   PipelineMetadata data; | 
 |   26   data.has_audio = true; | 
 |   27   data.has_video = true; | 
 |   28   data.video_decoder_config = VideoDecoderConfig( | 
 |   29       kCodecVP8, VP8PROFILE_ANY, VideoPixelFormat::PIXEL_FORMAT_I420, | 
 |   30       ColorSpace::COLOR_SPACE_SD_REC601, kCodedSize, kVisibleRect, kNaturalSize, | 
 |   31       EmptyExtraData(), Unencrypted()); | 
 |   32   data.audio_decoder_config = AudioDecoderConfig( | 
 |   33       kCodecOpus, SampleFormat::kSampleFormatU8, | 
 |   34       ChannelLayout::CHANNEL_LAYOUT_MONO, limits::kMinSampleRate, | 
 |   35       EmptyExtraData(), Unencrypted()); | 
 |   36   return data; | 
 |   37 } | 
 |   38  | 
 |   39 class FakeRemoter final : public mojom::Remoter { | 
 |   40  public: | 
 |   41   // |start_will_fail| indicates whether starting remoting will fail. | 
 |   42   FakeRemoter(mojom::RemotingSourcePtr source, bool start_will_fail) | 
 |   43       : source_(std::move(source)), start_will_fail_(start_will_fail) {} | 
 |   44   ~FakeRemoter() override {} | 
 |   45  | 
 |   46   // mojom::Remoter implementations. | 
 |   47   void Start() override { | 
 |   48     if (start_will_fail_) { | 
 |   49       base::ThreadTaskRunnerHandle::Get()->PostTask( | 
 |   50           FROM_HERE, | 
 |   51           base::Bind(&FakeRemoter::StartFailed, base::Unretained(this))); | 
 |   52     } else { | 
 |   53       base::ThreadTaskRunnerHandle::Get()->PostTask( | 
 |   54           FROM_HERE, base::Bind(&FakeRemoter::Started, base::Unretained(this))); | 
 |   55     } | 
 |   56   } | 
 |   57  | 
 |   58   void StartDataStreams( | 
 |   59       mojo::ScopedDataPipeConsumerHandle audio_pipe, | 
 |   60       mojo::ScopedDataPipeConsumerHandle video_pipe, | 
 |   61       mojom::RemotingDataStreamSenderRequest audio_sender_request, | 
 |   62       mojom::RemotingDataStreamSenderRequest video_sender_request) override {} | 
 |   63  | 
 |   64   void Stop(mojom::RemotingStopReason reason) override { | 
 |   65     base::ThreadTaskRunnerHandle::Get()->PostTask( | 
 |   66         FROM_HERE, | 
 |   67         base::Bind(&FakeRemoter::Stopped, base::Unretained(this), reason)); | 
 |   68   } | 
 |   69  | 
 |   70   void SendMessageToSink(const std::vector<uint8_t>& message) override {} | 
 |   71  | 
 |   72  private: | 
 |   73   void Started() { source_->OnStarted(); } | 
 |   74   void StartFailed() { | 
 |   75     source_->OnStartFailed(mojom::RemotingStartFailReason::ROUTE_TERMINATED); | 
 |   76   } | 
 |   77   void Stopped(mojom::RemotingStopReason reason) { source_->OnStopped(reason); } | 
 |   78  | 
 |   79   mojom::RemotingSourcePtr source_; | 
 |   80   bool start_will_fail_; | 
 |   81  | 
 |   82   DISALLOW_COPY_AND_ASSIGN(FakeRemoter); | 
 |   83 }; | 
 |   84  | 
 |   85 class FakeRemoterFactory final : public mojom::RemoterFactory { | 
 |   86  public: | 
 |   87   // |start_will_fail| indicates whether starting remoting will fail. | 
 |   88   explicit FakeRemoterFactory(bool start_will_fail) | 
 |   89       : start_will_fail_(start_will_fail) {} | 
 |   90   ~FakeRemoterFactory() override {} | 
 |   91  | 
 |   92   void Create(mojom::RemotingSourcePtr source, | 
 |   93               mojom::RemoterRequest request) override { | 
 |   94     mojo::MakeStrongBinding( | 
 |   95         base::MakeUnique<FakeRemoter>(std::move(source), start_will_fail_), | 
 |   96         std::move(request)); | 
 |   97   } | 
 |   98  | 
 |   99  private: | 
 |  100   bool start_will_fail_; | 
 |  101  | 
 |  102   DISALLOW_COPY_AND_ASSIGN(FakeRemoterFactory); | 
 |  103 }; | 
 |  104  | 
 |  105 std::unique_ptr<RemotingController> CreateRemotingController( | 
 |  106     mojom::RemoterFactory* remoter_factory) { | 
 |  107   mojom::RemotingSourcePtr remoting_source; | 
 |  108   mojom::RemotingSourceRequest remoting_source_request = | 
 |  109       mojo::GetProxy(&remoting_source); | 
 |  110   mojom::RemoterPtr remoter; | 
 |  111   remoter_factory->Create(std::move(remoting_source), mojo::GetProxy(&remoter)); | 
 |  112   std::unique_ptr<RemotingController> remoting_controller = | 
 |  113       base::MakeUnique<RemotingController>(std::move(remoting_source_request), | 
 |  114                                            std::move(remoter)); | 
 |  115   return remoting_controller; | 
 |  116 } | 
 |  117  | 
 |  118 }  // namespace | 
 |  119  | 
 |  120 class RemotingControllerTest : public ::testing::Test { | 
 |  121  public: | 
 |  122   RemotingControllerTest() | 
 |  123       : remoting_controller_( | 
 |  124             CreateRemotingController(new FakeRemoterFactory(false))), | 
 |  125         is_remoting_(false) { | 
 |  126     remoting_controller_->SetSwitchRendererCallback(base::Bind( | 
 |  127         &RemotingControllerTest::ToggleRenderer, base::Unretained(this))); | 
 |  128   } | 
 |  129   ~RemotingControllerTest() override {} | 
 |  130  | 
 |  131   void TearDown() final { RunUntilIdle(); } | 
 |  132  | 
 |  133   static void RunUntilIdle() { base::RunLoop().RunUntilIdle(); } | 
 |  134  | 
 |  135   void ToggleRenderer() { is_remoting_ = remoting_controller_->is_remoting(); } | 
 |  136  | 
 |  137   base::MessageLoop message_loop_; | 
 |  138  | 
 |  139  protected: | 
 |  140   std::unique_ptr<RemotingController> remoting_controller_; | 
 |  141   bool is_remoting_; | 
 |  142  | 
 |  143  private: | 
 |  144   DISALLOW_COPY_AND_ASSIGN(RemotingControllerTest); | 
 |  145 }; | 
 |  146  | 
 |  147 TEST_F(RemotingControllerTest, ToggleRenderer) { | 
 |  148   EXPECT_FALSE(is_remoting_); | 
 |  149   remoting_controller_->OnSinkAvailable(); | 
 |  150   remoting_controller_->OnEnteredFullscreen(); | 
 |  151   EXPECT_FALSE(is_remoting_); | 
 |  152   remoting_controller_->OnMetadataChanged(defaultMetadata()); | 
 |  153   RunUntilIdle(); | 
 |  154   EXPECT_TRUE(is_remoting_); | 
 |  155   remoting_controller_->OnExitedFullscreen(); | 
 |  156   RunUntilIdle(); | 
 |  157   EXPECT_FALSE(is_remoting_); | 
 |  158 } | 
 |  159  | 
 |  160 TEST_F(RemotingControllerTest, StartFailed) { | 
 |  161   EXPECT_FALSE(is_remoting_); | 
 |  162   remoting_controller_ = CreateRemotingController(new FakeRemoterFactory(true)); | 
 |  163   remoting_controller_->SetSwitchRendererCallback(base::Bind( | 
 |  164       &RemotingControllerTest::ToggleRenderer, base::Unretained(this))); | 
 |  165   remoting_controller_->OnSinkAvailable(); | 
 |  166   remoting_controller_->OnEnteredFullscreen(); | 
 |  167   remoting_controller_->OnMetadataChanged(defaultMetadata()); | 
 |  168   RunUntilIdle(); | 
 |  169   EXPECT_FALSE(is_remoting_); | 
 |  170 } | 
 |  171  | 
 |  172 }  // namespace media | 
| OLD | NEW |