Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / test / base / scoped_bundle_swizzler_mac.mm
blob90aaf14b3295af407479047fb8296dd594cb864e
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.
5 #include "chrome/test/base/scoped_bundle_swizzler_mac.h"
7 #import <Foundation/Foundation.h>
9 #include "base/logging.h"
10 #include "base/mac/foundation_util.h"
11 #include "base/mac/scoped_objc_class_swizzler.h"
12 #include "base/strings/sys_string_conversions.h"
13 #import "third_party/ocmock/OCMock/OCMock.h"
15 static NSBundle* g_original_main_bundle = nil;
16 static id g_swizzled_main_bundle = nil;
18 // A donor class that provides a +[NSBundle mainBundle] method that can be
19 // swapped with NSBundle.
20 @interface TestBundle : NSObject
21 + (NSBundle*)mainBundle;
22 @end
24 @implementation TestBundle
25 + (NSBundle*)mainBundle {
26   return g_swizzled_main_bundle;
28 @end
30 ScopedBundleSwizzlerMac::ScopedBundleSwizzlerMac() {
31   CHECK(!g_swizzled_main_bundle);
32   CHECK(!g_original_main_bundle);
34   g_original_main_bundle = [NSBundle mainBundle];
35   g_swizzled_main_bundle =
36       [[OCMockObject partialMockForObject:g_original_main_bundle] retain];
37   NSString* identifier = base::SysUTF8ToNSString(base::mac::BaseBundleID());
38   CHECK(identifier);
39   [[[g_swizzled_main_bundle stub] andReturn:identifier] bundleIdentifier];
41   class_swizzler_.reset(new base::mac::ScopedObjCClassSwizzler(
42       [NSBundle class], [TestBundle class], @selector(mainBundle)));
45 ScopedBundleSwizzlerMac::~ScopedBundleSwizzlerMac() {
46   [g_swizzled_main_bundle release];
47   g_swizzled_main_bundle = nil;
48   g_original_main_bundle = nil;