2012-10-19 20:39:44 +02:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2014-07-15 20:10:40 +02:00
|
|
|
#include "include/base/cef_bind.h"
|
2012-10-19 20:39:44 +02:00
|
|
|
#include "include/cef_geolocation.h"
|
2016-11-15 22:18:41 +01:00
|
|
|
#include "include/cef_waitable_event.h"
|
2014-07-15 20:10:40 +02:00
|
|
|
#include "include/wrapper/cef_closure_task.h"
|
2016-11-18 18:31:21 +01:00
|
|
|
#include "tests/ceftests/test_handler.h"
|
|
|
|
#include "tests/ceftests/test_util.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/gtest/include/gtest/gtest.h"
|
2012-10-19 20:39:44 +02:00
|
|
|
|
2016-08-31 13:25:56 +02:00
|
|
|
// Comment in this define if you have configured API keys that enable Google
|
|
|
|
// Maps Geolocation API support.
|
|
|
|
// See https://www.chromium.org/developers/how-tos/api-keys for details.
|
|
|
|
// #define HAS_GEOLOCATION_API_KEYS 1
|
|
|
|
|
2012-10-19 20:39:44 +02:00
|
|
|
namespace {
|
|
|
|
|
2016-02-05 01:49:19 +01:00
|
|
|
// Geolocation access is now restricted to "secure" origins.
|
|
|
|
const char* kTestOrigin = "https://tests/";
|
|
|
|
const char* kTestUrl = "https://tests/GeolocationTestHandler";
|
|
|
|
const char* kTestAllowUrl = "https://tests/GeolocationTestHandler.Allow";
|
|
|
|
const char* kTestDenyUrl = "https://tests/GeolocationTestHandler.Deny";
|
|
|
|
const char* kTestCancelUrl = "https://tests/GeolocationTestHandler.Cancel";
|
2012-10-19 20:39:44 +02:00
|
|
|
|
|
|
|
enum TestMode {
|
|
|
|
TEST_ALLOW,
|
|
|
|
TEST_DENY,
|
|
|
|
TEST_CANCEL,
|
|
|
|
};
|
|
|
|
|
|
|
|
class GeolocationTestHandler : public TestHandler {
|
|
|
|
public:
|
|
|
|
GeolocationTestHandler(const TestMode& mode, bool async)
|
2017-05-17 11:29:28 +02:00
|
|
|
: mode_(mode), async_(async), request_id_(-1) {}
|
2012-10-19 20:39:44 +02:00
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void RunTest() override {
|
2012-10-19 20:39:44 +02:00
|
|
|
std::string html =
|
|
|
|
"<html><head><script>"
|
|
|
|
"navigator.geolocation.getCurrentPosition("
|
|
|
|
// Success function
|
|
|
|
"function() {"
|
2017-05-17 11:29:28 +02:00
|
|
|
"window.location.href = '" +
|
|
|
|
std::string(kTestAllowUrl) +
|
|
|
|
"'; },"
|
2012-10-19 20:39:44 +02:00
|
|
|
// Error function
|
|
|
|
"function() {"
|
2017-05-17 11:29:28 +02:00
|
|
|
"window.location.href = '" +
|
|
|
|
std::string(kTestDenyUrl) + "'; });";
|
2012-10-19 20:39:44 +02:00
|
|
|
if (mode_ == TEST_CANCEL)
|
|
|
|
html += "window.location.href = '" + std::string(kTestCancelUrl) + "';";
|
|
|
|
html += "</script></head><body>TEST START</body></html>";
|
|
|
|
AddResource(kTestUrl, html, "text/html");
|
|
|
|
|
|
|
|
std::string end_html = "<html><body>TEST END</body></html>";
|
|
|
|
AddResource(kTestAllowUrl, end_html, "text/html");
|
|
|
|
AddResource(kTestDenyUrl, end_html, "text/html");
|
|
|
|
AddResource(kTestCancelUrl, end_html, "text/html");
|
|
|
|
|
|
|
|
// Create the browser
|
|
|
|
CreateBrowser(kTestUrl);
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
// Time out the test after a reasonable period of time.
|
|
|
|
SetTestTimeout();
|
2012-10-19 20:39:44 +02:00
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnLoadEnd(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
int httpStatusCode) override {
|
2012-10-19 20:39:44 +02:00
|
|
|
std::string url = frame->GetURL();
|
|
|
|
if (url != kTestUrl) {
|
|
|
|
if (url == kTestAllowUrl)
|
|
|
|
got_allow_.yes();
|
|
|
|
else if (url == kTestDenyUrl)
|
|
|
|
got_deny_.yes();
|
|
|
|
else if (url == kTestCancelUrl)
|
|
|
|
got_cancel_.yes();
|
|
|
|
|
|
|
|
DestroyTest();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ExecuteCallback(CefRefPtr<CefGeolocationCallback> callback) {
|
|
|
|
if (mode_ == TEST_ALLOW)
|
|
|
|
callback->Continue(true);
|
|
|
|
else if (mode_ == TEST_DENY)
|
|
|
|
callback->Continue(false);
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool OnRequestGeolocationPermission(
|
2012-10-19 20:39:44 +02:00
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
const CefString& requesting_url,
|
|
|
|
int request_id,
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefGeolocationCallback> callback) override {
|
2012-10-19 20:39:44 +02:00
|
|
|
got_requestgeolocationpermission_.yes();
|
|
|
|
|
2014-05-29 19:15:34 +02:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_UI));
|
2012-10-19 20:39:44 +02:00
|
|
|
|
|
|
|
EXPECT_STREQ(kTestOrigin, requesting_url.ToString().c_str());
|
|
|
|
request_id_ = request_id;
|
|
|
|
|
|
|
|
if (!async_) {
|
|
|
|
ExecuteCallback(callback);
|
|
|
|
} else {
|
2017-05-17 11:29:28 +02:00
|
|
|
CefPostTask(TID_UI, base::Bind(&GeolocationTestHandler::ExecuteCallback,
|
|
|
|
this, callback));
|
2012-10-19 20:39:44 +02:00
|
|
|
}
|
2014-07-02 20:25:22 +02:00
|
|
|
|
|
|
|
return true;
|
2012-10-19 20:39:44 +02:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void OnCancelGeolocationPermission(CefRefPtr<CefBrowser> browser,
|
|
|
|
int request_id) override {
|
2012-10-19 20:39:44 +02:00
|
|
|
got_cancelgeolocationpermission_.yes();
|
|
|
|
|
2014-05-29 19:15:34 +02:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_UI));
|
2012-10-19 20:39:44 +02:00
|
|
|
|
|
|
|
EXPECT_EQ(request_id, request_id_);
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void DestroyTest() override {
|
2012-10-19 20:39:44 +02:00
|
|
|
EXPECT_TRUE(got_requestgeolocationpermission_);
|
|
|
|
if (mode_ == TEST_CANCEL)
|
|
|
|
EXPECT_TRUE(got_cancelgeolocationpermission_);
|
|
|
|
else
|
|
|
|
EXPECT_FALSE(got_cancelgeolocationpermission_);
|
|
|
|
|
|
|
|
TestHandler::DestroyTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
TestMode mode_;
|
|
|
|
bool async_;
|
|
|
|
|
|
|
|
int request_id_;
|
|
|
|
|
|
|
|
TrackCallback got_requestgeolocationpermission_;
|
|
|
|
TrackCallback got_cancelgeolocationpermission_;
|
|
|
|
TrackCallback got_allow_;
|
|
|
|
TrackCallback got_cancel_;
|
|
|
|
TrackCallback got_deny_;
|
2015-09-09 16:05:39 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(GeolocationTestHandler);
|
2012-10-19 20:39:44 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(GeolocationTest, HandlerAllow) {
|
|
|
|
CefRefPtr<GeolocationTestHandler> handler =
|
|
|
|
new GeolocationTestHandler(TEST_ALLOW, false);
|
|
|
|
handler->ExecuteTest();
|
2016-08-31 13:25:56 +02:00
|
|
|
#if defined(HAS_GEOLOCATION_API_KEYS)
|
2012-10-19 20:39:44 +02:00
|
|
|
EXPECT_TRUE(handler->got_allow_);
|
2016-08-31 13:25:56 +02:00
|
|
|
#else
|
|
|
|
EXPECT_FALSE(handler->got_allow_);
|
|
|
|
#endif
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-10-19 20:39:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(GeolocationTest, HandlerAllowAsync) {
|
|
|
|
CefRefPtr<GeolocationTestHandler> handler =
|
|
|
|
new GeolocationTestHandler(TEST_ALLOW, true);
|
|
|
|
handler->ExecuteTest();
|
2016-08-31 13:25:56 +02:00
|
|
|
#if defined(HAS_GEOLOCATION_API_KEYS)
|
2012-10-19 20:39:44 +02:00
|
|
|
EXPECT_TRUE(handler->got_allow_);
|
2016-08-31 13:25:56 +02:00
|
|
|
#else
|
|
|
|
EXPECT_FALSE(handler->got_allow_);
|
|
|
|
#endif
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-10-19 20:39:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(GeolocationTest, HandlerDeny) {
|
|
|
|
CefRefPtr<GeolocationTestHandler> handler =
|
|
|
|
new GeolocationTestHandler(TEST_DENY, false);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
EXPECT_TRUE(handler->got_deny_);
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-10-19 20:39:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(GeolocationTest, HandlerDenyAsync) {
|
|
|
|
CefRefPtr<GeolocationTestHandler> handler =
|
|
|
|
new GeolocationTestHandler(TEST_DENY, true);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
EXPECT_TRUE(handler->got_deny_);
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-10-19 20:39:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(GeolocationTest, HandlerCancel) {
|
|
|
|
CefRefPtr<GeolocationTestHandler> handler =
|
|
|
|
new GeolocationTestHandler(TEST_CANCEL, false);
|
|
|
|
handler->ExecuteTest();
|
|
|
|
EXPECT_TRUE(handler->got_cancel_);
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-10-19 20:39:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class TestGetGeolocationCallback : public CefGetGeolocationCallback {
|
|
|
|
public:
|
2016-11-15 22:18:41 +01:00
|
|
|
explicit TestGetGeolocationCallback(CefRefPtr<CefWaitableEvent> event)
|
2017-05-17 11:29:28 +02:00
|
|
|
: event_(event) {}
|
2012-10-19 20:39:44 +02:00
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnLocationUpdate(const CefGeoposition& position) override {
|
2012-10-19 20:39:44 +02:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_UI));
|
2016-08-31 13:25:56 +02:00
|
|
|
#if defined(HAS_GEOLOCATION_API_KEYS)
|
|
|
|
EXPECT_EQ(GEOPOSITON_ERROR_NONE, position.error_code);
|
|
|
|
#else
|
|
|
|
EXPECT_EQ(GEOPOSITON_ERROR_POSITION_UNAVAILABLE, position.error_code);
|
|
|
|
#endif
|
|
|
|
EXPECT_NE(0.0, position.latitude);
|
|
|
|
EXPECT_NE(0.0, position.longitude);
|
|
|
|
EXPECT_NE(0.0, position.accuracy);
|
|
|
|
EXPECT_NE(0, position.timestamp.year);
|
2012-10-19 20:39:44 +02:00
|
|
|
event_->Signal();
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
private:
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event_;
|
2012-10-19 20:39:44 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(TestGetGeolocationCallback);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST(GeolocationTest, GetGeolocation) {
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(true, false);
|
|
|
|
CefGetGeolocation(new TestGetGeolocationCallback(event));
|
|
|
|
event->Wait();
|
2012-10-19 20:39:44 +02:00
|
|
|
}
|