summaryrefslogtreecommitdiffstats
path: root/content/browser/renderer_host/render_view_host_browsertest.cc
blob: 5ad79275454c5e70833643504bc5aae5d1b76277 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/utf_string_conversions.h"
#include "base/time.h"
#include "base/values.h"
#include "chrome/browser/content_settings/host_content_settings_map.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/tab_contents/tab_specific_content_settings.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/in_process_browser_test.h"
#include "chrome/test/ui_test_utils.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "content/browser/tab_contents/tab_contents_observer.h"
#include "content/common/view_messages.h"
#include "net/base/host_port_pair.h"
#include "net/test/test_server.h"

typedef std::pair<int, Value*> ExecuteDetailType;

namespace {

// NotificationObserver used to listen for EXECUTE_JAVASCRIPT_RESULT
// notifications.
class ExecuteNotificationObserver : public NotificationObserver {
 public:
  ExecuteNotificationObserver() : id_(0) {}

  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details) {
    id_ = (static_cast<Details<ExecuteDetailType > >(details))->first;
    Value* value = (static_cast<Details<ExecuteDetailType > >(details))->second;
    if (value)
      value_.reset(value->DeepCopy());
    MessageLoopForUI::current()->Quit();
  }

  int id() const { return id_; }

  Value* value() const { return value_.get(); }

 private:
  int id_;
  scoped_ptr<Value> value_;

  DISALLOW_COPY_AND_ASSIGN(ExecuteNotificationObserver);
};

}  // namespace

class RenderViewHostTest : public InProcessBrowserTest {
 public:
  RenderViewHostTest() : last_execute_id_(0) {}

  void ExecuteJavascriptAndGetValue(const char* script,
                                    ExecuteNotificationObserver* out_result) {
    RenderViewHost* rvh =
        browser()->GetSelectedTabContents()->render_view_host();
    ASSERT_TRUE(rvh);
    int execute_id = rvh->ExecuteJavascriptInWebFrameNotifyResult(
        string16(),
        ASCIIToUTF16(script));
    EXPECT_NE(execute_id, last_execute_id_);
    ExecuteNotificationObserver observer;
    ui_test_utils::RegisterAndWait(
        out_result,
        NotificationType::EXECUTE_JAVASCRIPT_RESULT,
        Source<RenderViewHost>(rvh));
    EXPECT_EQ(execute_id, out_result->id());
    ASSERT_TRUE(out_result->value());
    last_execute_id_ = execute_id;
  }

 private:
  int last_execute_id_;
};


// Makes sure ExecuteJavascriptInWebFrameNotifyResult works.
IN_PROC_BROWSER_TEST_F(RenderViewHostTest,
                       ExecuteJavascriptInWebFrameNotifyResult) {
  ASSERT_TRUE(test_server()->Start());
  GURL empty_url(test_server()->GetURL("files/empty.html"));
  ui_test_utils::NavigateToURL(browser(), empty_url);

  // Execute the script 'true' and make sure we get back true.
  {
    ExecuteNotificationObserver observer;
    ExecuteJavascriptAndGetValue("true;", &observer);
    EXPECT_EQ(Value::TYPE_BOOLEAN, observer.value()->GetType());
    bool bool_value;
    EXPECT_TRUE(observer.value()->GetAsBoolean(&bool_value));
    EXPECT_TRUE(bool_value);
  }

  // Execute the script 'false' and make sure we get back false.
  {
    ExecuteNotificationObserver observer;
    ExecuteJavascriptAndGetValue("false;", &observer);
    EXPECT_EQ(Value::TYPE_BOOLEAN, observer.value()->GetType());
    bool bool_value;
    EXPECT_TRUE(observer.value()->GetAsBoolean(&bool_value));
    EXPECT_FALSE(bool_value);
  }

  // And now, for something completely different, try a number.
  {
    ExecuteNotificationObserver observer;
    ExecuteJavascriptAndGetValue("42;", &observer);
    EXPECT_EQ(Value::TYPE_INTEGER, observer.value()->GetType());
    int int_value;
    EXPECT_TRUE(observer.value()->GetAsInteger(&int_value));
    EXPECT_EQ(42, int_value);
  }

  // Try a floating point number.
  {
    ExecuteNotificationObserver observer;
    ExecuteJavascriptAndGetValue("42.2;", &observer);
    EXPECT_EQ(Value::TYPE_DOUBLE, observer.value()->GetType());
    double double_value;
    EXPECT_TRUE(observer.value()->GetAsDouble(&double_value));
    EXPECT_EQ(42.2, double_value);
  }

  // Let's check out string.
  {
    ExecuteNotificationObserver observer;
    ExecuteJavascriptAndGetValue("\"something completely different\";",
                                 &observer);
    EXPECT_EQ(Value::TYPE_STRING, observer.value()->GetType());
    std::string string_value;
    EXPECT_TRUE(observer.value()->GetAsString(&string_value));
    EXPECT_EQ(std::string("something completely different"), string_value);
  }

  // Regular expressions might be fun.
  {
    ExecuteNotificationObserver observer;
    ExecuteJavascriptAndGetValue("/finder.*foo/g;", &observer);
    EXPECT_EQ(Value::TYPE_STRING, observer.value()->GetType());
    std::string string_value;
    EXPECT_TRUE(observer.value()->GetAsString(&string_value));
    EXPECT_EQ(std::string("/finder.*foo/g"), string_value);
  }

  // Let's test some date conversions.  First up, epoch.  Can't use 0 because
  // that means uninitialized, so use the next best thing.
  {
    ExecuteNotificationObserver observer;
    ExecuteJavascriptAndGetValue("new Date(1);", &observer);
    EXPECT_EQ(Value::TYPE_DOUBLE, observer.value()->GetType());
    double date_seconds;
    EXPECT_TRUE(observer.value()->GetAsDouble(&date_seconds));

    base::Time time = base::Time::FromDoubleT(date_seconds);

    base::Time::Exploded time_exploded;
    time.UTCExplode(&time_exploded);
    EXPECT_EQ(1970, time_exploded.year);
    EXPECT_EQ(1, time_exploded.month);
    EXPECT_EQ(1, time_exploded.day_of_month);
    EXPECT_EQ(0, time_exploded.hour);
    EXPECT_EQ(0, time_exploded.minute);
    EXPECT_EQ(0, time_exploded.second);
  }

  // Test date with a real date input.
  {
    ExecuteNotificationObserver observer;
    ExecuteJavascriptAndGetValue("new Date(Date.UTC(2006, 7, 16, 12, 0, 15));",
                                 &observer);
    EXPECT_EQ(Value::TYPE_DOUBLE, observer.value()->GetType());
    double date_seconds;
    EXPECT_TRUE(observer.value()->GetAsDouble(&date_seconds));

    base::Time time = base::Time::FromDoubleT(date_seconds);

    base::Time::Exploded time_exploded;
    time.UTCExplode(&time_exploded);
    EXPECT_EQ(2006, time_exploded.year);
    // Subtle; 0 based in JS, 1 based in base::Time:
    EXPECT_EQ(8, time_exploded.month);
    EXPECT_EQ(16, time_exploded.day_of_month);
    EXPECT_EQ(12, time_exploded.hour);
    EXPECT_EQ(0, time_exploded.minute);
    EXPECT_EQ(15, time_exploded.second);
  }

  // And something more complicated - get an array back as a list.
  {
    ExecuteNotificationObserver observer;
    ExecuteJavascriptAndGetValue("new Array(\"one\", 2, false);", &observer);
    EXPECT_EQ(Value::TYPE_LIST, observer.value()->GetType());
    ListValue* list_value;
    EXPECT_TRUE(observer.value()->GetAsList(&list_value));
    EXPECT_EQ(3U, list_value->GetSize());
    Value* value;
    EXPECT_TRUE(list_value->Get(0, &value));
    EXPECT_EQ(Value::TYPE_STRING, value->GetType());
    EXPECT_TRUE(list_value->Get(1, &value));
    EXPECT_EQ(Value::TYPE_INTEGER, value->GetType());
    EXPECT_TRUE(list_value->Get(2, &value));
    EXPECT_EQ(Value::TYPE_BOOLEAN, value->GetType());
  }
}

// Regression test for http://crbug.com/63649.
IN_PROC_BROWSER_TEST_F(RenderViewHostTest, RedirectLoopCookies) {
  ASSERT_TRUE(test_server()->Start());

  GURL test_url = test_server()->GetURL("files/redirect-loop.html");

  browser()->profile()->GetHostContentSettingsMap()->SetDefaultContentSetting(
      CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK);

  ui_test_utils::NavigateToURL(browser(), test_url);

  TabContents* tab_contents = browser()->GetSelectedTabContents();
  ASSERT_EQ(UTF8ToUTF16(test_url.spec() + " failed to load"),
            tab_contents->GetTitle());

  EXPECT_TRUE(tab_contents->GetTabSpecificContentSettings()->IsContentBlocked(
      CONTENT_SETTINGS_TYPE_COOKIES));
}

class RenderViewHostTestTabContentsObserver : public TabContentsObserver {
 public:
  explicit RenderViewHostTestTabContentsObserver(TabContents* tab_contents)
      : TabContentsObserver(tab_contents),
        navigation_count_(0) {}
  virtual ~RenderViewHostTestTabContentsObserver() {}

  virtual void DidNavigateMainFramePostCommit(
      const NavigationController::LoadCommittedDetails& details,
      const ViewHostMsg_FrameNavigate_Params& params) {
    observed_socket_address_ = params.socket_address;
    ++navigation_count_;
  }

  const net::HostPortPair& observed_socket_address() const {
    return observed_socket_address_;
  }

  int navigation_count() const { return navigation_count_; }

 private:
  net::HostPortPair observed_socket_address_;
  int navigation_count_;

  DISALLOW_COPY_AND_ASSIGN(RenderViewHostTestTabContentsObserver);
};

IN_PROC_BROWSER_TEST_F(RenderViewHostTest, FrameNavigateSocketAddress) {
  ASSERT_TRUE(test_server()->Start());
  RenderViewHostTestTabContentsObserver observer(
      browser()->GetSelectedTabContents());

  GURL test_url = test_server()->GetURL("files/simple.html");
  ui_test_utils::NavigateToURL(browser(), test_url);

  EXPECT_EQ(test_server()->host_port_pair().ToString(),
            observer.observed_socket_address().ToString());
  EXPECT_EQ(1, observer.navigation_count());
}