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.
5 #include "ios/chrome/browser/metrics/previous_session_info.h"
7 #include "base/strings/sys_string_conversions.h"
8 #include "components/version_info/version_info.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/gtest_mac.h"
14 // Key in the UserDefaults for a boolean value keeping track of memory warnings.
15 NSString* const kDidSeeMemoryWarningShortlyBeforeTerminating =
16 @"DidSeeMemoryWarning";
18 // Key in the NSUserDefaults for a string value that stores the version of the
20 NSString* const kLastRanVersion = @"LastRanVersion";
22 TEST(PreviousSessionInfoTest, InitializationWithEmptyDefaults) {
23 [PreviousSessionInfo resetSharedInstanceForTesting];
24 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
25 [defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
26 [defaults removeObjectForKey:kLastRanVersion];
28 // Instantiate the PreviousSessionInfo sharedInstance.
29 PreviousSessionInfo* sharedInstance = [PreviousSessionInfo sharedInstance];
31 // Checks the default values.
32 EXPECT_FALSE([sharedInstance didSeeMemoryWarningShortlyBeforeTerminating]);
33 EXPECT_TRUE([sharedInstance isFirstSessionAfterUpgrade]);
36 TEST(PreviousSessionInfoTest, InitializationWithSameVersionNoMemoryWarning) {
37 [PreviousSessionInfo resetSharedInstanceForTesting];
38 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
39 [defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
40 [defaults removeObjectForKey:kLastRanVersion];
42 // Set the current version as the last ran version.
43 NSString* currentVersion =
44 base::SysUTF8ToNSString(version_info::GetVersionNumber());
45 [defaults setObject:currentVersion forKey:kLastRanVersion];
47 // Instantiate the PreviousSessionInfo sharedInstance.
48 PreviousSessionInfo* sharedInstance = [PreviousSessionInfo sharedInstance];
51 EXPECT_FALSE([sharedInstance didSeeMemoryWarningShortlyBeforeTerminating]);
52 EXPECT_FALSE([sharedInstance isFirstSessionAfterUpgrade]);
55 TEST(PreviousSessionInfoTest, InitializationWithSameVersionMemoryWarning) {
56 [PreviousSessionInfo resetSharedInstanceForTesting];
57 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
58 [defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
59 [defaults removeObjectForKey:kLastRanVersion];
61 // Set the current version as the last ran version.
62 NSString* currentVersion =
63 base::SysUTF8ToNSString(version_info::GetVersionNumber());
64 [defaults setObject:currentVersion forKey:kLastRanVersion];
66 // Set the memory warning flag as a previous session would have.
67 [defaults setBool:YES forKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
69 // Instantiate the PreviousSessionInfo sharedInstance.
70 PreviousSessionInfo* sharedInstance = [PreviousSessionInfo sharedInstance];
73 EXPECT_TRUE([sharedInstance didSeeMemoryWarningShortlyBeforeTerminating]);
74 EXPECT_FALSE([sharedInstance isFirstSessionAfterUpgrade]);
77 TEST(PreviousSessionInfoTest, InitializationDifferentVersionNoMemoryWarning) {
78 [PreviousSessionInfo resetSharedInstanceForTesting];
79 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
80 [defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
81 [defaults removeObjectForKey:kLastRanVersion];
83 // Set the current version as the last ran version.
84 [defaults setObject:@"Fake Version" forKey:kLastRanVersion];
86 // Instantiate the PreviousSessionInfo sharedInstance.
87 PreviousSessionInfo* sharedInstance = [PreviousSessionInfo sharedInstance];
90 EXPECT_FALSE([sharedInstance didSeeMemoryWarningShortlyBeforeTerminating]);
91 EXPECT_TRUE([sharedInstance isFirstSessionAfterUpgrade]);
94 TEST(PreviousSessionInfoTest, InitializationDifferentVersionMemoryWarning) {
95 [PreviousSessionInfo resetSharedInstanceForTesting];
96 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
97 [defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
98 [defaults removeObjectForKey:kLastRanVersion];
100 // Set the current version as the last ran version.
101 [defaults setObject:@"Fake Version" forKey:kLastRanVersion];
103 // Set the memory warning flag as a previous session would have.
104 [defaults setBool:YES forKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
106 // Instantiate the PreviousSessionInfo sharedInstance.
107 PreviousSessionInfo* sharedInstance = [PreviousSessionInfo sharedInstance];
109 // Checks the values.
110 EXPECT_TRUE([sharedInstance didSeeMemoryWarningShortlyBeforeTerminating]);
111 EXPECT_TRUE([sharedInstance isFirstSessionAfterUpgrade]);
114 TEST(PreviousSessionInfoTest, BeginRecordingCurrentSession) {
115 [PreviousSessionInfo resetSharedInstanceForTesting];
116 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
117 [defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
118 [defaults removeObjectForKey:kLastRanVersion];
120 // Set the memory warning flag as a previous session would have.
121 [defaults setBool:YES forKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
123 [[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession];
125 // Check that the version has been updated.
126 EXPECT_NSEQ(base::SysUTF8ToNSString(version_info::GetVersionNumber()),
127 [defaults stringForKey:kLastRanVersion]);
129 // Check that the memory warning flag has been reset.
131 [defaults boolForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]);
134 TEST(PreviousSessionInfoTest, SetMemoryWarningFlagNoOpUntilRecordingBegins) {
135 [PreviousSessionInfo resetSharedInstanceForTesting];
136 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
137 [defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
138 [defaults removeObjectForKey:kLastRanVersion];
140 // Call the flag setter.
141 [[PreviousSessionInfo sharedInstance] setMemoryWarningFlag];
144 [defaults boolForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]);
147 TEST(PreviousSessionInfoTest, ResetMemoryWarningFlagNoOpUntilRecordingBegins) {
148 [PreviousSessionInfo resetSharedInstanceForTesting];
149 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
150 [defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
151 [defaults removeObjectForKey:kLastRanVersion];
153 // Set the memory warning flag as a previous session would have.
154 [defaults setBool:YES forKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
156 // Call the memory warning flag resetter.
157 [[PreviousSessionInfo sharedInstance] resetMemoryWarningFlag];
160 [defaults boolForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]);
163 TEST(PreviousSessionInfoTest, MemoryWarningFlagMethodsAfterRecordingBegins) {
164 [PreviousSessionInfo resetSharedInstanceForTesting];
165 NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
166 [defaults removeObjectForKey:kDidSeeMemoryWarningShortlyBeforeTerminating];
167 [defaults removeObjectForKey:kLastRanVersion];
169 // Launch the recording of the session.
170 [[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession];
173 [defaults boolForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]);
175 // Call the memory warning flag setter.
176 [[PreviousSessionInfo sharedInstance] setMemoryWarningFlag];
179 [defaults boolForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]);
181 // Call the memory warning flag resetter.
182 [[PreviousSessionInfo sharedInstance] resetMemoryWarningFlag];
185 [defaults boolForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]);