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

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: Remove blank line. Created 5 years, 5 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..90aaf14b3295af407479047fb8296dd594cb864e
--- /dev/null
+++ b/chrome/test/base/scoped_bundle_swizzler_mac.mm
@@ -0,0 +1,49 @@
+// 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/foundation_util.h"
+#include "base/mac/scoped_objc_class_swizzler.h"
+#include "base/strings/sys_string_conversions.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() {
+ CHECK(!g_swizzled_main_bundle);
+ CHECK(!g_original_main_bundle);
+
+ g_original_main_bundle = [NSBundle mainBundle];
+ g_swizzled_main_bundle =
+ [[OCMockObject partialMockForObject:g_original_main_bundle] retain];
+ NSString* identifier = base::SysUTF8ToNSString(base::mac::BaseBundleID());
+ CHECK(identifier);
+ [[[g_swizzled_main_bundle stub] andReturn:identifier] bundleIdentifier];
+
+ 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;
+}
« chrome/test/base/in_process_browser_test.h ('K') | « chrome/test/base/scoped_bundle_swizzler_mac.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698