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/frame_host/frame_tree_node.h" |
| 10 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 11 #include "content/browser/web_contents/web_contents_impl.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(RFHToFrameNodeID(rfh)); |
| 23 } |
| 24 |
| 25 void CancelWakeLock(RenderFrameHost* rfh) { |
| 26 GetWakeLockServiceContext()->CancelWakeLock(RFHToFrameNodeID(rfh)); |
| 27 } |
| 28 |
| 29 int RFHToFrameNodeID(RenderFrameHost* rfh) { |
| 30 RenderFrameHostImpl* rfh_impl = |
| 31 static_cast<RenderFrameHostImpl*>(rfh); |
| 32 return rfh_impl->frame_tree_node()->frame_tree_node_id(); |
| 33 } |
| 34 |
| 35 WakeLockServiceContext* GetWakeLockServiceContext() { |
| 36 WebContentsImpl* web_contents_impl = |
| 37 static_cast<WebContentsImpl*>(web_contents()); |
| 38 return web_contents_impl->GetWakeLockServiceContext(); |
| 39 } |
| 40 |
| 41 bool HasWakeLock() { |
| 42 return GetWakeLockServiceContext()->HasWakeLockForTests(); |
| 43 } |
| 44 }; |
| 45 |
| 46 TEST_F(WakeLockServiceContextTest, NoLockInitially) { |
| 47 EXPECT_FALSE(HasWakeLock()); |
| 48 } |
| 49 |
| 50 TEST_F(WakeLockServiceContextTest, LockUnlock) { |
| 51 ASSERT_TRUE(web_contents()); |
| 52 ASSERT_TRUE(main_rfh()); |
| 53 |
| 54 // Request wake lock for main frame |
| 55 RequestWakeLock(main_rfh()); |
| 56 |
| 57 // Should set the blocker |
| 58 EXPECT_TRUE(HasWakeLock()); |
| 59 |
| 60 // Remove wake lock request for main frame |
| 61 CancelWakeLock(main_rfh()); |
| 62 |
| 63 // Should remove the blocker |
| 64 EXPECT_FALSE(HasWakeLock()); |
| 65 } |
| 66 |
| 67 TEST_F(WakeLockServiceContextTest, RenderFrameDeleted) { |
| 68 ASSERT_TRUE(GetWakeLockServiceContext()); |
| 69 ASSERT_TRUE(web_contents()); |
| 70 ASSERT_TRUE(main_rfh()); |
| 71 |
| 72 // Request wake lock for main frame |
| 73 RequestWakeLock(main_rfh()); |
| 74 |
| 75 // Should set the blocker |
| 76 EXPECT_TRUE(HasWakeLock()); |
| 77 |
| 78 // Simulate render frame deletion |
| 79 GetWakeLockServiceContext()->RenderFrameDeleted(main_rfh()); |
| 80 |
| 81 // Should remove the blocker |
| 82 EXPECT_FALSE(HasWakeLock()); |
| 83 } |
| 84 |
| 85 TEST_F(WakeLockServiceContextTest, RenderFrameHostChanged) { |
| 86 ASSERT_TRUE(GetWakeLockServiceContext()); |
| 87 ASSERT_TRUE(web_contents()); |
| 88 ASSERT_TRUE(main_rfh()); |
| 89 |
| 90 // Request wake lock for main frame |
| 91 RequestWakeLock(main_rfh()); |
| 92 |
| 93 // Should set the blocker |
| 94 EXPECT_TRUE(HasWakeLock()); |
| 95 |
| 96 // Simulate render frame host change |
| 97 GetWakeLockServiceContext()->RenderFrameHostChanged(main_rfh(), nullptr); |
| 98 |
| 99 // Should remove the blocker |
| 100 EXPECT_FALSE(HasWakeLock()); |
| 101 } |
| 102 |
| 103 } // namespace content |
OLD | NEW |