Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import "base/mac/bind_objc_block.h" | |
|
Eugene But (OOO till 7-30)
2016/05/18 15:55:08
s/import/include
https://engdoc.corp.google.com/e
gambard
2016/05/19 09:35:04
Done.
| |
| 6 #include "base/mac/scoped_nsobject.h" | |
|
Eugene But (OOO till 7-30)
2016/05/18 15:55:09
s/include/import
gambard
2016/05/19 09:35:04
Done.
| |
| 7 #include "base/memory/memory_pressure_listener.h" | |
| 8 #include "base/threading/thread.h" | |
| 9 #import "ios/chrome/browser/metrics/previous_session_info.h" | |
| 10 #import "ios/chrome/app/application_delegate/memory_recovery_helper.h" | |
|
Eugene But (OOO till 7-30)
2016/05/18 15:55:09
This import should go first, then libraries, then
gambard
2016/05/19 09:35:03
Done.
| |
| 11 #include "testing/platform_test.h" | |
| 12 | |
| 13 namespace { | |
|
Eugene But (OOO till 7-30)
2016/05/18 15:55:08
NIT: There is no need to put tests into anonymous
gambard
2016/05/19 09:35:04
Done.
| |
| 14 class MemoryRecoveryHelperTest : public PlatformTest { | |
| 15 protected: | |
| 16 MemoryRecoveryHelperTest() { | |
| 17 // Set up |memory_pressure_listener_| to invoke |OnMemoryPressure| which | |
| 18 // will store the memory pressure level sent to the callback in | |
| 19 // |memory_pressure_level_| so that tests can verify the level is correct. | |
| 20 memory_pressure_listener_.reset(new base::MemoryPressureListener(base::Bind( | |
| 21 &MemoryRecoveryHelperTest::OnMemoryPressure, base::Unretained(this)))); | |
| 22 memory_pressure_level_ = | |
| 23 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; | |
| 24 } | |
| 25 | |
| 26 base::MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevel() { | |
| 27 return memory_pressure_level_; | |
| 28 } | |
| 29 | |
| 30 MemoryRecoveryHelper* GetMemoryHelper() { | |
| 31 if (!memory_helper_.get()) { | |
|
Eugene But (OOO till 7-30)
2016/05/18 15:55:09
Drop |.get()|
gambard
2016/05/19 09:35:04
Done.
| |
| 32 memory_helper_.reset([[MemoryRecoveryHelper alloc] init]); | |
| 33 } | |
| 34 return memory_helper_.get(); | |
| 35 } | |
| 36 | |
| 37 // Callback for |memory_pressure_listener_|. | |
| 38 void OnMemoryPressure( | |
| 39 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { | |
| 40 memory_pressure_level_ = memory_pressure_level; | |
| 41 message_loop_.QuitWhenIdle(); | |
| 42 } | |
| 43 | |
| 44 base::MessageLoop& message_loop() { return message_loop_; } | |
| 45 | |
| 46 private: | |
| 47 base::MessageLoop message_loop_; | |
| 48 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level_; | |
| 49 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; | |
| 50 base::scoped_nsobject<MemoryRecoveryHelper> memory_helper_; | |
| 51 | |
| 52 DISALLOW_COPY_AND_ASSIGN(MemoryRecoveryHelperTest); | |
| 53 }; | |
| 54 | |
| 55 // Invokes resetForegroundMemoryWarningCount and verifies the | |
| 56 // foregroundMemoryWarningCount is setted to 0 | |
|
Eugene But (OOO till 7-30)
2016/05/18 15:55:07
Optional NIT: finish the comments with full stop
gambard
2016/05/19 09:35:04
Done.
| |
| 57 TEST_F(MemoryRecoveryHelperTest, VerifyForegroundMemoryWarningCountReset) { | |
| 58 // Setup | |
| 59 MemoryRecoveryHelper* memoryHelper = GetMemoryHelper(); | |
| 60 [memoryHelper handleMemoryPressure]; | |
| 61 int previousForegroundMemoryWarningCount = | |
|
Eugene But (OOO till 7-30)
2016/05/18 15:55:09
NIT: Drop this variable and just use EXPECT_TRUE (
gambard
2016/05/19 09:35:04
Done.
| |
| 62 memoryHelper.foregroundMemoryWarningCount; | |
| 63 | |
| 64 // Action | |
| 65 [memoryHelper resetForegroundMemoryWarningCount]; | |
| 66 | |
| 67 // Test | |
| 68 EXPECT_TRUE(previousForegroundMemoryWarningCount != 0); | |
| 69 EXPECT_EQ(0, memoryHelper.foregroundMemoryWarningCount); | |
| 70 } | |
| 71 | |
| 72 // Invokes applicationDidReceiveMemoryWarning and verifies the memory pressure | |
| 73 // callback (i.e. MainControllerTest::OnMemoryPressure) is invoked. | |
| 74 TEST_F(MemoryRecoveryHelperTest, VerifyApplicationDidReceiveMemoryWarning) { | |
| 75 MemoryRecoveryHelper* memoryHelper = GetMemoryHelper(); | |
|
Eugene But (OOO till 7-30)
2016/05/18 15:55:09
Optional NIT: Drop this variable
gambard
2016/05/19 09:35:04
Done.
| |
| 76 [memoryHelper handleMemoryPressure]; | |
| 77 message_loop().Run(); | |
| 78 EXPECT_EQ(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, | |
| 79 GetMemoryPressureLevel()); | |
| 80 } | |
| 81 | |
| 82 // Invokes applicationDidReceiveMemoryWarning and verifies the flags (i.e. | |
| 83 // breakpad_helper and NSUserDefaults) are set | |
| 84 TEST_F(MemoryRecoveryHelperTest, VerifyHelperDidSetMemoryWarningFlags) { | |
| 85 // Setup | |
| 86 MemoryRecoveryHelper* memoryHelper = GetMemoryHelper(); | |
|
Eugene But (OOO till 7-30)
2016/05/18 15:55:09
Drop this variable, it is used only once and GetMe
gambard
2016/05/19 09:35:04
Done.
| |
| 87 [[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession]; | |
| 88 [[PreviousSessionInfo sharedInstance] resetMemoryWarningFlag]; | |
| 89 int foregroundMemoryWarningCountBeforeWarning = | |
| 90 GetMemoryHelper().foregroundMemoryWarningCount; | |
| 91 | |
| 92 BOOL memoryWarningFlagBeforeAlert = [[NSUserDefaults standardUserDefaults] | |
| 93 boolForKey:previous_session_info_constants:: | |
|
Eugene But (OOO till 7-30)
2016/05/18 15:55:07
Optional NIT: Could you please add using previous_
gambard
2016/05/19 09:35:03
Done.
| |
| 94 kDidSeeMemoryWarningShortlyBeforeTerminating]; | |
| 95 | |
| 96 // Action | |
| 97 [memoryHelper handleMemoryPressure]; | |
| 98 | |
| 99 // Tests | |
| 100 EXPECT_TRUE([[NSUserDefaults standardUserDefaults] | |
| 101 boolForKey:previous_session_info_constants:: | |
| 102 kDidSeeMemoryWarningShortlyBeforeTerminating]); | |
| 103 EXPECT_FALSE(memoryWarningFlagBeforeAlert); | |
| 104 EXPECT_EQ(foregroundMemoryWarningCountBeforeWarning + 1, | |
| 105 GetMemoryHelper().foregroundMemoryWarningCount); | |
| 106 } | |
| 107 } // namespace | |
| OLD | NEW |