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 #import "ios/web/crw_network_activity_indicator_manager.h"
7 #import <UIKit/UIKit.h>
9 #include "base/mac/scoped_nsobject.h"
10 #include "testing/gtest_mac.h"
11 #include "testing/platform_test.h"
15 NSString* const kNetworkActivityKeyOne =
16 @"CRWNetworkActivityIndicatorManagerTest.NetworkActivityIndicatorKeyOne";
17 NSString* const kNetworkActivityKeyTwo =
18 @"CRWNetworkActivityIndicatorManagerTest.NetworkActivityIndicatorKeyTwo";
20 class CRWNetworkActivityIndicatorManagerTest : public PlatformTest {
22 CRWNetworkActivityIndicatorManagerTest()
23 : manager_([[CRWNetworkActivityIndicatorManager alloc] init]) {}
26 void ExpectNetworkActivity(NSUInteger groupOneCount,
27 NSUInteger groupTwoCount) {
28 EXPECT_EQ([manager_ numNetworkTasksForGroup:kNetworkActivityKeyOne],
30 EXPECT_EQ([manager_ numNetworkTasksForGroup:kNetworkActivityKeyTwo],
32 EXPECT_EQ([manager_ numTotalNetworkTasks], groupOneCount + groupTwoCount);
33 if (groupOneCount + groupTwoCount > 0U) {
35 [[UIApplication sharedApplication]
36 isNetworkActivityIndicatorVisible]);
39 [[UIApplication sharedApplication]
40 isNetworkActivityIndicatorVisible]);
43 base::scoped_nsobject<CRWNetworkActivityIndicatorManager> manager_;
46 TEST_F(CRWNetworkActivityIndicatorManagerTest, TestNumNetworkTasksForGroup) {
47 EXPECT_EQ([manager_ numNetworkTasksForGroup:kNetworkActivityKeyOne], 0U);
48 EXPECT_EQ([manager_ numNetworkTasksForGroup:kNetworkActivityKeyTwo], 0U);
49 [manager_ startNetworkTasks:2U forGroup:kNetworkActivityKeyOne];
50 EXPECT_EQ([manager_ numNetworkTasksForGroup:kNetworkActivityKeyOne], 2U);
51 EXPECT_EQ([manager_ numNetworkTasksForGroup:kNetworkActivityKeyTwo], 0U);
52 [manager_ startNetworkTasks:3U forGroup:kNetworkActivityKeyTwo];
53 EXPECT_EQ([manager_ numNetworkTasksForGroup:kNetworkActivityKeyOne], 2U);
54 EXPECT_EQ([manager_ numNetworkTasksForGroup:kNetworkActivityKeyTwo], 3U);
55 [manager_ stopNetworkTasks:2U forGroup:kNetworkActivityKeyOne];
56 EXPECT_EQ([manager_ numNetworkTasksForGroup:kNetworkActivityKeyOne], 0U);
57 EXPECT_EQ([manager_ numNetworkTasksForGroup:kNetworkActivityKeyTwo], 3U);
58 [manager_ stopNetworkTasks:3U forGroup:kNetworkActivityKeyTwo];
59 EXPECT_EQ([manager_ numNetworkTasksForGroup:kNetworkActivityKeyOne], 0U);
60 EXPECT_EQ([manager_ numNetworkTasksForGroup:kNetworkActivityKeyTwo], 0U);
63 TEST_F(CRWNetworkActivityIndicatorManagerTest, TestNumTotalNetworkTasks) {
64 EXPECT_EQ([manager_ numTotalNetworkTasks], 0U);
65 [manager_ startNetworkTasks:2U forGroup:kNetworkActivityKeyOne];
66 EXPECT_EQ([manager_ numTotalNetworkTasks], 2U);
67 [manager_ startNetworkTasks:3U forGroup:kNetworkActivityKeyTwo];
68 EXPECT_EQ([manager_ numTotalNetworkTasks], 5U);
69 [manager_ stopNetworkTasks:2U forGroup:kNetworkActivityKeyOne];
70 EXPECT_EQ([manager_ numTotalNetworkTasks], 3U);
71 [manager_ stopNetworkTasks:3U forGroup:kNetworkActivityKeyTwo];
72 EXPECT_EQ([manager_ numTotalNetworkTasks], 0U);
75 TEST_F(CRWNetworkActivityIndicatorManagerTest,
76 StartStopNetworkTaskForOneGroup) {
77 ExpectNetworkActivity(0U, 0U);
78 [manager_ startNetworkTaskForGroup:kNetworkActivityKeyOne];
79 ExpectNetworkActivity(1U, 0U);
80 [manager_ stopNetworkTaskForGroup:kNetworkActivityKeyOne];
81 ExpectNetworkActivity(0U, 0U);
84 TEST_F(CRWNetworkActivityIndicatorManagerTest,
85 StartStopNetworkTaskForTwoGroups) {
86 ExpectNetworkActivity(0U, 0U);
87 [manager_ startNetworkTaskForGroup:kNetworkActivityKeyOne];
88 ExpectNetworkActivity(1U, 0U);
89 [manager_ startNetworkTaskForGroup:kNetworkActivityKeyTwo];
90 ExpectNetworkActivity(1U, 1U);
91 [manager_ stopNetworkTaskForGroup:kNetworkActivityKeyOne];
92 ExpectNetworkActivity(0U, 1U);
93 [manager_ stopNetworkTaskForGroup:kNetworkActivityKeyTwo];
94 ExpectNetworkActivity(0U, 0U);
97 TEST_F(CRWNetworkActivityIndicatorManagerTest,
98 StartStopNetworkTasksForOneGroup) {
99 ExpectNetworkActivity(0U, 0U);
100 [manager_ startNetworkTasks:2U forGroup:kNetworkActivityKeyOne];
101 ExpectNetworkActivity(2U, 0U);
102 [manager_ stopNetworkTasks:2U forGroup:kNetworkActivityKeyOne];
103 ExpectNetworkActivity(0U, 0U);
106 TEST_F(CRWNetworkActivityIndicatorManagerTest,
107 StartStopNetworkTasksForTwoGroups) {
108 ExpectNetworkActivity(0U, 0U);
109 [manager_ startNetworkTasks:2U forGroup:kNetworkActivityKeyOne];
110 ExpectNetworkActivity(2U, 0U);
111 [manager_ startNetworkTasks:3U forGroup:kNetworkActivityKeyTwo];
112 ExpectNetworkActivity(2U, 3U);
113 [manager_ stopNetworkTasks:2U forGroup:kNetworkActivityKeyOne];
114 ExpectNetworkActivity(0U, 3U);
115 [manager_ stopNetworkTasks:3U forGroup:kNetworkActivityKeyTwo];
116 ExpectNetworkActivity(0U, 0U);
119 TEST_F(CRWNetworkActivityIndicatorManagerTest, ClearNetworkTasksForGroup) {
120 ExpectNetworkActivity(0U, 0U);
121 [manager_ startNetworkTasks:2U forGroup:kNetworkActivityKeyOne];
122 ExpectNetworkActivity(2U, 0U);
123 [manager_ clearNetworkTasksForGroup:kNetworkActivityKeyOne];
124 ExpectNetworkActivity(0U, 0U);
127 TEST_F(CRWNetworkActivityIndicatorManagerTest,
128 ClearNetworkTasksForUnusedGroup) {
129 ExpectNetworkActivity(0U, 0U);
130 [manager_ clearNetworkTasksForGroup:kNetworkActivityKeyOne];
131 ExpectNetworkActivity(0U, 0U);
134 TEST_F(CRWNetworkActivityIndicatorManagerTest, StartStopNetworkTasksInChunks) {
135 ExpectNetworkActivity(0U, 0U);
136 [manager_ startNetworkTasks:2U forGroup:kNetworkActivityKeyOne];
137 ExpectNetworkActivity(2U, 0U);
138 [manager_ startNetworkTasks:3U forGroup:kNetworkActivityKeyTwo];
139 ExpectNetworkActivity(2U, 3U);
140 [manager_ startNetworkTasks:7U forGroup:kNetworkActivityKeyOne];
141 ExpectNetworkActivity(9U, 3U);
142 [manager_ startNetworkTaskForGroup:kNetworkActivityKeyTwo];
143 ExpectNetworkActivity(9U, 4U);
144 [manager_ stopNetworkTasks:4U forGroup:kNetworkActivityKeyOne];
145 ExpectNetworkActivity(5U, 4U);
146 [manager_ stopNetworkTasks:2U forGroup:kNetworkActivityKeyTwo];
147 ExpectNetworkActivity(5U, 2U);
148 [manager_ stopNetworkTasks:5U forGroup:kNetworkActivityKeyOne];
149 ExpectNetworkActivity(0U, 2U);
150 [manager_ clearNetworkTasksForGroup:kNetworkActivityKeyTwo];
151 ExpectNetworkActivity(0U, 0U);