| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/wake_lock/wake_lock_service_context.h" | 5 #include "content/browser/wake_lock/wake_lock_service_context.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/message_loop/message_loop.h" |
| 9 #include "base/process/kill.h" | 10 #include "base/process/kill.h" |
| 10 #include "content/browser/web_contents/web_contents_impl.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "content/public/browser/render_frame_host.h" | |
| 12 #include "content/public/browser/render_process_host.h" | |
| 13 #include "content/public/browser/web_contents.h" | |
| 14 #include "content/public/test/test_renderer_host.h" | |
| 15 | 12 |
| 16 namespace content { | 13 namespace content { |
| 17 | 14 |
| 18 class RenderFrameHost; | 15 class WakeLockServiceContextTest : public testing::Test { |
| 16 public: |
| 17 WakeLockServiceContextTest() |
| 18 : wake_lock_service_context_( |
| 19 base::ThreadTaskRunnerHandle::Get(), |
| 20 base::Bind(&WakeLockServiceContextTest::GetNativeView, |
| 21 base::Unretained(this))) {} |
| 19 | 22 |
| 20 class WakeLockServiceContextTest : public RenderViewHostTestHarness { | |
| 21 protected: | 23 protected: |
| 22 void RequestWakeLock(RenderFrameHost* rfh) { | 24 void RequestWakeLock() { GetWakeLockServiceContext()->RequestWakeLock(); } |
| 23 GetWakeLockServiceContext()->RequestWakeLock(rfh->GetProcess()->GetID(), | |
| 24 rfh->GetRoutingID()); | |
| 25 } | |
| 26 | 25 |
| 27 void CancelWakeLock(RenderFrameHost* rfh) { | 26 void CancelWakeLock() { GetWakeLockServiceContext()->CancelWakeLock(); } |
| 28 GetWakeLockServiceContext()->CancelWakeLock(rfh->GetProcess()->GetID(), | |
| 29 rfh->GetRoutingID()); | |
| 30 } | |
| 31 | 27 |
| 32 WakeLockServiceContext* GetWakeLockServiceContext() { | 28 WakeLockServiceContext* GetWakeLockServiceContext() { |
| 33 WebContentsImpl* web_contents_impl = | 29 return &wake_lock_service_context_; |
| 34 static_cast<WebContentsImpl*>(web_contents()); | |
| 35 return web_contents_impl->GetWakeLockServiceContext(); | |
| 36 } | 30 } |
| 37 | 31 |
| 38 bool HasWakeLock() { | 32 bool HasWakeLock() { |
| 39 return GetWakeLockServiceContext()->HasWakeLockForTests(); | 33 return GetWakeLockServiceContext()->HasWakeLockForTests(); |
| 40 } | 34 } |
| 35 |
| 36 private: |
| 37 gfx::NativeView GetNativeView() { return nullptr; } |
| 38 |
| 39 base::MessageLoop message_loop_; |
| 40 WakeLockServiceContext wake_lock_service_context_; |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 TEST_F(WakeLockServiceContextTest, NoLockInitially) { | 43 TEST_F(WakeLockServiceContextTest, NoLockInitially) { |
| 44 EXPECT_FALSE(HasWakeLock()); | 44 EXPECT_FALSE(HasWakeLock()); |
| 45 } | 45 } |
| 46 | 46 |
| 47 TEST_F(WakeLockServiceContextTest, LockUnlock) { | 47 TEST_F(WakeLockServiceContextTest, LockUnlock) { |
| 48 ASSERT_TRUE(GetWakeLockServiceContext()); | 48 ASSERT_TRUE(GetWakeLockServiceContext()); |
| 49 ASSERT_TRUE(web_contents()); | |
| 50 ASSERT_TRUE(main_rfh()); | |
| 51 | 49 |
| 52 // Request wake lock for main frame. | 50 // Request wake lock. |
| 53 RequestWakeLock(main_rfh()); | 51 RequestWakeLock(); |
| 54 | 52 |
| 55 // Should set the blocker. | 53 // Should set the blocker. |
| 56 EXPECT_TRUE(HasWakeLock()); | 54 EXPECT_TRUE(HasWakeLock()); |
| 57 | 55 |
| 58 // Remove wake lock request for main frame. | 56 // Remove wake lock request. |
| 59 CancelWakeLock(main_rfh()); | 57 CancelWakeLock(); |
| 60 | 58 |
| 61 // Should remove the blocker. | 59 // Should remove the blocker. |
| 62 EXPECT_FALSE(HasWakeLock()); | 60 EXPECT_FALSE(HasWakeLock()); |
| 63 } | 61 } |
| 64 | 62 |
| 65 TEST_F(WakeLockServiceContextTest, RenderFrameDeleted) { | |
| 66 ASSERT_TRUE(GetWakeLockServiceContext()); | |
| 67 ASSERT_TRUE(web_contents()); | |
| 68 ASSERT_TRUE(main_rfh()); | |
| 69 | |
| 70 // Request wake lock for main frame. | |
| 71 RequestWakeLock(main_rfh()); | |
| 72 | |
| 73 // Should set the blocker. | |
| 74 EXPECT_TRUE(HasWakeLock()); | |
| 75 | |
| 76 // Simulate render frame deletion. | |
| 77 GetWakeLockServiceContext()->RenderFrameDeleted(main_rfh()); | |
| 78 | |
| 79 // Should remove the blocker. | |
| 80 EXPECT_FALSE(HasWakeLock()); | |
| 81 } | |
| 82 | |
| 83 TEST_F(WakeLockServiceContextTest, NoLockForBogusFrameId) { | |
| 84 ASSERT_TRUE(GetWakeLockServiceContext()); | |
| 85 ASSERT_TRUE(web_contents()); | |
| 86 | |
| 87 // Request wake lock for non-existent render frame id. | |
| 88 int non_existent_render_frame_id = | |
| 89 main_rfh()->GetProcess()->GetNextRoutingID(); | |
| 90 GetWakeLockServiceContext()->RequestWakeLock( | |
| 91 main_rfh()->GetProcess()->GetID(), non_existent_render_frame_id); | |
| 92 | |
| 93 // Should not set the blocker. | |
| 94 EXPECT_FALSE(HasWakeLock()); | |
| 95 } | |
| 96 | |
| 97 } // namespace content | 63 } // namespace content |
| OLD | NEW |