Home | History | Annotate | Download | only in bridge
      1 // Copyright 2014 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 "remoting/ios/bridge/frame_consumer_bridge.h"
      6 
      7 #include <queue>
      8 #include <gtest/gtest.h>
      9 
     10 #include "base/bind.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
     13 #include "third_party/webrtc/modules/desktop_capture/desktop_region.h"
     14 
     15 namespace {
     16 const webrtc::DesktopSize kFrameSize(100, 100);
     17 const webrtc::DesktopVector kDpi(100, 100);
     18 
     19 const webrtc::DesktopRect FrameRect() {
     20   return webrtc::DesktopRect::MakeSize(kFrameSize);
     21 }
     22 
     23 webrtc::DesktopRegion FrameRegion() {
     24   return webrtc::DesktopRegion(FrameRect());
     25 }
     26 
     27 void FrameDelivery(const webrtc::DesktopSize& view_size,
     28                    webrtc::DesktopFrame* buffer,
     29                    const webrtc::DesktopRegion& region) {
     30   ASSERT_TRUE(view_size.equals(kFrameSize));
     31   ASSERT_TRUE(region.Equals(FrameRegion()));
     32 };
     33 
     34 }  // namespace
     35 
     36 namespace remoting {
     37 
     38 class FrameProducerTester : public FrameProducer {
     39  public:
     40   virtual ~FrameProducerTester() {};
     41 
     42   virtual void DrawBuffer(webrtc::DesktopFrame* buffer) OVERRIDE {
     43     frames.push(buffer);
     44   };
     45 
     46   virtual void InvalidateRegion(const webrtc::DesktopRegion& region) OVERRIDE {
     47     NOTIMPLEMENTED();
     48   };
     49 
     50   virtual void RequestReturnBuffers(const base::Closure& done) OVERRIDE {
     51     // Don't have to actually return the buffers.  This function is really
     52     // saying don't use the references anymore, they are now invalid.
     53     while (!frames.empty()) {
     54       frames.pop();
     55     }
     56     done.Run();
     57   };
     58 
     59   virtual void SetOutputSizeAndClip(const webrtc::DesktopSize& view_size,
     60                                     const webrtc::DesktopRect& clip_area)
     61       OVERRIDE {
     62     viewSize = view_size;
     63     clipArea = clip_area;
     64   };
     65 
     66   std::queue<webrtc::DesktopFrame*> frames;
     67   webrtc::DesktopSize viewSize;
     68   webrtc::DesktopRect clipArea;
     69 };
     70 
     71 class FrameConsumerBridgeTest : public ::testing::Test {
     72  protected:
     73   virtual void SetUp() OVERRIDE {
     74     frameProducer_.reset(new FrameProducerTester());
     75     frameConsumer_.reset(new FrameConsumerBridge(base::Bind(&FrameDelivery)));
     76     frameConsumer_->Initialize(frameProducer_.get());
     77   }
     78   virtual void TearDown() OVERRIDE {}
     79 
     80   scoped_ptr<FrameProducerTester> frameProducer_;
     81   scoped_ptr<FrameConsumerBridge> frameConsumer_;
     82 };
     83 
     84 TEST(FrameConsumerBridgeTest_NotInitialized, CreateAndRelease) {
     85   scoped_ptr<FrameConsumerBridge> frameConsumer_(
     86       new FrameConsumerBridge(base::Bind(&FrameDelivery)));
     87   ASSERT_TRUE(frameConsumer_.get() != NULL);
     88   frameConsumer_.reset();
     89   ASSERT_TRUE(frameConsumer_.get() == NULL);
     90 }
     91 
     92 TEST_F(FrameConsumerBridgeTest, ApplyBuffer) {
     93   webrtc::DesktopFrame* frame = NULL;
     94   ASSERT_EQ(0, frameProducer_->frames.size());
     95   frameConsumer_->SetSourceSize(kFrameSize, kDpi);
     96   ASSERT_EQ(1, frameProducer_->frames.size());
     97 
     98   // Return the frame, and ensure we get it back
     99   frame = frameProducer_->frames.front();
    100   frameProducer_->frames.pop();
    101   ASSERT_EQ(0, frameProducer_->frames.size());
    102   frameConsumer_->ApplyBuffer(
    103       kFrameSize, FrameRect(), frame, FrameRegion(), FrameRegion());
    104   ASSERT_EQ(1, frameProducer_->frames.size());
    105   ASSERT_TRUE(frame == frameProducer_->frames.front());
    106   ASSERT_TRUE(frame->data() == frameProducer_->frames.front()->data());
    107 
    108   // Change the SourceSize, we should get a new frame, but when the old frame is
    109   // submitted we will not get it back.
    110   frameConsumer_->SetSourceSize(webrtc::DesktopSize(1, 1), kDpi);
    111   ASSERT_EQ(2, frameProducer_->frames.size());
    112   frame = frameProducer_->frames.front();
    113   frameProducer_->frames.pop();
    114   ASSERT_EQ(1, frameProducer_->frames.size());
    115   frameConsumer_->ApplyBuffer(
    116       kFrameSize, FrameRect(), frame, FrameRegion(), FrameRegion());
    117   ASSERT_EQ(1, frameProducer_->frames.size());
    118 }
    119 
    120 TEST_F(FrameConsumerBridgeTest, SetSourceSize) {
    121   frameConsumer_->SetSourceSize(webrtc::DesktopSize(0, 0),
    122                                 webrtc::DesktopVector(0, 0));
    123   ASSERT_TRUE(frameProducer_->viewSize.equals(webrtc::DesktopSize(0, 0)));
    124   ASSERT_TRUE(frameProducer_->clipArea.equals(
    125       webrtc::DesktopRect::MakeLTRB(0, 0, 0, 0)));
    126   ASSERT_EQ(1, frameProducer_->frames.size());
    127   ASSERT_TRUE(
    128       frameProducer_->frames.front()->size().equals(webrtc::DesktopSize(0, 0)));
    129 
    130   frameConsumer_->SetSourceSize(kFrameSize, kDpi);
    131   ASSERT_TRUE(frameProducer_->viewSize.equals(kFrameSize));
    132   ASSERT_TRUE(frameProducer_->clipArea.equals(FrameRect()));
    133   ASSERT_EQ(2, frameProducer_->frames.size());
    134   frameProducer_->frames.pop();
    135   ASSERT_TRUE(frameProducer_->frames.front()->size().equals(kFrameSize));
    136 }
    137 
    138 }  // namespace remoting