Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2742)

Unified Diff: chrome/test/base/scoped_bundle_swizzler_mac.mm

Issue 1211213003: mac: Swizzle out +[NSBundle mainBundle] for in process browser tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Minor formatting, nits. Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/test/base/scoped_bundle_swizzler_mac.mm
diff --git a/chrome/test/base/scoped_bundle_swizzler_mac.mm b/chrome/test/base/scoped_bundle_swizzler_mac.mm
new file mode 100644
index 0000000000000000000000000000000000000000..435796a4c901ed8afef893ae7d332fb2f009e9ad
--- /dev/null
+++ b/chrome/test/base/scoped_bundle_swizzler_mac.mm
@@ -0,0 +1,46 @@
+// Copyright 2015 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/scoped_bundle_swizzler_mac.h"
+
+#import <Foundation/Foundation.h>
+
+#include "base/logging.h"
+#include "base/mac/scoped_objc_class_swizzler.h"
+#import "third_party/ocmock/OCMock/OCMock.h"
+
+static NSBundle* g_original_main_bundle = nil;
+static id g_swizzled_main_bundle = nil;
+
+// A donor class that provides a +[NSBundle mainBundle] method that can be
+// swapped with NSBundle.
+@interface TestBundle : NSObject
++ (NSBundle*)mainBundle;
+@end
+
+@implementation TestBundle
++ (NSBundle*)mainBundle {
+ return g_swizzled_main_bundle;
+}
+@end
+
+ScopedBundleSwizzlerMac::ScopedBundleSwizzlerMac() {
+ DCHECK(!g_swizzled_main_bundle);
+ DCHECK(!g_original_main_bundle);
+
+ g_original_main_bundle = [NSBundle mainBundle];
+ g_swizzled_main_bundle =
+ [[OCMockObject partialMockForObject:g_original_main_bundle] retain];
+ [[[g_swizzled_main_bundle stub]
+ andReturn:@"swizzled.bundle.id"] bundleIdentifier];
Robert Sesek 2015/06/26 22:36:54 Can this use bae::mac::BaseBundleID()?
erikchen 2015/06/26 23:14:53 Yup, done.
+
+ class_swizzler_.reset(new base::mac::ScopedObjCClassSwizzler(
+ [NSBundle class], [TestBundle class], @selector(mainBundle)));
+}
+
+ScopedBundleSwizzlerMac::~ScopedBundleSwizzlerMac() {
+ [g_swizzled_main_bundle release];
+ g_swizzled_main_bundle = nil;
+ g_original_main_bundle = nil;
+}

Powered by Google App Engine
This is Rietveld 408576698