summaryrefslogtreecommitdiffstats
path: root/base/observer_list_unittest.cc
diff options
context:
space:
mode:
authorwillchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-17 19:33:06 +0000
committerwillchan@chromium.org <willchan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-17 19:33:06 +0000
commit3c0d45e1a201dfabfefecf3f2bb81411b0272c37 (patch)
treefe73f0c57428370b71fe5b97403a10eb0e118704 /base/observer_list_unittest.cc
parent3e5fe0f02d7347d2d913f237ad8f06cb8a9defec (diff)
downloadchromium_src-3c0d45e1a201dfabfefecf3f2bb81411b0272c37.zip
chromium_src-3c0d45e1a201dfabfefecf3f2bb81411b0272c37.tar.gz
chromium_src-3c0d45e1a201dfabfefecf3f2bb81411b0272c37.tar.bz2
Fix removal of multiple observers in ObserverListThreadSafe.
BUG=55725 TEST=ObserverListThreadSafeTest.RemoveMultipleObservers Review URL: http://codereview.chromium.org/3381009 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@59833 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/observer_list_unittest.cc')
-rw-r--r--base/observer_list_unittest.cc45
1 files changed, 44 insertions, 1 deletions
diff --git a/base/observer_list_unittest.cc b/base/observer_list_unittest.cc
index 6982d3d..71c97d3 100644
--- a/base/observer_list_unittest.cc
+++ b/base/observer_list_unittest.cc
@@ -2,9 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/message_loop.h"
#include "base/observer_list.h"
#include "base/observer_list_threadsafe.h"
+
+#include <vector>
+
+#include "base/message_loop.h"
#include "base/platform_thread.h"
#include "base/ref_counted.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -224,6 +227,46 @@ TEST(ObserverListThreadSafeTest, BasicTest) {
EXPECT_EQ(d.total, -10);
}
+class FooRemover : public Foo {
+ public:
+ explicit FooRemover(ObserverListThreadSafe<Foo>* list) : list_(list) {}
+ virtual ~FooRemover() {}
+
+ void AddFooToRemove(Foo* foo) {
+ foos_.push_back(foo);
+ }
+
+ virtual void Observe(int x) {
+ std::vector<Foo*> tmp;
+ tmp.swap(foos_);
+ for (std::vector<Foo*>::iterator it = tmp.begin();
+ it != tmp.end(); ++it) {
+ list_->RemoveObserver(*it);
+ }
+ }
+
+ private:
+ const scoped_refptr<ObserverListThreadSafe<Foo> > list_;
+ std::vector<Foo*> foos_;
+};
+
+TEST(ObserverListThreadSafeTest, RemoveMultipleObservers) {
+ MessageLoop loop;
+ scoped_refptr<ObserverListThreadSafe<Foo> > observer_list(
+ new ObserverListThreadSafe<Foo>);
+
+ FooRemover a(observer_list);
+ Adder b(1);
+
+ observer_list->AddObserver(&a);
+ observer_list->AddObserver(&b);
+
+ a.AddFooToRemove(&a);
+ a.AddFooToRemove(&b);
+
+ observer_list->Notify(&Foo::Observe, 1);
+ loop.RunAllPending();
+}
// A test driver for a multi-threaded notification loop. Runs a number
// of observer threads, each of which constantly adds/removes itself