| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/chrome/app/application_delegate/memory_warning_helper.h" | 5 #import "ios/chrome/app/application_delegate/memory_warning_helper.h" |
| 6 | 6 |
| 7 #include "base/mac/bind_objc_block.h" | 7 #include "base/mac/bind_objc_block.h" |
| 8 #import "base/mac/scoped_nsobject.h" | 8 #import "base/mac/scoped_nsobject.h" |
| 9 #include "base/memory/memory_pressure_listener.h" | 9 #include "base/memory/memory_pressure_listener.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" |
| 10 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 11 #import "ios/chrome/browser/metrics/previous_session_info.h" | 13 #import "ios/chrome/browser/metrics/previous_session_info.h" |
| 12 #include "testing/platform_test.h" | 14 #include "testing/platform_test.h" |
| 13 | 15 |
| 14 using previous_session_info_constants:: | 16 using previous_session_info_constants:: |
| 15 kDidSeeMemoryWarningShortlyBeforeTerminating; | 17 kDidSeeMemoryWarningShortlyBeforeTerminating; |
| 16 | 18 |
| 17 class MemoryWarningHelperTest : public PlatformTest { | 19 class MemoryWarningHelperTest : public PlatformTest { |
| 18 protected: | 20 protected: |
| 19 MemoryWarningHelperTest() { | 21 MemoryWarningHelperTest() { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 34 if (!memory_helper_) { | 36 if (!memory_helper_) { |
| 35 memory_helper_.reset([[MemoryWarningHelper alloc] init]); | 37 memory_helper_.reset([[MemoryWarningHelper alloc] init]); |
| 36 } | 38 } |
| 37 return memory_helper_; | 39 return memory_helper_; |
| 38 } | 40 } |
| 39 | 41 |
| 40 // Callback for |memory_pressure_listener_|. | 42 // Callback for |memory_pressure_listener_|. |
| 41 void OnMemoryPressure( | 43 void OnMemoryPressure( |
| 42 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { | 44 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { |
| 43 memory_pressure_level_ = memory_pressure_level; | 45 memory_pressure_level_ = memory_pressure_level; |
| 44 message_loop_.QuitWhenIdle(); | 46 run_loop_.QuitWhenIdle(); |
| 45 } | 47 } |
| 46 | 48 |
| 47 base::MessageLoop& message_loop() { return message_loop_; } | 49 void RunMessageLoop() { run_loop_.Run(); } |
| 48 | 50 |
| 49 private: | 51 private: |
| 50 base::MessageLoop message_loop_; | 52 base::MessageLoop message_loop_; |
| 53 base::RunLoop run_loop_; |
| 51 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level_; | 54 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level_; |
| 52 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; | 55 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; |
| 53 base::scoped_nsobject<MemoryWarningHelper> memory_helper_; | 56 base::scoped_nsobject<MemoryWarningHelper> memory_helper_; |
| 54 | 57 |
| 55 DISALLOW_COPY_AND_ASSIGN(MemoryWarningHelperTest); | 58 DISALLOW_COPY_AND_ASSIGN(MemoryWarningHelperTest); |
| 56 }; | 59 }; |
| 57 | 60 |
| 58 // Invokes resetForegroundMemoryWarningCount and verifies the | 61 // Invokes resetForegroundMemoryWarningCount and verifies the |
| 59 // foregroundMemoryWarningCount is setted to 0. | 62 // foregroundMemoryWarningCount is setted to 0. |
| 60 TEST_F(MemoryWarningHelperTest, VerifyForegroundMemoryWarningCountReset) { | 63 TEST_F(MemoryWarningHelperTest, VerifyForegroundMemoryWarningCountReset) { |
| 61 // Setup. | 64 // Setup. |
| 62 [GetMemoryHelper() handleMemoryPressure]; | 65 [GetMemoryHelper() handleMemoryPressure]; |
| 63 ASSERT_TRUE(GetMemoryHelper().foregroundMemoryWarningCount != 0); | 66 ASSERT_TRUE(GetMemoryHelper().foregroundMemoryWarningCount != 0); |
| 64 | 67 |
| 65 // Action. | 68 // Action. |
| 66 [GetMemoryHelper() resetForegroundMemoryWarningCount]; | 69 [GetMemoryHelper() resetForegroundMemoryWarningCount]; |
| 67 | 70 |
| 68 // Test. | 71 // Test. |
| 69 EXPECT_EQ(0, GetMemoryHelper().foregroundMemoryWarningCount); | 72 EXPECT_EQ(0, GetMemoryHelper().foregroundMemoryWarningCount); |
| 70 } | 73 } |
| 71 | 74 |
| 72 // Invokes applicationDidReceiveMemoryWarning and verifies the memory pressure | 75 // Invokes applicationDidReceiveMemoryWarning and verifies the memory pressure |
| 73 // callback (i.e. MainControllerTest::OnMemoryPressure) is invoked. | 76 // callback (i.e. MainControllerTest::OnMemoryPressure) is invoked. |
| 74 TEST_F(MemoryWarningHelperTest, VerifyApplicationDidReceiveMemoryWarning) { | 77 TEST_F(MemoryWarningHelperTest, VerifyApplicationDidReceiveMemoryWarning) { |
| 75 [GetMemoryHelper() handleMemoryPressure]; | 78 [GetMemoryHelper() handleMemoryPressure]; |
| 76 message_loop().Run(); | 79 RunMessageLoop(); |
| 77 EXPECT_EQ(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, | 80 EXPECT_EQ(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, |
| 78 GetMemoryPressureLevel()); | 81 GetMemoryPressureLevel()); |
| 79 } | 82 } |
| 80 | 83 |
| 81 // Invokes applicationDidReceiveMemoryWarning and verifies the flags (i.e. | 84 // Invokes applicationDidReceiveMemoryWarning and verifies the flags (i.e. |
| 82 // breakpad_helper and NSUserDefaults) are set. | 85 // breakpad_helper and NSUserDefaults) are set. |
| 83 TEST_F(MemoryWarningHelperTest, VerifyHelperDidSetMemoryWarningFlags) { | 86 TEST_F(MemoryWarningHelperTest, VerifyHelperDidSetMemoryWarningFlags) { |
| 84 // Setup. | 87 // Setup. |
| 85 [[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession]; | 88 [[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession]; |
| 86 [[PreviousSessionInfo sharedInstance] resetMemoryWarningFlag]; | 89 [[PreviousSessionInfo sharedInstance] resetMemoryWarningFlag]; |
| 87 int foregroundMemoryWarningCountBeforeWarning = | 90 int foregroundMemoryWarningCountBeforeWarning = |
| 88 GetMemoryHelper().foregroundMemoryWarningCount; | 91 GetMemoryHelper().foregroundMemoryWarningCount; |
| 89 | 92 |
| 90 BOOL memoryWarningFlagBeforeAlert = [[NSUserDefaults standardUserDefaults] | 93 BOOL memoryWarningFlagBeforeAlert = [[NSUserDefaults standardUserDefaults] |
| 91 boolForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]; | 94 boolForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]; |
| 92 | 95 |
| 93 // Action. | 96 // Action. |
| 94 [GetMemoryHelper() handleMemoryPressure]; | 97 [GetMemoryHelper() handleMemoryPressure]; |
| 95 | 98 |
| 96 // Tests. | 99 // Tests. |
| 97 EXPECT_TRUE([[NSUserDefaults standardUserDefaults] | 100 EXPECT_TRUE([[NSUserDefaults standardUserDefaults] |
| 98 boolForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]); | 101 boolForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]); |
| 99 EXPECT_FALSE(memoryWarningFlagBeforeAlert); | 102 EXPECT_FALSE(memoryWarningFlagBeforeAlert); |
| 100 EXPECT_EQ(foregroundMemoryWarningCountBeforeWarning + 1, | 103 EXPECT_EQ(foregroundMemoryWarningCountBeforeWarning + 1, |
| 101 GetMemoryHelper().foregroundMemoryWarningCount); | 104 GetMemoryHelper().foregroundMemoryWarningCount); |
| 102 } | 105 } |
| OLD | NEW |