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