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