OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 #include <gtest/gtest.h> | |
6 | |
7 #include "chrome/browser/extensions/extension_idle_api.h" | |
8 | |
9 TEST(ExtensionIdleApiTest, CacheTest) { | |
10 double throttle_interval = ExtensionIdleCache::get_throttle_interval(); | |
11 int min_threshold = ExtensionIdleCache::get_min_threshold(); | |
12 double now = 10 * min_threshold; | |
13 | |
14 EXPECT_EQ(IDLE_STATE_UNKNOWN, | |
15 ExtensionIdleCache::CalculateState(min_threshold, now)); | |
16 | |
17 ExtensionIdleCache::Update(2 * min_threshold, IDLE_STATE_IDLE, now); | |
18 | |
19 EXPECT_EQ(IDLE_STATE_IDLE, | |
20 ExtensionIdleCache::CalculateState(2 * min_threshold, now)); | |
21 EXPECT_EQ(IDLE_STATE_IDLE, | |
22 ExtensionIdleCache::CalculateState(2 * min_threshold, | |
23 now + 0.9 * throttle_interval)); | |
24 EXPECT_EQ(IDLE_STATE_IDLE, | |
25 ExtensionIdleCache::CalculateState(min_threshold, | |
26 now + 0.1 * throttle_interval)); | |
27 // Threshold exeeds idle interval boundries. | |
28 EXPECT_EQ(IDLE_STATE_UNKNOWN, | |
29 ExtensionIdleCache::CalculateState(2 * min_threshold + 1, | |
30 now + 0.1 * throttle_interval)); | |
31 // It has been more than throttle interval since last query. | |
32 EXPECT_EQ(IDLE_STATE_UNKNOWN, | |
33 ExtensionIdleCache::CalculateState(min_threshold, | |
34 now + 1.1 * throttle_interval)); | |
35 | |
36 now += 10 * min_threshold; | |
37 // Idle interval does not overlap with previous one. | |
38 ExtensionIdleCache::Update(5 * min_threshold, IDLE_STATE_IDLE, now); | |
39 EXPECT_EQ(IDLE_STATE_UNKNOWN, | |
40 ExtensionIdleCache::CalculateState(7 * min_threshold, now)); | |
41 | |
42 now += min_threshold; | |
43 // Idle interval overlaps with previous one. | |
44 ExtensionIdleCache::Update(2 * min_threshold, IDLE_STATE_IDLE, now); | |
45 // Threshold exeeds last idle interval boundaries, but does not exeed union of | |
46 // two last (overlaping) idle intervals. | |
47 EXPECT_EQ(IDLE_STATE_IDLE, | |
48 ExtensionIdleCache::CalculateState(4 * min_threshold, now)); | |
49 | |
50 now += 0.2 * throttle_interval; | |
51 ExtensionIdleCache::Update(8 * min_threshold, IDLE_STATE_ACTIVE, now); | |
52 EXPECT_EQ(IDLE_STATE_IDLE, | |
53 ExtensionIdleCache::CalculateState(4 * min_threshold, | |
54 now + 0.3 * throttle_interval)); | |
55 | |
56 // If both idle and active conditions are satisfied, return ACTIVE (because | |
57 // obviously ACTIVE was reported after last idle interval). | |
58 ExtensionIdleCache::Update(3 * min_threshold, IDLE_STATE_ACTIVE, now); | |
59 EXPECT_EQ(IDLE_STATE_ACTIVE, | |
60 ExtensionIdleCache::CalculateState(4 * min_threshold, | |
61 now + 0.3 * throttle_interval)); | |
62 | |
63 now += 10 * min_threshold; | |
64 ExtensionIdleCache::Update(8 * min_threshold, IDLE_STATE_ACTIVE, now); | |
65 // Threshold does not exeed last active state, but the error is within | |
66 // throttle interval. | |
67 EXPECT_EQ(IDLE_STATE_ACTIVE, | |
68 ExtensionIdleCache::CalculateState(8 * min_threshold, | |
69 now + 0.3 * throttle_interval)); | |
70 // The error is not within throttle interval. | |
71 EXPECT_EQ(IDLE_STATE_UNKNOWN, | |
72 ExtensionIdleCache::CalculateState(8 * min_threshold, | |
73 now + 1.1 * throttle_interval)); | |
74 | |
75 // We report LOCKED iff it was last reported state was LOCKED and it has | |
76 // been less than throttle_interval since last query. | |
77 now += 10 * min_threshold; | |
78 ExtensionIdleCache::Update(4 * min_threshold, IDLE_STATE_LOCKED, now); | |
79 EXPECT_EQ(IDLE_STATE_LOCKED, | |
80 ExtensionIdleCache::CalculateState(2 * min_threshold, | |
81 now + 0.3 * throttle_interval)); | |
82 // More than throttle_interval since last query. | |
83 EXPECT_EQ(IDLE_STATE_UNKNOWN, | |
84 ExtensionIdleCache::CalculateState(2 * min_threshold, | |
85 now + 1.1 * throttle_interval)); | |
86 | |
87 now += 0.2 * throttle_interval; | |
88 ExtensionIdleCache::Update(4 * min_threshold, IDLE_STATE_ACTIVE, now); | |
89 // Last reported query was ACTIVE. | |
90 EXPECT_EQ(IDLE_STATE_UNKNOWN, | |
91 ExtensionIdleCache::CalculateState(2 * min_threshold, | |
92 now + 0.3 * throttle_interval)); | |
93 | |
94 now += 0.2 * throttle_interval; | |
95 ExtensionIdleCache::Update(2 * min_threshold, IDLE_STATE_LOCKED, now); | |
96 EXPECT_EQ(IDLE_STATE_LOCKED, | |
97 ExtensionIdleCache::CalculateState(5 * min_threshold, | |
98 now + 0.3 * throttle_interval)); | |
99 | |
100 now += 10 * min_threshold; | |
101 ExtensionIdleCache::Update(4 * min_threshold, IDLE_STATE_LOCKED, now); | |
102 | |
103 now += 0.2 * throttle_interval; | |
104 ExtensionIdleCache::Update(2 * min_threshold, IDLE_STATE_IDLE, now); | |
105 | |
106 // Last reported state was IDLE. | |
107 EXPECT_EQ(IDLE_STATE_UNKNOWN, | |
108 ExtensionIdleCache::CalculateState(3 * min_threshold, | |
109 now + 0.3 * throttle_interval)); | |
110 | |
111 now += min_threshold; | |
112 ExtensionIdleCache::Update(2 * min_threshold, IDLE_STATE_LOCKED, now); | |
113 | |
114 now += 0.2 * throttle_interval; | |
115 ExtensionIdleCache::Update(4 * min_threshold, IDLE_STATE_ACTIVE, now); | |
116 | |
117 // Last reported state was ACTIVE. | |
118 EXPECT_EQ(IDLE_STATE_ACTIVE, | |
119 ExtensionIdleCache::CalculateState(6 * min_threshold, | |
120 now + 0.3 * throttle_interval)); | |
121 } | |
OLD | NEW |