Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / log_private / log_private_apitest_chromeos.cc
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 <string>
6
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "chromeos/dbus/dbus_thread_manager.h"
12 #include "chromeos/dbus/fake_debug_daemon_client.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "extensions/common/extension_builder.h"
15 #include "net/dns/mock_host_resolver.h"
16 #include "net/test/embedded_test_server/embedded_test_server.h"
17 #include "net/test/embedded_test_server/http_request.h"
18 #include "net/test/embedded_test_server/http_response.h"
19
20 using net::test_server::BasicHttpResponse;
21 using net::test_server::HttpResponse;
22 using net::test_server::HttpRequest;
23
24 namespace {
25
26 class TestDebugDaemonClient : public chromeos::FakeDebugDaemonClient {
27  public:
28   explicit TestDebugDaemonClient(const base::FilePath& test_file)
29       : test_file_(test_file) {}
30
31   virtual ~TestDebugDaemonClient() {}
32
33   virtual void DumpDebugLogs(bool is_compressed,
34                              base::File file,
35                              scoped_refptr<base::TaskRunner> task_runner,
36                              const GetDebugLogsCallback& callback) OVERRIDE {
37     base::File* file_param = new base::File(file.Pass());
38     task_runner->PostTaskAndReply(
39         FROM_HERE,
40         base::Bind(
41             &GenerateTestLogDumpFile, test_file_, base::Owned(file_param)),
42         base::Bind(callback, true));
43   }
44
45   static void GenerateTestLogDumpFile(const base::FilePath& test_tar_file,
46                                       base::File* file) {
47     std::string test_file_content;
48     EXPECT_TRUE(base::ReadFileToString(test_tar_file, &test_file_content))
49         << "Cannot read content of file " << test_tar_file.value();
50     const int data_size = static_cast<int>(test_file_content.size());
51     EXPECT_EQ(data_size, file->Write(0, test_file_content.data(), data_size));
52     EXPECT_TRUE(file->SetLength(data_size));
53     file->Close();
54   }
55
56  private:
57   base::FilePath test_file_;
58 };
59
60 }  // namespace
61
62 namespace extensions {
63
64 class LogPrivateApiTest : public ExtensionApiTest {
65  public:
66   LogPrivateApiTest() {}
67
68   virtual ~LogPrivateApiTest() {}
69
70   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
71     base::FilePath tar_file_path =
72         test_data_dir_.Append("log_private/dump_logs/system_logs.tar");
73     chromeos::DBusThreadManager::GetSetterForTesting()->SetDebugDaemonClient(
74         scoped_ptr<chromeos::DebugDaemonClient>(
75             new TestDebugDaemonClient(tar_file_path)));
76     ExtensionApiTest::SetUpInProcessBrowserTestFixture();
77   }
78
79   scoped_ptr<HttpResponse> HandleRequest(const HttpRequest& request) {
80     scoped_ptr<BasicHttpResponse> response(new BasicHttpResponse);
81     response->set_code(net::HTTP_OK);
82     response->set_content(
83         "<html><head><title>LogPrivateTest</title>"
84         "</head><body>Hello!</body></html>");
85     return response.PassAs<HttpResponse>();
86   }
87 };
88
89 IN_PROC_BROWSER_TEST_F(LogPrivateApiTest, DumpLogsAndCaptureEvents) {
90   // Setup dummy HTTP server.
91   host_resolver()->AddRule("www.test.com", "127.0.0.1");
92   ASSERT_TRUE(StartEmbeddedTestServer());
93   embedded_test_server()->RegisterRequestHandler(
94       base::Bind(&LogPrivateApiTest::HandleRequest, base::Unretained(this)));
95
96   ASSERT_TRUE(RunExtensionTest("log_private/dump_logs"));
97 }
98
99 }  // namespace extensions