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/memory/memory_wedge.h"
7 #include "base/macros.h"
8 #include "ios/chrome/browser/memory/memory_metrics.h"
9 #include "testing/gtest/include/gtest/gtest-param-test.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace memory_wedge
{
16 // The number of bytes in a megabyte.
17 const uint64 kNumBytesInMB
= 1024 * 1024;
19 // Note: in the following tests, only memory_util::GetInternalVMBytes,
20 // and memory_util::GetRealMemoryUsedInBytes are used to check the state of the
21 // memory before and after an action.
22 // memory_util::GetFreePhysicalBytes and memory_util::GetDirtyVMBytes are not
23 // used because external events can change these values, making them not
26 // Performs a snapshot of the memory when constructed. Deviation from the
27 // initial values can be verified with VerifyDeviation. The comparison is
28 // on the integer part of the values in MB.
32 internal_vm_
= memory_util::GetInternalVMBytes() / kNumBytesInMB
;
33 real_memory_used_
= memory_util::GetRealMemoryUsedInBytes() / kNumBytesInMB
;
36 // Verifies that the memory metrics deviated only by |deviation| in MB.
37 void VerifyDeviation(uint64 deviation
) const {
38 EXPECT_NEAR(memory_util::GetInternalVMBytes() / kNumBytesInMB
,
39 deviation
+ internal_vm_
, 1);
40 EXPECT_NEAR(memory_util::GetRealMemoryUsedInBytes() / kNumBytesInMB
,
41 deviation
+ real_memory_used_
, 1);
46 uint64 real_memory_used_
;
48 DISALLOW_COPY_AND_ASSIGN(MemoryChecker
);
51 class MemoryWedgeTest
: public testing::TestWithParam
<size_t> {};
55 // Checks that the wedge size passed to AddWedge is indeed added to the global
56 // footprint of the app.
57 TEST_P(MemoryWedgeTest
, WedgeSize
) {
58 const MemoryChecker memory_checker
;
59 size_t wedge_size
= GetParam();
63 memory_checker
.VerifyDeviation(wedge_size
);
65 RemoveWedgeForTesting();
68 INSTANTIATE_TEST_CASE_P(
69 /* No InstantiationName */,
71 testing::Values(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100));
73 } // namespace memory_wedge