OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
satorux1
2012/10/16 03:12:18
Please create a separate patch for changes in this
| |
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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "chrome/browser/chromeos/gdata/gdata_wapi_service.h" | 10 #include "chrome/browser/chromeos/gdata/gdata_wapi_service.h" |
11 #include "chrome/browser/chromeos/gdata/test_servers/http_test_server.h" | |
11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
13 #include "chrome/test/base/in_process_browser_test.h" | 14 #include "chrome/test/base/in_process_browser_test.h" |
14 #include "chrome/test/base/ui_test_utils.h" | 15 #include "chrome/test/base/ui_test_utils.h" |
15 #include "net/test/test_server.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
17 | 17 |
18 namespace gdata { | 18 namespace gdata { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 class GDataTest : public InProcessBrowserTest { | 22 class GDataTest : public InProcessBrowserTest { |
23 public: | 23 public: |
24 GDataTest() | 24 GDataTest() |
25 : InProcessBrowserTest(), | 25 : InProcessBrowserTest() { |
26 gdata_test_server_(net::TestServer::TYPE_GDATA, | |
27 net::TestServer::kLocalhost, | |
28 FilePath(FILE_PATH_LITERAL("chrome/test/data"))) { | |
29 } | 26 } |
30 | 27 |
31 virtual void SetUpOnMainThread() OVERRIDE { | 28 virtual void SetUpOnMainThread() OVERRIDE { |
32 ASSERT_TRUE(gdata_test_server_.Start()); | |
33 service_.reset(new gdata::GDataWapiService); | 29 service_.reset(new gdata::GDataWapiService); |
34 service_->Initialize(browser()->profile()); | 30 service_->Initialize(browser()->profile()); |
35 service_->auth_service_for_testing()->set_access_token_for_testing( | 31 service_->auth_service_for_testing()->set_access_token_for_testing( |
36 net::TestServer::kGDataAuthToken); | 32 net::TestServer::kGDataAuthToken); |
33 gdata_test_server_.reset(new gdata::test_servers::HttpTestServer(true)); | |
34 SetUpTestServer(); | |
35 } | |
36 | |
37 virtual void SetUpTestServer() { | |
38 test_server_file_url_ = gdata_test_server_->RegisterFileResponse( | |
39 "some/path/testfile.txt", | |
40 FilePath(FILE_PATH_LITERAL( | |
41 "chrome/test/data/chromeos/gdata/testfile.txt")), | |
42 "text/plain", | |
43 gdata::test_servers::SUCCESS); | |
44 | |
45 test_server_root_feed_url_ = gdata_test_server_->RegisterFileResponse( | |
46 FilePath(FILE_PATH_LITERAL( | |
47 "chrome/test/data/chromeos/gdata/root_feed.json")), | |
48 "text/json"); | |
37 } | 49 } |
38 | 50 |
39 virtual void CleanUpOnMainThread() { | 51 virtual void CleanUpOnMainThread() { |
40 service_.reset(); | 52 service_.reset(); |
53 gdata_test_server_.reset(); | |
41 } | 54 } |
42 | 55 |
43 protected: | 56 protected: |
44 FilePath GetTestCachedFilePath(const FilePath& file_name) { | 57 FilePath GetTestCachedFilePath(const FilePath& file_name) { |
45 return browser()->profile()->GetPath().Append(file_name); | 58 return browser()->profile()->GetPath().Append(file_name); |
46 } | 59 } |
47 | 60 |
48 net::TestServer gdata_test_server_; | 61 scoped_ptr<gdata::test_servers::HttpTestServer> gdata_test_server_; |
49 scoped_ptr<gdata::GDataWapiService> service_; | 62 scoped_ptr<gdata::GDataWapiService> service_; |
63 GURL test_server_file_url_; | |
64 GURL test_server_root_feed_url_; | |
50 }; | 65 }; |
51 | 66 |
52 // The test callback for GDataWapiService::DownloadFile(). | 67 // The test callback for GDataWapiService::DownloadFile(). |
53 void TestDownloadCallback(gdata::GDataErrorCode* result, | 68 void TestDownloadCallback(gdata::GDataErrorCode* result, |
54 std::string* contents, | 69 std::string* contents, |
55 gdata::GDataErrorCode error, | 70 gdata::GDataErrorCode error, |
56 const GURL& content_url, | 71 const GURL& content_url, |
57 const FilePath& temp_file) { | 72 const FilePath& temp_file) { |
58 *result = error; | 73 *result = error; |
59 file_util::ReadFileToString(temp_file, contents); | 74 file_util::ReadFileToString(temp_file, contents); |
(...skipping 12 matching lines...) Expand all Loading... | |
72 } | 87 } |
73 | 88 |
74 } // namespace | 89 } // namespace |
75 | 90 |
76 IN_PROC_BROWSER_TEST_F(GDataTest, Download) { | 91 IN_PROC_BROWSER_TEST_F(GDataTest, Download) { |
77 gdata::GDataErrorCode result = gdata::GDATA_OTHER_ERROR; | 92 gdata::GDataErrorCode result = gdata::GDATA_OTHER_ERROR; |
78 std::string contents; | 93 std::string contents; |
79 service_->DownloadFile( | 94 service_->DownloadFile( |
80 FilePath("/dummy/gdata/testfile.txt"), | 95 FilePath("/dummy/gdata/testfile.txt"), |
81 GetTestCachedFilePath(FilePath("cached_testfile.txt")), | 96 GetTestCachedFilePath(FilePath("cached_testfile.txt")), |
82 gdata_test_server_.GetURL("files/chromeos/gdata/testfile.txt"), | 97 test_server_file_url_, |
83 base::Bind(&TestDownloadCallback, &result, &contents), | 98 base::Bind(&TestDownloadCallback, &result, &contents), |
84 gdata::GetContentCallback()); | 99 gdata::GetContentCallback()); |
85 content::RunMessageLoop(); | 100 content::RunMessageLoop(); |
86 | 101 |
87 EXPECT_EQ(gdata::HTTP_SUCCESS, result); | 102 EXPECT_EQ(gdata::HTTP_SUCCESS, result); |
88 FilePath expected_filepath = gdata_test_server_.document_root().Append( | 103 FilePath expected_filepath = FilePath( |
89 FilePath(FILE_PATH_LITERAL("chromeos/gdata/testfile.txt"))); | 104 FILE_PATH_LITERAL("chrome/test/data/chromeos/gdata/testfile.txt")); |
90 std::string expected_contents; | 105 std::string expected_contents; |
91 file_util::ReadFileToString(expected_filepath, &expected_contents); | 106 file_util::ReadFileToString(expected_filepath, &expected_contents); |
92 EXPECT_EQ(expected_contents, contents); | 107 EXPECT_EQ(expected_contents, contents); |
93 } | 108 } |
94 | 109 |
95 IN_PROC_BROWSER_TEST_F(GDataTest, NonExistingDownload) { | 110 IN_PROC_BROWSER_TEST_F(GDataTest, NonExistingDownload) { |
96 gdata::GDataErrorCode result = gdata::GDATA_OTHER_ERROR; | 111 gdata::GDataErrorCode result = gdata::GDATA_OTHER_ERROR; |
97 std::string dummy_contents; | 112 std::string dummy_contents; |
98 service_->DownloadFile( | 113 service_->DownloadFile( |
99 FilePath("/dummy/gdata/no-such-file.txt"), | 114 FilePath("/dummy/gdata/no-such-file.txt"), |
100 GetTestCachedFilePath(FilePath("cache_no-such-file.txt")), | 115 GetTestCachedFilePath(FilePath("cache_no-such-file.txt")), |
101 gdata_test_server_.GetURL("files/chromeos/gdata/no-such-file.txt"), | 116 gdata_test_server_->GetErrorURL(gdata::test_servers::NOT_FOUND), |
102 base::Bind(&TestDownloadCallback, &result, &dummy_contents), | 117 base::Bind(&TestDownloadCallback, &result, &dummy_contents), |
103 gdata::GetContentCallback()); | 118 gdata::GetContentCallback()); |
104 content::RunMessageLoop(); | 119 content::RunMessageLoop(); |
105 | 120 |
106 EXPECT_EQ(gdata::HTTP_NOT_FOUND, result); | 121 EXPECT_EQ(gdata::HTTP_NOT_FOUND, result); |
107 // Do not verify the not found message. | 122 // Do not verify the not found message. |
108 } | 123 } |
109 | 124 |
110 IN_PROC_BROWSER_TEST_F(GDataTest, GetDocuments) { | 125 IN_PROC_BROWSER_TEST_F(GDataTest, GetDocuments) { |
111 gdata::GDataErrorCode result = gdata::GDATA_OTHER_ERROR; | 126 gdata::GDataErrorCode result = gdata::GDATA_OTHER_ERROR; |
112 base::Value* result_data = NULL; | 127 base::Value* result_data = NULL; |
113 service_->GetDocuments( | 128 service_->GetDocuments( |
114 gdata_test_server_.GetURL("files/chromeos/gdata/root_feed.json"), | 129 test_server_root_feed_url_, |
115 0, // start_changestamp | 130 0, // start_changestamp |
116 std::string(), // search string | 131 std::string(), // search string |
117 std::string(), // directory resource ID | 132 std::string(), // directory resource ID |
118 base::Bind(&TestGetDocumentsCallback, &result, &result_data)); | 133 base::Bind(&TestGetDocumentsCallback, &result, &result_data)); |
119 content::RunMessageLoop(); | 134 content::RunMessageLoop(); |
120 | 135 |
121 EXPECT_EQ(gdata::HTTP_SUCCESS, result); | 136 EXPECT_EQ(gdata::HTTP_SUCCESS, result); |
122 ASSERT_TRUE(result_data); | 137 ASSERT_TRUE(result_data); |
123 FilePath expected_filepath = gdata_test_server_.document_root().Append( | 138 FilePath expected_filepath = FilePath( |
124 FilePath(FILE_PATH_LITERAL("chromeos/gdata/root_feed.json"))); | 139 FILE_PATH_LITERAL("chrome/test/data/chromeos/gdata/root_feed.json")); |
125 std::string expected_contents; | 140 std::string expected_contents; |
126 file_util::ReadFileToString(expected_filepath, &expected_contents); | 141 file_util::ReadFileToString(expected_filepath, &expected_contents); |
127 scoped_ptr<base::Value> expected_data( | 142 scoped_ptr<base::Value> expected_data( |
128 base::JSONReader::Read(expected_contents)); | 143 base::JSONReader::Read(expected_contents)); |
129 EXPECT_TRUE(base::Value::Equals(expected_data.get(), result_data)); | 144 EXPECT_TRUE(base::Value::Equals(expected_data.get(), result_data)); |
130 delete result_data; | 145 delete result_data; |
131 } | 146 } |
132 | 147 |
133 IN_PROC_BROWSER_TEST_F(GDataTest, GetDocumentsFailure) { | 148 IN_PROC_BROWSER_TEST_F(GDataTest, GetDocumentsFailure) { |
134 // testfile.txt exists but the response is not JSON, so it should | 149 // testfile.txt exists but the response is not JSON, so it should |
135 // emit a parse error instead. | 150 // emit a parse error instead. |
136 gdata::GDataErrorCode result = gdata::GDATA_OTHER_ERROR; | 151 gdata::GDataErrorCode result = gdata::GDATA_OTHER_ERROR; |
137 base::Value* result_data = NULL; | 152 base::Value* result_data = NULL; |
138 service_->GetDocuments( | 153 service_->GetDocuments( |
139 gdata_test_server_.GetURL("files/chromeos/gdata/testfile.txt"), | 154 test_server_file_url_, |
140 0, // start_changestamp | 155 0, // start_changestamp |
141 std::string(), // search string | 156 std::string(), // search string |
142 std::string(), // directory resource ID | 157 std::string(), // directory resource ID |
143 base::Bind(&TestGetDocumentsCallback, &result, &result_data)); | 158 base::Bind(&TestGetDocumentsCallback, &result, &result_data)); |
144 content::RunMessageLoop(); | 159 content::RunMessageLoop(); |
145 | 160 |
146 EXPECT_EQ(gdata::GDATA_PARSE_ERROR, result); | 161 EXPECT_EQ(gdata::GDATA_PARSE_ERROR, result); |
147 EXPECT_FALSE(result_data); | 162 EXPECT_FALSE(result_data); |
148 } | 163 } |
149 | 164 |
150 } // namespace gdata | 165 } // namespace gdata |
OLD | NEW |