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

Side by Side Diff: extensions/browser/api/runtime/restart_after_delay_api_unittest.cc

Issue 1970613003: Add a new app API to enable watchdog behavior restarts in kiosk apps (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments Created 4 years, 6 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/callback_helpers.h"
6 #include "base/run_loop.h"
7 #include "components/pref_registry/pref_registry_syncable.h"
8 #include "extensions/browser/api/runtime/runtime_api.h"
9 #include "extensions/browser/api_test_utils.h"
10 #include "extensions/browser/api_unittest.h"
11 #include "extensions/browser/test_extensions_browser_client.h"
12
13 namespace extensions {
14
15 namespace {
16
17 // An intercepter of the real RuntimeAPIDelegate that simulates a successful
18 // restart request every time.
19 class RestartOnWatchdogApiDelegate : public RuntimeAPIDelegate {
Devlin 2016/06/03 21:26:47 Could we just inherit from TestRuntimeAPIDelegate
afakhry 2016/06/03 23:06:41 Done. This was needed for the browser tests as I n
20 public:
21 // Takes ownership of the |real_api_delegate|.
22 RestartOnWatchdogApiDelegate(
23 std::unique_ptr<RuntimeAPIDelegate> real_api_delegate)
24 : real_api_delegate_(std::move(real_api_delegate)) {}
25 ~RestartOnWatchdogApiDelegate() override {}
26
27 // RuntimeAPIDelegate:
28 void AddUpdateObserver(UpdateObserver* observer) override {
29 real_api_delegate_->AddUpdateObserver(observer);
30 }
31
32 void RemoveUpdateObserver(UpdateObserver* observer) override {
33 real_api_delegate_->RemoveUpdateObserver(observer);
34 }
35
36 base::Version GetPreviousExtensionVersion(
37 const Extension* extension) override {
38 return real_api_delegate_->GetPreviousExtensionVersion(extension);
39 }
40
41 void ReloadExtension(const std::string& extension_id) override {
42 real_api_delegate_->ReloadExtension(extension_id);
43 }
44
45 bool CheckForUpdates(const std::string& extension_id,
46 const UpdateCheckCallback& callback) override {
47 return real_api_delegate_->CheckForUpdates(extension_id, callback);
48 }
49
50 void OpenURL(const GURL& uninstall_url) override {
51 real_api_delegate_->OpenURL(uninstall_url);
52 }
53
54 bool GetPlatformInfo(api::runtime::PlatformInfo* info) override {
55 return real_api_delegate_->GetPlatformInfo(info);
56 }
57
58 bool RestartDevice(std::string* error_message) override {
59 if (!quit_closure_.is_null())
60 base::ResetAndReturn(&quit_closure_).Run();
61
62 *error_message = "Success.";
63 restart_done_ = true;
64 return true;
65 }
66
67 base::TimeTicks WaitForSuccessfulRestart() {
68 if (!restart_done_) {
69 base::RunLoop run_loop;
70 quit_closure_ = run_loop.QuitClosure();
71 run_loop.Run();
72 }
73 restart_done_ = false;
74 return base::TimeTicks::Now();
75 }
76
77 private:
78 std::unique_ptr<RuntimeAPIDelegate> real_api_delegate_;
79
80 base::Closure quit_closure_;
81
82 bool restart_done_ = false;
83
84 DISALLOW_COPY_AND_ASSIGN(RestartOnWatchdogApiDelegate);
85 };
86
87 } // namespace
88
89 class RestartAfterDelayApiTest : public ApiUnitTest {
90 public:
91 RestartAfterDelayApiTest() : api_delegate_(nullptr) {}
92 ~RestartAfterDelayApiTest() override {}
93
94 void SetUp() override {
95 ApiUnitTest::SetUp();
96
97 RuntimeAPI* runtime_api =
98 RuntimeAPI::GetFactoryInstance()->Get(browser_context());
99 api_delegate_ =
100 new RestartOnWatchdogApiDelegate(std::move(runtime_api->delegate_));
101 runtime_api->delegate_.reset(api_delegate_);
102 runtime_api->set_min_duration_between_restarts_for_testing(
103 base::TimeDelta::FromSeconds(2));
104 runtime_api->AllowNonKiostAppsInRestartOnWatchdogForTesting();
105
106 RuntimeAPI::RegisterPrefs(
107 extensions_browser_client()->testing_pref_service()->registry());
108 }
109
110 base::TimeTicks WaitForSuccessfulRestart() {
111 return api_delegate_->WaitForSuccessfulRestart();
112 }
113
114 bool IsWatchdogTimerRunning() {
115 return RuntimeAPI::GetFactoryInstance()
116 ->Get(browser_context())
117 ->watchdog_timer_.IsRunning();
118 }
119
120 base::TimeTicks desired_restart_time() {
121 return RuntimeAPI::GetFactoryInstance()
122 ->Get(browser_context())
123 ->watchdog_timer_.desired_run_time();
124 }
125
126 void RunFunctionAssertNoError(UIThreadExtensionFunction* function,
127 const std::string& args) {
128 scoped_refptr<ExtensionFunction> function_owner(function);
129 function->set_extension(extension());
130 function->set_has_callback(true);
131 api_test_utils::RunFunction(function, args, browser_context());
132 ASSERT_TRUE(function->GetError().empty()) << function->GetError();
133 }
134
135 private:
136 RestartOnWatchdogApiDelegate* api_delegate_; // Not Owned.
137
138 DISALLOW_COPY_AND_ASSIGN(RestartAfterDelayApiTest);
139 };
140
141 TEST_F(RestartAfterDelayApiTest, RestartAfterDelayTest) {
142 RunFunctionAssertNoError(new RuntimeRestartAfterDelayFunction(), "[-1]");
143 ASSERT_FALSE(IsWatchdogTimerRunning());
Devlin 2016/06/03 21:26:47 nit: let's add a test for -2, and, optionally, a "
afakhry 2016/06/03 23:06:41 Both done.
144
145 // Request a restart after 3 seconds.
146 base::TimeTicks now = base::TimeTicks::Now();
147 RunFunctionAssertNoError(new RuntimeRestartAfterDelayFunction(), "[3]");
148 ASSERT_TRUE(IsWatchdogTimerRunning());
149 ASSERT_GE(desired_restart_time() - now, base::TimeDelta::FromSeconds(3));
150
151 // Request another restart after 4 seconds. It should reschedule the previous
152 // request.
153 now = base::TimeTicks::Now();
154 RunFunctionAssertNoError(new RuntimeRestartAfterDelayFunction(), "[4]");
155 ASSERT_TRUE(IsWatchdogTimerRunning());
156 ASSERT_GE(desired_restart_time() - now, base::TimeDelta::FromSeconds(4));
157
158 // Cancel restart requests.
159 RunFunctionAssertNoError(new RuntimeRestartAfterDelayFunction(), "[-1]");
160 ASSERT_FALSE(IsWatchdogTimerRunning());
161
162 // Schedule a restart and wait for it to happen.
163 now = base::TimeTicks::Now();
164 RunFunctionAssertNoError(new RuntimeRestartAfterDelayFunction(), "[1]");
165 ASSERT_TRUE(IsWatchdogTimerRunning());
166 ASSERT_GE(desired_restart_time() - now, base::TimeDelta::FromSeconds(1));
167 base::TimeTicks last_restart_time = WaitForSuccessfulRestart();
168 ASSERT_FALSE(IsWatchdogTimerRunning());
169 ASSERT_GE(base::TimeTicks::Now() - now, base::TimeDelta::FromSeconds(1));
170
171 // This is a restart request that will be throttled, because it happens too
172 // soon after a successful restart.
173 RunFunctionAssertNoError(new RuntimeRestartAfterDelayFunction(), "[1]");
174 ASSERT_TRUE(IsWatchdogTimerRunning());
175 // Restart will happen 2 seconds later, even though the request was just one
176 // second.
177 ASSERT_NEAR((desired_restart_time() - last_restart_time).InSecondsF(),
178 base::TimeDelta::FromSeconds(2).InSecondsF(), 0.01);
179 base::TimeTicks this_restart_time = WaitForSuccessfulRestart();
Devlin 2016/06/03 21:26:47 Do we need to wait for this restart, if we already
afakhry 2016/06/03 23:06:41 Right. We don't have to wait for it. Done. Tests o
180 ASSERT_FALSE(IsWatchdogTimerRunning());
181 ASSERT_NEAR((this_restart_time - last_restart_time).InSecondsF(),
182 base::TimeDelta::FromSeconds(2).InSecondsF(), 0.01);
183 }
184
185 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698