Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / resources / pdf / pdf_extension_test.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 "base/base_paths.h"
6 #include "base/files/file_util.h"
7 #include "base/path_service.h"
8 #include "chrome/browser/extensions/component_loader.h"
9 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/extensions/manifest_handlers/mime_types_handler.h"
14 #include "chrome/test/base/ui_test_utils.h"
15 #include "content/public/test/browser_test_utils.h"
16 #include "extensions/test/result_catcher.h"
17 #include "grit/browser_resources.h"
18 #include "net/test/embedded_test_server/embedded_test_server.h"
19
20 class PDFExtensionTest : public ExtensionApiTest {
21  public:
22   virtual ~PDFExtensionTest() {}
23
24   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
25     ExtensionApiTest::SetUpCommandLine(command_line);
26     command_line->AppendSwitch(switches::kOutOfProcessPdf);
27   }
28
29   virtual void SetUpOnMainThread() OVERRIDE {
30     ExtensionApiTest::SetUpOnMainThread();
31     ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
32   }
33
34
35   virtual void TearDownOnMainThread() OVERRIDE {
36     ASSERT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
37     ExtensionApiTest::TearDownOnMainThread();
38   }
39
40   void RunTestsInFile(std::string filename, bool requiresPlugin) {
41     base::FilePath pdf_plugin_src;
42     PathService::Get(base::DIR_SOURCE_ROOT, &pdf_plugin_src);
43     pdf_plugin_src = pdf_plugin_src.AppendASCII("pdf");
44     if (requiresPlugin && !base::DirectoryExists(pdf_plugin_src)) {
45       LOG(WARNING) << "Not running " << filename <<
46           " because it requires the PDF plugin which is not available.";
47       return;
48     }
49     ExtensionService* service = extensions::ExtensionSystem::Get(
50         profile())->extension_service();
51     service->component_loader()->Add(IDR_PDF_MANIFEST,
52         base::FilePath(FILE_PATH_LITERAL("pdf")));
53     const extensions::Extension* extension =
54         service->extensions()->GetByID("mhjfbmdgcfjbbpaeojofohoefgiehjai");
55     ASSERT_TRUE(extension);
56     ASSERT_TRUE(MimeTypesHandler::GetHandler(
57         extension)->CanHandleMIMEType("application/pdf"));
58
59     extensions::ResultCatcher catcher;
60
61     GURL url(embedded_test_server()->GetURL("/pdf/test.pdf"));
62     GURL extension_url(
63         "chrome-extension://mhjfbmdgcfjbbpaeojofohoefgiehjai/index.html?" +
64         url.spec());
65     ui_test_utils::NavigateToURL(browser(), extension_url);
66     content::WebContents* contents =
67         browser()->tab_strip_model()->GetActiveWebContents();
68     content::WaitForLoadStop(contents);
69
70     base::FilePath test_data_dir;
71     PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir);
72     test_data_dir = test_data_dir.Append(
73         FILE_PATH_LITERAL("chrome/test/data/pdf"));
74     test_data_dir = test_data_dir.AppendASCII(filename);
75
76     std::string test_js;
77     ASSERT_TRUE(base::ReadFileToString(test_data_dir, &test_js));
78     ASSERT_TRUE(content::ExecuteScript(contents, test_js));
79
80     if (!catcher.GetNextResult())
81       FAIL() << catcher.message();
82   }
83 };
84
85 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, Basic) {
86   RunTestsInFile("basic_test.js", false);
87 }
88
89 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, BasicPlugin) {
90   RunTestsInFile("basic_plugin_test.js", true);
91 }
92
93 IN_PROC_BROWSER_TEST_F(PDFExtensionTest, Viewport) {
94   RunTestsInFile("viewport_test.js", false);
95 }