Skip to content

Commit

Permalink
Fix crash in unit tests loading resources from Brave-Core
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon-T committed Jan 9, 2025
1 parent f5ed2af commit 9edd81a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ios/app/brave_core_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ OBJC_EXPORT
/// Should only be called in unit tests
+ (bool)initializeICUForTesting;

+ (void)initializeResourceBundleForTesting;

@end

NS_ASSUME_NONNULL_END
Expand Down
33 changes: 33 additions & 0 deletions ios/app/brave_core_main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include "base/apple/bundle_locations.h"
#include "base/apple/foundation_util.h"
#include "base/at_exit.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/files/file_path.h"
#include "base/i18n/icu_util.h"
Expand Down Expand Up @@ -77,6 +79,8 @@
#include "ios/public/provider/chrome/browser/ui_utils/ui_utils_api.h"
#include "ios/web/public/init/web_main.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_paths.h"

#if BUILDFLAG(IOS_CREDENTIAL_PROVIDER_ENABLED)
#include "ios/chrome/browser/credential_provider/model/credential_provider_service_factory.h"
Expand Down Expand Up @@ -516,6 +520,35 @@ + (bool)initializeICUForTesting {
return base::i18n::InitializeICU();
}

+ (void)initializeResourceBundleForTesting {
@autoreleasepool {
ios::RegisterPathProvider();
ui::RegisterPathProvider();
}

base::AtExitManager exit_manager;
base::CommandLine::Init(0, nullptr);

[BraveCoreMain initializeICUForTesting];

NSBundle* baseBundle = base::apple::OuterBundle();
base::apple::SetBaseBundleID(
base::SysNSStringToUTF8([baseBundle bundleIdentifier]).c_str());

// Register all providers before calling any Chromium code.
[ProviderRegistration registerProviders];

ui::ResourceBundle::InitSharedInstanceWithLocale(
"en-US", nullptr, ui::ResourceBundle::LOAD_COMMON_RESOURCES);

// Add Brave Resource Pack
base::FilePath brave_pack_path;
base::PathService::Get(base::DIR_ASSETS, &brave_pack_path);
brave_pack_path = brave_pack_path.AppendASCII("brave_resources.pak");
ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
brave_pack_path, ui::kScaleFactorNone);
}

#if BUILDFLAG(IOS_CREDENTIAL_PROVIDER_ENABLED)
- (void)performFaviconsCleanup {
ProfileIOS* browserState = _main_profile;
Expand Down
5 changes: 5 additions & 0 deletions ios/brave-ios/Tests/ClientTests/TabManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

import BraveCore
import Preferences
import Shared
import Storage
Expand Down Expand Up @@ -119,6 +120,10 @@ open class MockTabManagerDelegate: TabManagerDelegate {
private let privateBrowsingManager = PrivateBrowsingManager()
private let testWindowId = UUID()

override class func setUp() {
BraveCoreMain.initializeResourceBundleForTesting()
}

override func setUp() {
super.setUp()

Expand Down

0 comments on commit 9edd81a

Please sign in to comment.