Chromium Code Reviews| Index: ios/chrome/app/application_delegate/memory_recovery_helper_unittest.mm |
| diff --git a/ios/chrome/app/application_delegate/memory_recovery_helper_unittest.mm b/ios/chrome/app/application_delegate/memory_recovery_helper_unittest.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..99a5d426d6c71b258a38aceaf865872bf202bb9a |
| --- /dev/null |
| +++ b/ios/chrome/app/application_delegate/memory_recovery_helper_unittest.mm |
| @@ -0,0 +1,107 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#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.
|
| +#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.
|
| +#include "base/memory/memory_pressure_listener.h" |
| +#include "base/threading/thread.h" |
| +#import "ios/chrome/browser/metrics/previous_session_info.h" |
| +#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.
|
| +#include "testing/platform_test.h" |
| + |
| +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.
|
| +class MemoryRecoveryHelperTest : public PlatformTest { |
| + protected: |
| + MemoryRecoveryHelperTest() { |
| + // Set up |memory_pressure_listener_| to invoke |OnMemoryPressure| which |
| + // will store the memory pressure level sent to the callback in |
| + // |memory_pressure_level_| so that tests can verify the level is correct. |
| + memory_pressure_listener_.reset(new base::MemoryPressureListener(base::Bind( |
| + &MemoryRecoveryHelperTest::OnMemoryPressure, base::Unretained(this)))); |
| + memory_pressure_level_ = |
| + base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; |
| + } |
| + |
| + base::MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevel() { |
| + return memory_pressure_level_; |
| + } |
| + |
| + MemoryRecoveryHelper* GetMemoryHelper() { |
| + 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.
|
| + memory_helper_.reset([[MemoryRecoveryHelper alloc] init]); |
| + } |
| + return memory_helper_.get(); |
| + } |
| + |
| + // Callback for |memory_pressure_listener_|. |
| + void OnMemoryPressure( |
| + base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { |
| + memory_pressure_level_ = memory_pressure_level; |
| + message_loop_.QuitWhenIdle(); |
| + } |
| + |
| + base::MessageLoop& message_loop() { return message_loop_; } |
| + |
| + private: |
| + base::MessageLoop message_loop_; |
| + base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level_; |
| + std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; |
| + base::scoped_nsobject<MemoryRecoveryHelper> memory_helper_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MemoryRecoveryHelperTest); |
| +}; |
| + |
| +// Invokes resetForegroundMemoryWarningCount and verifies the |
| +// 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.
|
| +TEST_F(MemoryRecoveryHelperTest, VerifyForegroundMemoryWarningCountReset) { |
| + // Setup |
| + MemoryRecoveryHelper* memoryHelper = GetMemoryHelper(); |
| + [memoryHelper handleMemoryPressure]; |
| + 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.
|
| + memoryHelper.foregroundMemoryWarningCount; |
| + |
| + // Action |
| + [memoryHelper resetForegroundMemoryWarningCount]; |
| + |
| + // Test |
| + EXPECT_TRUE(previousForegroundMemoryWarningCount != 0); |
| + EXPECT_EQ(0, memoryHelper.foregroundMemoryWarningCount); |
| +} |
| + |
| +// Invokes applicationDidReceiveMemoryWarning and verifies the memory pressure |
| +// callback (i.e. MainControllerTest::OnMemoryPressure) is invoked. |
| +TEST_F(MemoryRecoveryHelperTest, VerifyApplicationDidReceiveMemoryWarning) { |
| + 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.
|
| + [memoryHelper handleMemoryPressure]; |
| + message_loop().Run(); |
| + EXPECT_EQ(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, |
| + GetMemoryPressureLevel()); |
| +} |
| + |
| +// Invokes applicationDidReceiveMemoryWarning and verifies the flags (i.e. |
| +// breakpad_helper and NSUserDefaults) are set |
| +TEST_F(MemoryRecoveryHelperTest, VerifyHelperDidSetMemoryWarningFlags) { |
| + // Setup |
| + 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.
|
| + [[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession]; |
| + [[PreviousSessionInfo sharedInstance] resetMemoryWarningFlag]; |
| + int foregroundMemoryWarningCountBeforeWarning = |
| + GetMemoryHelper().foregroundMemoryWarningCount; |
| + |
| + BOOL memoryWarningFlagBeforeAlert = [[NSUserDefaults standardUserDefaults] |
| + 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.
|
| + kDidSeeMemoryWarningShortlyBeforeTerminating]; |
| + |
| + // Action |
| + [memoryHelper handleMemoryPressure]; |
| + |
| + // Tests |
| + EXPECT_TRUE([[NSUserDefaults standardUserDefaults] |
| + boolForKey:previous_session_info_constants:: |
| + kDidSeeMemoryWarningShortlyBeforeTerminating]); |
| + EXPECT_FALSE(memoryWarningFlagBeforeAlert); |
| + EXPECT_EQ(foregroundMemoryWarningCountBeforeWarning + 1, |
| + GetMemoryHelper().foregroundMemoryWarningCount); |
| +} |
| +} // namespace |