Index: ios/clean/chrome/browser/ui/overlays/overlay_queue_unittest.mm |
diff --git a/ios/clean/chrome/browser/ui/overlays/overlay_queue_unittest.mm b/ios/clean/chrome/browser/ui/overlays/overlay_queue_unittest.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f06a123997d29f9b74bc69440371049c0ccb8d91 |
--- /dev/null |
+++ b/ios/clean/chrome/browser/ui/overlays/overlay_queue_unittest.mm |
@@ -0,0 +1,123 @@ |
+// Copyright 2017 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. |
+ |
+#import "ios/clean/chrome/browser/ui/overlays/overlay_queue.h" |
+ |
+#if !defined(__has_feature) || !__has_feature(objc_arc) |
+#error "This file requires ARC support." |
+#endif |
+ |
+#import "base/mac/scoped_nsobject.h" |
+#include "base/memory/ptr_util.h" |
+#import "ios/clean/chrome/browser/ui/commands/overlay_commands.h" |
+#import "ios/web/public/test/fakes/test_web_state.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "testing/gtest_mac.h" |
+#include "testing/platform_test.h" |
+ |
+// Test overlay coordinator subclass. |
+@interface TestOverlayCoordinator : OverlayCoordinator |
+// Whether the completion block has been cancelled. |
+@property(nonatomic, readonly) BOOL completionWasCancelled; |
+// The coordinator on which this has been started. |
+@property(nonatomic, readonly) BrowserCoordinator* webCoordinator; |
+@end |
+ |
+@implementation TestOverlayCoordinator |
+@synthesize completionWasCancelled = _completionWasCancelled; |
+@synthesize webCoordinator = _webCoordinator; |
+ |
+- (void)cancelWebKitCompletion { |
+ _completionWasCancelled = YES; |
+} |
+ |
+- (void)startOverlayingWebCoordinator:(BrowserCoordinator*)webCoordinator { |
+ _webCoordinator = webCoordinator; |
+} |
+ |
+@end |
+ |
+// Test overlay scheduler subclass. |
+@interface TestOverlayScheduler : NSObject<OverlaySchedulerCommands> |
+@property(nonatomic, readonly) web::WebState* nextWebState; |
+@end |
+ |
+@implementation TestOverlayScheduler |
+@synthesize nextWebState = _nextWebState; |
+ |
+- (void)scheduleOverlayForWebState:(web::WebState*)webState { |
+ _nextWebState = webState; |
+} |
+- (void)overlayWasStoppedForWebState:(web::WebState*)webState { |
+} |
+- (void)cancelOverlaysForWebState:(web::WebState*)webState { |
+} |
+@end |
+ |
+class OverlayQueueTest : public PlatformTest { |
+ public: |
+ OverlayQueueTest() |
+ : PlatformTest(), scheduler_([[TestOverlayScheduler alloc] init]) { |
+ OverlayQueue::CreateForWebState(&web_state_, scheduler_); |
+ } |
+ |
+ web::WebState* web_state() { return &web_state_; } |
+ |
+ OverlayQueue* queue() { return OverlayQueue::FromWebState(&web_state_); } |
+ |
+ TestOverlayScheduler* scheduler() { return scheduler_; } |
+ |
+ private: |
+ web::TestWebState web_state_; |
+ base::scoped_nsobject<TestOverlayScheduler> scheduler_; |
+}; |
+ |
+// Tests that adding an overlay to the queue updates state accordingly and |
+// dispatches a |-scheduleOverlayForWebState:| command. |
+TEST_F(OverlayQueueTest, AddOverlay) { |
+ TestOverlayCoordinator* overlay = |
+ [[TestOverlayCoordinator alloc] initWithWebState:web_state()]; |
+ queue()->AddOverlay(overlay); |
+ EXPECT_TRUE(queue()->HasQueuedOverlays()); |
+ EXPECT_FALSE(queue()->IsShowingOverlay()); |
+ EXPECT_EQ(web_state(), scheduler().nextWebState); |
+} |
+ |
+// Tests that starting an overlay updates state and starts the queued overlay. |
+TEST_F(OverlayQueueTest, StartOverlay) { |
+ TestOverlayCoordinator* overlay = |
+ [[TestOverlayCoordinator alloc] initWithWebState:web_state()]; |
+ queue()->AddOverlay(overlay); |
+ BrowserCoordinator* web_coordinator = [[BrowserCoordinator alloc] init]; |
+ queue()->StartNextOverlay(web_coordinator); |
+ EXPECT_TRUE(queue()->IsShowingOverlay()); |
+ EXPECT_EQ(web_coordinator, overlay.webCoordinator); |
+} |
+ |
+// Tests that OverlayWasStopped() updates state properly. |
+TEST_F(OverlayQueueTest, OverlayWasStopped) { |
+ TestOverlayCoordinator* overlay = |
+ [[TestOverlayCoordinator alloc] initWithWebState:web_state()]; |
+ queue()->AddOverlay(overlay); |
+ BrowserCoordinator* web_coordinator = [[BrowserCoordinator alloc] init]; |
+ queue()->StartNextOverlay(web_coordinator); |
+ queue()->OverlayWasStopped(overlay); |
+ EXPECT_FALSE(queue()->HasQueuedOverlays()); |
+ EXPECT_FALSE(queue()->IsShowingOverlay()); |
+} |
+ |
+// Tests that CancelOverlays() cancels all queued overlays for a WebState. |
+TEST_F(OverlayQueueTest, CancelOverlays) { |
+ TestOverlayCoordinator* overlay0 = |
+ [[TestOverlayCoordinator alloc] initWithWebState:web_state()]; |
+ TestOverlayCoordinator* overlay1 = |
+ [[TestOverlayCoordinator alloc] initWithWebState:web_state()]; |
+ queue()->AddOverlay(overlay0); |
+ queue()->AddOverlay(overlay1); |
+ queue()->CancelOverlays(); |
+ EXPECT_FALSE(queue()->HasQueuedOverlays()); |
+ EXPECT_FALSE(queue()->IsShowingOverlay()); |
+ EXPECT_TRUE(overlay0.completionWasCancelled); |
+ EXPECT_TRUE(overlay1.completionWasCancelled); |
+} |