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.
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"
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;
17 using android_webview::GlobalTileManager
;
18 using android_webview::GlobalTileManagerClient
;
21 class MockGlobalTileManagerClient
: public GlobalTileManagerClient
{
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() {
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_
; }
45 GlobalTileManager::Key key_
;
48 class GlobalTileManagerTest
: public Test
{
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
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
83 EXPECT_LE(total_tiles
, kNumTilesLimit
);
86 TEST_F(GlobalTileManagerTest
, RandomizedStressRequests
) {
87 MockGlobalTileManagerClient clients
[100];
89 for (size_t i
= 0; i
< 100; 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
++) {
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
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);