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; |
+} |