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/url_request/url_fetcher_impl.h" | 5 #include "net/url_request/url_fetcher_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
374 public: | 374 public: |
375 // URLFetcherDelegate | 375 // URLFetcherDelegate |
376 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 376 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
377 private: | 377 private: |
378 std::string data_; | 378 std::string data_; |
379 }; | 379 }; |
380 | 380 |
381 class URLFetcherFileTest : public URLFetcherTest { | 381 class URLFetcherFileTest : public URLFetcherTest { |
382 public: | 382 public: |
383 URLFetcherFileTest() : take_ownership_of_file_(false), | 383 URLFetcherFileTest() : take_ownership_of_file_(false), |
384 expected_file_error_(base::PLATFORM_FILE_OK) {} | 384 expected_file_error_(base::PLATFORM_FILE_OK) { |
385 PathService::Get(base::DIR_SOURCE_ROOT, &document_root_); | |
386 document_root_= document_root_.AppendASCII(FILE_PATH_LITERAL("chrome")) | |
387 .AppendASCII(FILE_PATH_LITERAL("test")) | |
388 .AppendASCII(FILE_PATH_LITERAL("data")); | |
wtc
2012/09/26 23:48:20
Instead of hardcoding the strings "chrome", "test"
Shouqun Liu
2012/09/27 01:24:51
yes, agree, Android is a special case.
On 2012/0
| |
389 } | |
385 | 390 |
386 void CreateFetcherForFile(const GURL& url, const FilePath& file_path); | 391 void CreateFetcherForFile(const GURL& url, const FilePath& file_path); |
387 void CreateFetcherForTempFile(const GURL& url); | 392 void CreateFetcherForTempFile(const GURL& url); |
388 | 393 |
389 // URLFetcherDelegate | 394 // URLFetcherDelegate |
390 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 395 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
391 | 396 |
392 protected: | 397 protected: |
398 FilePath document_root_; | |
393 FilePath expected_file_; | 399 FilePath expected_file_; |
394 FilePath file_path_; | 400 FilePath file_path_; |
395 | 401 |
396 // Set by the test. Used in OnURLFetchComplete() to decide if | 402 // Set by the test. Used in OnURLFetchComplete() to decide if |
397 // the URLFetcher should own the temp file, so that we can test | 403 // the URLFetcher should own the temp file, so that we can test |
398 // disowning prevents the file from being deleted. | 404 // disowning prevents the file from being deleted. |
399 bool take_ownership_of_file_; | 405 bool take_ownership_of_file_; |
400 | 406 |
401 // Expected file error code for the test. | 407 // Expected file error code for the test. |
402 // PLATFORM_FILE_OK when expecting success. | 408 // PLATFORM_FILE_OK when expecting success. |
(...skipping 669 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1072 // thread, this will test URLFetcher's ability to do everything on one | 1078 // thread, this will test URLFetcher's ability to do everything on one |
1073 // thread. | 1079 // thread. |
1074 CreateFetcher(test_server.GetURL("defaultresponse")); | 1080 CreateFetcher(test_server.GetURL("defaultresponse")); |
1075 | 1081 |
1076 MessageLoop::current()->Run(); | 1082 MessageLoop::current()->Run(); |
1077 } | 1083 } |
1078 | 1084 |
1079 TEST_F(URLFetcherFileTest, SmallGet) { | 1085 TEST_F(URLFetcherFileTest, SmallGet) { |
1080 TestServer test_server(TestServer::TYPE_HTTP, | 1086 TestServer test_server(TestServer::TYPE_HTTP, |
1081 TestServer::kLocalhost, | 1087 TestServer::kLocalhost, |
1082 FilePath(kDocRoot)); | 1088 FilePath(kDocRoot)); |
wtc
2012/09/28 21:43:13
Perhaps we can pass document_root_ or something ba
| |
1083 ASSERT_TRUE(test_server.Start()); | 1089 ASSERT_TRUE(test_server.Start()); |
1084 | 1090 |
1085 ScopedTempDir temp_dir; | 1091 ScopedTempDir temp_dir; |
1086 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1092 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
1087 | 1093 |
1088 // Get a small file. | 1094 // Get a small file. |
1089 static const char kFileToFetch[] = "simple.html"; | 1095 static const char kFileToFetch[] = "simple.html"; |
1090 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1096 expected_file_ = document_root_.AppendASCII(kFileToFetch); |
wtc
2012/09/26 23:48:20
This unit test is a representative example.
On li
Shouqun Liu
2012/09/27 01:24:51
Yes, for general cases, I think we should use tes
wtc
2012/09/28 21:43:13
Hi Shouqun,
Thank you for the explanation. We sho
Shouqun Liu
2012/09/29 06:16:07
Yes, thanks for your suggestion, I have refined t
| |
1091 CreateFetcherForFile( | 1097 CreateFetcherForFile( |
1092 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1098 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
1093 temp_dir.path().AppendASCII(kFileToFetch)); | 1099 temp_dir.path().AppendASCII(kFileToFetch)); |
1094 | 1100 |
1095 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1101 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
1096 | 1102 |
1097 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1103 ASSERT_FALSE(file_util::PathExists(file_path_)) |
1098 << file_path_.value() << " not removed."; | 1104 << file_path_.value() << " not removed."; |
1099 } | 1105 } |
1100 | 1106 |
1101 TEST_F(URLFetcherFileTest, LargeGet) { | 1107 TEST_F(URLFetcherFileTest, LargeGet) { |
1102 TestServer test_server(TestServer::TYPE_HTTP, | 1108 TestServer test_server(TestServer::TYPE_HTTP, |
1103 TestServer::kLocalhost, | 1109 TestServer::kLocalhost, |
1104 FilePath(kDocRoot)); | 1110 FilePath(kDocRoot)); |
1105 ASSERT_TRUE(test_server.Start()); | 1111 ASSERT_TRUE(test_server.Start()); |
1106 | 1112 |
1107 ScopedTempDir temp_dir; | 1113 ScopedTempDir temp_dir; |
1108 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1114 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
1109 | 1115 |
1110 // Get a file large enough to require more than one read into | 1116 // Get a file large enough to require more than one read into |
1111 // URLFetcher::Core's IOBuffer. | 1117 // URLFetcher::Core's IOBuffer. |
1112 static const char kFileToFetch[] = "animate1.gif"; | 1118 static const char kFileToFetch[] = "animate1.gif"; |
1113 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1119 expected_file_ = document_root_.AppendASCII(kFileToFetch); |
1114 CreateFetcherForFile( | 1120 CreateFetcherForFile( |
1115 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1121 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
1116 temp_dir.path().AppendASCII(kFileToFetch)); | 1122 temp_dir.path().AppendASCII(kFileToFetch)); |
1117 | 1123 |
1118 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1124 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
1119 } | 1125 } |
1120 | 1126 |
1121 TEST_F(URLFetcherFileTest, CanTakeOwnershipOfFile) { | 1127 TEST_F(URLFetcherFileTest, CanTakeOwnershipOfFile) { |
1122 TestServer test_server(TestServer::TYPE_HTTP, | 1128 TestServer test_server(TestServer::TYPE_HTTP, |
1123 TestServer::kLocalhost, | 1129 TestServer::kLocalhost, |
1124 FilePath(kDocRoot)); | 1130 FilePath(kDocRoot)); |
1125 ASSERT_TRUE(test_server.Start()); | 1131 ASSERT_TRUE(test_server.Start()); |
1126 | 1132 |
1127 ScopedTempDir temp_dir; | 1133 ScopedTempDir temp_dir; |
1128 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1134 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
1129 | 1135 |
1130 // Get a small file. | 1136 // Get a small file. |
1131 static const char kFileToFetch[] = "simple.html"; | 1137 static const char kFileToFetch[] = "simple.html"; |
1132 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1138 expected_file_ = document_root_.AppendASCII(kFileToFetch); |
1133 CreateFetcherForFile( | 1139 CreateFetcherForFile( |
1134 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1140 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
1135 temp_dir.path().AppendASCII(kFileToFetch)); | 1141 temp_dir.path().AppendASCII(kFileToFetch)); |
1136 | 1142 |
1137 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1143 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
1138 | 1144 |
1139 MessageLoop::current()->RunAllPending(); | 1145 MessageLoop::current()->RunAllPending(); |
1140 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1146 ASSERT_FALSE(file_util::PathExists(file_path_)) |
1141 << file_path_.value() << " not removed."; | 1147 << file_path_.value() << " not removed."; |
1142 } | 1148 } |
1143 | 1149 |
1144 | 1150 |
1145 TEST_F(URLFetcherFileTest, OverwriteExistingFile) { | 1151 TEST_F(URLFetcherFileTest, OverwriteExistingFile) { |
1146 TestServer test_server(TestServer::TYPE_HTTP, | 1152 TestServer test_server(TestServer::TYPE_HTTP, |
1147 TestServer::kLocalhost, | 1153 TestServer::kLocalhost, |
1148 FilePath(kDocRoot)); | 1154 FilePath(kDocRoot)); |
1149 ASSERT_TRUE(test_server.Start()); | 1155 ASSERT_TRUE(test_server.Start()); |
1150 | 1156 |
1151 ScopedTempDir temp_dir; | 1157 ScopedTempDir temp_dir; |
1152 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 1158 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
1153 | 1159 |
1154 // Create a file before trying to fetch. | 1160 // Create a file before trying to fetch. |
1155 static const char kFileToFetch[] = "simple.html"; | 1161 static const char kFileToFetch[] = "simple.html"; |
1156 static const char kData[] = "abcdefghijklmnopqrstuvwxyz"; | 1162 static const char kData[] = "abcdefghijklmnopqrstuvwxyz"; |
1157 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); | 1163 file_path_ = temp_dir.path().AppendASCII(kFileToFetch); |
1158 const int data_size = arraysize(kData); | 1164 const int data_size = arraysize(kData); |
1159 ASSERT_EQ(file_util::WriteFile(file_path_, kData, data_size), data_size); | 1165 ASSERT_EQ(file_util::WriteFile(file_path_, kData, data_size), data_size); |
1160 ASSERT_TRUE(file_util::PathExists(file_path_)); | 1166 ASSERT_TRUE(file_util::PathExists(file_path_)); |
1161 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1167 expected_file_ = document_root_.AppendASCII(kFileToFetch); |
1162 ASSERT_FALSE(file_util::ContentsEqual(file_path_, expected_file_)); | 1168 ASSERT_FALSE(file_util::ContentsEqual(file_path_, expected_file_)); |
1163 | 1169 |
1164 // Get a small file. | 1170 // Get a small file. |
1165 CreateFetcherForFile( | 1171 CreateFetcherForFile( |
1166 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), | 1172 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch), |
1167 file_path_); | 1173 file_path_); |
1168 | 1174 |
1169 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1175 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
1170 } | 1176 } |
1171 | 1177 |
(...skipping 25 matching lines...) Expand all Loading... | |
1197 } | 1203 } |
1198 | 1204 |
1199 TEST_F(URLFetcherFileTest, SmallGetToTempFile) { | 1205 TEST_F(URLFetcherFileTest, SmallGetToTempFile) { |
1200 TestServer test_server(TestServer::TYPE_HTTP, | 1206 TestServer test_server(TestServer::TYPE_HTTP, |
1201 TestServer::kLocalhost, | 1207 TestServer::kLocalhost, |
1202 FilePath(kDocRoot)); | 1208 FilePath(kDocRoot)); |
1203 ASSERT_TRUE(test_server.Start()); | 1209 ASSERT_TRUE(test_server.Start()); |
1204 | 1210 |
1205 // Get a small file. | 1211 // Get a small file. |
1206 static const char kFileToFetch[] = "simple.html"; | 1212 static const char kFileToFetch[] = "simple.html"; |
1207 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1213 expected_file_ = document_root_.AppendASCII(kFileToFetch); |
1208 CreateFetcherForTempFile( | 1214 CreateFetcherForTempFile( |
1209 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); | 1215 test_server.GetURL(std::string(kTestServerFilePrefix) + kFileToFetch)); |
1210 | 1216 |
1211 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1217 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
1212 | 1218 |
1213 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1219 ASSERT_FALSE(file_util::PathExists(file_path_)) |
1214 << file_path_.value() << " not removed."; | 1220 << file_path_.value() << " not removed."; |
1215 } | 1221 } |
1216 | 1222 |
1217 TEST_F(URLFetcherFileTest, LargeGetToTempFile) { | 1223 TEST_F(URLFetcherFileTest, LargeGetToTempFile) { |
1218 TestServer test_server(TestServer::TYPE_HTTP, | 1224 TestServer test_server(TestServer::TYPE_HTTP, |
1219 TestServer::kLocalhost, | 1225 TestServer::kLocalhost, |
1220 FilePath(kDocRoot)); | 1226 FilePath(kDocRoot)); |
1221 ASSERT_TRUE(test_server.Start()); | 1227 ASSERT_TRUE(test_server.Start()); |
1222 | 1228 |
1223 // Get a file large enough to require more than one read into | 1229 // Get a file large enough to require more than one read into |
1224 // URLFetcher::Core's IOBuffer. | 1230 // URLFetcher::Core's IOBuffer. |
1225 static const char kFileToFetch[] = "animate1.gif"; | 1231 static const char kFileToFetch[] = "animate1.gif"; |
1226 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1232 expected_file_ = document_root_.AppendASCII(kFileToFetch); |
1227 CreateFetcherForTempFile(test_server.GetURL( | 1233 CreateFetcherForTempFile(test_server.GetURL( |
1228 std::string(kTestServerFilePrefix) + kFileToFetch)); | 1234 std::string(kTestServerFilePrefix) + kFileToFetch)); |
1229 | 1235 |
1230 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1236 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
1231 } | 1237 } |
1232 | 1238 |
1233 TEST_F(URLFetcherFileTest, CanTakeOwnershipOfTempFile) { | 1239 TEST_F(URLFetcherFileTest, CanTakeOwnershipOfTempFile) { |
1234 TestServer test_server(TestServer::TYPE_HTTP, | 1240 TestServer test_server(TestServer::TYPE_HTTP, |
1235 TestServer::kLocalhost, | 1241 TestServer::kLocalhost, |
1236 FilePath(kDocRoot)); | 1242 FilePath(kDocRoot)); |
1237 ASSERT_TRUE(test_server.Start()); | 1243 ASSERT_TRUE(test_server.Start()); |
1238 | 1244 |
1239 // Get a small file. | 1245 // Get a small file. |
1240 static const char kFileToFetch[] = "simple.html"; | 1246 static const char kFileToFetch[] = "simple.html"; |
1241 expected_file_ = test_server.document_root().AppendASCII(kFileToFetch); | 1247 expected_file_ = document_root_.AppendASCII(kFileToFetch); |
1242 CreateFetcherForTempFile(test_server.GetURL( | 1248 CreateFetcherForTempFile(test_server.GetURL( |
1243 std::string(kTestServerFilePrefix) + kFileToFetch)); | 1249 std::string(kTestServerFilePrefix) + kFileToFetch)); |
1244 | 1250 |
1245 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1251 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
1246 | 1252 |
1247 MessageLoop::current()->RunAllPending(); | 1253 MessageLoop::current()->RunAllPending(); |
1248 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1254 ASSERT_FALSE(file_util::PathExists(file_path_)) |
1249 << file_path_.value() << " not removed."; | 1255 << file_path_.value() << " not removed."; |
1250 } | 1256 } |
1251 | 1257 |
1252 } // namespace | 1258 } // namespace |
1253 | 1259 |
1254 } // namespace net | 1260 } // namespace net |
OLD | NEW |