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

Side by Side Diff: ios/clean/chrome/browser/ui/overlay_service/internal/overlay_queue_unittest.mm

Issue 2921833002: [iOS Clean] Created OverlayService.
Patch Set: rebase & Mark's comments Created 3 years, 5 months 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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.
4
5 #import "ios/clean/chrome/browser/ui/overlay_service/internal/overlay_queue.h"
6
7 #import "base/mac/scoped_nsobject.h"
8 #include "base/memory/ptr_util.h"
9 #import "ios/clean/chrome/browser/ui/overlay_service/browser_coordinator+overlay _support.h"
10 #import "ios/clean/chrome/browser/ui/overlay_service/internal/overlay_queue_obse rver.h"
11 #import "ios/shared/chrome/browser/ui/coordinators/browser_coordinator+internal. h"
12 #import "ios/web/public/test/fakes/test_web_state.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/gtest_mac.h"
15 #include "testing/platform_test.h"
16
17 #if !defined(__has_feature) || !__has_feature(objc_arc)
18 #error "This file requires ARC support."
19 #endif
20
21 // BrowserCoordinator that can be used as the parent for overlays.
22 @interface ParentCoordinator : BrowserCoordinator
23 @end
24
25 @implementation ParentCoordinator
26 - (UIViewController*)viewController {
27 return nil;
28 }
29 @end
30
31 // Test OverlayQueue implementation. Subclass-specific functionality will be
32 // tested in other files.
33 class TestOverlayQueue : public OverlayQueue {
34 public:
35 TestOverlayQueue() : parent_([[ParentCoordinator alloc] init]) {}
36 void StartNextOverlay() override {
37 [parent_ addChildCoordinator:GetFirstOverlay()];
38 [GetFirstOverlay() start];
39 OverlayWasStarted();
40 }
41 void AddOverlay(BrowserCoordinator* overlay) {
42 OverlayQueue::AddOverlay(overlay);
43 }
44
45 private:
46 ParentCoordinator* parent_;
47 };
48
49 // Test BrowserCoordinator.
50 @interface TestBrowserCoordinator : BrowserCoordinator {
51 OverlayQueue* _overlayQueue;
52 }
53 @property(nonatomic, readonly) BOOL cancelled;
54 @end
55
56 @implementation TestBrowserCoordinator
57 @synthesize cancelled = _cancelled;
58 - (void)stop {
59 [super stop];
60 [self overlayWasStopped];
61 }
62 - (void)cancelOverlay {
63 _cancelled = YES;
64 }
65 @end
66
67 @implementation TestBrowserCoordinator (OverlaySupport)
68 - (BOOL)supportsOverlaying {
69 return YES;
70 }
71 - (void)setOverlayQueue:(OverlayQueue*)overlayQueue {
72 _overlayQueue = overlayQueue;
73 }
74 - (OverlayQueue*)overlayQueue {
75 return _overlayQueue;
76 }
77 @end
78
79 // Test OverlayQueueObserver.
80 class TestOverlayQueueObserver : public OverlayQueueObserver {
81 public:
82 TestOverlayQueueObserver()
83 : did_add_called_(false),
84 will_replace_called_(false),
85 stop_visible_called_(false),
86 did_cancel_called_(false) {}
87
88 void OverlayQueueDidAddOverlay(OverlayQueue* queue) override {
89 did_add_called_ = true;
90 }
91 void OverlayQueueWillReplaceVisibleOverlay(OverlayQueue* queue) override {
92 will_replace_called_ = true;
93 }
94 void OverlayQueueDidStopVisibleOverlay(OverlayQueue* queue) override {
95 stop_visible_called_ = true;
96 }
97 void OverlayQueueDidCancelOverlays(OverlayQueue* queue) override {
98 did_cancel_called_ = true;
99 }
100
101 bool did_add_called() { return did_add_called_; }
102 bool will_replace_called() { return will_replace_called_; }
103 bool stop_visible_called() { return stop_visible_called_; }
104 bool did_cancel_called() { return did_cancel_called_; }
105
106 private:
107 bool did_add_called_;
108 bool will_replace_called_;
109 bool stop_visible_called_;
110 bool did_cancel_called_;
111 };
112
113 class OverlayQueueTest : public PlatformTest {
114 public:
115 OverlayQueueTest() : PlatformTest() { queue_.AddObserver(&observer_); }
116
117 TestOverlayQueue& queue() { return queue_; }
118 TestOverlayQueueObserver& observer() { return observer_; }
119
120 private:
121 TestOverlayQueue queue_;
122 TestOverlayQueueObserver observer_;
123 };
124
125 // Tests that adding an overlay to the queue updates state accordingly and
126 // notifies observers.
127 TEST_F(OverlayQueueTest, AddOverlay) {
128 TestBrowserCoordinator* overlay = [[TestBrowserCoordinator alloc] init];
129 queue().AddOverlay(overlay);
130 EXPECT_TRUE(queue().HasQueuedOverlays());
131 EXPECT_FALSE(queue().IsShowingOverlay());
132 EXPECT_TRUE(observer().did_add_called());
133 }
134
135 // Tests that OverlayWasStopped() updates state properly.
136 TEST_F(OverlayQueueTest, OverlayWasStopped) {
137 TestBrowserCoordinator* overlay = [[TestBrowserCoordinator alloc] init];
138 queue().AddOverlay(overlay);
139 queue().StartNextOverlay();
140 queue().OverlayWasStopped(overlay);
141 EXPECT_FALSE(queue().HasQueuedOverlays());
142 EXPECT_FALSE(queue().IsShowingOverlay());
143 EXPECT_TRUE(observer().stop_visible_called());
144 }
145
146 // Tests that the observer is notified when
147 TEST_F(OverlayQueueTest, ReplaceOverlay) {
148 TestBrowserCoordinator* overlay = [[TestBrowserCoordinator alloc] init];
149 queue().AddOverlay(overlay);
150 queue().StartNextOverlay();
151 TestBrowserCoordinator* replacement = [[TestBrowserCoordinator alloc] init];
152 queue().ReplaceVisibleOverlay(replacement);
153 EXPECT_TRUE(observer().will_replace_called());
154 EXPECT_TRUE(observer().stop_visible_called());
155 }
156
157 // Tests that CancelOverlays() cancels all queued overlays for a WebState.
158 TEST_F(OverlayQueueTest, CancelOverlays) {
159 TestBrowserCoordinator* overlay0 = [[TestBrowserCoordinator alloc] init];
160 TestBrowserCoordinator* overlay1 = [[TestBrowserCoordinator alloc] init];
161 queue().AddOverlay(overlay0);
162 queue().AddOverlay(overlay1);
163 queue().CancelOverlays();
164 EXPECT_FALSE(queue().HasQueuedOverlays());
165 EXPECT_FALSE(queue().IsShowingOverlay());
166 EXPECT_TRUE(observer().did_cancel_called());
167 EXPECT_TRUE(overlay0.cancelled);
168 EXPECT_TRUE(overlay1.cancelled);
169 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698