Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / android_webview / browser / global_tile_manager_unittest.cc
blob9f090842f033b08992b80bcc5e44f5ece59fb618
1 // Copyright 2014 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 <algorithm>
6 #include "android_webview/browser/global_tile_manager.h"
7 #include "android_webview/browser/global_tile_manager_client.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 namespace {
11 // This should be the same as the one defined global_tile_manager.cc
12 const size_t kNumTilesLimit = 450;
13 const size_t kDefaultNumTiles = 150;
15 } // namespace
17 using android_webview::GlobalTileManager;
18 using android_webview::GlobalTileManagerClient;
19 using testing::Test;
21 class MockGlobalTileManagerClient : public GlobalTileManagerClient {
22 public:
23 virtual size_t GetNumTiles() const OVERRIDE { return num_tiles_; }
24 virtual void SetNumTiles(size_t num_tiles,
25 bool effective_immediately) OVERRIDE {
26 num_tiles_ = num_tiles;
29 MockGlobalTileManagerClient() {
30 num_tiles_ = 0;
31 tile_request_ = kDefaultNumTiles;
32 key_ = GlobalTileManager::GetInstance()->PushBack(this);
35 virtual ~MockGlobalTileManagerClient() {
36 GlobalTileManager::GetInstance()->Remove(key_);
39 size_t GetTileRequest() { return tile_request_; }
40 GlobalTileManager::Key GetKey() { return key_; }
42 private:
43 size_t num_tiles_;
44 size_t tile_request_;
45 GlobalTileManager::Key key_;
48 class GlobalTileManagerTest : public Test {
49 public:
50 virtual void SetUp() {}
52 GlobalTileManager* manager() { return GlobalTileManager::GetInstance(); }
55 TEST_F(GlobalTileManagerTest, RequestTilesUnderLimit) {
56 MockGlobalTileManagerClient clients[2];
58 for (size_t i = 0; i < 2; i++) {
59 manager()->RequestTiles(clients[i].GetTileRequest(), clients[i].GetKey());
60 manager()->DidUse(clients[i].GetKey());
62 // Ensure clients get what they asked for when the manager is under tile
63 // limit.
64 EXPECT_EQ(clients[i].GetNumTiles(), kDefaultNumTiles);
68 TEST_F(GlobalTileManagerTest, EvictHappensWhenOverLimit) {
69 MockGlobalTileManagerClient clients[4];
71 for (size_t i = 0; i < 4; i++) {
72 manager()->RequestTiles(clients[i].GetTileRequest(), clients[i].GetKey());
73 manager()->DidUse(clients[i].GetKey());
76 size_t total_tiles = 0;
77 for (size_t i = 0; i < 4; i++) {
78 total_tiles += clients[i].GetNumTiles();
81 // Ensure that eviction happened and kept the total number of tiles within
82 // kNumTilesLimit.
83 EXPECT_LE(total_tiles, kNumTilesLimit);
86 TEST_F(GlobalTileManagerTest, RandomizedStressRequests) {
87 MockGlobalTileManagerClient clients[100];
88 size_t index[100];
89 for (size_t i = 0; i < 100; i++) {
90 index[i] = i;
93 // Simulate a random request order of clients.
94 std::random_shuffle(&index[0], &index[99]);
96 for (size_t i = 0; i < 100; i++) {
97 size_t j = index[i];
98 manager()->RequestTiles(clients[j].GetTileRequest(), clients[j].GetKey());
99 manager()->DidUse(clients[j].GetKey());
102 size_t total_tiles = 0;
103 for (size_t i = 0; i < 100; i++) {
104 total_tiles += clients[i].GetNumTiles();
107 // Ensure that eviction happened and kept the total number of tiles within
108 // kNumTilesLimit.
109 EXPECT_LE(total_tiles, kNumTilesLimit);
112 TEST_F(GlobalTileManagerTest, FixedOrderedRequests) {
113 MockGlobalTileManagerClient clients[10];
115 // 10 clients requesting resources in a fixed order. Do that for 5 rounds.
116 for (int round = 0; round < 5; round++) {
117 for (size_t i = 0; i < 10; i++) {
118 manager()->RequestTiles(clients[i].GetTileRequest(), clients[i].GetKey());
119 manager()->DidUse(clients[i].GetKey());
123 // Ensure that the total tiles are divided evenly among all clients.
124 for (size_t i = 0; i < 10; i++) {
125 EXPECT_EQ(clients[i].GetNumTiles(), kNumTilesLimit / 10);
129 TEST_F(GlobalTileManagerTest, FixedOrderedRequestsWithInactiveClients) {
130 MockGlobalTileManagerClient clients[20];
132 // 20 clients request resources.
133 for (size_t i = 0; i < 20; i++) {
134 manager()->RequestTiles(clients[i].GetTileRequest(), clients[i].GetKey());
135 manager()->DidUse(clients[i].GetKey());
138 // Now the last 10 clients become inactive. Only the first 10 clients remain
139 // active resource requesters.
140 // 10 clients requesting resources in a fixed order. Do that for 5 rounds.
141 for (int round = 0; round < 5; round++) {
142 for (size_t i = 0; i < 10; i++) {
143 manager()->RequestTiles(clients[i].GetTileRequest(), clients[i].GetKey());
144 manager()->DidUse(clients[i].GetKey());
148 // Ensure that the total tiles are divided evenly among all clients.
149 for (size_t i = 0; i < 10; i++) {
150 EXPECT_EQ(clients[i].GetNumTiles(), kNumTilesLimit / 10);
153 // Ensure that the inactive tiles are evicted.
154 for (size_t i = 11; i < 20; i++) {
155 EXPECT_EQ(clients[i].GetNumTiles(), 0u);