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