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

Unified Diff: ios/chrome/test/base/scoped_block_swizzler.h

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.h
diff --git a/ios/chrome/test/base/scoped_block_swizzler.h b/ios/chrome/test/base/scoped_block_swizzler.h
new file mode 100644
index 0000000000000000000000000000000000000000..ecfea71222b64a86939c89a482712749fef2dab2
--- /dev/null
+++ b/ios/chrome/test/base/scoped_block_swizzler.h
@@ -0,0 +1,40 @@
+// 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.
+//
+#ifndef IOS_CHROME_TEST_BASE_SCOPED_BLOCK_SWIZZLER_H_
+#define IOS_CHROME_TEST_BASE_SCOPED_BLOCK_SWIZZLER_H_
+
+#include <objc/runtime.h>
+
+#include "base/macros.h"
+
+// Helper class that replaces a method implementation with a given block.
+// ScopedBlockSwizzler automatically swizzles when it is constructed and
+// reinstalls the original method implementation when it goes out of scope.
+class ScopedBlockSwizzler {
+ public:
+ // Constructs a new ScopedBlockSwizzler object and replaces the implementation
+ // of |selector| on the |target| class with the given |block|.
+ // ScopedBlockSwizzler first tries to swizzle a class method; if one is not
+ // found, it tries to swizzle an instance method. It is an error to pass a
+ // |selector| that does not exist on the |target| class.
+ ScopedBlockSwizzler(Class target, SEL selector, id block);
+
+ // Destroys the ScopedBlockSwizzler object, removing the swizzled method and
+ // reinstalling the original method implementation.
+ virtual ~ScopedBlockSwizzler();
+
+ private:
+ // The method that is to be swizzled. Can be either a class method or an
+ // instance method.
+ Method method_;
+
+ // The original implementation of the swizzled method, saved so that it can be
+ // reinstalled when this object goes out of scope.
+ IMP original_imp_;
+
+ DISALLOW_COPY_AND_ASSIGN(ScopedBlockSwizzler);
+};
+
+#endif // IOS_CHROME_TEST_BASE_SCOPED_BLOCK_SWIZZLER_H_

Powered by Google App Engine
This is Rietveld 408576698