OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/files/file_path.h" | 5 #include "base/files/file_path.h" |
6 #include "base/files/file_util.h" | 6 #include "base/files/file_util.h" |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/threading/thread_restrictions.h" |
9 #include "base/values.h" | 10 #include "base/values.h" |
10 #include "build/build_config.h" | 11 #include "build/build_config.h" |
11 #include "chrome/browser/extensions/extension_apitest.h" | 12 #include "chrome/browser/extensions/extension_apitest.h" |
12 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
14 #include "chrome/common/chrome_paths.h" | 15 #include "chrome/common/chrome_paths.h" |
15 #include "chrome/test/base/ui_test_utils.h" | 16 #include "chrome/test/base/ui_test_utils.h" |
16 #include "content/public/browser/render_frame_host.h" | 17 #include "content/public/browser/render_frame_host.h" |
17 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
18 #include "content/public/test/browser_test_utils.h" | 19 #include "content/public/test/browser_test_utils.h" |
19 #include "extensions/browser/event_router.h" | 20 #include "extensions/browser/event_router.h" |
20 #include "extensions/common/api/test.h" | 21 #include "extensions/common/api/test.h" |
21 #include "extensions/common/extension.h" | 22 #include "extensions/common/extension.h" |
22 #include "extensions/test/extension_test_message_listener.h" | 23 #include "extensions/test/extension_test_message_listener.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
24 | 25 |
25 namespace extensions { | 26 namespace extensions { |
26 | 27 |
27 namespace OnMessage = api::test::OnMessage; | 28 namespace OnMessage = api::test::OnMessage; |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
31 // Tests running extension APIs on WebUI. | 32 // Tests running extension APIs on WebUI. |
32 class ExtensionWebUITest : public ExtensionApiTest { | 33 class ExtensionWebUITest : public ExtensionApiTest { |
33 protected: | 34 protected: |
34 testing::AssertionResult RunTest(const char* name, | 35 testing::AssertionResult RunTest(const char* name, |
35 const GURL& page_url, | 36 const GURL& page_url, |
36 const GURL& frame_url, | 37 const GURL& frame_url, |
37 bool expected_result) { | 38 bool expected_result) { |
38 // Tests are located in chrome/test/data/extensions/webui/$(name). | 39 std::string script; |
39 base::FilePath path; | 40 { |
40 PathService::Get(chrome::DIR_TEST_DATA, &path); | 41 base::ThreadRestrictions::ScopedAllowIO allow_io; |
41 path = | 42 // Tests are located in chrome/test/data/extensions/webui/$(name). |
42 path.AppendASCII("extensions").AppendASCII("webui").AppendASCII(name); | 43 base::FilePath path; |
| 44 PathService::Get(chrome::DIR_TEST_DATA, &path); |
| 45 path = |
| 46 path.AppendASCII("extensions").AppendASCII("webui").AppendASCII(name); |
43 | 47 |
44 // Read the test. | 48 // Read the test. |
45 if (!base::PathExists(path)) | 49 if (!base::PathExists(path)) |
46 return testing::AssertionFailure() << "Couldn't find " << path.value(); | 50 return testing::AssertionFailure() << "Couldn't find " << path.value(); |
47 std::string script; | 51 base::ReadFileToString(path, &script); |
48 base::ReadFileToString(path, &script); | 52 script = "(function(){'use strict';" + script + "}());"; |
49 script = "(function(){'use strict';" + script + "}());"; | 53 } |
50 | 54 |
51 // Run the test. | 55 // Run the test. |
52 bool actual_result = false; | 56 bool actual_result = false; |
53 content::RenderFrameHost* webui = NavigateToWebUI(page_url, frame_url); | 57 content::RenderFrameHost* webui = NavigateToWebUI(page_url, frame_url); |
54 if (!webui) | 58 if (!webui) |
55 return testing::AssertionFailure() << "Failed to navigate to WebUI"; | 59 return testing::AssertionFailure() << "Failed to navigate to WebUI"; |
56 CHECK(content::ExecuteScriptAndExtractBool(webui, script, &actual_result)); | 60 CHECK(content::ExecuteScriptAndExtractBool(webui, script, &actual_result)); |
57 return (expected_result == actual_result) | 61 return (expected_result == actual_result) |
58 ? testing::AssertionSuccess() | 62 ? testing::AssertionSuccess() |
59 : (testing::AssertionFailure() << "Check console output"); | 63 : (testing::AssertionFailure() << "Check console output"); |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 listener->Reply(extension_id); | 248 listener->Reply(extension_id); |
245 listener.reset(new ExtensionTestMessageListener("createfailed", false)); | 249 listener.reset(new ExtensionTestMessageListener("createfailed", false)); |
246 ASSERT_TRUE(listener->WaitUntilSatisfied()); | 250 ASSERT_TRUE(listener->WaitUntilSatisfied()); |
247 } | 251 } |
248 | 252 |
249 #endif | 253 #endif |
250 | 254 |
251 } // namespace | 255 } // namespace |
252 | 256 |
253 } // namespace extensions | 257 } // namespace extensions |
OLD | NEW |