OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/http/http_cache.h" | 5 #include "net/http/http_cache.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
55 net::CompletionCallback callback_; | 55 net::CompletionCallback callback_; |
56 | 56 |
57 DISALLOW_COPY_AND_ASSIGN(DeleteCacheCompletionCallback); | 57 DISALLOW_COPY_AND_ASSIGN(DeleteCacheCompletionCallback); |
58 }; | 58 }; |
59 | 59 |
60 //----------------------------------------------------------------------------- | 60 //----------------------------------------------------------------------------- |
61 // helpers | 61 // helpers |
62 | 62 |
63 class TestHttpTransactionDelegate : public net::HttpTransactionDelegate { | 63 class TestHttpTransactionDelegate : public net::HttpTransactionDelegate { |
64 public: | 64 public: |
65 explicit TestHttpTransactionDelegate(int num_actions_to_observe) | 65 explicit TestHttpTransactionDelegate(int num_cache_actions_to_observe, |
rvargas (doing something else)
2012/08/16 01:18:40
not explicit
tburkard
2012/08/16 22:37:43
Done.
| |
66 : num_remaining_actions_to_observe_(num_actions_to_observe), | 66 int num_network_actions_to_observe) |
67 action_in_progress_(false) { | 67 : num_remaining_cache_actions_to_observe_(num_cache_actions_to_observe), |
68 num_remaining_network_actions_to_observe_( | |
69 num_network_actions_to_observe), | |
70 cache_action_in_progress_(false), | |
71 network_action_in_progress_(false) { | |
68 } | 72 } |
69 virtual ~TestHttpTransactionDelegate() { | 73 virtual ~TestHttpTransactionDelegate() { |
70 EXPECT_EQ(0, num_remaining_actions_to_observe_); | 74 EXPECT_EQ(0, num_remaining_cache_actions_to_observe_); |
71 EXPECT_FALSE(action_in_progress_); | 75 EXPECT_EQ(0, num_remaining_network_actions_to_observe_); |
76 EXPECT_FALSE(cache_action_in_progress_); | |
77 EXPECT_FALSE(network_action_in_progress_); | |
72 } | 78 } |
73 virtual void OnCacheActionStart() { | 79 virtual void OnCacheActionStart() { |
74 EXPECT_FALSE(action_in_progress_); | 80 EXPECT_FALSE(cache_action_in_progress_); |
75 EXPECT_GT(num_remaining_actions_to_observe_, 0); | 81 EXPECT_FALSE(network_action_in_progress_); |
76 num_remaining_actions_to_observe_--; | 82 EXPECT_GT(num_remaining_cache_actions_to_observe_, 0); |
77 action_in_progress_ = true; | 83 num_remaining_cache_actions_to_observe_--; |
84 cache_action_in_progress_ = true; | |
78 } | 85 } |
79 virtual void OnCacheActionFinish() { | 86 virtual void OnCacheActionFinish() { |
80 EXPECT_TRUE(action_in_progress_); | 87 EXPECT_TRUE(cache_action_in_progress_); |
81 action_in_progress_ = false; | 88 cache_action_in_progress_ = false; |
89 } | |
90 virtual void OnNetworkActionStart() { | |
91 EXPECT_FALSE(cache_action_in_progress_); | |
92 EXPECT_FALSE(network_action_in_progress_); | |
93 EXPECT_GT(num_remaining_network_actions_to_observe_, 0); | |
94 num_remaining_network_actions_to_observe_--; | |
95 network_action_in_progress_ = true; | |
96 } | |
97 virtual void OnNetworkActionFinish() { | |
98 EXPECT_TRUE(network_action_in_progress_); | |
99 network_action_in_progress_ = false; | |
82 } | 100 } |
83 | 101 |
84 private: | 102 private: |
85 int num_remaining_actions_to_observe_; | 103 int num_remaining_cache_actions_to_observe_; |
86 bool action_in_progress_; | 104 int num_remaining_network_actions_to_observe_; |
105 bool cache_action_in_progress_; | |
106 bool network_action_in_progress_; | |
87 }; | 107 }; |
88 | 108 |
89 void ReadAndVerifyTransaction(net::HttpTransaction* trans, | 109 void ReadAndVerifyTransaction(net::HttpTransaction* trans, |
90 const MockTransaction& trans_info) { | 110 const MockTransaction& trans_info) { |
91 std::string content; | 111 std::string content; |
92 int rv = ReadTransaction(trans, &content); | 112 int rv = ReadTransaction(trans, &content); |
93 | 113 |
94 EXPECT_EQ(net::OK, rv); | 114 EXPECT_EQ(net::OK, rv); |
95 std::string expected(trans_info.data); | 115 std::string expected(trans_info.data); |
96 EXPECT_EQ(expected, content); | 116 EXPECT_EQ(expected, content); |
97 } | 117 } |
98 | 118 |
99 const int kNoDelegateTransactionCheck = -1; | 119 const int kNoDelegateTransactionCheck = -1; |
100 | 120 |
101 void RunTransactionTestWithRequestAndLogAndDelegate( | 121 void RunTransactionTestWithRequestAndLogAndDelegate( |
102 net::HttpCache* cache, | 122 net::HttpCache* cache, |
103 const MockTransaction& trans_info, | 123 const MockTransaction& trans_info, |
104 const MockHttpRequest& request, | 124 const MockHttpRequest& request, |
105 net::HttpResponseInfo* response_info, | 125 net::HttpResponseInfo* response_info, |
106 const net::BoundNetLog& net_log, | 126 const net::BoundNetLog& net_log, |
107 int num_delegate_actions) { | 127 int num_cache_delegate_actions, |
128 int num_network_delegate_actions) { | |
108 net::TestCompletionCallback callback; | 129 net::TestCompletionCallback callback; |
109 | 130 |
110 // write to the cache | 131 // write to the cache |
111 | 132 |
112 scoped_ptr<TestHttpTransactionDelegate> delegate; | 133 scoped_ptr<TestHttpTransactionDelegate> delegate; |
113 if (num_delegate_actions != kNoDelegateTransactionCheck) { | 134 if (num_cache_delegate_actions != kNoDelegateTransactionCheck && |
114 delegate.reset(new TestHttpTransactionDelegate(num_delegate_actions)); | 135 num_network_delegate_actions != kNoDelegateTransactionCheck) { |
136 delegate.reset( | |
137 new TestHttpTransactionDelegate(num_cache_delegate_actions, | |
138 num_network_delegate_actions)); | |
115 } | 139 } |
116 scoped_ptr<net::HttpTransaction> trans; | 140 scoped_ptr<net::HttpTransaction> trans; |
117 int rv = cache->CreateTransaction(&trans, delegate.get()); | 141 int rv = cache->CreateTransaction(&trans, delegate.get()); |
118 EXPECT_EQ(net::OK, rv); | 142 EXPECT_EQ(net::OK, rv); |
119 ASSERT_TRUE(trans.get()); | 143 ASSERT_TRUE(trans.get()); |
120 | 144 |
121 rv = trans->Start(&request, callback.callback(), net_log); | 145 rv = trans->Start(&request, callback.callback(), net_log); |
122 if (rv == net::ERR_IO_PENDING) | 146 if (rv == net::ERR_IO_PENDING) |
123 rv = callback.WaitForResult(); | 147 rv = callback.WaitForResult(); |
124 ASSERT_EQ(net::OK, rv); | 148 ASSERT_EQ(net::OK, rv); |
125 | 149 |
126 const net::HttpResponseInfo* response = trans->GetResponseInfo(); | 150 const net::HttpResponseInfo* response = trans->GetResponseInfo(); |
127 ASSERT_TRUE(response); | 151 ASSERT_TRUE(response); |
128 | 152 |
129 if (response_info) | 153 if (response_info) |
130 *response_info = *response; | 154 *response_info = *response; |
131 | 155 |
132 ReadAndVerifyTransaction(trans.get(), trans_info); | 156 ReadAndVerifyTransaction(trans.get(), trans_info); |
133 } | 157 } |
134 | 158 |
135 void RunTransactionTestWithRequest(net::HttpCache* cache, | 159 void RunTransactionTestWithRequest(net::HttpCache* cache, |
136 const MockTransaction& trans_info, | 160 const MockTransaction& trans_info, |
137 const MockHttpRequest& request, | 161 const MockHttpRequest& request, |
138 net::HttpResponseInfo* response_info) { | 162 net::HttpResponseInfo* response_info) { |
139 RunTransactionTestWithRequestAndLogAndDelegate( | 163 RunTransactionTestWithRequestAndLogAndDelegate( |
140 cache, trans_info, request, response_info, net::BoundNetLog(), | 164 cache, trans_info, request, response_info, net::BoundNetLog(), |
141 kNoDelegateTransactionCheck); | 165 kNoDelegateTransactionCheck, kNoDelegateTransactionCheck); |
142 } | 166 } |
143 | 167 |
144 void RunTransactionTestWithLog(net::HttpCache* cache, | 168 void RunTransactionTestWithLog(net::HttpCache* cache, |
145 const MockTransaction& trans_info, | 169 const MockTransaction& trans_info, |
146 const net::BoundNetLog& log) { | 170 const net::BoundNetLog& log) { |
147 RunTransactionTestWithRequestAndLogAndDelegate( | 171 RunTransactionTestWithRequestAndLogAndDelegate( |
148 cache, trans_info, MockHttpRequest(trans_info), NULL, log, | 172 cache, trans_info, MockHttpRequest(trans_info), NULL, log, |
149 kNoDelegateTransactionCheck); | 173 kNoDelegateTransactionCheck, kNoDelegateTransactionCheck); |
150 } | 174 } |
151 | 175 |
152 void RunTransactionTestWithDelegate(net::HttpCache* cache, | 176 void RunTransactionTestWithDelegate(net::HttpCache* cache, |
153 const MockTransaction& trans_info, | 177 const MockTransaction& trans_info, |
154 int num_delegate_actions) { | 178 int num_cache_delegate_actions, |
179 int num_network_delegate_actions) { | |
155 RunTransactionTestWithRequestAndLogAndDelegate( | 180 RunTransactionTestWithRequestAndLogAndDelegate( |
156 cache, trans_info, MockHttpRequest(trans_info), NULL, net::BoundNetLog(), | 181 cache, trans_info, MockHttpRequest(trans_info), NULL, net::BoundNetLog(), |
157 num_delegate_actions); | 182 num_cache_delegate_actions, num_network_delegate_actions); |
158 } | 183 } |
159 | 184 |
160 void RunTransactionTest(net::HttpCache* cache, | 185 void RunTransactionTest(net::HttpCache* cache, |
161 const MockTransaction& trans_info) { | 186 const MockTransaction& trans_info) { |
162 RunTransactionTestWithLog(cache, trans_info, net::BoundNetLog()); | 187 RunTransactionTestWithLog(cache, trans_info, net::BoundNetLog()); |
163 } | 188 } |
164 | 189 |
165 void RunTransactionTestWithResponseInfo(net::HttpCache* cache, | 190 void RunTransactionTestWithResponseInfo(net::HttpCache* cache, |
166 const MockTransaction& trans_info, | 191 const MockTransaction& trans_info, |
167 net::HttpResponseInfo* response) { | 192 net::HttpResponseInfo* response) { |
(...skipping 4848 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
5016 EXPECT_TRUE(truncated); | 5041 EXPECT_TRUE(truncated); |
5017 entry->Close(); | 5042 entry->Close(); |
5018 } | 5043 } |
5019 | 5044 |
5020 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit_TransactionDelegate) { | 5045 TEST(HttpCache, SimpleGET_LoadOnlyFromCache_Hit_TransactionDelegate) { |
5021 MockHttpCache cache; | 5046 MockHttpCache cache; |
5022 | 5047 |
5023 // Write to the cache. | 5048 // Write to the cache. |
5024 RunTransactionTestWithDelegate(cache.http_cache(), | 5049 RunTransactionTestWithDelegate(cache.http_cache(), |
5025 kSimpleGET_Transaction, | 5050 kSimpleGET_Transaction, |
5026 8); | 5051 8, |
5052 3); | |
5027 | 5053 |
5028 // Force this transaction to read from the cache. | 5054 // Force this transaction to read from the cache. |
5029 MockTransaction transaction(kSimpleGET_Transaction); | 5055 MockTransaction transaction(kSimpleGET_Transaction); |
5030 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; | 5056 transaction.load_flags |= net::LOAD_ONLY_FROM_CACHE; |
5031 | 5057 |
5032 RunTransactionTestWithDelegate(cache.http_cache(), | 5058 RunTransactionTestWithDelegate(cache.http_cache(), |
5033 kSimpleGET_Transaction, | 5059 kSimpleGET_Transaction, |
5034 5); | 5060 5, |
5061 0); | |
5035 } | 5062 } |
rvargas (doing something else)
2012/08/16 01:18:40
There is no test for request cancellation. That wa
tburkard
2012/08/16 22:37:43
Done.
| |
OLD | NEW |