| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include <algorithm> | 5 #include <algorithm> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/string16.h" | 9 #include "base/string16.h" |
| 10 #include "base/time.h" | 10 #include "base/time.h" |
| 11 #include "chrome/browser/chromeos/oom_priority_manager.h" | 11 #include "chrome/browser/chromeos/oom_priority_manager.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace chromeos { | 14 namespace chromeos { |
| 15 | 15 |
| 16 typedef testing::Test OomPriorityManagerTest; | 16 typedef testing::Test OomPriorityManagerTest; |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 enum TestIndicies { | 19 enum TestIndicies { |
| 20 kMostImportant, | 20 kSelected, |
| 21 kNotPinned, | 21 kPinned, |
| 22 kNotSelected, | 22 kApp, |
| 23 kSimilarTime, | 23 kRecent, |
| 24 kSimilarTimeOverThreshold, | 24 kOld, |
| 25 kReallyOld, | 25 kReallyOld, |
| 26 kOldButPinned | 26 kOldButPinned |
| 27 }; | 27 }; |
| 28 } // namespace | 28 } // namespace |
| 29 | 29 |
| 30 // Tests the sorting comparator so that we know it's producing the | 30 // Tests the sorting comparator so that we know it's producing the |
| 31 // desired order. | 31 // desired order. |
| 32 TEST_F(OomPriorityManagerTest, Comparator) { | 32 TEST_F(OomPriorityManagerTest, Comparator) { |
| 33 chromeos::OomPriorityManager::TabStatsList test_list; | 33 chromeos::OomPriorityManager::TabStatsList test_list; |
| 34 const base::TimeTicks now = base::TimeTicks::Now(); | 34 const base::TimeTicks now = base::TimeTicks::Now(); |
| 35 | 35 |
| 36 // Add kSelected last to verify we are sorting the array. |
| 37 |
| 36 { | 38 { |
| 37 OomPriorityManager::TabStats stats; | 39 OomPriorityManager::TabStats stats; |
| 38 stats.is_selected = true; | |
| 39 stats.is_pinned = true; | 40 stats.is_pinned = true; |
| 40 stats.last_selected = now; | 41 stats.renderer_handle = kPinned; |
| 41 stats.renderer_handle = kMostImportant; | |
| 42 test_list.push_back(stats); | 42 test_list.push_back(stats); |
| 43 } | 43 } |
| 44 | 44 |
| 45 { | 45 { |
| 46 OomPriorityManager::TabStats stats; | 46 OomPriorityManager::TabStats stats; |
| 47 stats.is_selected = true; | 47 stats.is_app = true; |
| 48 stats.is_pinned = false; | 48 stats.renderer_handle = kApp; |
| 49 stats.last_selected = now; | |
| 50 stats.renderer_handle = kNotPinned; | |
| 51 test_list.push_back(stats); | 49 test_list.push_back(stats); |
| 52 } | 50 } |
| 53 | 51 |
| 54 { | 52 { |
| 55 OomPriorityManager::TabStats stats; | 53 OomPriorityManager::TabStats stats; |
| 56 stats.is_selected = false; | 54 stats.last_selected = now - base::TimeDelta::FromSeconds(10); |
| 57 stats.is_pinned = false; | 55 stats.renderer_handle = kRecent; |
| 58 stats.last_selected = now; | |
| 59 stats.renderer_handle = kNotSelected; | |
| 60 test_list.push_back(stats); | 56 test_list.push_back(stats); |
| 61 } | 57 } |
| 62 | 58 |
| 63 { | 59 { |
| 64 OomPriorityManager::TabStats stats; | 60 OomPriorityManager::TabStats stats; |
| 65 stats.is_selected = false; | 61 stats.last_selected = now - base::TimeDelta::FromMinutes(15); |
| 66 stats.is_pinned = false; | 62 stats.renderer_handle = kOld; |
| 67 stats.last_selected = now - base::TimeDelta::FromSeconds(10); | |
| 68 stats.renderer_handle = kSimilarTime; | |
| 69 test_list.push_back(stats); | 63 test_list.push_back(stats); |
| 70 } | 64 } |
| 71 | 65 |
| 72 { | 66 { |
| 73 OomPriorityManager::TabStats stats; | 67 OomPriorityManager::TabStats stats; |
| 74 stats.is_selected = false; | 68 stats.last_selected = now - base::TimeDelta::FromDays(365); |
| 75 stats.is_pinned = false; | 69 stats.renderer_handle = kReallyOld; |
| 76 stats.last_selected = now - base::TimeDelta::FromMinutes(15); | |
| 77 stats.renderer_handle = kSimilarTimeOverThreshold; | |
| 78 test_list.push_back(stats); | 70 test_list.push_back(stats); |
| 79 } | 71 } |
| 80 | 72 |
| 81 { | 73 { |
| 82 OomPriorityManager::TabStats stats; | 74 OomPriorityManager::TabStats stats; |
| 83 stats.is_selected = false; | 75 stats.is_pinned = true; |
| 84 stats.is_pinned = false; | |
| 85 stats.last_selected = now - base::TimeDelta::FromDays(365); | 76 stats.last_selected = now - base::TimeDelta::FromDays(365); |
| 86 stats.renderer_handle = kReallyOld; | 77 stats.renderer_handle = kOldButPinned; |
| 87 test_list.push_back(stats); | 78 test_list.push_back(stats); |
| 88 } | 79 } |
| 89 | 80 |
| 90 // This also is out of order, so verifies that we are actually | 81 // This entry sorts to the front, so by adding it last we verify that |
| 91 // sorting the array. | 82 // we are actually sorting the array. |
| 92 { | 83 { |
| 93 OomPriorityManager::TabStats stats; | 84 OomPriorityManager::TabStats stats; |
| 94 stats.is_selected = false; | 85 stats.is_selected = true; |
| 95 stats.is_pinned = true; | 86 stats.renderer_handle = kSelected; |
| 96 stats.last_selected = now - base::TimeDelta::FromDays(365); | |
| 97 stats.renderer_handle = kOldButPinned; | |
| 98 test_list.push_back(stats); | 87 test_list.push_back(stats); |
| 99 } | 88 } |
| 100 | 89 |
| 101 std::sort(test_list.begin(), | 90 std::sort(test_list.begin(), |
| 102 test_list.end(), | 91 test_list.end(), |
| 103 OomPriorityManager::CompareTabStats); | 92 OomPriorityManager::CompareTabStats); |
| 104 | 93 |
| 105 EXPECT_EQ(kMostImportant, test_list[0].renderer_handle); | 94 EXPECT_EQ(kSelected, test_list[0].renderer_handle); |
| 106 EXPECT_EQ(kNotPinned, test_list[1].renderer_handle); | 95 EXPECT_EQ(kPinned, test_list[1].renderer_handle); |
| 107 EXPECT_EQ(kOldButPinned, test_list[2].renderer_handle); | 96 EXPECT_EQ(kOldButPinned, test_list[2].renderer_handle); |
| 108 // The order of kNotSelected and kSimilarTime is indeterminate: | 97 EXPECT_EQ(kApp, test_list[3].renderer_handle); |
| 109 // they are equal in the eyes of the sort. | 98 EXPECT_EQ(kRecent, test_list[4].renderer_handle); |
| 110 EXPECT_TRUE((test_list[3].renderer_handle == kNotSelected && | 99 EXPECT_EQ(kOld, test_list[5].renderer_handle); |
| 111 test_list[4].renderer_handle == kSimilarTime) || | |
| 112 (test_list[3].renderer_handle == kSimilarTime && | |
| 113 test_list[4].renderer_handle == kNotSelected)); | |
| 114 EXPECT_EQ(kSimilarTimeOverThreshold, test_list[5].renderer_handle); | |
| 115 EXPECT_EQ(kReallyOld, test_list[6].renderer_handle); | 100 EXPECT_EQ(kReallyOld, test_list[6].renderer_handle); |
| 116 } | 101 } |
| 117 | 102 |
| 118 } // namespace chromeos | 103 } // namespace chromeos |
| OLD | NEW |