Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: chrome/browser/lifetime/keep_alive_registry_unittest.cc

Issue 1725883002: Add KeepAliveStateObserver, add the Restart option (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@KeepAlive
Patch Set: Address comments Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "chrome/browser/lifetime/keep_alive_registry.h" 5 #include "chrome/browser/lifetime/keep_alive_registry.h"
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/browser/lifetime/application_lifetime.h" 8 #include "chrome/browser/lifetime/application_lifetime.h"
9 #include "chrome/browser/lifetime/keep_alive_state_observer.h"
9 #include "chrome/browser/lifetime/keep_alive_types.h" 10 #include "chrome/browser/lifetime/keep_alive_types.h"
10 #include "chrome/browser/lifetime/scoped_keep_alive.h" 11 #include "chrome/browser/lifetime/scoped_keep_alive.h"
11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
12 13
13 // Test the WillKeepAlive state and when we interact with the browser with 14 class KeepAliveRegistryTest : public testing::Test,
15 public KeepAliveStateObserver {
16 public:
17 KeepAliveRegistryTest()
18 : on_restart_allowed_call_count_(0),
19 on_restart_forbidden_call_count_(0),
20 registry_(KeepAliveRegistry::GetInstance()) {
21 registry_->AddObserver(this);
22
23 EXPECT_FALSE(registry_->IsKeepingAlive());
24 }
25
26 ~KeepAliveRegistryTest() override {
27 registry_->RemoveObserver(this);
28
29 EXPECT_FALSE(registry_->IsKeepingAlive());
30 }
31
32 void OnKeepAliveRestartStateChanged(bool can_restart) override {
33 if (can_restart)
34 ++on_restart_allowed_call_count_;
35 else
36 ++on_restart_forbidden_call_count_;
37 }
38
39 protected:
40 int on_restart_allowed_call_count_;
41 int on_restart_forbidden_call_count_;
42 KeepAliveRegistry* registry_;
43 };
44
45 // Test the IsKeepingAlive state and when we interact with the browser with
14 // a KeepAlive registered. 46 // a KeepAlive registered.
15 TEST(KeepAliveRegistryTest, BasicKeepAliveTest) { 47 TEST_F(KeepAliveRegistryTest, BasicKeepAliveTest) {
16 const int base_keep_alive_count = chrome::GetKeepAliveCountForTesting(); 48 const int base_keep_alive_count = chrome::GetKeepAliveCountForTesting();
17 KeepAliveRegistry* registry = KeepAliveRegistry::GetInstance(); 49 KeepAliveRegistry* registry = KeepAliveRegistry::GetInstance();
18 50
19 EXPECT_FALSE(registry->WillKeepAlive()); 51 EXPECT_FALSE(registry->IsKeepingAlive());
20 52
21 { 53 {
22 // Arbitrarily chosen Origin 54 // Arbitrarily chosen Origin
23 ScopedKeepAlive test_keep_alive(KeepAliveOrigin::CHROME_APP_DELEGATE); 55 ScopedKeepAlive test_keep_alive(KeepAliveOrigin::CHROME_APP_DELEGATE,
56 KeepAliveRestartOption::DISABLED);
24 57
25 // We should require the browser to stay alive 58 // We should require the browser to stay alive
26 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); 59 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting());
27 EXPECT_TRUE(registry->WillKeepAlive()); 60 EXPECT_TRUE(registry_->IsKeepingAlive());
28 } 61 }
29 62
30 // We should be back to normal now. 63 // We should be back to normal now.
31 EXPECT_EQ(base_keep_alive_count, chrome::GetKeepAliveCountForTesting()); 64 EXPECT_EQ(base_keep_alive_count, chrome::GetKeepAliveCountForTesting());
32 EXPECT_FALSE(registry->WillKeepAlive()); 65 EXPECT_FALSE(registry_->IsKeepingAlive());
33 } 66 }
34 67
35 // Test the WillKeepAlive state and when we interact with the browser with 68 // Test the IsKeepingAlive state and when we interact with the browser with
36 // more than one KeepAlive registered. 69 // more than one KeepAlive registered.
37 TEST(KeepAliveRegistryTest, DoubleKeepAliveTest) { 70 TEST_F(KeepAliveRegistryTest, DoubleKeepAliveTest) {
38 const int base_keep_alive_count = chrome::GetKeepAliveCountForTesting(); 71 const int base_keep_alive_count = chrome::GetKeepAliveCountForTesting();
39 KeepAliveRegistry* registry = KeepAliveRegistry::GetInstance();
40 scoped_ptr<ScopedKeepAlive> keep_alive_1, keep_alive_2; 72 scoped_ptr<ScopedKeepAlive> keep_alive_1, keep_alive_2;
41 73
42 keep_alive_1.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE)); 74 keep_alive_1.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE,
75 KeepAliveRestartOption::DISABLED));
43 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); 76 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting());
44 EXPECT_TRUE(registry->WillKeepAlive()); 77 EXPECT_TRUE(registry_->IsKeepingAlive());
45 78
46 keep_alive_2.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE)); 79 keep_alive_2.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE,
80 KeepAliveRestartOption::DISABLED));
47 // We should not increment the count twice 81 // We should not increment the count twice
48 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); 82 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting());
49 EXPECT_TRUE(registry->WillKeepAlive()); 83 EXPECT_TRUE(registry_->IsKeepingAlive());
50 84
51 keep_alive_1.reset(); 85 keep_alive_1.reset();
52 // We should not decrement the count before the last keep alive is released. 86 // We should not decrement the count before the last keep alive is released.
53 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); 87 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting());
54 EXPECT_TRUE(registry->WillKeepAlive()); 88 EXPECT_TRUE(registry_->IsKeepingAlive());
55 89
56 keep_alive_2.reset(); 90 keep_alive_2.reset();
57 EXPECT_EQ(base_keep_alive_count, chrome::GetKeepAliveCountForTesting()); 91 EXPECT_EQ(base_keep_alive_count, chrome::GetKeepAliveCountForTesting());
58 EXPECT_FALSE(registry->WillKeepAlive()); 92 EXPECT_FALSE(registry_->IsKeepingAlive());
59 } 93 }
94
95 // Test the IsKeepingAlive state and when we interact with the browser with
96 // more than one KeepAlive registered.
97 TEST_F(KeepAliveRegistryTest, RestartOptionTest) {
98 scoped_ptr<ScopedKeepAlive> keep_alive, keep_alive_restart;
99
100 EXPECT_EQ(0, on_restart_allowed_call_count_);
101 EXPECT_EQ(0, on_restart_forbidden_call_count_);
102
103 // With a normal keep alive, restart should not be allowed
104 keep_alive.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE,
105 KeepAliveRestartOption::DISABLED));
106 ASSERT_EQ(1, on_restart_forbidden_call_count_--); // decrement to ack
107
108 // Restart should not be allowed if all KA don't allow it.
109 keep_alive_restart.reset(new ScopedKeepAlive(
110 KeepAliveOrigin::CHROME_APP_DELEGATE, KeepAliveRestartOption::ENABLED));
111 EXPECT_EQ(0, on_restart_allowed_call_count_);
112
113 // Now restart should be allowed, the only one left allows it.
114 keep_alive.reset();
115 ASSERT_EQ(1, on_restart_allowed_call_count_--);
116
117 // No keep alive, we should no prevent restarts.
118 keep_alive.reset();
119 EXPECT_EQ(0, on_restart_forbidden_call_count_);
120
121 // Make sure all calls were checked.
122 EXPECT_EQ(0, on_restart_allowed_call_count_);
123 EXPECT_EQ(0, on_restart_forbidden_call_count_);
124 }
OLDNEW
« no previous file with comments | « chrome/browser/lifetime/keep_alive_registry.cc ('k') | chrome/browser/lifetime/keep_alive_state_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698