1 // Copyright 2013 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 "ash/shelf/scoped_observer_with_duplicated_sources.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "testing/gtest/include/gtest/gtest.h"
16 DISALLOW_COPY_AND_ASSIGN(TestObserver
);
21 TestSource() : observer_count_(0) {}
24 void AddObserver(TestObserver
* observer
) {
27 void RemoveObserver(TestObserver
* observer
) {
31 int GetObserverCount() {
32 return observer_count_
;
38 DISALLOW_COPY_AND_ASSIGN(TestSource
);
41 TEST(ScopedObserverWithDuplicatedSourcesTest
, DuplicatedSource
) {
42 TestObserver observer
;
46 ScopedObserverWithDuplicatedSources
<TestSource
, TestObserver
>
48 EXPECT_EQ(0, source1
.GetObserverCount());
49 EXPECT_FALSE(observers
.IsObserving(&source1
));
50 EXPECT_EQ(0, source2
.GetObserverCount());
51 EXPECT_FALSE(observers
.IsObserving(&source2
));
54 observers
.Add(&source1
);
55 EXPECT_EQ(1, source1
.GetObserverCount());
56 EXPECT_TRUE(observers
.IsObserving(&source1
));
57 // AddObserver of TestSource is called only once.
58 observers
.Add(&source1
);
59 EXPECT_EQ(1, source1
.GetObserverCount());
60 EXPECT_TRUE(observers
.IsObserving(&source1
));
63 observers
.Add(&source2
);
64 EXPECT_EQ(1, source2
.GetObserverCount());
65 EXPECT_TRUE(observers
.IsObserving(&source2
));
68 observers
.Remove(&source1
);
69 EXPECT_EQ(1, source1
.GetObserverCount());
70 EXPECT_TRUE(observers
.IsObserving(&source1
));
73 observers
.Remove(&source2
);
74 EXPECT_EQ(0, source2
.GetObserverCount());
75 EXPECT_FALSE(observers
.IsObserving(&source2
));
77 // Remove |source1| again.
78 observers
.Remove(&source1
);
79 // In this time, |observer| is removed from |source1|.
80 EXPECT_EQ(0, source1
.GetObserverCount());
81 EXPECT_FALSE(observers
.IsObserving(&source1
));