summaryrefslogtreecommitdiffstats
path: root/chrome/browser/geolocation/geolocation_browsertest.cc
blob: 40c7bd9f8aa4ead0b6b19a7502840c149311aeb6 (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
// Copyright (c) 2009 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/string_util.h"
#include "chrome/browser/app_modal_dialog.h"
#include "chrome/browser/browser.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/renderer_host/render_view_host.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/geoposition.h"
#include "chrome/common/notification_details.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"
#include "chrome/common/render_messages.h"
#include "chrome/test/in_process_browser_test.h"
#include "chrome/test/ui_test_utils.h"
#include "net/base/net_util.h"

// This is a browser test for Geolocation.
// It exercises various integration points from javascript <-> browser:
// 1. Infobar is displayed when a geolocation is requested from an unauthorized
// origin.
// 2. Denying the infobar triggers the correct error callback.
// 3. Allowing the infobar does not trigger an error, and allow a geoposition to
// be passed to javascript.
// 4. Permissions persisted in disk are respected.
// 5. Off the record profiles don't use saved permissions.
class GeolocationBrowserTest
    : public InProcessBrowserTest, public NotificationObserver {
 public:
  GeolocationBrowserTest() : infobar_(NULL), current_browser_(NULL) {
    EnableDOMAutomation();
  }

  enum InitializationOptions {
    INITIALIZATION_NONE,
    INITIALIZATION_OFFTHERECORD,
    INITIALIZATION_NEWTAB,
  };

  void Initialize(InitializationOptions options) {
    if (!server_.get()) {
      server_ = StartHTTPServer();
    }
    GURL url = server_->TestServerPage("files/geolocation/simple.html");
    if (options == INITIALIZATION_OFFTHERECORD) {
      ui_test_utils::OpenURLOffTheRecord(browser()->profile(), url);
      current_browser_ = BrowserList::FindBrowserWithType(
          browser()->profile()->GetOffTheRecordProfile(), Browser::TYPE_NORMAL);
    } else if (options == INITIALIZATION_NEWTAB) {
      current_browser_ = browser();
      current_browser_->NewTab();
      ui_test_utils::NavigateToURL(current_browser_, url);
    } else {
      current_browser_ = browser();
      ui_test_utils::NavigateToURL(current_browser_, url);
    }
    EXPECT_TRUE(current_browser_);

    int watch_id = 0;
    EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractInt(
        current_browser_->GetSelectedTabContents()->render_view_host(), L"",
        UTF8ToWide("window.domAutomationController.send(geoStart());"),
        &watch_id));
    EXPECT_GT(watch_id, 0);
  }

  void SendGeoposition(bool wait_for_infobar, const Geoposition& geoposition) {
    if (wait_for_infobar) {
      // Observe infobar notification.
      registrar_.Add(
          this, NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
          NotificationService::AllSources());
    }

    // Sending the Geoposition makes webkit trigger the permission request flow.
    // If the origin is already allowed, no infobar will be displayed.
    RenderViewHost* render_view_host =
        current_browser_->GetSelectedTabContents()->render_view_host();
    render_view_host->Send(
        new ViewMsg_Geolocation_PositionUpdated(
            render_view_host->routing_id(), geoposition));

    if (wait_for_infobar) {
      ui_test_utils::RunMessageLoop();
      EXPECT_TRUE(infobar_);
      registrar_.Remove(this, NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
                        NotificationService::AllSources());
    }
  }

  void WaitForJSPrompt() {
    AppModalDialog* alert = ui_test_utils::WaitForAppModalDialog();
    ASSERT_TRUE(alert);
    alert->CloseModalDialog();
  }

  void SetInfobarResponse(bool allowed) {
    registrar_.Add(
        this, NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
        NotificationService::AllSources());
    ASSERT_TRUE(infobar_);
    if (allowed)
      infobar_->AsConfirmInfoBarDelegate()->Accept();
    else
      infobar_->AsConfirmInfoBarDelegate()->Cancel();
    current_browser_->GetSelectedTabContents()->RemoveInfoBar(infobar_);
    EXPECT_FALSE(infobar_);
    registrar_.Remove(this, NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
                      NotificationService::AllSources());
  }

  void CheckValueFromJavascript(
      const std::string& expected, const std::string& function) {
    std::string js_call = StringPrintf(
        "window.domAutomationController.send(%s);", function.c_str());
    std::string value;
    EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractString(
        current_browser_->GetSelectedTabContents()->render_view_host(), L"",
        UTF8ToWide(js_call),
        &value));
    EXPECT_EQ(expected, value);
  }

  // InProcessBrowserTest
  virtual void SetUpCommandLine(CommandLine* command_line) {
    InProcessBrowserTest::SetUpCommandLine(command_line);
    command_line->AppendSwitch(switches::kEnableGeolocation);
  }

  // NotificationObserver
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details) {
    if (type.value == NotificationType::TAB_CONTENTS_INFOBAR_ADDED) {
      infobar_ = Details<InfoBarDelegate>(details).ptr();
      ASSERT_TRUE(infobar_->GetIcon());
      ASSERT_TRUE(infobar_->AsConfirmInfoBarDelegate());
      MessageLoopForUI::current()->Quit();
    } else if (type.value == NotificationType::TAB_CONTENTS_INFOBAR_REMOVED) {
      infobar_ = NULL;
    }
  }

  NotificationRegistrar registrar_;
  InfoBarDelegate* infobar_;
  Browser* current_browser_;
  scoped_refptr<HTTPTestServer> server_;
};

IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, DisplaysPermissionBar) {
  Initialize(INITIALIZATION_NONE);
  SendGeoposition(true, Geoposition());
}

IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, ErrorOnPermissionDenied) {
  Initialize(INITIALIZATION_NONE);
  SendGeoposition(true, Geoposition());
  // Infobar was displayed, deny access and check for error code.
  SetInfobarResponse(false);
  WaitForJSPrompt();
  CheckValueFromJavascript("1", "geoGetLastError()");
}

IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, NoInfobarForSecondTab) {
#if 0
  Initialize(INITIALIZATION_NONE);
  SendGeoposition(true, Geoposition());
  SetInfobarResponse(true);
  WaitForJSPrompt();
  // Checks infobar will not be created a second tab.
  Initialize(INITIALIZATION_NEWTAB);
  SendGeoposition(false, Geoposition());
  WaitForJSPrompt();
  CheckValueFromJavascript("0", "geoGetLastError()");
#endif
}

IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, NoInfobarForDeniedOrigin) {
#if 0
  WritePermissionFile("{\"allowed\":false}");
  // Checks no infobar will be created.
  Initialize(INITIALIZATION_NONE);
  SendGeoposition(false, Geoposition());
  WaitForJSPrompt();
  CheckValueFromJavascript("1", "geoGetLastError()");
  // Checks infobar will not be created a second tab.
  Initialize(INITIALIZATION_NEWTAB);
  SendGeoposition(false, Geoposition());
  WaitForJSPrompt();
  CheckValueFromJavascript("1", "geoGetLastError()");
#endif
}

IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, NoInfobarForAllowedOrigin) {
#if 0
  WritePermissionFile("{\"allowed\":true}");
  // Checks no infobar will be created and there's no error callback.
  Initialize(INITIALIZATION_NONE);
  SendGeoposition(false, Geoposition());
  WaitForJSPrompt();
  CheckValueFromJavascript("0", "geoGetLastError()");
#endif
}

IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, InfobarForOffTheRecord) {
#if 0
  // Checks infobar will be created for regular profile.
  Initialize(INITIALIZATION_NONE);
  SendGeoposition(true, Geoposition());
  SetInfobarResponse(true);
  WaitForJSPrompt();
  CheckValueFromJavascript("0", "geoGetLastError()");
  // Go off the record, and checks infobar will be created and an error callback
  // is triggered.
  Initialize(INITIALIZATION_OFFTHERECORD);
  SendGeoposition(true, Geoposition());
  SetInfobarResponse(false);
  WaitForJSPrompt();
  CheckValueFromJavascript("1", "geoGetLastError()");
#endif
}

IN_PROC_BROWSER_TEST_F(GeolocationBrowserTest, Geoposition) {
  // Checks infobar will be created.
  Initialize(INITIALIZATION_NONE);
  SendGeoposition(true, Geoposition());
  // Infobar was displayed, allow access and check there's no error code.
  SetInfobarResponse(true);
  WaitForJSPrompt();
  CheckValueFromJavascript("0", "geoGetLastError()");
  // Sends a Geoposition over IPC, and check it arrives in the javascript side.
  Geoposition geoposition;
  geoposition.latitude = 3.17;
  geoposition.longitude = 4.23;
  SendGeoposition(false, geoposition);
  WaitForJSPrompt();
  // Checks we have no error.
  CheckValueFromJavascript("0", "geoGetLastError()");
  CheckValueFromJavascript(
      DoubleToString(geoposition.latitude), "geoGetLastPositionLatitude()");
  CheckValueFromJavascript(
      DoubleToString(geoposition.longitude), "geoGetLastPositionLongitude()");
}