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