previous_session_info_unittest.mm 31 KB

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