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/engine/renderer/scheduler.h" |
| 6 |
| 7 #include "base/run_loop.h" |
| 8 #include "base/threading/thread_task_runner_handle.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace blimp { |
| 12 namespace engine { |
| 13 namespace { |
| 14 |
| 15 class SchedulerForTesting : public Scheduler { |
| 16 public: |
| 17 explicit SchedulerForTesting(SchedulerClient* client) |
| 18 // Use a zero time delta to let tests run main frames back-to-back. |
| 19 : Scheduler(base::TimeDelta::FromSeconds(0), |
| 20 base::ThreadTaskRunnerHandle::Get(), |
| 21 client) {} |
| 22 ~SchedulerForTesting() override = default; |
| 23 }; |
| 24 |
| 25 class SchedulerTest : public testing::Test, public SchedulerClient { |
| 26 public: |
| 27 SchedulerTest() : scheduler_(this) {} |
| 28 ~SchedulerTest() override {} |
| 29 |
| 30 // SchedulerClient implementation. |
| 31 void StartFrameUpdate() override { |
| 32 num_main_frames_++; |
| 33 if (send_client_update_during_main_frame_) { |
| 34 scheduler_.DidSendFrameUpdateToClient(); |
| 35 send_client_update_during_main_frame_ = false; |
| 36 } |
| 37 } |
| 38 |
| 39 protected: |
| 40 base::MessageLoop loop_; |
| 41 SchedulerForTesting scheduler_; |
| 42 int num_main_frames_ = 0; |
| 43 |
| 44 bool send_client_update_during_main_frame_ = false; |
| 45 }; |
| 46 |
| 47 TEST_F(SchedulerTest, FirstMainFrameRequest) { |
| 48 // The request for the first main frame should be responded to right away. |
| 49 scheduler_.SetNeedsFrameUpdate(); |
| 50 base::RunLoop().RunUntilIdle(); |
| 51 EXPECT_EQ(1, num_main_frames_); |
| 52 } |
| 53 |
| 54 TEST_F(SchedulerTest, MultipleMainFrameRequestsResultInSingleFrame) { |
| 55 // Multiple main frame requests result in a single callback to the client. |
| 56 scheduler_.SetNeedsFrameUpdate(); |
| 57 scheduler_.SetNeedsFrameUpdate(); |
| 58 base::RunLoop().RunUntilIdle(); |
| 59 EXPECT_EQ(1, num_main_frames_); |
| 60 } |
| 61 |
| 62 TEST_F(SchedulerTest, MainFrameBlockedOnAckFromClient) { |
| 63 // Request a main frame, start it and send an update to the client. The second |
| 64 // frame should remain blocked till an ack is received. |
| 65 scheduler_.SetNeedsFrameUpdate(); |
| 66 send_client_update_during_main_frame_ = true; |
| 67 base::RunLoop().RunUntilIdle(); |
| 68 EXPECT_EQ(1, num_main_frames_); |
| 69 |
| 70 // Request a frame and ensure that the client is not asked to start it. |
| 71 scheduler_.SetNeedsFrameUpdate(); |
| 72 base::RunLoop().RunUntilIdle(); |
| 73 EXPECT_EQ(1, num_main_frames_); |
| 74 |
| 75 // Now we have an ack from the client. This should start another frame. |
| 76 scheduler_.DidReceiveFrameUpdateAck(); |
| 77 base::RunLoop().RunUntilIdle(); |
| 78 EXPECT_EQ(2, num_main_frames_); |
| 79 } |
| 80 |
| 81 } // namespace |
| 82 } // namespace engine |
| 83 } // namespace blimp |
OLD | NEW |