1 // Copyright 2014 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 "chrome/browser/ui/views/frame/web_contents_close_handler.h"
7 #include "base/message_loop/message_loop.h"
8 #include "chrome/browser/ui/views/frame/web_contents_close_handler_delegate.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 class MockWebContentsCloseHandlerDelegate
12 : public WebContentsCloseHandlerDelegate
{
14 explicit MockWebContentsCloseHandlerDelegate()
18 virtual ~MockWebContentsCloseHandlerDelegate() {}
21 got_clone_
= got_destroy_
= false;
24 bool got_clone() const { return got_clone_
; }
25 void clear_got_clone() { got_clone_
= false; }
27 bool got_destroy() const { return got_destroy_
; }
28 void clear_got_destroy() { got_destroy_
= false; }
30 // WebContentsCloseHandlerDelegate:
31 virtual void CloneWebContentsLayer() OVERRIDE
{
34 virtual void DestroyClonedLayer() OVERRIDE
{
39 base::MessageLoopForUI message_loop_
;
43 DISALLOW_COPY_AND_ASSIGN(MockWebContentsCloseHandlerDelegate
);
46 // -----------------------------------------------------------------------------
48 class WebContentsCloseHandlerTest
: public testing::Test
{
50 WebContentsCloseHandlerTest() : close_handler_(&close_handler_delegate_
) {}
51 virtual ~WebContentsCloseHandlerTest() {}
54 bool IsTimerRunning() const {
55 return close_handler_
.timer_
.IsRunning();
58 MockWebContentsCloseHandlerDelegate close_handler_delegate_
;
59 WebContentsCloseHandler close_handler_
;
62 DISALLOW_COPY_AND_ASSIGN(WebContentsCloseHandlerTest
);
65 // Verifies ActiveTabChanged() sends the right functions to the delegate.
66 TEST_F(WebContentsCloseHandlerTest
, ChangingActiveTabDestroys
) {
67 close_handler_
.ActiveTabChanged();
68 EXPECT_TRUE(close_handler_delegate_
.got_destroy());
69 EXPECT_FALSE(close_handler_delegate_
.got_clone());
70 EXPECT_FALSE(IsTimerRunning());
73 // Verifies ActiveTabChanged() while in a close does nothing.
74 TEST_F(WebContentsCloseHandlerTest
, DontCloneOnChangeWhenClosing
) {
75 close_handler_
.WillCloseAllTabs();
76 EXPECT_FALSE(close_handler_delegate_
.got_destroy());
77 EXPECT_TRUE(close_handler_delegate_
.got_clone());
78 EXPECT_FALSE(IsTimerRunning());
79 close_handler_delegate_
.Clear();
81 close_handler_
.ActiveTabChanged();
82 EXPECT_FALSE(close_handler_delegate_
.got_destroy());
83 EXPECT_FALSE(close_handler_delegate_
.got_clone());
84 EXPECT_FALSE(IsTimerRunning());
87 // Verifies the timer is started after a close.
88 TEST_F(WebContentsCloseHandlerTest
, DontDestroyImmediatleyAfterCancel
) {
89 close_handler_
.WillCloseAllTabs();
90 close_handler_delegate_
.Clear();
91 close_handler_
.CloseAllTabsCanceled();
92 EXPECT_FALSE(close_handler_delegate_
.got_destroy());
93 EXPECT_FALSE(close_handler_delegate_
.got_clone());
94 EXPECT_TRUE(IsTimerRunning());