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

Side by Side Diff: services/files/c/tests/test_utils.cc

Issue 1133933002: Move //services/files/c -> //mojo/services/files/public/c. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: remove data dep Created 5 years, 7 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
« no previous file with comments | « services/files/c/tests/test_utils.h ('k') | services/files/c/tests/test_utils_unittest.cc » ('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 2015 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 "services/files/c/tests/test_utils.h"
6
7 #include "mojo/public/cpp/environment/logging.h"
8 #include "mojo/services/files/public/interfaces/file.mojom.h"
9 #include "mojo/services/files/public/interfaces/types.mojom.h"
10 #include "services/files/c/lib/template_util.h"
11
12 namespace mojio {
13 namespace test {
14
15 void MakeDirAt(mojo::files::DirectoryPtr* root, const char* path) {
16 MOJO_CHECK(root);
17 MOJO_CHECK(path);
18 mojo::files::DirectoryPtr& dir = *root;
19
20 mojo::files::Error error = mojo::files::ERROR_INTERNAL;
21 dir->OpenDirectory(path, nullptr,
22 mojo::files::kOpenFlagRead | mojo::files::kOpenFlagWrite |
23 mojo::files::kOpenFlagCreate,
24 Capture(&error));
25 MOJO_CHECK(dir.WaitForIncomingMethodCall());
26 MOJO_CHECK(error == mojo::files::ERROR_OK);
27 }
28
29 mojo::files::FilePtr OpenFileAt(mojo::files::DirectoryPtr* root,
30 const char* path,
31 uint32_t open_flags) {
32 MOJO_CHECK(root);
33 MOJO_CHECK(path);
34 mojo::files::DirectoryPtr& dir = *root;
35
36 mojo::files::FilePtr file;
37 mojo::files::Error error = mojo::files::ERROR_INTERNAL;
38 dir->OpenFile(path, mojo::GetProxy(&file), open_flags, Capture(&error));
39 MOJO_CHECK(dir.WaitForIncomingMethodCall());
40 if (error != mojo::files::ERROR_OK)
41 return nullptr;
42
43 return file.Pass();
44 }
45
46 void CreateTestFileAt(mojo::files::DirectoryPtr* root,
47 const char* path,
48 size_t size) {
49 mojo::files::FilePtr file = OpenFileAt(
50 root, path, mojo::files::kOpenFlagWrite | mojo::files::kOpenFlagCreate |
51 mojo::files::kOpenFlagExclusive);
52 MOJO_CHECK(file);
53
54 if (!size)
55 return;
56
57 std::vector<uint8_t> bytes_to_write(size);
58 for (size_t i = 0; i < size; i++)
59 bytes_to_write[i] = static_cast<uint8_t>(i);
60 mojo::files::Error error = mojo::files::ERROR_INTERNAL;
61 uint32_t num_bytes_written = 0;
62 file->Write(mojo::Array<uint8_t>::From(bytes_to_write), 0,
63 mojo::files::WHENCE_FROM_CURRENT,
64 Capture(&error, &num_bytes_written));
65 MOJO_CHECK(file.WaitForIncomingMethodCall());
66 MOJO_CHECK(error == mojo::files::ERROR_OK);
67 MOJO_CHECK(num_bytes_written == bytes_to_write.size());
68 }
69
70 int64_t GetFileSize(mojo::files::DirectoryPtr* root, const char* path) {
71 mojo::files::FilePtr file =
72 OpenFileAt(root, path, mojo::files::kOpenFlagRead);
73 if (!file)
74 return -1;
75
76 // Seek to the end to get the file size (we could also stat).
77 mojo::files::Error error = mojo::files::ERROR_INTERNAL;
78 int64_t position = -1;
79 file->Seek(0, mojo::files::WHENCE_FROM_END, Capture(&error, &position));
80 MOJO_CHECK(file.WaitForIncomingMethodCall());
81 MOJO_CHECK(error == mojo::files::ERROR_OK);
82 MOJO_CHECK(position >= 0);
83 return position;
84 }
85
86 std::string GetFileContents(mojo::files::DirectoryPtr* root, const char* path) {
87 mojo::files::FilePtr file =
88 OpenFileAt(root, path, mojo::files::kOpenFlagRead);
89 MOJO_CHECK(file);
90
91 mojo::Array<uint8_t> bytes_read;
92 mojo::files::Error error = mojo::files::ERROR_INTERNAL;
93 file->Read(1024 * 1024, 0, mojo::files::WHENCE_FROM_START,
94 Capture(&error, &bytes_read));
95 MOJO_CHECK(file.WaitForIncomingMethodCall());
96 if (!bytes_read.size())
97 return std::string();
98 return std::string(reinterpret_cast<const char*>(&bytes_read[0]),
99 bytes_read.size());
100 }
101
102 } // namespace test
103 } // namespace mojio
OLDNEW
« no previous file with comments | « services/files/c/tests/test_utils.h ('k') | services/files/c/tests/test_utils_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698