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 "ios/chrome/app/application_delegate/memory_warning_helper.h" |
| 6 |
| 7 #include "base/mac/bind_objc_block.h" |
| 8 #import "base/mac/scoped_nsobject.h" |
| 9 #include "base/memory/memory_pressure_listener.h" |
| 10 #include "base/threading/thread.h" |
| 11 #import "ios/chrome/browser/metrics/previous_session_info.h" |
| 12 #include "testing/platform_test.h" |
| 13 |
| 14 using previous_session_info_constants:: |
| 15 kDidSeeMemoryWarningShortlyBeforeTerminating; |
| 16 |
| 17 class MemoryWarningHelperTest : public PlatformTest { |
| 18 protected: |
| 19 MemoryWarningHelperTest() { |
| 20 // Set up |memory_pressure_listener_| to invoke |OnMemoryPressure| which |
| 21 // will store the memory pressure level sent to the callback in |
| 22 // |memory_pressure_level_| so that tests can verify the level is correct. |
| 23 memory_pressure_listener_.reset(new base::MemoryPressureListener(base::Bind( |
| 24 &MemoryWarningHelperTest::OnMemoryPressure, base::Unretained(this)))); |
| 25 memory_pressure_level_ = |
| 26 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; |
| 27 } |
| 28 |
| 29 base::MemoryPressureListener::MemoryPressureLevel GetMemoryPressureLevel() { |
| 30 return memory_pressure_level_; |
| 31 } |
| 32 |
| 33 MemoryWarningHelper* GetMemoryHelper() { |
| 34 if (!memory_helper_) { |
| 35 memory_helper_.reset([[MemoryWarningHelper alloc] init]); |
| 36 } |
| 37 return memory_helper_; |
| 38 } |
| 39 |
| 40 // Callback for |memory_pressure_listener_|. |
| 41 void OnMemoryPressure( |
| 42 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { |
| 43 memory_pressure_level_ = memory_pressure_level; |
| 44 message_loop_.QuitWhenIdle(); |
| 45 } |
| 46 |
| 47 base::MessageLoop& message_loop() { return message_loop_; } |
| 48 |
| 49 private: |
| 50 base::MessageLoop message_loop_; |
| 51 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level_; |
| 52 std::unique_ptr<base::MemoryPressureListener> memory_pressure_listener_; |
| 53 base::scoped_nsobject<MemoryWarningHelper> memory_helper_; |
| 54 |
| 55 DISALLOW_COPY_AND_ASSIGN(MemoryWarningHelperTest); |
| 56 }; |
| 57 |
| 58 // Invokes resetForegroundMemoryWarningCount and verifies the |
| 59 // foregroundMemoryWarningCount is setted to 0. |
| 60 TEST_F(MemoryWarningHelperTest, VerifyForegroundMemoryWarningCountReset) { |
| 61 // Setup. |
| 62 [GetMemoryHelper() handleMemoryPressure]; |
| 63 ASSERT_TRUE(GetMemoryHelper().foregroundMemoryWarningCount != 0); |
| 64 |
| 65 // Action. |
| 66 [GetMemoryHelper() resetForegroundMemoryWarningCount]; |
| 67 |
| 68 // Test. |
| 69 EXPECT_EQ(0, GetMemoryHelper().foregroundMemoryWarningCount); |
| 70 } |
| 71 |
| 72 // Invokes applicationDidReceiveMemoryWarning and verifies the memory pressure |
| 73 // callback (i.e. MainControllerTest::OnMemoryPressure) is invoked. |
| 74 TEST_F(MemoryWarningHelperTest, VerifyApplicationDidReceiveMemoryWarning) { |
| 75 [GetMemoryHelper() handleMemoryPressure]; |
| 76 message_loop().Run(); |
| 77 EXPECT_EQ(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL, |
| 78 GetMemoryPressureLevel()); |
| 79 } |
| 80 |
| 81 // Invokes applicationDidReceiveMemoryWarning and verifies the flags (i.e. |
| 82 // breakpad_helper and NSUserDefaults) are set. |
| 83 TEST_F(MemoryWarningHelperTest, VerifyHelperDidSetMemoryWarningFlags) { |
| 84 // Setup. |
| 85 [[PreviousSessionInfo sharedInstance] beginRecordingCurrentSession]; |
| 86 [[PreviousSessionInfo sharedInstance] resetMemoryWarningFlag]; |
| 87 int foregroundMemoryWarningCountBeforeWarning = |
| 88 GetMemoryHelper().foregroundMemoryWarningCount; |
| 89 |
| 90 BOOL memoryWarningFlagBeforeAlert = [[NSUserDefaults standardUserDefaults] |
| 91 boolForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]; |
| 92 |
| 93 // Action. |
| 94 [GetMemoryHelper() handleMemoryPressure]; |
| 95 |
| 96 // Tests. |
| 97 EXPECT_TRUE([[NSUserDefaults standardUserDefaults] |
| 98 boolForKey:kDidSeeMemoryWarningShortlyBeforeTerminating]); |
| 99 EXPECT_FALSE(memoryWarningFlagBeforeAlert); |
| 100 EXPECT_EQ(foregroundMemoryWarningCountBeforeWarning + 1, |
| 101 GetMemoryHelper().foregroundMemoryWarningCount); |
| 102 } |
OLD | NEW |