summaryrefslogtreecommitdiffstats
path: root/chrome/browser
diff options
context:
space:
mode:
authorrsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-08-20 22:10:08 +0000
committerrsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-08-20 22:10:08 +0000
commit149b62c5e7f27b9b5f22d69a24a02825adb3238e (patch)
tree09d5a8669750ad6f47419c3975da18f91cfed7af /chrome/browser
parent4ba8b62ce0766d328b67e3e93bf0f7a6d211ee90 (diff)
downloadchromium_src-149b62c5e7f27b9b5f22d69a24a02825adb3238e.zip
chromium_src-149b62c5e7f27b9b5f22d69a24a02825adb3238e.tar.gz
chromium_src-149b62c5e7f27b9b5f22d69a24a02825adb3238e.tar.bz2
[Mac] Add a function to make it easy to test around the infamous Chromium delayed selector hack.
BUG=none TEST=RunLoopTesting.RunAllPending unittest Review URL: http://codereview.chromium.org/7692014 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@97585 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r--chrome/browser/ui/cocoa/run_loop_testing.h30
-rw-r--r--chrome/browser/ui/cocoa/run_loop_testing.mm59
-rw-r--r--chrome/browser/ui/cocoa/run_loop_testing_unittest.mm59
3 files changed, 148 insertions, 0 deletions
diff --git a/chrome/browser/ui/cocoa/run_loop_testing.h b/chrome/browser/ui/cocoa/run_loop_testing.h
new file mode 100644
index 0000000..ac5b1be
--- /dev/null
+++ b/chrome/browser/ui/cocoa/run_loop_testing.h
@@ -0,0 +1,30 @@
+// Copyright (c) 2011 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 CHROME_BROWSER_UI_COCOA_RUN_LOOP_TESTING_H_
+#define CHROME_BROWSER_UI_COCOA_RUN_LOOP_TESTING_H_
+#pragma once
+
+namespace chrome {
+namespace testing {
+
+// A common pattern in Chromium is to get a selector to execute on the next
+// iteration of the outermost run loop, done like so:
+//
+// [someObj performSelector:@selector(someSel:) withObject:nil afterDelay:0];
+//
+// This is used when performing the work will negatively affect something
+// currently on the stack. Unfortunately this also affects the testability of
+// objects that do this. A call to this function will pump work like this from
+// the event queue and run it until all such work, as of the time of calling
+// this, has been processed.
+//
+// Note that this is not a NSApplication-based loop, and so things like NSEvents
+// are *not* pumped from the event queue.
+void NSRunLoopRunAllPending();
+
+} // namespace testing
+} // namespace chrome
+
+#endif // CHROME_BROWSER_UI_COCOA_RUN_LOOP_TESTING_H_
diff --git a/chrome/browser/ui/cocoa/run_loop_testing.mm b/chrome/browser/ui/cocoa/run_loop_testing.mm
new file mode 100644
index 0000000..d20d3ce
--- /dev/null
+++ b/chrome/browser/ui/cocoa/run_loop_testing.mm
@@ -0,0 +1,59 @@
+// Copyright (c) 2011 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/browser/ui/cocoa/run_loop_testing.h"
+
+#import <Foundation/Foundation.h>
+
+#include "base/memory/scoped_nsobject.h"
+#include "base/message_pump_mac.h"
+
+// This class is scheduled with a delayed selector to quit the message pump.
+@interface CocoaQuitTask : NSObject {
+ @private
+ base::MessagePumpNSRunLoop* pump_;
+}
+- (id)initWithMessagePump:(base::MessagePumpNSRunLoop*)pump;
+- (void)doQuit;
+@end
+
+@implementation CocoaQuitTask
+- (id)initWithMessagePump:(base::MessagePumpNSRunLoop*)pump {
+ if ((self = [super init])) {
+ pump_ = pump;
+ }
+ return self;
+}
+
+- (void)doQuit {
+ pump_->Quit();
+}
+@end
+
+////////////////////////////////////////////////////////////////////////////////
+
+namespace chrome {
+namespace testing {
+
+void NSRunLoopRunAllPending() {
+ scoped_refptr<base::MessagePumpNSRunLoop> message_pump(
+ new base::MessagePumpNSRunLoop);
+
+ // Put a delayed selector on the queue. All other pending delayed selectors
+ // will run before this, after which the internal loop can end.
+ scoped_nsobject<CocoaQuitTask> quit_task(
+ [[CocoaQuitTask alloc] initWithMessagePump:message_pump]);
+
+ [quit_task performSelector:@selector(doQuit)
+ withObject:nil
+ afterDelay:0];
+
+ // Spin the internal loop, running it until the quit task is pumped. Pass NULL
+ // because there is no delegate MessageLoop; only the Cocoa work queues will
+ // be pumped.
+ message_pump->Run(NULL);
+}
+
+} // namespace testing
+} // namespace chrome
diff --git a/chrome/browser/ui/cocoa/run_loop_testing_unittest.mm b/chrome/browser/ui/cocoa/run_loop_testing_unittest.mm
new file mode 100644
index 0000000..d281c46
--- /dev/null
+++ b/chrome/browser/ui/cocoa/run_loop_testing_unittest.mm
@@ -0,0 +1,59 @@
+// Copyright (c) 2011 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/browser/ui/cocoa/run_loop_testing.h"
+
+#import <Foundation/Foundation.h>
+
+#include "base/memory/scoped_nsobject.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+@interface TestDelayed : NSObject {
+ @private
+ BOOL didWork_;
+ TestDelayed* next_;
+}
+@property(readonly, nonatomic) BOOL didWork;
+@property(assign, nonatomic) TestDelayed* next;
+@end
+
+@implementation TestDelayed
+@synthesize didWork = didWork_;
+@synthesize next = next_;
+
+- (id)init {
+ if ((self = [super init])) {
+ [self performSelector:@selector(doWork) withObject:nil afterDelay:0];
+ }
+ return self;
+}
+
+- (void)doWork {
+ didWork_ = YES;
+ [next_ performSelector:@selector(doWork) withObject:nil afterDelay:0];
+}
+@end
+
+TEST(RunLoopTesting, RunAllPending) {
+ scoped_nsobject<TestDelayed> tester([[TestDelayed alloc] init]);
+ EXPECT_FALSE([tester didWork]);
+
+ chrome::testing::NSRunLoopRunAllPending();
+
+ EXPECT_TRUE([tester didWork]);
+}
+
+TEST(RunLoopTesting, NestedWork) {
+ scoped_nsobject<TestDelayed> tester([[TestDelayed alloc] init]);
+ scoped_nsobject<TestDelayed> nested([[TestDelayed alloc] init]);
+ [tester setNext:nested];
+
+ EXPECT_FALSE([tester didWork]);
+ EXPECT_FALSE([nested didWork]);
+
+ chrome::testing::NSRunLoopRunAllPending();
+
+ EXPECT_TRUE([tester didWork]);
+ EXPECT_TRUE([nested didWork]);
+}