1 // Copyright (c) 2012 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 "chrome/browser/memory/oom_priority_manager.h"
10 #include "base/logging.h"
11 #include "base/strings/string16.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/memory/tab_stats.h"
14 #include "chrome/common/url_constants.h"
15 #include "testing/gtest/include/gtest/gtest.h"
20 typedef testing::Test OomPriorityManagerTest
;
36 // Tests the sorting comparator so that we know it's producing the
38 TEST_F(OomPriorityManagerTest
, Comparator
) {
39 TabStatsList test_list
;
40 const base::TimeTicks now
= base::TimeTicks::Now();
42 // Add kSelected last to verify we are sorting the array.
46 stats
.is_pinned
= true;
47 stats
.child_process_host_id
= kPinned
;
48 test_list
.push_back(stats
);
54 stats
.child_process_host_id
= kApp
;
55 test_list
.push_back(stats
);
60 stats
.is_playing_audio
= true;
61 stats
.child_process_host_id
= kPlayingAudio
;
62 test_list
.push_back(stats
);
67 stats
.last_active
= now
- base::TimeDelta::FromSeconds(10);
68 stats
.child_process_host_id
= kRecent
;
69 test_list
.push_back(stats
);
74 stats
.last_active
= now
- base::TimeDelta::FromMinutes(15);
75 stats
.child_process_host_id
= kOld
;
76 test_list
.push_back(stats
);
81 stats
.last_active
= now
- base::TimeDelta::FromDays(365);
82 stats
.child_process_host_id
= kReallyOld
;
83 test_list
.push_back(stats
);
88 stats
.is_pinned
= true;
89 stats
.last_active
= now
- base::TimeDelta::FromDays(365);
90 stats
.child_process_host_id
= kOldButPinned
;
91 test_list
.push_back(stats
);
96 stats
.is_internal_page
= true;
97 stats
.child_process_host_id
= kInternalPage
;
98 test_list
.push_back(stats
);
101 // This entry sorts to the front, so by adding it last we verify that
102 // we are actually sorting the array.
105 stats
.is_selected
= true;
106 stats
.child_process_host_id
= kSelected
;
107 test_list
.push_back(stats
);
110 std::sort(test_list
.begin(), test_list
.end(),
111 OomPriorityManager::CompareTabStats
);
114 EXPECT_EQ(kSelected
, test_list
[index
++].child_process_host_id
);
115 EXPECT_EQ(kPlayingAudio
, test_list
[index
++].child_process_host_id
);
116 EXPECT_EQ(kPinned
, test_list
[index
++].child_process_host_id
);
117 EXPECT_EQ(kOldButPinned
, test_list
[index
++].child_process_host_id
);
118 EXPECT_EQ(kApp
, test_list
[index
++].child_process_host_id
);
119 EXPECT_EQ(kRecent
, test_list
[index
++].child_process_host_id
);
120 EXPECT_EQ(kOld
, test_list
[index
++].child_process_host_id
);
121 EXPECT_EQ(kReallyOld
, test_list
[index
++].child_process_host_id
);
122 EXPECT_EQ(kInternalPage
, test_list
[index
++].child_process_host_id
);
125 TEST_F(OomPriorityManagerTest
, IsInternalPage
) {
127 OomPriorityManager::IsInternalPage(GURL(chrome::kChromeUIDownloadsURL
)));
129 OomPriorityManager::IsInternalPage(GURL(chrome::kChromeUIHistoryURL
)));
131 OomPriorityManager::IsInternalPage(GURL(chrome::kChromeUINewTabURL
)));
133 OomPriorityManager::IsInternalPage(GURL(chrome::kChromeUISettingsURL
)));
135 // Debugging URLs are not included.
136 #if defined(OS_CHROMEOS)
138 OomPriorityManager::IsInternalPage(GURL(chrome::kChromeUIDiscardsURL
)));
140 EXPECT_FALSE(OomPriorityManager::IsInternalPage(
141 GURL(chrome::kChromeUINetInternalsURL
)));
143 // Prefix matches are included.
144 EXPECT_TRUE(OomPriorityManager::IsInternalPage(
145 GURL("chrome://settings/fakeSetting")));
148 } // namespace memory