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/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/extensions/extension_apitest.h" | 9 #include "chrome/browser/extensions/extension_apitest.h" |
10 #include "chrome/browser/extensions/extension_test_message_listener.h" | 10 #include "chrome/browser/extensions/extension_test_message_listener.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 if (*out != NULL) { | 33 if (*out != NULL) { |
34 ADD_FAILURE() << "Found multiple frames at " << url; | 34 ADD_FAILURE() << "Found multiple frames at " << url; |
35 } | 35 } |
36 *out = frame; | 36 *out = frame; |
37 } | 37 } |
38 } | 38 } |
39 | 39 |
40 // Tests running extension APIs on WebUI. | 40 // Tests running extension APIs on WebUI. |
41 class ExtensionWebUITest : public ExtensionApiTest { | 41 class ExtensionWebUITest : public ExtensionApiTest { |
42 protected: | 42 protected: |
43 testing::AssertionResult RunTest(const char* name) { | 43 testing::AssertionResult RunTest(const char* name, |
| 44 const GURL& page_url, |
| 45 const GURL& frame_url, |
| 46 bool expected_result) { |
44 // Tests are located in chrome/test/data/extensions/webui/$(name). | 47 // Tests are located in chrome/test/data/extensions/webui/$(name). |
45 base::FilePath path; | 48 base::FilePath path; |
46 PathService::Get(chrome::DIR_TEST_DATA, &path); | 49 PathService::Get(chrome::DIR_TEST_DATA, &path); |
47 path = | 50 path = |
48 path.AppendASCII("extensions").AppendASCII("webui").AppendASCII(name); | 51 path.AppendASCII("extensions").AppendASCII("webui").AppendASCII(name); |
49 | 52 |
50 // Read the test. | 53 // Read the test. |
51 if (!base::PathExists(path)) | 54 if (!base::PathExists(path)) |
52 return testing::AssertionFailure() << "Couldn't find " << path.value(); | 55 return testing::AssertionFailure() << "Couldn't find " << path.value(); |
53 std::string script; | 56 std::string script; |
54 base::ReadFileToString(path, &script); | 57 base::ReadFileToString(path, &script); |
55 script = "(function(){'use strict';" + script + "}());"; | 58 script = "(function(){'use strict';" + script + "}());"; |
56 | 59 |
57 // Run the test. | 60 // Run the test. |
58 bool result = false; | 61 bool actual_result = false; |
59 content::RenderFrameHost* webui = NavigateToWebUI(); | 62 content::RenderFrameHost* webui = NavigateToWebUI(page_url, frame_url); |
60 if (!webui) | 63 if (!webui) |
61 return testing::AssertionFailure() << "Failed to navigate to WebUI"; | 64 return testing::AssertionFailure() << "Failed to navigate to WebUI"; |
62 CHECK(content::ExecuteScriptAndExtractBool(webui, script, &result)); | 65 CHECK(content::ExecuteScriptAndExtractBool(webui, script, &actual_result)); |
63 return result ? testing::AssertionSuccess() | 66 return (expected_result == actual_result) |
64 : (testing::AssertionFailure() << "Check console output"); | 67 ? testing::AssertionSuccess() |
| 68 : (testing::AssertionFailure() << "Check console output"); |
65 } | 69 } |
66 | 70 |
67 private: | 71 testing::AssertionResult RunTestOnExtensions(const char* name, |
68 // Navigates the browser to a WebUI page and returns the RenderFrameHost for | 72 bool expected_result) { |
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 | 73 // 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, | 74 // 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 | 75 // 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 | 76 // context is never created, so the bindings are never set up, and |
78 // apparently the call to ExecuteScriptAndExtractString doesn't adequately | 77 // apparently the call to ExecuteScriptAndExtractString doesn't adequately |
79 // set them up either. | 78 // set them up either. |
| 79 return RunTest(name, |
| 80 GURL("chrome://extensions"), |
| 81 GURL("chrome://extensions-frame"), |
| 82 expected_result); |
| 83 } |
| 84 |
| 85 testing::AssertionResult RunTestOnAbout(const char* name, |
| 86 bool expected_result) { |
| 87 // chrome://about is an innocuous page that doesn't have any bindings. |
| 88 return RunTest( |
| 89 name, GURL("chrome://about"), GURL("chrome://about"), expected_result); |
| 90 } |
| 91 |
| 92 private: |
| 93 // Navigates the browser to a WebUI page and returns the RenderFrameHost for |
| 94 // that page. |
| 95 content::RenderFrameHost* NavigateToWebUI(const GURL& page_url, |
| 96 const GURL& frame_url) { |
| 97 ui_test_utils::NavigateToURL(browser(), page_url); |
| 98 |
| 99 content::WebContents* active_web_contents = |
| 100 browser()->tab_strip_model()->GetActiveWebContents(); |
| 101 |
| 102 if (active_web_contents->GetLastCommittedURL() == frame_url) |
| 103 return active_web_contents->GetMainFrame(); |
| 104 |
80 content::RenderFrameHost* frame_host = NULL; | 105 content::RenderFrameHost* frame_host = NULL; |
81 browser()->tab_strip_model()->GetActiveWebContents()->ForEachFrame( | 106 active_web_contents->ForEachFrame( |
82 base::Bind( | 107 base::Bind(&FindFrame, frame_url, &frame_host)); |
83 &FindFrame, GURL("chrome://extensions-frame/"), &frame_host)); | |
84 return frame_host; | 108 return frame_host; |
85 } | 109 } |
86 }; | 110 }; |
87 | 111 |
88 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, SanityCheckAvailableAPIs) { | 112 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, SanityCheckAvailableAPIs) { |
89 ASSERT_TRUE(RunTest("sanity_check_available_apis.js")); | 113 ASSERT_TRUE(RunTestOnExtensions("sanity_check_available_apis.js", true)); |
| 114 } |
| 115 |
| 116 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, SanityCheckUnavailableAPIs) { |
| 117 ASSERT_TRUE(RunTestOnAbout("sanity_check_available_apis.js", false)); |
90 } | 118 } |
91 | 119 |
92 // Tests chrome.test.sendMessage, which exercises WebUI making a | 120 // Tests chrome.test.sendMessage, which exercises WebUI making a |
93 // function call and receiving a response. | 121 // function call and receiving a response. |
94 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, SendMessage) { | 122 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, SendMessage) { |
95 scoped_ptr<ExtensionTestMessageListener> listener( | 123 scoped_ptr<ExtensionTestMessageListener> listener( |
96 new ExtensionTestMessageListener("ping", true)); | 124 new ExtensionTestMessageListener("ping", true)); |
97 | 125 |
98 ASSERT_TRUE(RunTest("send_message.js")); | 126 ASSERT_TRUE(RunTestOnExtensions("send_message.js", true)); |
99 | 127 |
100 ASSERT_TRUE(listener->WaitUntilSatisfied()); | 128 ASSERT_TRUE(listener->WaitUntilSatisfied()); |
101 listener->Reply("pong"); | 129 listener->Reply("pong"); |
102 | 130 |
103 listener.reset(new ExtensionTestMessageListener(false)); | 131 listener.reset(new ExtensionTestMessageListener(false)); |
104 ASSERT_TRUE(listener->WaitUntilSatisfied()); | 132 ASSERT_TRUE(listener->WaitUntilSatisfied()); |
105 EXPECT_EQ("true", listener->message()); | 133 EXPECT_EQ("true", listener->message()); |
106 } | 134 } |
107 | 135 |
108 // Tests chrome.runtime.onMessage, which exercises WebUI registering and | 136 // Tests chrome.runtime.onMessage, which exercises WebUI registering and |
109 // receiving an event. | 137 // receiving an event. |
110 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, OnMessage) { | 138 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, OnMessage) { |
111 ASSERT_TRUE(RunTest("on_message.js")); | 139 ASSERT_TRUE(RunTestOnExtensions("on_message.js", true)); |
112 | 140 |
113 OnMessage::Info info; | 141 OnMessage::Info info; |
114 info.data = "hi"; | 142 info.data = "hi"; |
115 info.last_message = true; | 143 info.last_message = true; |
116 EventRouter::Get(profile())->BroadcastEvent(make_scoped_ptr( | 144 EventRouter::Get(profile())->BroadcastEvent(make_scoped_ptr( |
117 new Event(OnMessage::kEventName, OnMessage::Create(info)))); | 145 new Event(OnMessage::kEventName, OnMessage::Create(info)))); |
118 | 146 |
119 scoped_ptr<ExtensionTestMessageListener> listener( | 147 scoped_ptr<ExtensionTestMessageListener> listener( |
120 new ExtensionTestMessageListener(false)); | 148 new ExtensionTestMessageListener(false)); |
121 ASSERT_TRUE(listener->WaitUntilSatisfied()); | 149 ASSERT_TRUE(listener->WaitUntilSatisfied()); |
122 EXPECT_EQ("true", listener->message()); | 150 EXPECT_EQ("true", listener->message()); |
123 } | 151 } |
124 | 152 |
125 // Tests chrome.runtime.lastError, which exercises WebUI accessing a property | 153 // 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. | 154 // on an API which it doesn't actually have access to. A bindings test really. |
127 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, RuntimeLastError) { | 155 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, RuntimeLastError) { |
128 scoped_ptr<ExtensionTestMessageListener> listener( | 156 scoped_ptr<ExtensionTestMessageListener> listener( |
129 new ExtensionTestMessageListener("ping", true)); | 157 new ExtensionTestMessageListener("ping", true)); |
130 | 158 |
131 ASSERT_TRUE(RunTest("runtime_last_error.js")); | 159 ASSERT_TRUE(RunTestOnExtensions("runtime_last_error.js", true)); |
132 | 160 |
133 ASSERT_TRUE(listener->WaitUntilSatisfied()); | 161 ASSERT_TRUE(listener->WaitUntilSatisfied()); |
134 listener->ReplyWithError("unknown host"); | 162 listener->ReplyWithError("unknown host"); |
135 | 163 |
136 listener.reset(new ExtensionTestMessageListener(false)); | 164 listener.reset(new ExtensionTestMessageListener(false)); |
137 ASSERT_TRUE(listener->WaitUntilSatisfied()); | 165 ASSERT_TRUE(listener->WaitUntilSatisfied()); |
138 EXPECT_EQ("true", listener->message()); | 166 EXPECT_EQ("true", listener->message()); |
139 } | 167 } |
140 | 168 |
141 } // namespace | 169 } // namespace |
142 | 170 |
143 } // namespace extensions | 171 } // namespace extensions |
OLD | NEW |