1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/previous_session_info/previous_session_info.h"
#include "base/functional/callback_helpers.h"
#include "base/strings/sys_string_conversions.h"
#include "base/test/ios/wait_util.h"
#include "base/time/time.h"
#include "components/previous_session_info/previous_session_info_private.h"
#include "components/version_info/version_info.h"
#include "ios/web/public/test/web_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/gtest_mac.h"
#include "testing/platform_test.h"
using previous_session_info_constants::kPreviousSessionInfoInactiveTabCount;
using previous_session_info_constants::kPreviousSessionInfoMemoryFootprint;
using previous_session_info_constants::kPreviousSessionInfoOTRTabCount;
using previous_session_info_constants::kPreviousSessionInfoParamsPrefix;
using previous_session_info_constants::kPreviousSessionInfoRestoringSession;
using previous_session_info_constants::kPreviousSessionInfoTabCount;
namespace {
const NSInteger kTabCount = 15;
const NSInteger kInactiveTabCount = 30;
// Key in the UserDefaults for a boolean value keeping track of memory warnings.
NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating =
previous_session_info_constants::
kDidSeeMemoryWarningShortlyBeforeTerminating;
// Key in the NSUserDefaults for a string value that stores the version of the
// last session.
NSString* const kLastRanVersion = @"LastRanVersion";
// Key in the NSUserDefaults for a string value that stores the language of the
// last session.
NSString* const kLastRanLanguage = @"LastRanLanguage";
// IDs to be used for testing scene sessions.
NSString* const kTestSession1ID = @"test_session_1";
NSString* const kTestSession2ID = @"test_session_2";
NSString* const kTestSession3ID = @"test_session_3";
NSDictionary* GetParamsDictionary() {
NSMutableDictionary* reportParameters = [[NSMutableDictionary alloc] init];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSUInteger prefix_length = kPreviousSessionInfoParamsPrefix.length;
for (NSString* key in [defaults dictionaryRepresentation].allKeys) {
if ([key hasPrefix:kPreviousSessionInfoParamsPrefix]) {
NSString* crash_key = [key substringFromIndex:prefix_length];
reportParameters[crash_key] = [defaults stringForKey:key];
}
}
return reportParameters;
}
using PreviousSessionInfoTest = PlatformTest;
TEST_F(PreviousSessionInfoTest, InitializationWithEmptyDefaults) {
[PreviousSessionInfo resetSharedInstanceForTesting];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
[defaults removeObjectForKey:kLastRanVersion];
[defaults removeObjectForKey:kLastRanLanguage];
// Instantiate the PreviousSessionInfo sharedInstance.
PreviousSessionInfo* sharedInstance = [PreviousSessionInfo sharedInstance];
// Checks the default values.
EXPECT_FALSE([sharedInstance didSeeMemoryWarningShortlyBeforeTerminating]);
EXPECT_TRUE([sharedInstance isFirstSessionAfterUpgrade]);
EXPECT_TRUE([sharedInstance isFirstSessionAfterLanguageChange]);
}
TEST_F(PreviousSessionInfoTest, InitializationWithSameLanguage) {
[PreviousSessionInfo resetSharedInstanceForTesting];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kLastRanLanguage];
// Set the current language as the last ran language.
NSString* currentVersion = [[NSLocale preferredLanguages] objectAtIndex:0];
[defaults setObject:currentVersion forKey:kLastRanVersion];
// Instantiate the PreviousSessionInfo sharedInstance.
PreviousSessionInfo* sharedInstance = [PreviousSessionInfo sharedInstance];
// Checks the values.
EXPECT_TRUE([sharedInstance isFirstSessionAfterLanguageChange]);
}
TEST_F(PreviousSessionInfoTest, InitializationWithDifferentLanguage) {
[PreviousSessionInfo resetSharedInstanceForTesting];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kLastRanLanguage];
// Set the current language as the last ran language.
NSString* currentVersion = @"Fake Language";
[defaults setObject:currentVersion forKey:kLastRanVersion];
// Instantiate the PreviousSessionInfo sharedInstance.
PreviousSessionInfo* sharedInstance = [PreviousSessionInfo sharedInstance];
// Checks the values.
EXPECT_TRUE([sharedInstance isFirstSessionAfterLanguageChange]);
}
TEST_F(PreviousSessionInfoTest, InitializationWithSameVersionNoMemoryWarning) {
[PreviousSessionInfo resetSharedInstanceForTesting];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
[defaults removeObjectForKey:kLastRanVersion];
// Set the current version as the last ran version.
NSString* currentVersion =
base::SysUTF8ToNSString(version_info::GetVersionNumber());
[defaults setObject:currentVersion forKey:kLastRanVersion];
// Instantiate the PreviousSessionInfo sharedInstance.
PreviousSessionInfo* sharedInstance = [PreviousSessionInfo sharedInstance];
// Checks the values.
EXPECT_FALSE([sharedInstance didSeeMemoryWarningShortlyBeforeTerminating]);
EXPECT_FALSE([sharedInstance isFirstSessionAfterUpgrade]);
}
TEST_F(PreviousSessionInfoTest, InitializationWithSameVersionMemoryWarning) {
[PreviousSessionInfo resetSharedInstanceForTesting];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
[defaults removeObjectForKey:kLastRanVersion];
// Set the current version as the last ran version.
NSString* currentVersion =
base::SysUTF8ToNSString(version_info::GetVersionNumber());
[defaults setObject:currentVersion forKey:kLastRanVersion];
// Set the memory warning flag as a previous session would have.
[defaults setBool:YES forKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
// Instantiate the PreviousSessionInfo sharedInstance.
PreviousSessionInfo* sharedInstance = [PreviousSessionInfo sharedInstance];
// Checks the values.
EXPECT_TRUE([sharedInstance didSeeMemoryWarningShortlyBeforeTerminating]);
EXPECT_FALSE([sharedInstance isFirstSessionAfterUpgrade]);
}
TEST_F(PreviousSessionInfoTest, InitializationDifferentVersionNoMemoryWarning) {
[PreviousSessionInfo resetSharedInstanceForTesting];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
[defaults removeObjectForKey:kLastRanVersion];
// Set the current version as the last ran version.
[defaults setObject:@"Fake Version" forKey:kLastRanVersion];
// Instantiate the PreviousSessionInfo sharedInstance.
PreviousSessionInfo* sharedInstance = [PreviousSessionInfo sharedInstance];
// Checks the values.
EXPECT_FALSE([sharedInstance didSeeMemoryWarningShortlyBeforeTerminating]);
EXPECT_TRUE([sharedInstance isFirstSessionAfterUpgrade]);
}
TEST_F(PreviousSessionInfoTest, InitializationDifferentVersionMemoryWarning) {
[PreviousSessionInfo resetSharedInstanceForTesting];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
[defaults removeObjectForKey:kLastRanVersion];
// Set the current version as the last ran version.
[defaults setObject:@"Fake Version" forKey:kLastRanVersion];
// Set the memory warning flag as a previous session would have.
[defaults setBool:YES forKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
// Instantiate the PreviousSessionInfo sharedInstance.
PreviousSessionInfo* sharedInstance = [PreviousSessionInfo sharedInstance];
// Checks the values.
EXPECT_TRUE([sharedInstance didSeeMemoryWarningShortlyBeforeTerminating]);
EXPECT_TRUE([sharedInstance isFirstSessionAfterUpgrade]);
}
// Creates conditions that exist on the first app run and tests
// OSRestartedAfterPreviousSession property.
TEST_F(PreviousSessionInfoTest, InitializationWithoutSystemStartTime) {
[PreviousSessionInfo resetSharedInstanceForTesting];
[[NSUserDefaults standardUserDefaults]
removeObjectForKey:previous_session_info_constants::kOSStartTime];
EXPECT_FALSE(
[[PreviousSessionInfo sharedInstance] OSRestartedAfterPreviousSession]);
}
// Creates conditions that exist when OS was restarted after the previous app
// run and tests OSRestartedAfterPreviousSession property.
TEST_F(PreviousSessionInfoTest, InitializationAfterOSRestart) {
[PreviousSessionInfo resetSharedInstanceForTesting];
// For the previous session OS started 60 seconds before OS has started for
// this session.
NSTimeInterval current_system_start_time =
NSDate.timeIntervalSinceReferenceDate -
NSProcessInfo.processInfo.systemUptime;
[[NSUserDefaults standardUserDefaults]
setDouble:current_system_start_time - 60
forKey:previous_session_info_constants::kOSStartTime];
EXPECT_TRUE(
[[PreviousSessionInfo sharedInstance] OSRestartedAfterPreviousSession]);
}
// Creates conditions that exist when OS was not restarted after the previous
// app run and tests OSRestartedAfterPreviousSession property.
TEST_F(PreviousSessionInfoTest, InitializationForSecondSessionAfterOSRestart) {
[PreviousSessionInfo resetSharedInstanceForTesting];
// OS startup time is the same for this and previous session.
NSTimeInterval current_system_start_time =
NSDate.timeIntervalSinceReferenceDate -
NSProcessInfo.processInfo.systemUptime;
[[NSUserDefaults standardUserDefaults]
setDouble:current_system_start_time
forKey:previous_session_info_constants::kOSStartTime];
EXPECT_FALSE(
[[PreviousSessionInfo sharedInstance] OSRestartedAfterPreviousSession]);
}
TEST_F(PreviousSessionInfoTest, BeginRecordingCurrentSession) {
[PreviousSessionInfo resetSharedInstanceForTesting];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
[defaults removeObjectForKey:kLastRanVersion];
// Set the memory warning flag as a previous session would have.
[defaults setBool:YES forKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
[[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession];
// Check that the version has been updated.
EXPECT_NSEQ(base::SysUTF8ToNSString(version_info::GetVersionNumber()),
[defaults stringForKey:kLastRanVersion]);
// Check that the memory warning flag has been reset.
EXPECT_FALSE(
[defaults boolForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]);
}
TEST_F(PreviousSessionInfoTest, SetMemoryWarningFlagNoOpUntilRecordingBegins) {
[PreviousSessionInfo resetSharedInstanceForTesting];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
[defaults removeObjectForKey:kLastRanVersion];
// Call the flag setter.
[[PreviousSessionInfo sharedInstance] setMemoryWarningFlag];
EXPECT_FALSE(
[defaults boolForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]);
}
TEST_F(PreviousSessionInfoTest,
ResetMemoryWarningFlagNoOpUntilRecordingBegins) {
[PreviousSessionInfo resetSharedInstanceForTesting];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
[defaults removeObjectForKey:kLastRanVersion];
// Set the memory warning flag as a previous session would have.
[defaults setBool:YES forKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
// Call the memory warning flag resetter.
[[PreviousSessionInfo sharedInstance] resetMemoryWarningFlag];
EXPECT_TRUE(
[defaults boolForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]);
}
TEST_F(PreviousSessionInfoTest, MemoryWarningFlagMethodsAfterRecordingBegins) {
[PreviousSessionInfo resetSharedInstanceForTesting];
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
[defaults removeObjectForKey:kLastRanVersion];
// Launch the recording of the session.
[[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession];
EXPECT_FALSE(
[defaults boolForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]);
// Call the memory warning flag setter.
[[PreviousSessionInfo sharedInstance] setMemoryWarningFlag];
EXPECT_TRUE(
[defaults boolForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]);
// Call the memory warning flag resetter.
[[PreviousSessionInfo sharedInstance] resetMemoryWarningFlag];
EXPECT_FALSE(
[defaults boolForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]);
}
// Tests restoringSession is in sync with User Defaults.
TEST_F(PreviousSessionInfoTest, NoSessionRestorationInProgress) {
[PreviousSessionInfo resetSharedInstanceForTesting];
[NSUserDefaults.standardUserDefaults
removeObjectForKey:kPreviousSessionInfoRestoringSession];
[[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession];
EXPECT_FALSE([[PreviousSessionInfo sharedInstance]
terminatedDuringSessionRestoration]);
}
// Tests restoringSession is in sync with User Defaults.
TEST_F(PreviousSessionInfoTest, SessionRestorationInProgress) {
[PreviousSessionInfo resetSharedInstanceForTesting];
[NSUserDefaults.standardUserDefaults
setBool:YES
forKey:kPreviousSessionInfoRestoringSession];
[[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession];
EXPECT_TRUE([[PreviousSessionInfo sharedInstance]
terminatedDuringSessionRestoration]);
}
// Tests that resetSessionRestorationFlag resets User Defaults.
TEST_F(PreviousSessionInfoTest, ResetSessionRestorationFlag) {
[PreviousSessionInfo resetSharedInstanceForTesting];
[NSUserDefaults.standardUserDefaults
setBool:YES
forKey:kPreviousSessionInfoRestoringSession];
[[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession];
ASSERT_TRUE([NSUserDefaults.standardUserDefaults
boolForKey:kPreviousSessionInfoRestoringSession]);
EXPECT_TRUE([[PreviousSessionInfo sharedInstance]
terminatedDuringSessionRestoration]);
[[PreviousSessionInfo sharedInstance] resetSessionRestorationFlag];
EXPECT_FALSE([NSUserDefaults.standardUserDefaults
boolForKey:kPreviousSessionInfoRestoringSession]);
EXPECT_FALSE([[PreviousSessionInfo sharedInstance]
terminatedDuringSessionRestoration]);
}
// Tests that scoped object returned from startSessionRestoration correctly
// resets User Defaults.
TEST_F(PreviousSessionInfoTest, ParallelSessionRestorations) {
[PreviousSessionInfo resetSharedInstanceForTesting];
[NSUserDefaults.standardUserDefaults
removeObjectForKey:kPreviousSessionInfoRestoringSession];
[[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession];
ASSERT_FALSE([[PreviousSessionInfo sharedInstance]
terminatedDuringSessionRestoration]);
{
base::ScopedClosureRunner scoped_restoration =
[[PreviousSessionInfo sharedInstance] startSessionRestoration];
EXPECT_TRUE([NSUserDefaults.standardUserDefaults
boolForKey:kPreviousSessionInfoRestoringSession]);
// This should reset to NO after beginRecordingCurrentSession or
// resetSessionRestorationFlag
EXPECT_FALSE([[PreviousSessionInfo sharedInstance]
terminatedDuringSessionRestoration]);
{
base::ScopedClosureRunner scoped_restoration2 =
[[PreviousSessionInfo sharedInstance] startSessionRestoration];
EXPECT_TRUE([NSUserDefaults.standardUserDefaults
boolForKey:kPreviousSessionInfoRestoringSession]);
// This should reset to NO after beginRecordingCurrentSession or
// resetSessionRestorationFlag
EXPECT_FALSE([[PreviousSessionInfo sharedInstance]
terminatedDuringSessionRestoration]);
}
EXPECT_TRUE([NSUserDefaults.standardUserDefaults
boolForKey:kPreviousSessionInfoRestoringSession]);
// This should reset to NO after beginRecordingCurrentSession or
// resetSessionRestorationFlag
EXPECT_FALSE([[PreviousSessionInfo sharedInstance]
terminatedDuringSessionRestoration]);
}
EXPECT_FALSE([NSUserDefaults.standardUserDefaults
boolForKey:kPreviousSessionInfoRestoringSession]);
EXPECT_FALSE([[PreviousSessionInfo sharedInstance]
terminatedDuringSessionRestoration]);
}
// Tests that resetSessionRestorationFlag resets the flag during session
// restoration and that flag is kept reset after restoration is finished.
TEST_F(PreviousSessionInfoTest,
ResetSessionRestorationFlagDuringParallelSessionRestorations) {
[PreviousSessionInfo resetSharedInstanceForTesting];
[NSUserDefaults.standardUserDefaults
removeObjectForKey:kPreviousSessionInfoRestoringSession];
[[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession];
ASSERT_FALSE([[PreviousSessionInfo sharedInstance]
terminatedDuringSessionRestoration]);
{
base::ScopedClosureRunner scoped_restoration =
[[PreviousSessionInfo sharedInstance] startSessionRestoration];
EXPECT_TRUE([NSUserDefaults.standardUserDefaults
boolForKey:kPreviousSessionInfoRestoringSession]);
// This should reset to NO after beginRecordingCurrentSession or
// resetSessionRestorationFlag
EXPECT_FALSE([[PreviousSessionInfo sharedInstance]
terminatedDuringSessionRestoration]);
{
base::ScopedClosureRunner scoped_restoration2 =
[[PreviousSessionInfo sharedInstance] startSessionRestoration];
EXPECT_TRUE([NSUserDefaults.standardUserDefaults
boolForKey:kPreviousSessionInfoRestoringSession]);
// This should reset to NO after beginRecordingCurrentSession or
// resetSessionRestorationFlag
EXPECT_FALSE([[PreviousSessionInfo sharedInstance]
terminatedDuringSessionRestoration]);
[[PreviousSessionInfo sharedInstance] resetSessionRestorationFlag];
EXPECT_FALSE([[PreviousSessionInfo sharedInstance]
terminatedDuringSessionRestoration]);
EXPECT_FALSE([NSUserDefaults.standardUserDefaults
boolForKey:kPreviousSessionInfoRestoringSession]);
}
// scoped_restoration2 should not set |restoringSession| to previous state
// (YES), but rather leave the reset state.
EXPECT_FALSE([NSUserDefaults.standardUserDefaults
boolForKey:kPreviousSessionInfoRestoringSession]);
EXPECT_FALSE([[PreviousSessionInfo sharedInstance]
terminatedDuringSessionRestoration]);
}
EXPECT_FALSE([NSUserDefaults.standardUserDefaults
boolForKey:kPreviousSessionInfoRestoringSession]);
EXPECT_FALSE([[PreviousSessionInfo sharedInstance]
terminatedDuringSessionRestoration]);
}
// Tests adding and removing report parameters.
TEST_F(PreviousSessionInfoTest, ReportParameters) {
// Default state.
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
for (NSString* key in [defaults dictionaryRepresentation].allKeys) {
if ([key hasPrefix:kPreviousSessionInfoParamsPrefix]) {
[defaults removeObjectForKey:key];
}
}
[PreviousSessionInfo resetSharedInstanceForTesting];
EXPECT_EQ([PreviousSessionInfo sharedInstance].reportParameters.count, 0ul);
// Removing non-existing key does not crash.
NSString* const kKey0 = @"url0";
[[PreviousSessionInfo sharedInstance] removeReportParameterForKey:kKey0];
[PreviousSessionInfo resetSharedInstanceForTesting];
EXPECT_EQ([PreviousSessionInfo sharedInstance].reportParameters.count, 0ul);
// Add first URL.
[[PreviousSessionInfo sharedInstance]
setReportParameterURL:GURL("https://example.test/path")
forKey:kKey0];
NSDictionary<NSString*, NSString*>* URLs = GetParamsDictionary();
EXPECT_NSEQ(@{kKey0 : @"https://example.test/"}, URLs); // stores only origin
[PreviousSessionInfo resetSharedInstanceForTesting];
EXPECT_NSEQ(URLs, [[PreviousSessionInfo sharedInstance] reportParameters]);
// Update first URL.
[[PreviousSessionInfo sharedInstance]
setReportParameterURL:GURL("https://example2.test/path")
forKey:kKey0];
URLs = GetParamsDictionary();
EXPECT_NSEQ(@{kKey0 : @"https://example2.test/"}, URLs);
// Add second URL.
NSString* const kKey1 = @"url1";
[[PreviousSessionInfo sharedInstance]
setReportParameterURL:GURL("https://example3.test/path")
forKey:kKey1];
URLs = GetParamsDictionary();
NSDictionary<NSString*, NSString*>* expected = @{
kKey0 : @"https://example2.test/",
kKey1 : @"https://example3.test/",
};
EXPECT_NSEQ(expected, URLs);
// Removing non-existing key does not crash.
[[PreviousSessionInfo sharedInstance] removeReportParameterForKey:@"url2"];
// Remove first URL.
[[PreviousSessionInfo sharedInstance] removeReportParameterForKey:kKey0];
URLs = GetParamsDictionary();
EXPECT_NSEQ(@{kKey1 : @"https://example3.test/"}, URLs);
// Remove second URL.
[[PreviousSessionInfo sharedInstance] removeReportParameterForKey:kKey1];
URLs = GetParamsDictionary();
EXPECT_EQ(URLs.count, 0ul);
[PreviousSessionInfo resetSharedInstanceForTesting];
EXPECT_EQ([PreviousSessionInfo sharedInstance].reportParameters.count, 0ul);
[PreviousSessionInfo resetSharedInstanceForTesting];
// Write a param with spaces, and other non-standard characters.
NSString* const kAtypicalKey1 = @"* \xe2\x99\xa0";
NSString* const kAtypicalKey2 = @"http:// not a url.";
NSString* const kAtypicalKey3 =
@"\xef\xbf\xbd,\xef\xbf\xbd,\xf0\x90\x8c\x80z,\xef\xbf\xbds";
[[PreviousSessionInfo sharedInstance] setReportParameterValue:kAtypicalKey1
forKey:kAtypicalKey1];
[[PreviousSessionInfo sharedInstance] setReportParameterValue:kAtypicalKey2
forKey:kAtypicalKey2];
[[PreviousSessionInfo sharedInstance] setReportParameterValue:kAtypicalKey3
forKey:kAtypicalKey3];
expected = @{
kAtypicalKey1 : kAtypicalKey1,
kAtypicalKey2 : kAtypicalKey2,
kAtypicalKey3 : kAtypicalKey3,
};
EXPECT_NSEQ(expected, GetParamsDictionary());
[[PreviousSessionInfo sharedInstance]
removeReportParameterForKey:kAtypicalKey1];
[[PreviousSessionInfo sharedInstance]
removeReportParameterForKey:kAtypicalKey2];
[[PreviousSessionInfo sharedInstance]
removeReportParameterForKey:kAtypicalKey3];
[PreviousSessionInfo resetSharedInstanceForTesting];
EXPECT_EQ([PreviousSessionInfo sharedInstance].reportParameters.count, 0ul);
[PreviousSessionInfo resetSharedInstanceForTesting];
}
// Tests that memory footprint gets written to NSUserDefaults after
// startRecordingMemoryFootprintWithInterval: call.
TEST_F(PreviousSessionInfoTest, MemoryFootprintRecording) {
web::WebTaskEnvironment task_environment;
[PreviousSessionInfo resetSharedInstanceForTesting];
[NSUserDefaults.standardUserDefaults
removeObjectForKey:kPreviousSessionInfoMemoryFootprint];
[[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession];
[[PreviousSessionInfo sharedInstance]
startRecordingMemoryFootprintWithInterval:base::Milliseconds(1)];
// Memory footprint should be updated after timeout.
EXPECT_FALSE([NSUserDefaults.standardUserDefaults
objectForKey:kPreviousSessionInfoMemoryFootprint]);
EXPECT_TRUE(
base::test::ios::WaitUntilConditionOrTimeout(base::Seconds(1), ^bool {
base::RunLoop().RunUntilIdle();
return [[NSUserDefaults.standardUserDefaults
objectForKey:kPreviousSessionInfoMemoryFootprint]
integerValue] > 0;
}));
}
// Tests tabCount property.
TEST_F(PreviousSessionInfoTest, TabCount) {
[PreviousSessionInfo resetSharedInstanceForTesting];
[NSUserDefaults.standardUserDefaults setInteger:kTabCount
forKey:kPreviousSessionInfoTabCount];
[[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession];
EXPECT_EQ(kTabCount, [PreviousSessionInfo sharedInstance].tabCount);
}
// Tests tab count gets written to NSUserDefaults.
TEST_F(PreviousSessionInfoTest, TabCountRecording) {
[PreviousSessionInfo resetSharedInstanceForTesting];
[NSUserDefaults.standardUserDefaults
removeObjectForKey:kPreviousSessionInfoTabCount];
[[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession];
[[PreviousSessionInfo sharedInstance] updateCurrentSessionTabCount:kTabCount];
EXPECT_NSEQ(@(kTabCount), [NSUserDefaults.standardUserDefaults
objectForKey:kPreviousSessionInfoTabCount]);
}
// Tests inactiveTabCount property.
TEST_F(PreviousSessionInfoTest, InactiveTabCount) {
[PreviousSessionInfo resetSharedInstanceForTesting];
[NSUserDefaults.standardUserDefaults
setInteger:kInactiveTabCount
forKey:kPreviousSessionInfoInactiveTabCount];
[[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession];
EXPECT_EQ(kInactiveTabCount,
[PreviousSessionInfo sharedInstance].inactiveTabCount);
}
// Tests inactive tab count gets written to NSUserDefaults.
TEST_F(PreviousSessionInfoTest, InactiveTabCountRecording) {
[PreviousSessionInfo resetSharedInstanceForTesting];
[NSUserDefaults.standardUserDefaults
removeObjectForKey:kPreviousSessionInfoInactiveTabCount];
[[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession];
[[PreviousSessionInfo sharedInstance]
updateCurrentSessionInactiveTabCount:kInactiveTabCount];
EXPECT_NSEQ(@(kInactiveTabCount),
[NSUserDefaults.standardUserDefaults
objectForKey:kPreviousSessionInfoInactiveTabCount]);
}
// Tests OTRTabCount property.
TEST_F(PreviousSessionInfoTest, OtrTabCount) {
[PreviousSessionInfo resetSharedInstanceForTesting];
[NSUserDefaults.standardUserDefaults
setInteger:kTabCount
forKey:kPreviousSessionInfoOTRTabCount];
[[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession];
EXPECT_EQ(kTabCount, [PreviousSessionInfo sharedInstance].OTRTabCount);
}
// Tests OTR tab count gets written to NSUserDefaults.
TEST_F(PreviousSessionInfoTest, OtrTabCountRecording) {
[PreviousSessionInfo resetSharedInstanceForTesting];
[NSUserDefaults.standardUserDefaults
removeObjectForKey:kPreviousSessionInfoOTRTabCount];
[[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession];
[[PreviousSessionInfo sharedInstance]
updateCurrentSessionOTRTabCount:kTabCount];
EXPECT_NSEQ(@(kTabCount), [NSUserDefaults.standardUserDefaults
objectForKey:kPreviousSessionInfoOTRTabCount]);
}
// Tests memoryFootprint property.
TEST_F(PreviousSessionInfoTest, MemoryFootprint) {
[PreviousSessionInfo resetSharedInstanceForTesting];
NSInteger kMemoryFootprint = 1869;
[NSUserDefaults.standardUserDefaults
setInteger:kMemoryFootprint
forKey:kPreviousSessionInfoMemoryFootprint];
[[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession];
EXPECT_EQ(kMemoryFootprint,
[PreviousSessionInfo sharedInstance].memoryFootprint);
}
// Tests data collection pausing.
TEST_F(PreviousSessionInfoTest, PausePreviousSessionInfoCollection) {
// Default state.
[NSUserDefaults.standardUserDefaults
removeObjectForKey:previous_session_info_constants::
kPreviousSessionInfoApplicationState];
[PreviousSessionInfo resetSharedInstanceForTesting];
EXPECT_FALSE([NSUserDefaults.standardUserDefaults
valueForKey:previous_session_info_constants::
kPreviousSessionInfoApplicationState]);
// Start recording. This should update the state.
[[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession];
EXPECT_TRUE([NSUserDefaults.standardUserDefaults
valueForKey:previous_session_info_constants::
kPreviousSessionInfoApplicationState]);
// Cleanup.
[NSUserDefaults.standardUserDefaults
removeObjectForKey:previous_session_info_constants::
kPreviousSessionInfoApplicationState];
EXPECT_FALSE([NSUserDefaults.standardUserDefaults
valueForKey:previous_session_info_constants::
kPreviousSessionInfoApplicationState]);
// Updating state should work when recording is enabled.
[[PreviousSessionInfo sharedInstance] updateApplicationState];
EXPECT_TRUE([NSUserDefaults.standardUserDefaults
valueForKey:previous_session_info_constants::
kPreviousSessionInfoApplicationState]);
// Cleanup.
[NSUserDefaults.standardUserDefaults
removeObjectForKey:previous_session_info_constants::
kPreviousSessionInfoApplicationState];
EXPECT_FALSE([NSUserDefaults.standardUserDefaults
valueForKey:previous_session_info_constants::
kPreviousSessionInfoApplicationState]);
// Updating state should be noop when recording is paused.
[[PreviousSessionInfo sharedInstance] pauseRecordingCurrentSession];
[[PreviousSessionInfo sharedInstance] updateApplicationState];
EXPECT_FALSE([NSUserDefaults.standardUserDefaults
valueForKey:previous_session_info_constants::
kPreviousSessionInfoApplicationState]);
// Resume recording should update the state.
[[PreviousSessionInfo sharedInstance] resumeRecordingCurrentSession];
EXPECT_TRUE([NSUserDefaults.standardUserDefaults
valueForKey:previous_session_info_constants::
kPreviousSessionInfoApplicationState]);
// Cleanup
[NSUserDefaults.standardUserDefaults
removeObjectForKey:previous_session_info_constants::
kPreviousSessionInfoApplicationState];
EXPECT_FALSE([NSUserDefaults.standardUserDefaults
valueForKey:previous_session_info_constants::
kPreviousSessionInfoApplicationState]);
// Updating state should work when recording is enabled.
[[PreviousSessionInfo sharedInstance] updateApplicationState];
EXPECT_TRUE([NSUserDefaults.standardUserDefaults
valueForKey:previous_session_info_constants::
kPreviousSessionInfoApplicationState]);
// Cleanup.
[NSUserDefaults.standardUserDefaults
removeObjectForKey:previous_session_info_constants::
kPreviousSessionInfoApplicationState];
[PreviousSessionInfo resetSharedInstanceForTesting];
}
} // namespace
|