OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/test/base/scoped_bundle_swizzler_mac.h" | |
6 | |
7 #import <Foundation/Foundation.h> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/mac/foundation_util.h" | |
11 #include "base/mac/scoped_objc_class_swizzler.h" | |
12 #import "third_party/ocmock/OCMock/OCMock.h" | |
13 | |
14 static NSBundle* g_original_main_bundle = nil; | |
15 static id g_swizzled_main_bundle = nil; | |
16 | |
17 // A donor class that provides a +[NSBundle mainBundle] method that can be | |
18 // swapped with NSBundle. | |
19 @interface TestBundle : NSObject | |
20 + (NSBundle*)mainBundle; | |
21 @end | |
22 | |
23 @implementation TestBundle | |
24 + (NSBundle*)mainBundle { | |
25 return g_swizzled_main_bundle; | |
26 } | |
27 @end | |
28 | |
29 ScopedBundleSwizzlerMac::ScopedBundleSwizzlerMac() { | |
30 CHECK(!g_swizzled_main_bundle); | |
31 CHECK(!g_original_main_bundle); | |
32 | |
33 g_original_main_bundle = [NSBundle mainBundle]; | |
34 g_swizzled_main_bundle = | |
35 [[OCMockObject partialMockForObject:g_original_main_bundle] retain]; | |
36 NSString* identifier = | |
37 [NSString stringWithUTF8String:base::mac::BaseBundleID()]; | |
Robert Sesek
2015/06/30 22:21:07
sys_string_conversions.h is preferred.
erikchen
2015/07/06 22:00:06
Done.
| |
38 CHECK(identifier); | |
39 [[[g_swizzled_main_bundle stub] andReturn:identifier] bundleIdentifier]; | |
40 | |
41 class_swizzler_.reset(new base::mac::ScopedObjCClassSwizzler( | |
42 [NSBundle class], [TestBundle class], @selector(mainBundle))); | |
43 } | |
44 | |
45 ScopedBundleSwizzlerMac::~ScopedBundleSwizzlerMac() { | |
46 [g_swizzled_main_bundle release]; | |
47 g_swizzled_main_bundle = nil; | |
48 g_original_main_bundle = nil; | |
49 } | |
OLD | NEW |