Commit 1ab9b4ef authored by Elly Fong-Jones's avatar Elly Fong-Jones Committed by Commit Bot

mac: register with LaunchServices for protocol handler test

As of 10.15, an app must be registered with LaunchServices before
the corresponding bundle ID can become a protocol scheme handler. This
happens as part of app startup normally, but in tests where there is
no app, it needs to be done manually. Unfortunately these tests do not
have a bundle to register, so this change has them try to use the main
browser bundle when registering.

This is a less evil version of
https://chromium-review.googlesource.com/c/chromium/src/+/1890960

Bug: 998740
Change-Id: I96b2d21b1966c304dba3d61e4539308d96221d28
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1893582Reviewed-by: default avatarNico Weber <thakis@chromium.org>
Reviewed-by: default avatarMark Mentovai <mark@chromium.org>
Commit-Queue: Elly Fong-Jones <ellyjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#712191}
parent ad5097b9
...@@ -70,6 +70,10 @@ ...@@ -70,6 +70,10 @@
#include "url/url_constants.h" #include "url/url_constants.h"
#endif #endif
#if defined(OS_MACOSX)
#include "chrome/test/base/launchservices_utils_mac.h"
#endif
namespace content { namespace content {
namespace { namespace {
...@@ -909,6 +913,9 @@ class ProtocolHandlerTest : public InProcessBrowserTest { ...@@ -909,6 +913,9 @@ class ProtocolHandlerTest : public InProcessBrowserTest {
}; };
IN_PROC_BROWSER_TEST_F(ProtocolHandlerTest, CustomHandler) { IN_PROC_BROWSER_TEST_F(ProtocolHandlerTest, CustomHandler) {
#if defined(OS_MACOSX)
ASSERT_TRUE(test::RegisterAppWithLaunchServices());
#endif
AddProtocolHandler("news", "https://abc.xyz/?url=%s"); AddProtocolHandler("news", "https://abc.xyz/?url=%s");
ui_test_utils::NavigateToURL(browser(), GURL("news:something")); ui_test_utils::NavigateToURL(browser(), GURL("news:something"));
......
...@@ -110,6 +110,8 @@ static_library("test_support") { ...@@ -110,6 +110,8 @@ static_library("test_support") {
"base/chrome_unit_test_suite.h", "base/chrome_unit_test_suite.h",
"base/find_result_waiter.cc", "base/find_result_waiter.cc",
"base/find_result_waiter.h", "base/find_result_waiter.h",
"base/launchservices_utils_mac.h",
"base/launchservices_utils_mac.mm",
"base/process_inspector_win.cc", "base/process_inspector_win.cc",
"base/process_inspector_win.h", "base/process_inspector_win.h",
"base/process_lineage_win.cc", "base/process_lineage_win.cc",
......
// Copyright 2019 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.
#ifndef CHROME_TEST_BASE_LAUNCHSERVICES_UTILS_MAC_H_
#define CHROME_TEST_BASE_LAUNCHSERVICES_UTILS_MAC_H_
namespace test {
// Attempts to guess the path to the Chromium app bundle and register it with
// LaunchServices. This is necessary in tests that want to install protocol
// handlers, since as of macOS 10.15 a bundle ID cannot be the handler for a
// protocol unless a corresponding app is already registered with
// LaunchServices.
//
// Note that there is no exposed LaunchServices API to unregister an app, so
// there is no way to undo this, but registering again will replace the old
// registration.
//
// Returns true if LaunchServices claimed that the registration succeeded. Note
// that success does not necessarily mean the app was successfully registered
// since part of the registration process is asynchronous.
bool RegisterAppWithLaunchServices();
} // namespace test
#endif // CHROME_TEST_BASE_LAUNCHSERVICES_UTILS_MAC_H_
// Copyright 2019 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 "chrome/test/base/launchservices_utils_mac.h"
#include <Foundation/Foundation.h>
#include "base/base_paths.h"
#include "base/files/file_path.h"
#include "base/mac/foundation_util.h"
#include "base/path_service.h"
#include "build/branding_buildflags.h"
namespace test {
bool RegisterAppWithLaunchServices() {
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
const char kAppSuffix[] = "Google Chrome.app";
#else
const char kAppSuffix[] = "Chromium.app";
#endif
// Try to guess the path to the real org.chromium.Chromium and/or
// org.google.Chrome bundle if the current main bundle's path isn't already a
// .app directory:
NSURL* bundleURL = [[NSBundle mainBundle] bundleURL];
if (![bundleURL.lastPathComponent hasSuffix:@".app"]) {
base::FilePath bundle_path;
if (!base::PathService::Get(base::DIR_EXE, &bundle_path))
return false;
bundle_path = bundle_path.Append(kAppSuffix);
bundleURL = base::mac::FilePathToNSURL(bundle_path);
}
if (![NSFileManager.defaultManager fileExistsAtPath:bundleURL.path])
return false;
return LSRegisterURL(base::mac::NSToCFCast(bundleURL), false) == noErr;
}
} // namespace test
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment