OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/wake_lock/wake_lock_service_context.h" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/process/kill.h" |
| 9 #include "content/browser/web_contents/web_contents_impl.h" |
| 10 #include "content/public/browser/render_frame_host.h" |
| 11 #include "content/public/browser/render_process_host.h" |
| 12 #include "content/public/browser/web_contents.h" |
| 13 #include "content/public/test/test_renderer_host.h" |
| 14 |
| 15 namespace content { |
| 16 |
| 17 class RenderFrameHost; |
| 18 |
| 19 class WakeLockServiceContextTest : public RenderViewHostTestHarness { |
| 20 protected: |
| 21 void RequestWakeLock(RenderFrameHost* rfh) { |
| 22 GetWakeLockServiceContext()->RequestWakeLock(rfh->GetProcess()->GetID(), |
| 23 rfh->GetRoutingID()); |
| 24 } |
| 25 |
| 26 void CancelWakeLock(RenderFrameHost* rfh) { |
| 27 GetWakeLockServiceContext()->CancelWakeLock(rfh->GetProcess()->GetID(), |
| 28 rfh->GetRoutingID()); |
| 29 } |
| 30 |
| 31 WakeLockServiceContext* GetWakeLockServiceContext() { |
| 32 WebContentsImpl* web_contents_impl = |
| 33 static_cast<WebContentsImpl*>(web_contents()); |
| 34 return web_contents_impl->GetWakeLockServiceContext(); |
| 35 } |
| 36 |
| 37 bool HasWakeLock() { |
| 38 return GetWakeLockServiceContext()->HasWakeLockForTests(); |
| 39 } |
| 40 }; |
| 41 |
| 42 TEST_F(WakeLockServiceContextTest, NoLockInitially) { |
| 43 EXPECT_FALSE(HasWakeLock()); |
| 44 } |
| 45 |
| 46 TEST_F(WakeLockServiceContextTest, LockUnlock) { |
| 47 ASSERT_TRUE(GetWakeLockServiceContext()); |
| 48 ASSERT_TRUE(web_contents()); |
| 49 ASSERT_TRUE(main_rfh()); |
| 50 |
| 51 // Request wake lock for main frame. |
| 52 RequestWakeLock(main_rfh()); |
| 53 |
| 54 // Should set the blocker. |
| 55 EXPECT_TRUE(HasWakeLock()); |
| 56 |
| 57 // Remove wake lock request for main frame. |
| 58 CancelWakeLock(main_rfh()); |
| 59 |
| 60 // Should remove the blocker. |
| 61 EXPECT_FALSE(HasWakeLock()); |
| 62 } |
| 63 |
| 64 TEST_F(WakeLockServiceContextTest, RenderFrameDeleted) { |
| 65 ASSERT_TRUE(GetWakeLockServiceContext()); |
| 66 ASSERT_TRUE(web_contents()); |
| 67 ASSERT_TRUE(main_rfh()); |
| 68 |
| 69 // Request wake lock for main frame. |
| 70 RequestWakeLock(main_rfh()); |
| 71 |
| 72 // Should set the blocker. |
| 73 EXPECT_TRUE(HasWakeLock()); |
| 74 |
| 75 // Simulate render frame deletion. |
| 76 GetWakeLockServiceContext()->RenderFrameDeleted(main_rfh()); |
| 77 |
| 78 // Should remove the blocker. |
| 79 EXPECT_FALSE(HasWakeLock()); |
| 80 } |
| 81 |
| 82 TEST_F(WakeLockServiceContextTest, NoLockForBogusFrameId) { |
| 83 ASSERT_TRUE(GetWakeLockServiceContext()); |
| 84 ASSERT_TRUE(web_contents()); |
| 85 |
| 86 // Request wake lock for non-existent render frame id. |
| 87 int non_existent_render_frame_id = |
| 88 main_rfh()->GetProcess()->GetNextRoutingID(); |
| 89 GetWakeLockServiceContext()->RequestWakeLock( |
| 90 main_rfh()->GetProcess()->GetID(), non_existent_render_frame_id); |
| 91 |
| 92 // Should not set the blocker. |
| 93 EXPECT_FALSE(HasWakeLock()); |
| 94 } |
| 95 |
| 96 } // namespace content |
OLD | NEW |