Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(774)

Side by Side Diff: chrome/browser/chromeos/drive/file_task_executor_unittest.cc

Issue 394103003: Add unit tests for drive::FileTaskExecutor. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/drive/fake_drive_service.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/chromeos/drive/file_task_executor.h"
6
7 #include <set>
8 #include <string>
9
10 #include "base/run_loop.h"
11 #include "chrome/browser/chromeos/drive/fake_file_system.h"
12 #include "chrome/browser/drive/fake_drive_service.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "google_apis/drive/drive_api_parser.h"
15 #include "google_apis/drive/test_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "webkit/browser/fileapi/file_system_url.h"
18
19 namespace drive {
20
21 namespace {
22
23 // Test harness for verifying the behavior of FileTaskExecutor.
24 class TestDelegate : public FileTaskExecutorDelegate {
25 public:
26 explicit TestDelegate(std::set<std::string>* opend_urls)
27 : opend_urls_(opend_urls),
28 fake_drive_service_(new FakeDriveService),
29 fake_file_system_(new test_util::FakeFileSystem(
30 fake_drive_service_.get())) {
31 fake_drive_service_->set_open_url_format("http://openlink/%s/%s");
32 }
33
34 // FileTaskExecutorDelegate overrides.
35 virtual FileSystemInterface* GetFileSystem() OVERRIDE {
36 return fake_file_system_.get();
37 }
38
39 virtual DriveServiceInterface* GetDriveService() OVERRIDE {
40 return fake_drive_service_.get();
41 }
42
43 virtual void OpenBrowserWindow(const GURL& open_link) OVERRIDE {
44 opend_urls_->insert(open_link.spec());
45 }
46
47 // Sets up files on the fake Drive service.
48 bool SetUpTestFiles() {
49 {
50 google_apis::GDataErrorCode result = google_apis::GDATA_OTHER_ERROR;
51 scoped_ptr<google_apis::FileResource> file;
52 fake_drive_service_->AddNewFileWithResourceId(
53 "id1",
54 "text/plain",
55 "random data",
56 fake_drive_service_->GetRootResourceId(),
57 "file1.txt",
58 false,
59 google_apis::test_util::CreateCopyResultCallback(&result, &file));
60 base::RunLoop().RunUntilIdle();
61 if (result != google_apis::HTTP_CREATED)
62 return false;
63 }
64 {
65 google_apis::GDataErrorCode result = google_apis::GDATA_OTHER_ERROR;
66 scoped_ptr<google_apis::FileResource> file;
67 fake_drive_service_->AddNewFileWithResourceId(
68 "id2",
69 "text/plain",
70 "random data",
71 fake_drive_service_->GetRootResourceId(),
72 "file2.txt",
73 false,
74 google_apis::test_util::CreateCopyResultCallback(&result, &file));
75 base::RunLoop().RunUntilIdle();
76 if (result != google_apis::HTTP_CREATED)
77 return false;
78 }
79 return true;
80 }
81
82 private:
83 std::set<std::string>* opend_urls_;
hirono 2014/07/16 04:33:19 nit: std::set<std::string>* const
kinaba 2014/07/16 04:40:25 Done.
84 scoped_ptr<FakeDriveService> fake_drive_service_;
85 scoped_ptr<test_util::FakeFileSystem> fake_file_system_;
86 };
87
88 } // namespace
89
90 TEST(FileTaskExecutorTest, DriveAppOpenSuccess) {
91 content::TestBrowserThreadBundle thread_bundle;
92
93 std::set<std::string> opend_urls;
94
95 // |delegate_ptr| will be owned by |executor|.
96 TestDelegate* delegate_ptr = new TestDelegate(&opend_urls);
hirono 2014/07/16 04:33:20 nit: TestDelegate* const
kinaba 2014/07/16 04:40:26 Done.
97 ASSERT_TRUE(delegate_ptr->SetUpTestFiles());
98 // |executor| deletes itself after Execute() is finished.
99 FileTaskExecutor* executor = new FileTaskExecutor(
hirono 2014/07/16 04:33:19 nit: FileTaskExecutor* const
kinaba 2014/07/16 04:40:26 Done.
100 scoped_ptr<FileTaskExecutorDelegate>(delegate_ptr), "test-app-id");
101
102 std::vector<fileapi::FileSystemURL> urls;
103 urls.push_back(fileapi::FileSystemURL::CreateForTest(
104 GURL("http://origin/"),
105 fileapi::kFileSystemTypeDrive,
106 base::FilePath::FromUTF8Unsafe("/special/drive/root/file1.txt")));
107 urls.push_back(fileapi::FileSystemURL::CreateForTest(
108 GURL("http://origin/"),
109 fileapi::kFileSystemTypeDrive,
110 base::FilePath::FromUTF8Unsafe("/special/drive/root/file2.txt")));
111
112 extensions::api::file_browser_private::TaskResult result =
hirono 2014/07/16 04:33:19 nit: const
kinaba 2014/07/16 04:40:26 This is to be modified in CreateCopyResultCallback
113 extensions::api::file_browser_private::TASK_RESULT_NONE;
114 executor->Execute(urls,
115 google_apis::test_util::CreateCopyResultCallback(&result));
116 base::RunLoop().RunUntilIdle();
117
118 EXPECT_EQ(extensions::api::file_browser_private::TASK_RESULT_OPENED, result);
119 ASSERT_EQ(2u, opend_urls.size());
120 EXPECT_TRUE(opend_urls.count("http://openlink/id1/test-app-id"));
121 EXPECT_TRUE(opend_urls.count("http://openlink/id2/test-app-id"));
122 }
123
124 TEST(FileTaskExecutorTest, DriveAppOpenFailForNonExistingFile) {
125 content::TestBrowserThreadBundle thread_bundle;
126
127 std::set<std::string> opend_urls;
128
129 // |delegate_ptr| will be owned by |executor|.
130 TestDelegate* delegate_ptr = new TestDelegate(&opend_urls);
hirono 2014/07/16 04:33:19 ditto.
kinaba 2014/07/16 04:40:26 Done.
131 ASSERT_TRUE(delegate_ptr->SetUpTestFiles());
132 // |executor| deletes itself after Execute() is finished.
133 FileTaskExecutor* executor = new FileTaskExecutor(
hirono 2014/07/16 04:33:19 ditto.
kinaba 2014/07/16 04:40:26 Done.
134 scoped_ptr<FileTaskExecutorDelegate>(delegate_ptr), "test-app-id");
135
136 std::vector<fileapi::FileSystemURL> urls;
137 urls.push_back(fileapi::FileSystemURL::CreateForTest(
138 GURL("http://origin/"),
139 fileapi::kFileSystemTypeDrive,
140 base::FilePath::FromUTF8Unsafe("/special/drive/root/not-exist.txt")));
141
142 extensions::api::file_browser_private::TaskResult result =
hirono 2014/07/16 04:33:19 ditto
kinaba 2014/07/16 04:40:26 ditto (it has to be mutable.)
143 extensions::api::file_browser_private::TASK_RESULT_NONE;
144 executor->Execute(urls,
145 google_apis::test_util::CreateCopyResultCallback(&result));
146 base::RunLoop().RunUntilIdle();
147
148 EXPECT_EQ(extensions::api::file_browser_private::TASK_RESULT_FAILED, result);
149 ASSERT_TRUE(opend_urls.empty());
150 }
151
152 } // namespace drive
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/drive/fake_drive_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698