| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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/utf_string_conversions.h" | |
| 6 #include "base/time.h" | |
| 7 #include "base/values.h" | |
| 8 #include "chrome/browser/content_settings/host_content_settings_map.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "chrome/browser/renderer_host/render_view_host.h" | |
| 11 #include "chrome/browser/tab_contents/tab_contents.h" | |
| 12 #include "chrome/browser/tab_contents/tab_contents_observer.h" | |
| 13 #include "chrome/browser/tab_contents/tab_specific_content_settings.h" | |
| 14 #include "chrome/browser/ui/browser.h" | |
| 15 #include "chrome/common/render_messages_params.h" | |
| 16 #include "chrome/test/in_process_browser_test.h" | |
| 17 #include "chrome/test/ui_test_utils.h" | |
| 18 #include "net/base/host_port_pair.h" | |
| 19 #include "net/test/test_server.h" | |
| 20 | |
| 21 typedef std::pair<int, Value*> ExecuteDetailType; | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // NotificationObserver used to listen for EXECUTE_JAVASCRIPT_RESULT | |
| 26 // notifications. | |
| 27 class ExecuteNotificationObserver : public NotificationObserver { | |
| 28 public: | |
| 29 ExecuteNotificationObserver() : id_(0) {} | |
| 30 | |
| 31 virtual void Observe(NotificationType type, | |
| 32 const NotificationSource& source, | |
| 33 const NotificationDetails& details) { | |
| 34 id_ = (static_cast<Details<ExecuteDetailType > >(details))->first; | |
| 35 Value* value = (static_cast<Details<ExecuteDetailType > >(details))->second; | |
| 36 if (value) | |
| 37 value_.reset(value->DeepCopy()); | |
| 38 MessageLoopForUI::current()->Quit(); | |
| 39 } | |
| 40 | |
| 41 int id() const { return id_; } | |
| 42 | |
| 43 Value* value() const { return value_.get(); } | |
| 44 | |
| 45 private: | |
| 46 int id_; | |
| 47 scoped_ptr<Value> value_; | |
| 48 | |
| 49 DISALLOW_COPY_AND_ASSIGN(ExecuteNotificationObserver); | |
| 50 }; | |
| 51 | |
| 52 } // namespace | |
| 53 | |
| 54 class RenderViewHostTest : public InProcessBrowserTest { | |
| 55 public: | |
| 56 RenderViewHostTest() : last_execute_id_(0) {} | |
| 57 | |
| 58 void ExecuteJavascriptAndGetValue(const char* script, | |
| 59 ExecuteNotificationObserver* out_result) { | |
| 60 RenderViewHost* rvh = | |
| 61 browser()->GetSelectedTabContents()->render_view_host(); | |
| 62 ASSERT_TRUE(rvh); | |
| 63 int execute_id = rvh->ExecuteJavascriptInWebFrameNotifyResult( | |
| 64 string16(), | |
| 65 ASCIIToUTF16(script)); | |
| 66 EXPECT_NE(execute_id, last_execute_id_); | |
| 67 ExecuteNotificationObserver observer; | |
| 68 ui_test_utils::RegisterAndWait( | |
| 69 out_result, | |
| 70 NotificationType::EXECUTE_JAVASCRIPT_RESULT, | |
| 71 Source<RenderViewHost>(rvh)); | |
| 72 EXPECT_EQ(execute_id, out_result->id()); | |
| 73 ASSERT_TRUE(out_result->value()); | |
| 74 last_execute_id_ = execute_id; | |
| 75 } | |
| 76 | |
| 77 private: | |
| 78 int last_execute_id_; | |
| 79 }; | |
| 80 | |
| 81 | |
| 82 // Makes sure ExecuteJavascriptInWebFrameNotifyResult works. | |
| 83 IN_PROC_BROWSER_TEST_F(RenderViewHostTest, | |
| 84 ExecuteJavascriptInWebFrameNotifyResult) { | |
| 85 ASSERT_TRUE(test_server()->Start()); | |
| 86 GURL empty_url(test_server()->GetURL("files/empty.html")); | |
| 87 ui_test_utils::NavigateToURL(browser(), empty_url); | |
| 88 | |
| 89 // Execute the script 'true' and make sure we get back true. | |
| 90 { | |
| 91 ExecuteNotificationObserver observer; | |
| 92 ExecuteJavascriptAndGetValue("true;", &observer); | |
| 93 EXPECT_EQ(Value::TYPE_BOOLEAN, observer.value()->GetType()); | |
| 94 bool bool_value; | |
| 95 EXPECT_TRUE(observer.value()->GetAsBoolean(&bool_value)); | |
| 96 EXPECT_TRUE(bool_value); | |
| 97 } | |
| 98 | |
| 99 // Execute the script 'false' and make sure we get back false. | |
| 100 { | |
| 101 ExecuteNotificationObserver observer; | |
| 102 ExecuteJavascriptAndGetValue("false;", &observer); | |
| 103 EXPECT_EQ(Value::TYPE_BOOLEAN, observer.value()->GetType()); | |
| 104 bool bool_value; | |
| 105 EXPECT_TRUE(observer.value()->GetAsBoolean(&bool_value)); | |
| 106 EXPECT_FALSE(bool_value); | |
| 107 } | |
| 108 | |
| 109 // And now, for something completely different, try a number. | |
| 110 { | |
| 111 ExecuteNotificationObserver observer; | |
| 112 ExecuteJavascriptAndGetValue("42;", &observer); | |
| 113 EXPECT_EQ(Value::TYPE_INTEGER, observer.value()->GetType()); | |
| 114 int int_value; | |
| 115 EXPECT_TRUE(observer.value()->GetAsInteger(&int_value)); | |
| 116 EXPECT_EQ(42, int_value); | |
| 117 } | |
| 118 | |
| 119 // Try a floating point number. | |
| 120 { | |
| 121 ExecuteNotificationObserver observer; | |
| 122 ExecuteJavascriptAndGetValue("42.2;", &observer); | |
| 123 EXPECT_EQ(Value::TYPE_DOUBLE, observer.value()->GetType()); | |
| 124 double double_value; | |
| 125 EXPECT_TRUE(observer.value()->GetAsDouble(&double_value)); | |
| 126 EXPECT_EQ(42.2, double_value); | |
| 127 } | |
| 128 | |
| 129 // Let's check out string. | |
| 130 { | |
| 131 ExecuteNotificationObserver observer; | |
| 132 ExecuteJavascriptAndGetValue("\"something completely different\";", | |
| 133 &observer); | |
| 134 EXPECT_EQ(Value::TYPE_STRING, observer.value()->GetType()); | |
| 135 std::string string_value; | |
| 136 EXPECT_TRUE(observer.value()->GetAsString(&string_value)); | |
| 137 EXPECT_EQ(std::string("something completely different"), string_value); | |
| 138 } | |
| 139 | |
| 140 // Regular expressions might be fun. | |
| 141 { | |
| 142 ExecuteNotificationObserver observer; | |
| 143 ExecuteJavascriptAndGetValue("/finder.*foo/g;", &observer); | |
| 144 EXPECT_EQ(Value::TYPE_STRING, observer.value()->GetType()); | |
| 145 std::string string_value; | |
| 146 EXPECT_TRUE(observer.value()->GetAsString(&string_value)); | |
| 147 EXPECT_EQ(std::string("/finder.*foo/g"), string_value); | |
| 148 } | |
| 149 | |
| 150 // Let's test some date conversions. First up, epoch. Can't use 0 because | |
| 151 // that means uninitialized, so use the next best thing. | |
| 152 { | |
| 153 ExecuteNotificationObserver observer; | |
| 154 ExecuteJavascriptAndGetValue("new Date(1);", &observer); | |
| 155 EXPECT_EQ(Value::TYPE_DOUBLE, observer.value()->GetType()); | |
| 156 double date_seconds; | |
| 157 EXPECT_TRUE(observer.value()->GetAsDouble(&date_seconds)); | |
| 158 | |
| 159 base::Time time = base::Time::FromDoubleT(date_seconds); | |
| 160 | |
| 161 base::Time::Exploded time_exploded; | |
| 162 time.UTCExplode(&time_exploded); | |
| 163 EXPECT_EQ(1970, time_exploded.year); | |
| 164 EXPECT_EQ(1, time_exploded.month); | |
| 165 EXPECT_EQ(1, time_exploded.day_of_month); | |
| 166 EXPECT_EQ(0, time_exploded.hour); | |
| 167 EXPECT_EQ(0, time_exploded.minute); | |
| 168 EXPECT_EQ(0, time_exploded.second); | |
| 169 } | |
| 170 | |
| 171 // Test date with a real date input. | |
| 172 { | |
| 173 ExecuteNotificationObserver observer; | |
| 174 ExecuteJavascriptAndGetValue("new Date(Date.UTC(2006, 7, 16, 12, 0, 15));", | |
| 175 &observer); | |
| 176 EXPECT_EQ(Value::TYPE_DOUBLE, observer.value()->GetType()); | |
| 177 double date_seconds; | |
| 178 EXPECT_TRUE(observer.value()->GetAsDouble(&date_seconds)); | |
| 179 | |
| 180 base::Time time = base::Time::FromDoubleT(date_seconds); | |
| 181 | |
| 182 base::Time::Exploded time_exploded; | |
| 183 time.UTCExplode(&time_exploded); | |
| 184 EXPECT_EQ(2006, time_exploded.year); | |
| 185 // Subtle; 0 based in JS, 1 based in base::Time: | |
| 186 EXPECT_EQ(8, time_exploded.month); | |
| 187 EXPECT_EQ(16, time_exploded.day_of_month); | |
| 188 EXPECT_EQ(12, time_exploded.hour); | |
| 189 EXPECT_EQ(0, time_exploded.minute); | |
| 190 EXPECT_EQ(15, time_exploded.second); | |
| 191 } | |
| 192 | |
| 193 // And something more complicated - get an array back as a list. | |
| 194 { | |
| 195 ExecuteNotificationObserver observer; | |
| 196 ExecuteJavascriptAndGetValue("new Array(\"one\", 2, false);", &observer); | |
| 197 EXPECT_EQ(Value::TYPE_LIST, observer.value()->GetType()); | |
| 198 ListValue* list_value; | |
| 199 EXPECT_TRUE(observer.value()->GetAsList(&list_value)); | |
| 200 EXPECT_EQ(3U, list_value->GetSize()); | |
| 201 Value* value; | |
| 202 EXPECT_TRUE(list_value->Get(0, &value)); | |
| 203 EXPECT_EQ(Value::TYPE_STRING, value->GetType()); | |
| 204 EXPECT_TRUE(list_value->Get(1, &value)); | |
| 205 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType()); | |
| 206 EXPECT_TRUE(list_value->Get(2, &value)); | |
| 207 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType()); | |
| 208 } | |
| 209 } | |
| 210 | |
| 211 // Regression test for http://crbug.com/63649. | |
| 212 IN_PROC_BROWSER_TEST_F(RenderViewHostTest, RedirectLoopCookies) { | |
| 213 ASSERT_TRUE(test_server()->Start()); | |
| 214 | |
| 215 GURL test_url = test_server()->GetURL("files/redirect-loop.html"); | |
| 216 | |
| 217 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting( | |
| 218 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); | |
| 219 | |
| 220 ui_test_utils::NavigateToURL(browser(), test_url); | |
| 221 | |
| 222 TabContents* tab_contents = browser()->GetSelectedTabContents(); | |
| 223 ASSERT_EQ(UTF8ToUTF16(test_url.spec() + " failed to load"), | |
| 224 tab_contents->GetTitle()); | |
| 225 | |
| 226 EXPECT_TRUE(tab_contents->GetTabSpecificContentSettings()->IsContentBlocked( | |
| 227 CONTENT_SETTINGS_TYPE_COOKIES)); | |
| 228 } | |
| 229 | |
| 230 class RenderViewHostTestTabContentsObserver : public TabContentsObserver { | |
| 231 public: | |
| 232 explicit RenderViewHostTestTabContentsObserver(TabContents* tab_contents) | |
| 233 : TabContentsObserver(tab_contents), | |
| 234 navigation_count_(0) {} | |
| 235 virtual ~RenderViewHostTestTabContentsObserver() {} | |
| 236 | |
| 237 virtual void DidNavigateMainFramePostCommit( | |
| 238 const NavigationController::LoadCommittedDetails& details, | |
| 239 const ViewHostMsg_FrameNavigate_Params& params) { | |
| 240 observed_socket_address_ = params.socket_address; | |
| 241 ++navigation_count_; | |
| 242 } | |
| 243 | |
| 244 const net::HostPortPair& observed_socket_address() const { | |
| 245 return observed_socket_address_; | |
| 246 } | |
| 247 | |
| 248 int navigation_count() const { return navigation_count_; } | |
| 249 | |
| 250 private: | |
| 251 net::HostPortPair observed_socket_address_; | |
| 252 int navigation_count_; | |
| 253 | |
| 254 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestTabContentsObserver); | |
| 255 }; | |
| 256 | |
| 257 IN_PROC_BROWSER_TEST_F(RenderViewHostTest, FrameNavigateSocketAddress) { | |
| 258 ASSERT_TRUE(test_server()->Start()); | |
| 259 RenderViewHostTestTabContentsObserver observer( | |
| 260 browser()->GetSelectedTabContents()); | |
| 261 | |
| 262 GURL test_url = test_server()->GetURL("files/simple.html"); | |
| 263 ui_test_utils::NavigateToURL(browser(), test_url); | |
| 264 | |
| 265 EXPECT_EQ(test_server()->host_port_pair().ToString(), | |
| 266 observer.observed_socket_address().ToString()); | |
| 267 EXPECT_EQ(1, observer.navigation_count()); | |
| 268 } | |
| OLD | NEW |