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