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_dispatcher_host.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 WakeLockDispatcherHostTest : public RenderViewHostTestHarness { |
| 18 protected: |
| 19 void RequestWakeLock(RenderFrameHost* rfh) { |
| 20 ASSERT_TRUE(GetWakeLockDispatcherHost()); |
| 21 GetWakeLockDispatcherHost()->RequestWakeLock(rfh); |
| 22 } |
| 23 |
| 24 void CancelWakeLock(RenderFrameHost* rfh) { |
| 25 ASSERT_TRUE(GetWakeLockDispatcherHost()); |
| 26 GetWakeLockDispatcherHost()->CancelWakeLock(rfh); |
| 27 } |
| 28 |
| 29 WakeLockDispatcherHost* GetWakeLockDispatcherHost() { |
| 30 WebContentsImpl* web_contents_impl = |
| 31 static_cast<WebContentsImpl*>(web_contents()); |
| 32 return web_contents_impl->wake_lock_dispatcher_host(); |
| 33 } |
| 34 }; |
| 35 |
| 36 TEST_F(WakeLockDispatcherHostTest, NoLockInitially) { |
| 37 ASSERT_TRUE(GetWakeLockDispatcherHost()); |
| 38 EXPECT_FALSE(GetWakeLockDispatcherHost()->has_blocker_for_testing()); |
| 39 } |
| 40 |
| 41 TEST_F(WakeLockDispatcherHostTest, LockUnlock) { |
| 42 ASSERT_TRUE(GetWakeLockDispatcherHost()); |
| 43 ASSERT_TRUE(web_contents()); |
| 44 ASSERT_TRUE(main_rfh()); |
| 45 // Request wake lock for main frame |
| 46 RequestWakeLock(main_rfh()); |
| 47 // Should set the blocker |
| 48 EXPECT_TRUE(GetWakeLockDispatcherHost()->has_blocker_for_testing()); |
| 49 // Remove wake lock request for main frame |
| 50 CancelWakeLock(main_rfh()); |
| 51 // Should remove the blocker |
| 52 EXPECT_FALSE(GetWakeLockDispatcherHost()->has_blocker_for_testing()); |
| 53 } |
| 54 |
| 55 TEST_F(WakeLockDispatcherHostTest, RenderFrameDeleted) { |
| 56 ASSERT_TRUE(GetWakeLockDispatcherHost()); |
| 57 ASSERT_TRUE(web_contents()); |
| 58 ASSERT_TRUE(main_rfh()); |
| 59 // Request wake lock for main frame |
| 60 RequestWakeLock(main_rfh()); |
| 61 // Should set the blocker |
| 62 EXPECT_TRUE(GetWakeLockDispatcherHost()->has_blocker_for_testing()); |
| 63 // Simulate render frame deletion |
| 64 GetWakeLockDispatcherHost()->RenderFrameDeleted(main_rfh()); |
| 65 // Should remove the blocker |
| 66 EXPECT_FALSE(GetWakeLockDispatcherHost()->has_blocker_for_testing()); |
| 67 } |
| 68 |
| 69 TEST_F(WakeLockDispatcherHostTest, RenderFrameHostChanged) { |
| 70 ASSERT_TRUE(GetWakeLockDispatcherHost()); |
| 71 ASSERT_TRUE(web_contents()); |
| 72 ASSERT_TRUE(main_rfh()); |
| 73 // Request wake lock for main frame |
| 74 RequestWakeLock(main_rfh()); |
| 75 // Should set the blocker |
| 76 EXPECT_TRUE(GetWakeLockDispatcherHost()->has_blocker_for_testing()); |
| 77 // Simulate render frame host change |
| 78 GetWakeLockDispatcherHost()->RenderFrameHostChanged(main_rfh(), nullptr); |
| 79 // Should remove the blocker |
| 80 EXPECT_FALSE(GetWakeLockDispatcherHost()->has_blocker_for_testing()); |
| 81 } |
| 82 |
| 83 } // namespace content |
OLD | NEW |