OLD | NEW |
(Empty) | |
| 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/file_util.h" |
| 6 #include "base/files/file_path.h" |
| 7 #include "base/path_service.h" |
| 8 #include "base/values.h" |
| 9 #include "chrome/browser/extensions/extension_apitest.h" |
| 10 #include "chrome/browser/extensions/extension_test_message_listener.h" |
| 11 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 13 #include "chrome/common/chrome_paths.h" |
| 14 #include "chrome/test/base/ui_test_utils.h" |
| 15 #include "content/public/browser/render_frame_host.h" |
| 16 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/test/browser_test_utils.h" |
| 18 #include "extensions/browser/event_router.h" |
| 19 #include "extensions/common/api/test.h" |
| 20 #include "extensions/common/extension.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 |
| 23 namespace extensions { |
| 24 |
| 25 namespace OnMessage = core_api::test::OnMessage; |
| 26 |
| 27 namespace { |
| 28 |
| 29 void FindFrame(const GURL& url, |
| 30 content::RenderFrameHost** out, |
| 31 content::RenderFrameHost* frame) { |
| 32 if (frame->GetLastCommittedURL() == url) { |
| 33 if (*out != NULL) { |
| 34 ADD_FAILURE() << "Found multiple frames at " << url; |
| 35 } |
| 36 *out = frame; |
| 37 } |
| 38 } |
| 39 |
| 40 // Tests running extension APIs on WebUI. |
| 41 class ExtensionWebUITest : public ExtensionApiTest { |
| 42 protected: |
| 43 testing::AssertionResult RunTest(const char* name) { |
| 44 // Tests are located in chrome/test/data/extensions/webui/$(name). |
| 45 base::FilePath path; |
| 46 PathService::Get(chrome::DIR_TEST_DATA, &path); |
| 47 path = |
| 48 path.AppendASCII("extensions").AppendASCII("webui").AppendASCII(name); |
| 49 |
| 50 // Read the test. |
| 51 if (!base::PathExists(path)) |
| 52 return testing::AssertionFailure() << "Couldn't find " << path.value(); |
| 53 std::string script; |
| 54 base::ReadFileToString(path, &script); |
| 55 script = "(function(){'use strict';" + script + "}());"; |
| 56 |
| 57 // Run the test. |
| 58 bool result = false; |
| 59 content::RenderFrameHost* webui = NavigateToWebUI(); |
| 60 if (!webui) |
| 61 return testing::AssertionFailure() << "Failed to navigate to WebUI"; |
| 62 CHECK(content::ExecuteScriptAndExtractBool(webui, script, &result)); |
| 63 return result ? testing::AssertionSuccess() |
| 64 : (testing::AssertionFailure() << "Check console output"); |
| 65 } |
| 66 |
| 67 private: |
| 68 // Navigates the browser to a WebUI page and returns the RenderFrameHost for |
| 69 // that page. |
| 70 content::RenderFrameHost* NavigateToWebUI() { |
| 71 // Use the chrome://extensions page, cos, why not. |
| 72 ui_test_utils::NavigateToURL(browser(), GURL("chrome://extensions/")); |
| 73 |
| 74 // In the current design the URL of the chrome://extensions page it's |
| 75 // actually chrome://extensions-frame/ -- and it's important we find it, |
| 76 // because the top-level frame doesn't execute any code, so a script |
| 77 // context is never created, so the bindings are never set up, and |
| 78 // apparently the call to ExecuteScriptAndExtractString doesn't adequately |
| 79 // set them up either. |
| 80 content::RenderFrameHost* frame_host = NULL; |
| 81 browser()->tab_strip_model()->GetActiveWebContents()->ForEachFrame( |
| 82 base::Bind( |
| 83 &FindFrame, GURL("chrome://extensions-frame/"), &frame_host)); |
| 84 return frame_host; |
| 85 } |
| 86 }; |
| 87 |
| 88 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, SanityCheckAvailableAPIs) { |
| 89 ASSERT_TRUE(RunTest("sanity_check_available_apis.js")); |
| 90 } |
| 91 |
| 92 // Tests chrome.test.sendMessage, which exercises WebUI making a |
| 93 // function call and receiving a response. |
| 94 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, SendMessage) { |
| 95 scoped_ptr<ExtensionTestMessageListener> listener( |
| 96 new ExtensionTestMessageListener("ping", true)); |
| 97 |
| 98 ASSERT_TRUE(RunTest("send_message.js")); |
| 99 |
| 100 ASSERT_TRUE(listener->WaitUntilSatisfied()); |
| 101 listener->Reply("pong"); |
| 102 |
| 103 listener.reset(new ExtensionTestMessageListener(false)); |
| 104 ASSERT_TRUE(listener->WaitUntilSatisfied()); |
| 105 EXPECT_EQ("true", listener->message()); |
| 106 } |
| 107 |
| 108 // Tests chrome.runtime.onMessage, which exercises WebUI registering and |
| 109 // receiving an event. |
| 110 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, OnMessage) { |
| 111 ASSERT_TRUE(RunTest("on_message.js")); |
| 112 |
| 113 OnMessage::Info info; |
| 114 info.data = "hi"; |
| 115 info.last_message = true; |
| 116 EventRouter::Get(profile())->BroadcastEvent(make_scoped_ptr( |
| 117 new Event(OnMessage::kEventName, OnMessage::Create(info)))); |
| 118 |
| 119 scoped_ptr<ExtensionTestMessageListener> listener( |
| 120 new ExtensionTestMessageListener(false)); |
| 121 ASSERT_TRUE(listener->WaitUntilSatisfied()); |
| 122 EXPECT_EQ("true", listener->message()); |
| 123 } |
| 124 |
| 125 // Tests chrome.runtime.lastError, which exercises WebUI accessing a property |
| 126 // on an API which it doesn't actually have access to. A bindings test really. |
| 127 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, RuntimeLastError) { |
| 128 scoped_ptr<ExtensionTestMessageListener> listener( |
| 129 new ExtensionTestMessageListener("ping", true)); |
| 130 |
| 131 ASSERT_TRUE(RunTest("runtime_last_error.js")); |
| 132 |
| 133 ASSERT_TRUE(listener->WaitUntilSatisfied()); |
| 134 listener->ReplyWithError("unknown host"); |
| 135 |
| 136 listener.reset(new ExtensionTestMessageListener(false)); |
| 137 ASSERT_TRUE(listener->WaitUntilSatisfied()); |
| 138 EXPECT_EQ("true", listener->message()); |
| 139 } |
| 140 |
| 141 } // namespace |
| 142 |
| 143 } // namespace extensions |
OLD | NEW |