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

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: Rebase + Convert browser tests to unit tests. 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 {
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 return true;
64 }
65
66 base::TimeTicks WaitForSuccessfulRestart() {
67 base::RunLoop run_loop;
Devlin 2016/06/01 21:29:15 Isn't this racy if the restart happens quickly eno
afakhry 2016/06/02 01:43:40 Added a bool to detect if the restart was done alr
68 quit_closure_ = run_loop.QuitClosure();
69 run_loop.Run();
70 return base::TimeTicks::Now();
71 }
72
73 private:
74 std::unique_ptr<RuntimeAPIDelegate> real_api_delegate_;
75
76 base::Closure quit_closure_;
77
78 DISALLOW_COPY_AND_ASSIGN(RestartOnWatchdogApiDelegate);
79 };
80
81 } // namespace
82
83 class RestartAfterDelayApiTest : public ApiUnitTest {
84 public:
85 RestartAfterDelayApiTest() : api_delegate_(nullptr) {}
86 ~RestartAfterDelayApiTest() override {}
87
88 void SetUp() override {
89 ApiUnitTest::SetUp();
90
91 RuntimeAPI* runtime_api =
92 RuntimeAPI::GetFactoryInstance()->Get(browser_context());
93 api_delegate_ =
94 new RestartOnWatchdogApiDelegate(std::move(runtime_api->delegate_));
95 runtime_api->delegate_.reset(api_delegate_);
96 runtime_api->set_min_duration_between_restarts_for_testing(
97 base::TimeDelta::FromSeconds(3));
98 runtime_api->AllowNonKiostAppsInRestartOnWatchdogForTesting();
99
100 RuntimeAPI::RegisterPrefs(
101 extensions_browser_client()->testing_pref_service()->registry());
102 }
103
104 base::TimeTicks WaitForSuccessfulRestart() {
105 return api_delegate_->WaitForSuccessfulRestart();
106 }
107
108 bool IsWatchdogTimerRunning() {
109 return RuntimeAPI::GetFactoryInstance()
110 ->Get(browser_context())
111 ->watchdog_timer_.IsRunning();
112 }
113
114 base::TimeTicks desired_restart_time() {
115 return RuntimeAPI::GetFactoryInstance()
116 ->Get(browser_context())
117 ->watchdog_timer_.desired_run_time();
118 }
119
120 void RunFunctionAssertNoError(UIThreadExtensionFunction* function,
121 const std::string& args) {
122 scoped_refptr<ExtensionFunction> function_owner(function);
123 function->set_extension(extension());
124 function->set_has_callback(true);
125 api_test_utils::RunFunction(function, args, browser_context());
126 ASSERT_TRUE(function->GetError().empty()) << function->GetError();
127 }
128
129 private:
130 RestartOnWatchdogApiDelegate* api_delegate_; // Not Owned.
131
132 DISALLOW_COPY_AND_ASSIGN(RestartAfterDelayApiTest);
133 };
134
135 TEST_F(RestartAfterDelayApiTest, RestartAfterDelayTest) {
136 RunFunctionAssertNoError(new RuntimeRestartAfterDelayFunction(), "[-1]");
137 ASSERT_FALSE(IsWatchdogTimerRunning());
138
139 // Request a restart after 3 seconds.
140 base::TimeTicks now = base::TimeTicks::Now();
141 RunFunctionAssertNoError(new RuntimeRestartAfterDelayFunction(), "[3]");
142 ASSERT_TRUE(IsWatchdogTimerRunning());
143 ASSERT_GE(desired_restart_time() - now, base::TimeDelta::FromSeconds(3));
144
145 // Request another restart after 4 seconds. It should reschedule the previous
146 // request.
147 now = base::TimeTicks::Now();
148 RunFunctionAssertNoError(new RuntimeRestartAfterDelayFunction(), "[4]");
149 ASSERT_TRUE(IsWatchdogTimerRunning());
150 ASSERT_GE(desired_restart_time() - now, base::TimeDelta::FromSeconds(4));
151
152 // Cancel restart requests.
153 RunFunctionAssertNoError(new RuntimeRestartAfterDelayFunction(), "[-1]");
154 ASSERT_FALSE(IsWatchdogTimerRunning());
155
156 // Schedule a restart and wait for it to happen.
157 now = base::TimeTicks::Now();
158 RunFunctionAssertNoError(new RuntimeRestartAfterDelayFunction(), "[3]");
159 ASSERT_TRUE(IsWatchdogTimerRunning());
160 ASSERT_GE(desired_restart_time() - now, base::TimeDelta::FromSeconds(3));
161 base::TimeTicks last_restart_time = WaitForSuccessfulRestart();
Devlin 2016/06/01 21:29:15 Spinning for 3 seconds is really innefficient. Wh
afakhry 2016/06/02 01:43:40 Right. Since we reset (using -1) in the previous r
162 ASSERT_FALSE(IsWatchdogTimerRunning());
163 ASSERT_GE(base::TimeTicks::Now() - now, base::TimeDelta::FromSeconds(3));
164
165 // This is a restart request that will be throttled, because it happens too
166 // soon after a successful restart.
167 RunFunctionAssertNoError(new RuntimeRestartAfterDelayFunction(), "[1]");
168 ASSERT_TRUE(IsWatchdogTimerRunning());
169 // Restart will happen 3 seconds later, even though the request was just one
170 // second.
171 ASSERT_NEAR((desired_restart_time() - last_restart_time).InSecondsF(),
172 base::TimeDelta::FromSeconds(3).InSecondsF(), 0.01);
173 base::TimeTicks this_restart_time = WaitForSuccessfulRestart();
174 ASSERT_FALSE(IsWatchdogTimerRunning());
175 ASSERT_NEAR((this_restart_time - last_restart_time).InSecondsF(),
176 base::TimeDelta::FromSeconds(3).InSecondsF(), 0.01);
177 }
178
179 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698