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

Side by Side Diff: extensions/browser/api/idle/idle_api_unittest.cc

Issue 1902873002: Convert //extensions/browser/api from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "extensions/browser/api/idle/idle_api.h" 5 #include "extensions/browser/api/idle/idle_api.h"
6 6
7 #include <limits.h> 7 #include <limits.h>
8
9 #include <memory>
8 #include <string> 10 #include <string>
9 11
10 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/ptr_util.h"
11 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
12 #include "extensions/browser/api/idle/idle_api_constants.h" 14 #include "extensions/browser/api/idle/idle_api_constants.h"
15 #include "extensions/browser/api/idle/idle_manager.h"
13 #include "extensions/browser/api/idle/idle_manager_factory.h" 16 #include "extensions/browser/api/idle/idle_manager_factory.h"
14 #include "extensions/browser/api/idle/idle_manager.h"
15 #include "extensions/browser/api_unittest.h" 17 #include "extensions/browser/api_unittest.h"
16 #include "extensions/browser/event_router.h" 18 #include "extensions/browser/event_router.h"
17 #include "extensions/browser/extension_registry.h" 19 #include "extensions/browser/extension_registry.h"
18 #include "extensions/common/api/idle.h" 20 #include "extensions/common/api/idle.h"
19 #include "extensions/common/extension.h" 21 #include "extensions/common/extension.h"
20 #include "testing/gmock/include/gmock/gmock.h" 22 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
22 24
23 using ::testing::_; 25 using ::testing::_;
24 26
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 extension_id_, GURL(), NULL); 107 extension_id_, GURL(), NULL);
106 idle_manager_->OnListenerAdded(details); 108 idle_manager_->OnListenerAdded(details);
107 } 109 }
108 110
109 ScopedListen::~ScopedListen() { 111 ScopedListen::~ScopedListen() {
110 const EventListenerInfo details(idle::OnStateChanged::kEventName, 112 const EventListenerInfo details(idle::OnStateChanged::kEventName,
111 extension_id_, GURL(), NULL); 113 extension_id_, GURL(), NULL);
112 idle_manager_->OnListenerRemoved(details); 114 idle_manager_->OnListenerRemoved(details);
113 } 115 }
114 116
115 scoped_ptr<KeyedService> IdleManagerTestFactory( 117 std::unique_ptr<KeyedService> IdleManagerTestFactory(
116 content::BrowserContext* context) { 118 content::BrowserContext* context) {
117 return make_scoped_ptr(new IdleManager(context)); 119 return base::WrapUnique(new IdleManager(context));
118 } 120 }
119 121
120 } // namespace 122 } // namespace
121 123
122 class IdleTest : public ApiUnitTest { 124 class IdleTest : public ApiUnitTest {
123 public: 125 public:
124 void SetUp() override; 126 void SetUp() override;
125 127
126 protected: 128 protected:
127 IdleManager* idle_manager_; 129 IdleManager* idle_manager_;
128 TestIdleProvider* idle_provider_; 130 TestIdleProvider* idle_provider_;
129 testing::StrictMock<MockEventDelegate>* event_delegate_; 131 testing::StrictMock<MockEventDelegate>* event_delegate_;
130 }; 132 };
131 133
132 void IdleTest::SetUp() { 134 void IdleTest::SetUp() {
133 ApiUnitTest::SetUp(); 135 ApiUnitTest::SetUp();
134 136
135 IdleManagerFactory::GetInstance()->SetTestingFactory(browser_context(), 137 IdleManagerFactory::GetInstance()->SetTestingFactory(browser_context(),
136 &IdleManagerTestFactory); 138 &IdleManagerTestFactory);
137 idle_manager_ = IdleManagerFactory::GetForBrowserContext(browser_context()); 139 idle_manager_ = IdleManagerFactory::GetForBrowserContext(browser_context());
138 140
139 idle_provider_ = new TestIdleProvider(); 141 idle_provider_ = new TestIdleProvider();
140 idle_manager_->SetIdleTimeProviderForTest( 142 idle_manager_->SetIdleTimeProviderForTest(
141 scoped_ptr<IdleManager::IdleTimeProvider>(idle_provider_)); 143 std::unique_ptr<IdleManager::IdleTimeProvider>(idle_provider_));
142 event_delegate_ = new testing::StrictMock<MockEventDelegate>(); 144 event_delegate_ = new testing::StrictMock<MockEventDelegate>();
143 idle_manager_->SetEventDelegateForTest( 145 idle_manager_->SetEventDelegateForTest(
144 scoped_ptr<IdleManager::EventDelegate>(event_delegate_)); 146 std::unique_ptr<IdleManager::EventDelegate>(event_delegate_));
145 idle_manager_->Init(); 147 idle_manager_->Init();
146 } 148 }
147 149
148 // Verifies that "locked" takes priority over "active". 150 // Verifies that "locked" takes priority over "active".
149 TEST_F(IdleTest, QueryLockedActive) { 151 TEST_F(IdleTest, QueryLockedActive) {
150 idle_provider_->set_locked(true); 152 idle_provider_->set_locked(true);
151 idle_provider_->set_idle_time(0); 153 idle_provider_->set_idle_time(0);
152 154
153 scoped_ptr<base::Value> result( 155 std::unique_ptr<base::Value> result(
154 RunFunctionAndReturnValue(new IdleQueryStateFunction(), "[60]")); 156 RunFunctionAndReturnValue(new IdleQueryStateFunction(), "[60]"));
155 157
156 std::string idle_state; 158 std::string idle_state;
157 ASSERT_TRUE(result->GetAsString(&idle_state)); 159 ASSERT_TRUE(result->GetAsString(&idle_state));
158 EXPECT_EQ("locked", idle_state); 160 EXPECT_EQ("locked", idle_state);
159 } 161 }
160 162
161 // Verifies that "locked" takes priority over "idle". 163 // Verifies that "locked" takes priority over "idle".
162 TEST_F(IdleTest, QueryLockedIdle) { 164 TEST_F(IdleTest, QueryLockedIdle) {
163 idle_provider_->set_locked(true); 165 idle_provider_->set_locked(true);
164 idle_provider_->set_idle_time(INT_MAX); 166 idle_provider_->set_idle_time(INT_MAX);
165 167
166 scoped_ptr<base::Value> result( 168 std::unique_ptr<base::Value> result(
167 RunFunctionAndReturnValue(new IdleQueryStateFunction(), "[60]")); 169 RunFunctionAndReturnValue(new IdleQueryStateFunction(), "[60]"));
168 170
169 std::string idle_state; 171 std::string idle_state;
170 ASSERT_TRUE(result->GetAsString(&idle_state)); 172 ASSERT_TRUE(result->GetAsString(&idle_state));
171 EXPECT_EQ("locked", idle_state); 173 EXPECT_EQ("locked", idle_state);
172 } 174 }
173 175
174 // Verifies that any amount of idle time less than the detection interval 176 // Verifies that any amount of idle time less than the detection interval
175 // translates to a state of "active". 177 // translates to a state of "active".
176 TEST_F(IdleTest, QueryActive) { 178 TEST_F(IdleTest, QueryActive) {
177 idle_provider_->set_locked(false); 179 idle_provider_->set_locked(false);
178 180
179 for (int time = 0; time < 60; ++time) { 181 for (int time = 0; time < 60; ++time) {
180 SCOPED_TRACE(time); 182 SCOPED_TRACE(time);
181 idle_provider_->set_idle_time(time); 183 idle_provider_->set_idle_time(time);
182 184
183 scoped_ptr<base::Value> result( 185 std::unique_ptr<base::Value> result(
184 RunFunctionAndReturnValue(new IdleQueryStateFunction(), "[60]")); 186 RunFunctionAndReturnValue(new IdleQueryStateFunction(), "[60]"));
185 187
186 std::string idle_state; 188 std::string idle_state;
187 ASSERT_TRUE(result->GetAsString(&idle_state)); 189 ASSERT_TRUE(result->GetAsString(&idle_state));
188 EXPECT_EQ("active", idle_state); 190 EXPECT_EQ("active", idle_state);
189 } 191 }
190 } 192 }
191 193
192 // Verifies that an idle time >= the detection interval returns the "idle" 194 // Verifies that an idle time >= the detection interval returns the "idle"
193 // state. 195 // state.
194 TEST_F(IdleTest, QueryIdle) { 196 TEST_F(IdleTest, QueryIdle) {
195 idle_provider_->set_locked(false); 197 idle_provider_->set_locked(false);
196 198
197 for (int time = 80; time >= 60; --time) { 199 for (int time = 80; time >= 60; --time) {
198 SCOPED_TRACE(time); 200 SCOPED_TRACE(time);
199 idle_provider_->set_idle_time(time); 201 idle_provider_->set_idle_time(time);
200 202
201 scoped_ptr<base::Value> result( 203 std::unique_ptr<base::Value> result(
202 RunFunctionAndReturnValue(new IdleQueryStateFunction(), "[60]")); 204 RunFunctionAndReturnValue(new IdleQueryStateFunction(), "[60]"));
203 205
204 std::string idle_state; 206 std::string idle_state;
205 ASSERT_TRUE(result->GetAsString(&idle_state)); 207 ASSERT_TRUE(result->GetAsString(&idle_state));
206 EXPECT_EQ("idle", idle_state); 208 EXPECT_EQ("idle", idle_state);
207 } 209 }
208 } 210 }
209 211
210 // Verifies that requesting a detection interval < 15 has the same effect as 212 // Verifies that requesting a detection interval < 15 has the same effect as
211 // passing in 15. 213 // passing in 15.
212 TEST_F(IdleTest, QueryMinThreshold) { 214 TEST_F(IdleTest, QueryMinThreshold) {
213 idle_provider_->set_locked(false); 215 idle_provider_->set_locked(false);
214 216
215 for (int threshold = 0; threshold < 20; ++threshold) { 217 for (int threshold = 0; threshold < 20; ++threshold) {
216 for (int time = 10; time < 60; ++time) { 218 for (int time = 10; time < 60; ++time) {
217 SCOPED_TRACE(threshold); 219 SCOPED_TRACE(threshold);
218 SCOPED_TRACE(time); 220 SCOPED_TRACE(time);
219 idle_provider_->set_idle_time(time); 221 idle_provider_->set_idle_time(time);
220 222
221 std::string args = "[" + base::IntToString(threshold) + "]"; 223 std::string args = "[" + base::IntToString(threshold) + "]";
222 scoped_ptr<base::Value> result( 224 std::unique_ptr<base::Value> result(
223 RunFunctionAndReturnValue(new IdleQueryStateFunction(), args)); 225 RunFunctionAndReturnValue(new IdleQueryStateFunction(), args));
224 226
225 std::string idle_state; 227 std::string idle_state;
226 ASSERT_TRUE(result->GetAsString(&idle_state)); 228 ASSERT_TRUE(result->GetAsString(&idle_state));
227 229
228 int real_threshold = (threshold < 15) ? 15 : threshold; 230 int real_threshold = (threshold < 15) ? 15 : threshold;
229 const char* expected = (time < real_threshold) ? "active" : "idle"; 231 const char* expected = (time < real_threshold) ? "active" : "idle";
230 EXPECT_EQ(expected, idle_state); 232 EXPECT_EQ(expected, idle_state);
231 } 233 }
232 } 234 }
233 } 235 }
234 236
235 // Verifies that passing in a detection interval > 4 hours has the same effect 237 // Verifies that passing in a detection interval > 4 hours has the same effect
236 // as passing in 4 hours. 238 // as passing in 4 hours.
237 TEST_F(IdleTest, QueryMaxThreshold) { 239 TEST_F(IdleTest, QueryMaxThreshold) {
238 idle_provider_->set_locked(false); 240 idle_provider_->set_locked(false);
239 241
240 const int kFourHoursInSeconds = 4 * 60 * 60; 242 const int kFourHoursInSeconds = 4 * 60 * 60;
241 243
242 for (int threshold = kFourHoursInSeconds - 20; 244 for (int threshold = kFourHoursInSeconds - 20;
243 threshold < (kFourHoursInSeconds + 20); ++threshold) { 245 threshold < (kFourHoursInSeconds + 20); ++threshold) {
244 for (int time = kFourHoursInSeconds - 30; time < kFourHoursInSeconds + 30; 246 for (int time = kFourHoursInSeconds - 30; time < kFourHoursInSeconds + 30;
245 ++time) { 247 ++time) {
246 SCOPED_TRACE(threshold); 248 SCOPED_TRACE(threshold);
247 SCOPED_TRACE(time); 249 SCOPED_TRACE(time);
248 idle_provider_->set_idle_time(time); 250 idle_provider_->set_idle_time(time);
249 251
250 std::string args = "[" + base::IntToString(threshold) + "]"; 252 std::string args = "[" + base::IntToString(threshold) + "]";
251 scoped_ptr<base::Value> result( 253 std::unique_ptr<base::Value> result(
252 RunFunctionAndReturnValue(new IdleQueryStateFunction(), args)); 254 RunFunctionAndReturnValue(new IdleQueryStateFunction(), args));
253 255
254 std::string idle_state; 256 std::string idle_state;
255 ASSERT_TRUE(result->GetAsString(&idle_state)); 257 ASSERT_TRUE(result->GetAsString(&idle_state));
256 258
257 int real_threshold = 259 int real_threshold =
258 (threshold > kFourHoursInSeconds) ? kFourHoursInSeconds : threshold; 260 (threshold > kFourHoursInSeconds) ? kFourHoursInSeconds : threshold;
259 const char* expected = (time < real_threshold) ? "active" : "idle"; 261 const char* expected = (time < real_threshold) ? "active" : "idle";
260 EXPECT_EQ(expected, idle_state); 262 EXPECT_EQ(expected, idle_state);
261 } 263 }
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 idle_manager_->UpdateIdleState(); 394 idle_manager_->UpdateIdleState();
393 } 395 }
394 396
395 // Verifies that setDetectionInterval changes the detection interval from the 397 // Verifies that setDetectionInterval changes the detection interval from the
396 // default of 60 seconds, and that the call only affects a single extension's 398 // default of 60 seconds, and that the call only affects a single extension's
397 // IdleMonitor. 399 // IdleMonitor.
398 TEST_F(IdleTest, SetDetectionInterval) { 400 TEST_F(IdleTest, SetDetectionInterval) {
399 ScopedListen listen_default(idle_manager_, "default"); 401 ScopedListen listen_default(idle_manager_, "default");
400 ScopedListen listen_extension(idle_manager_, extension()->id()); 402 ScopedListen listen_extension(idle_manager_, extension()->id());
401 403
402 scoped_ptr<base::Value> result45(RunFunctionAndReturnValue( 404 std::unique_ptr<base::Value> result45(RunFunctionAndReturnValue(
403 new IdleSetDetectionIntervalFunction(), "[45]")); 405 new IdleSetDetectionIntervalFunction(), "[45]"));
404 406
405 idle_provider_->set_locked(false); 407 idle_provider_->set_locked(false);
406 idle_provider_->set_idle_time(44); 408 idle_provider_->set_idle_time(44);
407 idle_manager_->UpdateIdleState(); 409 idle_manager_->UpdateIdleState();
408 410
409 idle_provider_->set_idle_time(45); 411 idle_provider_->set_idle_time(45);
410 EXPECT_CALL(*event_delegate_, 412 EXPECT_CALL(*event_delegate_,
411 OnStateChanged(extension()->id(), ui::IDLE_STATE_IDLE)); 413 OnStateChanged(extension()->id(), ui::IDLE_STATE_IDLE));
412 idle_manager_->UpdateIdleState(); 414 idle_manager_->UpdateIdleState();
413 // Verify that the expectation has been fulfilled before incrementing the 415 // Verify that the expectation has been fulfilled before incrementing the
414 // time again. 416 // time again.
415 testing::Mock::VerifyAndClearExpectations(event_delegate_); 417 testing::Mock::VerifyAndClearExpectations(event_delegate_);
416 418
417 idle_provider_->set_idle_time(60); 419 idle_provider_->set_idle_time(60);
418 EXPECT_CALL(*event_delegate_, OnStateChanged("default", ui::IDLE_STATE_IDLE)); 420 EXPECT_CALL(*event_delegate_, OnStateChanged("default", ui::IDLE_STATE_IDLE));
419 idle_manager_->UpdateIdleState(); 421 idle_manager_->UpdateIdleState();
420 } 422 }
421 423
422 // Verifies that setting the detection interval before creating the listener 424 // Verifies that setting the detection interval before creating the listener
423 // works correctly. 425 // works correctly.
424 TEST_F(IdleTest, SetDetectionIntervalBeforeListener) { 426 TEST_F(IdleTest, SetDetectionIntervalBeforeListener) {
425 scoped_ptr<base::Value> result45(RunFunctionAndReturnValue( 427 std::unique_ptr<base::Value> result45(RunFunctionAndReturnValue(
426 new IdleSetDetectionIntervalFunction(), "[45]")); 428 new IdleSetDetectionIntervalFunction(), "[45]"));
427 429
428 ScopedListen listen_extension(idle_manager_, extension()->id()); 430 ScopedListen listen_extension(idle_manager_, extension()->id());
429 431
430 idle_provider_->set_locked(false); 432 idle_provider_->set_locked(false);
431 idle_provider_->set_idle_time(44); 433 idle_provider_->set_idle_time(44);
432 idle_manager_->UpdateIdleState(); 434 idle_manager_->UpdateIdleState();
433 435
434 idle_provider_->set_idle_time(45); 436 idle_provider_->set_idle_time(45);
435 EXPECT_CALL(*event_delegate_, 437 EXPECT_CALL(*event_delegate_,
436 OnStateChanged(extension()->id(), ui::IDLE_STATE_IDLE)); 438 OnStateChanged(extension()->id(), ui::IDLE_STATE_IDLE));
437 idle_manager_->UpdateIdleState(); 439 idle_manager_->UpdateIdleState();
438 } 440 }
439 441
440 // Verifies that setting a detection interval above the maximum value results 442 // Verifies that setting a detection interval above the maximum value results
441 // in an interval of 4 hours. 443 // in an interval of 4 hours.
442 TEST_F(IdleTest, SetDetectionIntervalMaximum) { 444 TEST_F(IdleTest, SetDetectionIntervalMaximum) {
443 ScopedListen listen_extension(idle_manager_, extension()->id()); 445 ScopedListen listen_extension(idle_manager_, extension()->id());
444 446
445 scoped_ptr<base::Value> result( 447 std::unique_ptr<base::Value> result(
446 RunFunctionAndReturnValue(new IdleSetDetectionIntervalFunction(), 448 RunFunctionAndReturnValue(new IdleSetDetectionIntervalFunction(),
447 "[18000]")); // five hours in seconds 449 "[18000]")); // five hours in seconds
448 450
449 idle_provider_->set_locked(false); 451 idle_provider_->set_locked(false);
450 idle_provider_->set_idle_time(4 * 60 * 60 - 1); 452 idle_provider_->set_idle_time(4 * 60 * 60 - 1);
451 idle_manager_->UpdateIdleState(); 453 idle_manager_->UpdateIdleState();
452 454
453 idle_provider_->set_idle_time(4 * 60 * 60); 455 idle_provider_->set_idle_time(4 * 60 * 60);
454 EXPECT_CALL(*event_delegate_, 456 EXPECT_CALL(*event_delegate_,
455 OnStateChanged(extension()->id(), ui::IDLE_STATE_IDLE)); 457 OnStateChanged(extension()->id(), ui::IDLE_STATE_IDLE));
456 idle_manager_->UpdateIdleState(); 458 idle_manager_->UpdateIdleState();
457 } 459 }
458 460
459 // Verifies that setting a detection interval below the minimum value results 461 // Verifies that setting a detection interval below the minimum value results
460 // in an interval of 15 seconds. 462 // in an interval of 15 seconds.
461 TEST_F(IdleTest, SetDetectionIntervalMinimum) { 463 TEST_F(IdleTest, SetDetectionIntervalMinimum) {
462 ScopedListen listen_extension(idle_manager_, extension()->id()); 464 ScopedListen listen_extension(idle_manager_, extension()->id());
463 465
464 scoped_ptr<base::Value> result(RunFunctionAndReturnValue( 466 std::unique_ptr<base::Value> result(RunFunctionAndReturnValue(
465 new IdleSetDetectionIntervalFunction(), "[10]")); 467 new IdleSetDetectionIntervalFunction(), "[10]"));
466 468
467 idle_provider_->set_locked(false); 469 idle_provider_->set_locked(false);
468 idle_provider_->set_idle_time(14); 470 idle_provider_->set_idle_time(14);
469 idle_manager_->UpdateIdleState(); 471 idle_manager_->UpdateIdleState();
470 472
471 idle_provider_->set_idle_time(15); 473 idle_provider_->set_idle_time(15);
472 EXPECT_CALL(*event_delegate_, 474 EXPECT_CALL(*event_delegate_,
473 OnStateChanged(extension()->id(), ui::IDLE_STATE_IDLE)); 475 OnStateChanged(extension()->id(), ui::IDLE_STATE_IDLE));
474 idle_manager_->UpdateIdleState(); 476 idle_manager_->UpdateIdleState();
475 } 477 }
476 478
477 // Verifies that an extension's detection interval is discarded when it unloads. 479 // Verifies that an extension's detection interval is discarded when it unloads.
478 TEST_F(IdleTest, UnloadCleanup) { 480 TEST_F(IdleTest, UnloadCleanup) {
479 { 481 {
480 ScopedListen listen(idle_manager_, extension()->id()); 482 ScopedListen listen(idle_manager_, extension()->id());
481 483
482 scoped_ptr<base::Value> result45(RunFunctionAndReturnValue( 484 std::unique_ptr<base::Value> result45(RunFunctionAndReturnValue(
483 new IdleSetDetectionIntervalFunction(), "[15]")); 485 new IdleSetDetectionIntervalFunction(), "[15]"));
484 } 486 }
485 487
486 // Listener count dropping to zero does not reset threshold. 488 // Listener count dropping to zero does not reset threshold.
487 489
488 { 490 {
489 ScopedListen listen(idle_manager_, extension()->id()); 491 ScopedListen listen(idle_manager_, extension()->id());
490 idle_provider_->set_idle_time(16); 492 idle_provider_->set_idle_time(16);
491 EXPECT_CALL(*event_delegate_, 493 EXPECT_CALL(*event_delegate_,
492 OnStateChanged(extension()->id(), ui::IDLE_STATE_IDLE)); 494 OnStateChanged(extension()->id(), ui::IDLE_STATE_IDLE));
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
547 549
548 { 550 {
549 // Nothing should have fired, the listener wasn't added until afterward. 551 // Nothing should have fired, the listener wasn't added until afterward.
550 ScopedListen listen(idle_manager_, "test"); 552 ScopedListen listen(idle_manager_, "test");
551 idle_manager_->UpdateIdleState(); 553 idle_manager_->UpdateIdleState();
552 testing::Mock::VerifyAndClearExpectations(event_delegate_); 554 testing::Mock::VerifyAndClearExpectations(event_delegate_);
553 } 555 }
554 } 556 }
555 557
556 } // namespace extensions 558 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/api/hid/hid_device_manager.cc ('k') | extensions/browser/api/idle/idle_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698