| 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 "blimp/client/feature/compositor/blimp_compositor_manager.h" | |
| 6 | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "cc/proto/compositor_message.pb.h" | |
| 9 #include "testing/gmock/include/gmock/gmock.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 #include "ui/events/gesture_detection/motion_event_generic.h" | |
| 12 | |
| 13 using testing::_; | |
| 14 using testing::InSequence; | |
| 15 using testing::Sequence; | |
| 16 | |
| 17 namespace blimp { | |
| 18 namespace client { | |
| 19 | |
| 20 class MockRenderWidgetFeature : public RenderWidgetFeature { | |
| 21 public: | |
| 22 MOCK_METHOD3(SendCompositorMessage, | |
| 23 void(const int, | |
| 24 const int, | |
| 25 const cc::proto::CompositorMessage&)); | |
| 26 MOCK_METHOD3(SendInputEvent, void(const int, | |
| 27 const int, | |
| 28 const blink::WebInputEvent&)); | |
| 29 MOCK_METHOD2(SetDelegate, void(int, RenderWidgetFeatureDelegate*)); | |
| 30 MOCK_METHOD1(RemoveDelegate, void(const int)); | |
| 31 }; | |
| 32 | |
| 33 class MockBlimpCompositor : public BlimpCompositor { | |
| 34 public : | |
| 35 explicit MockBlimpCompositor(const int render_widget_id) | |
| 36 : BlimpCompositor(render_widget_id, nullptr) { | |
| 37 } | |
| 38 | |
| 39 MOCK_METHOD1(SetVisible, void(bool)); | |
| 40 MOCK_METHOD1(SetAcceleratedWidget, void(gfx::AcceleratedWidget)); | |
| 41 MOCK_METHOD0(ReleaseAcceleratedWidget, void()); | |
| 42 MOCK_METHOD1(OnTouchEvent, bool(const ui::MotionEvent& motion_event)); | |
| 43 | |
| 44 void OnCompositorMessageReceived( | |
| 45 std::unique_ptr<cc::proto::CompositorMessage> message) override { | |
| 46 MockableOnCompositorMessageReceived(*message); | |
| 47 } | |
| 48 MOCK_METHOD1(MockableOnCompositorMessageReceived, | |
| 49 void(const cc::proto::CompositorMessage&)); | |
| 50 }; | |
| 51 | |
| 52 class BlimpCompositorManagerForTesting : public BlimpCompositorManager { | |
| 53 public: | |
| 54 explicit BlimpCompositorManagerForTesting( | |
| 55 RenderWidgetFeature* render_widget_feature) | |
| 56 : BlimpCompositorManager(render_widget_feature, nullptr) {} | |
| 57 | |
| 58 using BlimpCompositorManager::GetCompositor; | |
| 59 | |
| 60 std::unique_ptr<BlimpCompositor> CreateBlimpCompositor( | |
| 61 int render_widget_id, | |
| 62 BlimpCompositorClient* client) override { | |
| 63 return base::WrapUnique(new MockBlimpCompositor(render_widget_id)); | |
| 64 } | |
| 65 }; | |
| 66 | |
| 67 class BlimpCompositorManagerTest : public testing::Test { | |
| 68 public: | |
| 69 void SetUp() override { | |
| 70 EXPECT_CALL(render_widget_feature_, SetDelegate(_, _)).Times(1); | |
| 71 EXPECT_CALL(render_widget_feature_, RemoveDelegate(_)).Times(1); | |
| 72 | |
| 73 compositor_manager_.reset( | |
| 74 new BlimpCompositorManagerForTesting(&render_widget_feature_)); | |
| 75 } | |
| 76 | |
| 77 void TearDown() override { | |
| 78 compositor_manager_.reset(); | |
| 79 } | |
| 80 | |
| 81 void SetUpCompositors() { | |
| 82 delegate()->OnRenderWidgetCreated(1); | |
| 83 delegate()->OnRenderWidgetCreated(2); | |
| 84 | |
| 85 mock_compositor1_ = static_cast<MockBlimpCompositor*> | |
| 86 (compositor_manager_->GetCompositor(1)); | |
| 87 mock_compositor2_ = static_cast<MockBlimpCompositor*> | |
| 88 (compositor_manager_->GetCompositor(2)); | |
| 89 | |
| 90 EXPECT_NE(mock_compositor1_, nullptr); | |
| 91 EXPECT_NE(mock_compositor2_, nullptr); | |
| 92 | |
| 93 EXPECT_EQ(mock_compositor1_->render_widget_id(), 1); | |
| 94 EXPECT_EQ(mock_compositor2_->render_widget_id(), 2); | |
| 95 } | |
| 96 | |
| 97 RenderWidgetFeature::RenderWidgetFeatureDelegate* delegate() const { | |
| 98 DCHECK(compositor_manager_); | |
| 99 return static_cast<RenderWidgetFeature::RenderWidgetFeatureDelegate*> | |
| 100 (compositor_manager_.get()); | |
| 101 } | |
| 102 | |
| 103 std::unique_ptr<BlimpCompositorManagerForTesting> compositor_manager_; | |
| 104 MockRenderWidgetFeature render_widget_feature_; | |
| 105 MockBlimpCompositor* mock_compositor1_; | |
| 106 MockBlimpCompositor* mock_compositor2_; | |
| 107 }; | |
| 108 | |
| 109 TEST_F(BlimpCompositorManagerTest, ForwardsMessagesToCorrectCompositor) { | |
| 110 SetUpCompositors(); | |
| 111 | |
| 112 // Ensure that the compositor messages for a render widget are forwarded to | |
| 113 // the correct compositor. | |
| 114 EXPECT_CALL(*mock_compositor1_, | |
| 115 MockableOnCompositorMessageReceived(_)).Times(2); | |
| 116 EXPECT_CALL(*mock_compositor2_, | |
| 117 MockableOnCompositorMessageReceived(_)).Times(1); | |
| 118 EXPECT_CALL(*mock_compositor1_, | |
| 119 SetVisible(false)).Times(1); | |
| 120 EXPECT_CALL(*mock_compositor1_, | |
| 121 SetAcceleratedWidget(gfx::kNullAcceleratedWidget)).Times(1); | |
| 122 | |
| 123 delegate()->OnCompositorMessageReceived( | |
| 124 1, base::WrapUnique(new cc::proto::CompositorMessage)); | |
| 125 delegate()->OnRenderWidgetInitialized(1); | |
| 126 delegate()->OnCompositorMessageReceived( | |
| 127 2, base::WrapUnique(new cc::proto::CompositorMessage)); | |
| 128 delegate()->OnCompositorMessageReceived( | |
| 129 1, base::WrapUnique(new cc::proto::CompositorMessage)); | |
| 130 | |
| 131 delegate()->OnRenderWidgetDeleted(1); | |
| 132 EXPECT_EQ(compositor_manager_->GetCompositor(1), nullptr); | |
| 133 } | |
| 134 | |
| 135 TEST_F(BlimpCompositorManagerTest, ForwardsViewEventsToCorrectCompositor) { | |
| 136 InSequence sequence; | |
| 137 SetUpCompositors(); | |
| 138 | |
| 139 EXPECT_CALL(*mock_compositor1_, SetVisible(true)); | |
| 140 EXPECT_CALL(*mock_compositor1_, SetAcceleratedWidget( | |
| 141 gfx::kNullAcceleratedWidget)); | |
| 142 EXPECT_CALL(*mock_compositor1_, OnTouchEvent(_)); | |
| 143 EXPECT_CALL(*mock_compositor1_, SetVisible(false)); | |
| 144 EXPECT_CALL(*mock_compositor1_, ReleaseAcceleratedWidget()); | |
| 145 | |
| 146 EXPECT_CALL(*mock_compositor2_, SetVisible(true)); | |
| 147 EXPECT_CALL(*mock_compositor2_, SetAcceleratedWidget( | |
| 148 gfx::kNullAcceleratedWidget)); | |
| 149 EXPECT_CALL(*mock_compositor2_, SetVisible(false)); | |
| 150 EXPECT_CALL(*mock_compositor2_, ReleaseAcceleratedWidget()); | |
| 151 | |
| 152 // Make the compositor manager visible and give it the accelerated widget | |
| 153 // while we don't have any render widget initialized. | |
| 154 compositor_manager_->SetVisible(true); | |
| 155 compositor_manager_->SetAcceleratedWidget(gfx::kNullAcceleratedWidget); | |
| 156 | |
| 157 // Initialize the first render widget. This should propagate the visibility, | |
| 158 // the accelerated widget and the touch events to the corresponding | |
| 159 // compositor. | |
| 160 delegate()->OnRenderWidgetInitialized(1); | |
| 161 compositor_manager_->OnTouchEvent( | |
| 162 ui::MotionEventGeneric(ui::MotionEvent::Action::ACTION_NONE, | |
| 163 base::TimeTicks::Now(), ui::PointerProperties())); | |
| 164 | |
| 165 // Now initialize the second render widget. This should swap the compositors | |
| 166 // and make the first one invisible and release the accelerated widget. | |
| 167 delegate()->OnRenderWidgetInitialized(2); | |
| 168 | |
| 169 // Now make the compositor manager invisible and release the accelerated | |
| 170 // widget from it. This should make the current compositor invisible and | |
| 171 // release the widget. | |
| 172 compositor_manager_->SetVisible(false); | |
| 173 compositor_manager_->ReleaseAcceleratedWidget(); | |
| 174 | |
| 175 // Destroy all the widgets. We should not be receiving any calls for the view | |
| 176 // events forwarded after this. | |
| 177 delegate()->OnRenderWidgetDeleted(1); | |
| 178 delegate()->OnRenderWidgetDeleted(2); | |
| 179 | |
| 180 compositor_manager_->SetVisible(true); | |
| 181 } | |
| 182 | |
| 183 } // namespace client | |
| 184 } // namespace blimp | |
| OLD | NEW |