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

Unified Diff: ios/chrome/test/base/scoped_block_swizzler_unittest.mm

Issue 2551083002: [ios] Adds ScopedBlockSwizzler. (Closed)
Patch Set: Created 4 years 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: ios/chrome/test/base/scoped_block_swizzler_unittest.mm
diff --git a/ios/chrome/test/base/scoped_block_swizzler_unittest.mm b/ios/chrome/test/base/scoped_block_swizzler_unittest.mm
new file mode 100644
index 0000000000000000000000000000000000000000..2e390a4d1b82cd3ee750cec1a86ad3c64b82e892
--- /dev/null
+++ b/ios/chrome/test/base/scoped_block_swizzler_unittest.mm
@@ -0,0 +1,103 @@
+// Copyright 2013 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 "base/mac/foundation_util.h"
+#import "base/mac/scoped_nsobject.h"
+#import "ios/chrome/test/base/scoped_block_swizzler.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/gtest_mac.h"
+#include "testing/platform_test.h"
+
+// Class containing two methods that will be swizzled by the unittests.
+@interface ScopedBlockSwizzlerTestClass : NSObject
+
+// An NSString property that will be accessed by one of the swizzled methods.
+@property(nonatomic, copy) NSString* value;
+
++ (NSString*)classMethodToSwizzle;
+- (NSString*)instanceMethodToSwizzle;
+@end
+
+namespace {
+
+NSString* const kOriginalClassValue = @"Bar";
+NSString* const kSwizzledClassValue = @"Foo";
+NSString* const kOriginalInstanceValue = @"Bizz";
+NSString* const kSwizzledInstanceValue = @"Buzz";
+
+// Tests that swizzling a class method works properly.
+TEST(ScopedBlockSwizzlerTest, SwizzlingClassMethods) {
+ EXPECT_NSEQ(kOriginalClassValue,
+ [ScopedBlockSwizzlerTestClass classMethodToSwizzle]);
+
+ {
+ id block = ^NSString*(id self) { return kSwizzledClassValue; };
+ ScopedBlockSwizzler swizzler([ScopedBlockSwizzlerTestClass class],
+ @selector(classMethodToSwizzle), block);
+ EXPECT_NSEQ(kSwizzledClassValue,
+ [ScopedBlockSwizzlerTestClass classMethodToSwizzle]);
+ }
+
+ EXPECT_NSEQ(kOriginalClassValue,
+ [ScopedBlockSwizzlerTestClass classMethodToSwizzle]);
+}
+
+// Tests that swizzling an instance method works properly.
+TEST(ScopedBlockSwizzlerTest, SwizzlingInstanceMethod) {
+ base::scoped_nsobject<ScopedBlockSwizzlerTestClass> target(
+ [[ScopedBlockSwizzlerTestClass alloc] init]);
+ target.get().value = kSwizzledInstanceValue;
+
+ EXPECT_NSEQ(kOriginalInstanceValue, [target instanceMethodToSwizzle]);
+ EXPECT_FALSE([[target instanceMethodToSwizzle]
+ isEqualToString:kSwizzledInstanceValue]);
+
+ {
+ id block = ^NSString*(id self) {
+ return base::mac::ObjCCastStrict<ScopedBlockSwizzlerTestClass>(self)
+ .value;
+ };
+ ScopedBlockSwizzler swizzler([ScopedBlockSwizzlerTestClass class],
+ @selector(instanceMethodToSwizzle), block);
+ EXPECT_NSEQ(kSwizzledInstanceValue, [target instanceMethodToSwizzle]);
+ }
+
+ EXPECT_NSEQ(kOriginalInstanceValue, [target instanceMethodToSwizzle]);
+}
+
+// Tests that calling |ScopedBlockSwizzler::reset()| properly unswizzles the
+// method.
+TEST(ScopedBlockSwizzlerTest, TestReset) {
+ EXPECT_NSEQ(kOriginalClassValue,
+ [ScopedBlockSwizzlerTestClass classMethodToSwizzle]);
+
+ id block = ^NSString*(id self) { return kSwizzledClassValue; };
+ std::unique_ptr<ScopedBlockSwizzler> swizzler(
+ new ScopedBlockSwizzler([ScopedBlockSwizzlerTestClass class],
+ @selector(classMethodToSwizzle), block));
+ EXPECT_NSEQ(kSwizzledClassValue,
+ [ScopedBlockSwizzlerTestClass classMethodToSwizzle]);
+
+ swizzler.reset();
+ EXPECT_NSEQ(kOriginalClassValue,
+ [ScopedBlockSwizzlerTestClass classMethodToSwizzle]);
+}
+
+} // namespace
+
+#pragma mark - ScopedBlockSwizzlerTestClass
+
+@implementation ScopedBlockSwizzlerTestClass
+
+@synthesize value = _value;
+
++ (NSString*)classMethodToSwizzle {
+ return kOriginalClassValue;
+}
+
+- (NSString*)instanceMethodToSwizzle {
+ return kOriginalInstanceValue;
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698