Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: chrome/browser/renderer_host/test/render_view_host_browsertest.cc

Issue 4924001: JavaScript to Value bridge. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix documentation. Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/renderer_host/render_view_host.cc ('k') | chrome/common/notification_type.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 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/utf_string_conversions.h" 5 #include "base/utf_string_conversions.h"
6 #include "base/time.h"
6 #include "base/values.h" 7 #include "base/values.h"
7 #include "chrome/browser/content_settings/host_content_settings_map.h" 8 #include "chrome/browser/content_settings/host_content_settings_map.h"
8 #include "chrome/browser/profiles/profile.h" 9 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/renderer_host/render_view_host.h" 10 #include "chrome/browser/renderer_host/render_view_host.h"
10 #include "chrome/browser/tab_contents/tab_contents.h" 11 #include "chrome/browser/tab_contents/tab_contents.h"
11 #include "chrome/browser/tab_contents/tab_specific_content_settings.h" 12 #include "chrome/browser/tab_contents/tab_specific_content_settings.h"
12 #include "chrome/browser/ui/browser.h" 13 #include "chrome/browser/ui/browser.h"
13 #include "chrome/test/in_process_browser_test.h" 14 #include "chrome/test/in_process_browser_test.h"
14 #include "chrome/test/ui_test_utils.h" 15 #include "chrome/test/ui_test_utils.h"
15 #include "net/test/test_server.h" 16 #include "net/test/test_server.h"
16 17
17 typedef InProcessBrowserTest RenderViewHostTest;
18
19 typedef std::pair<int, Value*> ExecuteDetailType; 18 typedef std::pair<int, Value*> ExecuteDetailType;
20 19
21 namespace { 20 namespace {
22 21
23 // NotificationObserver used to listen for EXECUTE_JAVASCRIPT_RESULT 22 // NotificationObserver used to listen for EXECUTE_JAVASCRIPT_RESULT
24 // notifications. 23 // notifications.
25 class ExecuteNotificationObserver : public NotificationObserver { 24 class ExecuteNotificationObserver : public NotificationObserver {
26 public: 25 public:
27 ExecuteNotificationObserver() : id_(0) {} 26 ExecuteNotificationObserver() : id_(0) {}
28 27
(...skipping 13 matching lines...) Expand all
42 41
43 private: 42 private:
44 int id_; 43 int id_;
45 scoped_ptr<Value> value_; 44 scoped_ptr<Value> value_;
46 45
47 DISALLOW_COPY_AND_ASSIGN(ExecuteNotificationObserver); 46 DISALLOW_COPY_AND_ASSIGN(ExecuteNotificationObserver);
48 }; 47 };
49 48
50 } // namespace 49 } // namespace
51 50
51 class RenderViewHostTest : public InProcessBrowserTest {
52 public:
53 RenderViewHostTest() : last_execute_id_(0) {}
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 NotificationType::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 };
77
78
52 // Makes sure ExecuteJavascriptInWebFrameNotifyResult works. 79 // Makes sure ExecuteJavascriptInWebFrameNotifyResult works.
53 IN_PROC_BROWSER_TEST_F(RenderViewHostTest, 80 IN_PROC_BROWSER_TEST_F(RenderViewHostTest,
54 ExecuteJavascriptInWebFrameNotifyResult) { 81 ExecuteJavascriptInWebFrameNotifyResult) {
55 ASSERT_TRUE(test_server()->Start()); 82 ASSERT_TRUE(test_server()->Start());
56 GURL empty_url(test_server()->GetURL("files/empty.html")); 83 GURL empty_url(test_server()->GetURL("files/empty.html"));
57 ui_test_utils::NavigateToURL(browser(), empty_url); 84 ui_test_utils::NavigateToURL(browser(), empty_url);
58 RenderViewHost* rvh = browser()->GetSelectedTabContents()->render_view_host();
59 ASSERT_TRUE(rvh);
60 85
61 // Execute the script 'true' and make sure we get back true. 86 // Execute the script 'true' and make sure we get back true.
62 int execute_id = rvh->ExecuteJavascriptInWebFrameNotifyResult(
63 string16(),
64 ASCIIToUTF16("true;"));
65 { 87 {
66 ExecuteNotificationObserver observer; 88 ExecuteNotificationObserver observer;
67 ui_test_utils::RegisterAndWait( 89 ExecuteJavascriptAndGetValue("true;", &observer);
68 &observer, 90 EXPECT_EQ(Value::TYPE_BOOLEAN, observer.value()->GetType());
69 NotificationType::EXECUTE_JAVASCRIPT_RESULT,
70 Source<RenderViewHost>(rvh));
71 EXPECT_EQ(execute_id, observer.id());
72 ASSERT_TRUE(observer.value());
73 bool bool_value; 91 bool bool_value;
74 ASSERT_TRUE(observer.value()->GetAsBoolean(&bool_value)); 92 EXPECT_TRUE(observer.value()->GetAsBoolean(&bool_value));
75 EXPECT_TRUE(bool_value); 93 EXPECT_TRUE(bool_value);
76 } 94 }
77 95
78 // Execute the script 'false' and make sure we get back false. 96 // Execute the script 'false' and make sure we get back false.
79 int execute_id2 = rvh->ExecuteJavascriptInWebFrameNotifyResult(
80 string16(),
81 ASCIIToUTF16("false;"));
82 // The ids should change.
83 EXPECT_NE(execute_id, execute_id2);
84 { 97 {
85 ExecuteNotificationObserver observer; 98 ExecuteNotificationObserver observer;
86 ui_test_utils::RegisterAndWait( 99 ExecuteJavascriptAndGetValue("false;", &observer);
87 &observer, 100 EXPECT_EQ(Value::TYPE_BOOLEAN, observer.value()->GetType());
88 NotificationType::EXECUTE_JAVASCRIPT_RESULT,
89 Source<RenderViewHost>(rvh));
90 EXPECT_EQ(execute_id2, observer.id());
91 ASSERT_TRUE(observer.value());
92 bool bool_value; 101 bool bool_value;
93 ASSERT_TRUE(observer.value()->GetAsBoolean(&bool_value)); 102 EXPECT_TRUE(observer.value()->GetAsBoolean(&bool_value));
94 EXPECT_FALSE(bool_value); 103 EXPECT_FALSE(bool_value);
95 } 104 }
105
106 // And now, for something completely different, try a number.
107 {
108 ExecuteNotificationObserver observer;
109 ExecuteJavascriptAndGetValue("42;", &observer);
110 EXPECT_EQ(Value::TYPE_INTEGER, observer.value()->GetType());
111 int int_value;
112 EXPECT_TRUE(observer.value()->GetAsInteger(&int_value));
113 EXPECT_EQ(42, int_value);
114 }
115
116 // Try a floating point number.
117 {
118 ExecuteNotificationObserver observer;
119 ExecuteJavascriptAndGetValue("42.2;", &observer);
120 EXPECT_EQ(Value::TYPE_REAL, observer.value()->GetType());
121 double double_value;
122 EXPECT_TRUE(observer.value()->GetAsReal(&double_value));
123 EXPECT_EQ(42.2, double_value);
124 }
125
126 // Let's check out string.
127 {
128 ExecuteNotificationObserver observer;
129 ExecuteJavascriptAndGetValue("\"something completely different\";",
130 &observer);
131 EXPECT_EQ(Value::TYPE_STRING, observer.value()->GetType());
132 std::string string_value;
133 EXPECT_TRUE(observer.value()->GetAsString(&string_value));
134 EXPECT_EQ(std::string("something completely different"), string_value);
135 }
136
137 // Regular expressions might be fun.
138 {
139 ExecuteNotificationObserver observer;
140 ExecuteJavascriptAndGetValue("/finder.*foo/g;", &observer);
141 EXPECT_EQ(Value::TYPE_STRING, observer.value()->GetType());
142 std::string string_value;
143 EXPECT_TRUE(observer.value()->GetAsString(&string_value));
144 EXPECT_EQ(std::string("/finder.*foo/g"), string_value);
145 }
146
147 // Let's test some date conversions. First up, epoch. Can't use 0 because
148 // that means uninitialized, so use the next best thing.
149 {
150 ExecuteNotificationObserver observer;
151 ExecuteJavascriptAndGetValue("new Date(1);", &observer);
152 EXPECT_EQ(Value::TYPE_REAL, observer.value()->GetType());
153 double date_seconds;
154 EXPECT_TRUE(observer.value()->GetAsReal(&date_seconds));
155
156 base::Time time = base::Time::FromDoubleT(date_seconds);
157
158 base::Time::Exploded time_exploded;
159 time.UTCExplode(&time_exploded);
160 EXPECT_EQ(1970, time_exploded.year);
161 EXPECT_EQ(1, time_exploded.month);
162 EXPECT_EQ(1, time_exploded.day_of_month);
163 EXPECT_EQ(0, time_exploded.hour);
164 EXPECT_EQ(0, time_exploded.minute);
165 EXPECT_EQ(0, time_exploded.second);
166 }
167
168 // Test date with a real date input.
169 {
170 ExecuteNotificationObserver observer;
171 ExecuteJavascriptAndGetValue("new Date(Date.UTC(2006, 7, 16, 12, 0, 15));",
172 &observer);
173 EXPECT_EQ(Value::TYPE_REAL, observer.value()->GetType());
174 double date_seconds;
175 EXPECT_TRUE(observer.value()->GetAsReal(&date_seconds));
176
177 base::Time time = base::Time::FromDoubleT(date_seconds);
178
179 base::Time::Exploded time_exploded;
180 time.UTCExplode(&time_exploded);
181 EXPECT_EQ(2006, time_exploded.year);
182 // Subtle; 0 based in JS, 1 based in base::Time:
183 EXPECT_EQ(8, time_exploded.month);
184 EXPECT_EQ(16, time_exploded.day_of_month);
185 EXPECT_EQ(12, time_exploded.hour);
186 EXPECT_EQ(0, time_exploded.minute);
187 EXPECT_EQ(15, time_exploded.second);
188 }
189
190 // And something more complicated - get an array back as a list.
191 {
192 ExecuteNotificationObserver observer;
193 ExecuteJavascriptAndGetValue("new Array(\"one\", 2, false);", &observer);
194 EXPECT_EQ(Value::TYPE_LIST, observer.value()->GetType());
195 ListValue* list_value;
196 EXPECT_TRUE(observer.value()->GetAsList(&list_value));
197 EXPECT_EQ(3U, list_value->GetSize());
198 Value* value;
199 EXPECT_TRUE(list_value->Get(0, &value));
200 EXPECT_EQ(Value::TYPE_STRING, value->GetType());
201 EXPECT_TRUE(list_value->Get(1, &value));
202 EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
203 EXPECT_TRUE(list_value->Get(2, &value));
204 EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType());
205 }
96 } 206 }
97 207
98 // Regression test for http://crbug.com/63649. 208 // Regression test for http://crbug.com/63649.
99 IN_PROC_BROWSER_TEST_F(RenderViewHostTest, RedirectLoopCookies) { 209 IN_PROC_BROWSER_TEST_F(RenderViewHostTest, RedirectLoopCookies) {
100 ASSERT_TRUE(test_server()->Start()); 210 ASSERT_TRUE(test_server()->Start());
101 211
102 GURL test_url = test_server()->GetURL("files/redirect-loop.html"); 212 GURL test_url = test_server()->GetURL("files/redirect-loop.html");
103 213
104 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting( 214 browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
105 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); 215 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK);
106 216
107 ui_test_utils::NavigateToURL(browser(), test_url); 217 ui_test_utils::NavigateToURL(browser(), test_url);
108 218
109 TabContents* tab_contents = browser()->GetSelectedTabContents(); 219 TabContents* tab_contents = browser()->GetSelectedTabContents();
110 ASSERT_EQ(UTF8ToUTF16(test_url.spec() + " failed to load"), 220 ASSERT_EQ(UTF8ToUTF16(test_url.spec() + " failed to load"),
111 tab_contents->GetTitle()); 221 tab_contents->GetTitle());
112 222
113 EXPECT_TRUE(tab_contents->GetTabSpecificContentSettings()->IsContentBlocked( 223 EXPECT_TRUE(tab_contents->GetTabSpecificContentSettings()->IsContentBlocked(
114 CONTENT_SETTINGS_TYPE_COOKIES)); 224 CONTENT_SETTINGS_TYPE_COOKIES));
115 } 225 }
OLDNEW
« no previous file with comments | « chrome/browser/renderer_host/render_view_host.cc ('k') | chrome/common/notification_type.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698