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

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

Issue 1778873002: Replace Increment/DecrementKeepAliveCount by ScopedKeepAlives (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@KAObserver
Patch Set: 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_state_observer.h"
10 #include "chrome/browser/lifetime/keep_alive_types.h" 10 #include "chrome/browser/lifetime/keep_alive_types.h"
11 #include "chrome/browser/lifetime/scoped_keep_alive.h" 11 #include "chrome/browser/lifetime/scoped_keep_alive.h"
12 #include "chrome/test/base/testing_browser_process.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 class KeepAliveRegistryTest : public testing::Test, 15 class KeepAliveRegistryTest : public testing::Test,
15 public KeepAliveStateObserver { 16 public KeepAliveStateObserver {
16 public: 17 public:
17 KeepAliveRegistryTest() 18 KeepAliveRegistryTest()
18 : on_restart_allowed_call_count_(0), 19 : on_restart_allowed_call_count_(0),
19 on_restart_forbidden_call_count_(0), 20 on_restart_forbidden_call_count_(0),
20 registry_(KeepAliveRegistry::GetInstance()) { 21 registry_(KeepAliveRegistry::GetInstance()) {
21 registry_->AddObserver(this); 22 registry_->AddObserver(this);
(...skipping 16 matching lines...) Expand all
38 39
39 protected: 40 protected:
40 int on_restart_allowed_call_count_; 41 int on_restart_allowed_call_count_;
41 int on_restart_forbidden_call_count_; 42 int on_restart_forbidden_call_count_;
42 KeepAliveRegistry* registry_; 43 KeepAliveRegistry* registry_;
43 }; 44 };
44 45
45 // Test the IsKeepingAlive state and when we interact with the browser with 46 // Test the IsKeepingAlive state and when we interact with the browser with
46 // a KeepAlive registered. 47 // a KeepAlive registered.
47 TEST_F(KeepAliveRegistryTest, BasicKeepAliveTest) { 48 TEST_F(KeepAliveRegistryTest, BasicKeepAliveTest) {
48 const int base_keep_alive_count = chrome::GetKeepAliveCountForTesting(); 49 const int base_module_ref_count =
50 TestingBrowserProcess::GetGlobal()->GetModuleRefCount();
49 KeepAliveRegistry* registry = KeepAliveRegistry::GetInstance(); 51 KeepAliveRegistry* registry = KeepAliveRegistry::GetInstance();
50 52
51 EXPECT_FALSE(registry->IsKeepingAlive()); 53 EXPECT_FALSE(registry->IsKeepingAlive());
52 54
53 { 55 {
54 // Arbitrarily chosen Origin 56 // Arbitrarily chosen Origin
55 ScopedKeepAlive test_keep_alive(KeepAliveOrigin::CHROME_APP_DELEGATE, 57 ScopedKeepAlive test_keep_alive(KeepAliveOrigin::CHROME_APP_DELEGATE,
56 KeepAliveRestartOption::DISABLED); 58 KeepAliveRestartOption::DISABLED);
57 59
58 // We should require the browser to stay alive 60 // We should require the browser to stay alive
59 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); 61 EXPECT_EQ(base_module_ref_count + 1,
62 TestingBrowserProcess::GetGlobal()->GetModuleRefCount());
60 EXPECT_TRUE(registry_->IsKeepingAlive()); 63 EXPECT_TRUE(registry_->IsKeepingAlive());
61 } 64 }
62 65
63 // We should be back to normal now. 66 // We should be back to normal now.
64 EXPECT_EQ(base_keep_alive_count, chrome::GetKeepAliveCountForTesting()); 67 EXPECT_EQ(base_module_ref_count,
68 TestingBrowserProcess::GetGlobal()->GetModuleRefCount());
65 EXPECT_FALSE(registry_->IsKeepingAlive()); 69 EXPECT_FALSE(registry_->IsKeepingAlive());
66 } 70 }
67 71
68 // Test the IsKeepingAlive state and when we interact with the browser with 72 // Test the IsKeepingAlive state and when we interact with the browser with
69 // more than one KeepAlive registered. 73 // more than one KeepAlive registered.
70 TEST_F(KeepAliveRegistryTest, DoubleKeepAliveTest) { 74 TEST_F(KeepAliveRegistryTest, DoubleKeepAliveTest) {
71 const int base_keep_alive_count = chrome::GetKeepAliveCountForTesting(); 75 const int base_module_ref_count =
76 TestingBrowserProcess::GetGlobal()->GetModuleRefCount();
72 scoped_ptr<ScopedKeepAlive> keep_alive_1, keep_alive_2; 77 scoped_ptr<ScopedKeepAlive> keep_alive_1, keep_alive_2;
73 78
74 keep_alive_1.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE, 79 keep_alive_1.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE,
75 KeepAliveRestartOption::DISABLED)); 80 KeepAliveRestartOption::DISABLED));
76 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); 81 EXPECT_EQ(base_module_ref_count + 1,
82 TestingBrowserProcess::GetGlobal()->GetModuleRefCount());
77 EXPECT_TRUE(registry_->IsKeepingAlive()); 83 EXPECT_TRUE(registry_->IsKeepingAlive());
78 84
79 keep_alive_2.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE, 85 keep_alive_2.reset(new ScopedKeepAlive(KeepAliveOrigin::CHROME_APP_DELEGATE,
80 KeepAliveRestartOption::DISABLED)); 86 KeepAliveRestartOption::DISABLED));
81 // We should not increment the count twice 87 // We should not increment the count twice
82 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); 88 EXPECT_EQ(base_module_ref_count + 1,
89 TestingBrowserProcess::GetGlobal()->GetModuleRefCount());
83 EXPECT_TRUE(registry_->IsKeepingAlive()); 90 EXPECT_TRUE(registry_->IsKeepingAlive());
84 91
85 keep_alive_1.reset(); 92 keep_alive_1.reset();
86 // We should not decrement the count before the last keep alive is released. 93 // We should not decrement the count before the last keep alive is released.
87 EXPECT_EQ(base_keep_alive_count + 1, chrome::GetKeepAliveCountForTesting()); 94 EXPECT_EQ(base_module_ref_count + 1,
95 TestingBrowserProcess::GetGlobal()->GetModuleRefCount());
88 EXPECT_TRUE(registry_->IsKeepingAlive()); 96 EXPECT_TRUE(registry_->IsKeepingAlive());
89 97
90 keep_alive_2.reset(); 98 keep_alive_2.reset();
91 EXPECT_EQ(base_keep_alive_count, chrome::GetKeepAliveCountForTesting()); 99 EXPECT_EQ(base_module_ref_count,
100 TestingBrowserProcess::GetGlobal()->GetModuleRefCount());
92 EXPECT_FALSE(registry_->IsKeepingAlive()); 101 EXPECT_FALSE(registry_->IsKeepingAlive());
93 } 102 }
94 103
95 // Test the IsKeepingAlive state and when we interact with the browser with 104 // Test the IsKeepingAlive state and when we interact with the browser with
96 // more than one KeepAlive registered. 105 // more than one KeepAlive registered.
97 TEST_F(KeepAliveRegistryTest, RestartOptionTest) { 106 TEST_F(KeepAliveRegistryTest, RestartOptionTest) {
98 scoped_ptr<ScopedKeepAlive> keep_alive, keep_alive_restart; 107 scoped_ptr<ScopedKeepAlive> keep_alive, keep_alive_restart;
99 108
100 EXPECT_EQ(0, on_restart_allowed_call_count_); 109 EXPECT_EQ(0, on_restart_allowed_call_count_);
101 EXPECT_EQ(0, on_restart_forbidden_call_count_); 110 EXPECT_EQ(0, on_restart_forbidden_call_count_);
(...skipping 13 matching lines...) Expand all
115 ASSERT_EQ(1, on_restart_allowed_call_count_--); 124 ASSERT_EQ(1, on_restart_allowed_call_count_--);
116 125
117 // No keep alive, we should no prevent restarts. 126 // No keep alive, we should no prevent restarts.
118 keep_alive.reset(); 127 keep_alive.reset();
119 EXPECT_EQ(0, on_restart_forbidden_call_count_); 128 EXPECT_EQ(0, on_restart_forbidden_call_count_);
120 129
121 // Make sure all calls were checked. 130 // Make sure all calls were checked.
122 EXPECT_EQ(0, on_restart_allowed_call_count_); 131 EXPECT_EQ(0, on_restart_allowed_call_count_);
123 EXPECT_EQ(0, on_restart_forbidden_call_count_); 132 EXPECT_EQ(0, on_restart_forbidden_call_count_);
124 } 133 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698