OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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/drive_file_stream_reader.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/file_util.h" | |
10 #include "base/files/file_path.h" | |
11 #include "base/message_loop.h" | |
12 #include "chrome/browser/google_apis/task_util.h" | |
13 #include "chrome/browser/google_apis/test_util.h" | |
14 #include "content/public/test/test_browser_thread.h" | |
15 #include "net/base/file_stream.h" | |
16 #include "net/base/io_buffer.h" | |
17 #include "net/base/net_errors.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 | |
20 using content::BrowserThread; | |
21 | |
22 namespace drive { | |
23 namespace internal { | |
24 | |
25 TEST(LocalReaderProxyTest, Read) { | |
26 // Prepare the test content. | |
27 const base::FilePath kTestFile( | |
28 google_apis::test_util::GetTestFilePath("chromeos/drive/applist.json")); | |
29 std::string expected_content; | |
30 ASSERT_TRUE(file_util::ReadFileToString(kTestFile, &expected_content)); | |
31 | |
32 // The LocaReaderProxy should live on IO thread. | |
33 MessageLoopForIO io_loop; | |
34 content::TestBrowserThread io_thread(BrowserThread::IO, &io_loop); | |
35 | |
36 // Open the file first. | |
37 scoped_ptr<net::FileStream> file_stream(new net::FileStream(NULL)); | |
38 int result; | |
39 net::CompletionCallback callback = | |
hashimoto
2013/04/16 08:38:29
How about using net::TestCompletionCallback?
hidehiko
2013/04/16 08:57:11
Good to know. Done.
| |
40 google_apis::CreateComposedCallback( | |
41 base::Bind(google_apis::test_util::RunAndQuit), | |
42 google_apis::test_util::CreateCopyResultCallback(&result)); | |
43 result = file_stream->Open( | |
44 google_apis::test_util::GetTestFilePath("chromeos/drive/applist.json"), | |
45 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ | | |
46 base::PLATFORM_FILE_ASYNC, | |
47 callback); | |
48 if (result == net::ERR_IO_PENDING) { | |
49 io_loop.Run(); | |
50 } | |
51 ASSERT_EQ(net::OK, result); | |
52 | |
53 // Test instance. | |
54 LocalReaderProxy proxy(file_stream.Pass()); | |
55 | |
56 // Prepare the buffer, which size is smaller than the whole data size. | |
hashimoto
2013/04/16 08:38:29
nit: s/which/whose/?
hidehiko
2013/04/16 08:57:11
Oops. Done.
| |
57 const int kBufferSize = 10; | |
58 ASSERT_LE(static_cast<size_t>(kBufferSize), expected_content.size()); | |
59 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize)); | |
60 | |
61 // Read repeatedly, until it is finished. | |
62 std::string concatenated_content; | |
63 while (concatenated_content.size() < expected_content.size()) { | |
64 result = proxy.Read(buffer.get(), kBufferSize, callback); | |
65 if (result == net::ERR_IO_PENDING) { | |
66 io_loop.Run(); | |
67 } | |
68 | |
69 // The read content size should be smaller than the buffer size. | |
70 ASSERT_LE(result, kBufferSize); | |
71 concatenated_content.append(buffer->data(), result); | |
72 } | |
73 | |
74 // Make sure the read contant is as same as the file. | |
75 EXPECT_EQ(expected_content, concatenated_content); | |
76 } | |
77 | |
78 } // namespace internal | |
79 } // namespace drive | |
OLD | NEW |