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 ~MockWebContentsCloseHandlerDelegate() override
{}
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 void CloneWebContentsLayer() override
{ got_clone_
= true; }
32 void DestroyClonedLayer() override
{ got_destroy_
= true; }
35 base::MessageLoopForUI message_loop_
;
39 DISALLOW_COPY_AND_ASSIGN(MockWebContentsCloseHandlerDelegate
);
42 // -----------------------------------------------------------------------------
44 class WebContentsCloseHandlerTest
: public testing::Test
{
46 WebContentsCloseHandlerTest() : close_handler_(&close_handler_delegate_
) {}
47 ~WebContentsCloseHandlerTest() override
{}
50 bool IsTimerRunning() const {
51 return close_handler_
.timer_
.IsRunning();
54 MockWebContentsCloseHandlerDelegate close_handler_delegate_
;
55 WebContentsCloseHandler close_handler_
;
58 DISALLOW_COPY_AND_ASSIGN(WebContentsCloseHandlerTest
);
61 // Verifies ActiveTabChanged() sends the right functions to the delegate.
62 TEST_F(WebContentsCloseHandlerTest
, ChangingActiveTabDestroys
) {
63 close_handler_
.ActiveTabChanged();
64 EXPECT_TRUE(close_handler_delegate_
.got_destroy());
65 EXPECT_FALSE(close_handler_delegate_
.got_clone());
66 EXPECT_FALSE(IsTimerRunning());
69 // Verifies ActiveTabChanged() while in a close does nothing.
70 TEST_F(WebContentsCloseHandlerTest
, DontCloneOnChangeWhenClosing
) {
71 close_handler_
.WillCloseAllTabs();
72 EXPECT_FALSE(close_handler_delegate_
.got_destroy());
73 EXPECT_TRUE(close_handler_delegate_
.got_clone());
74 EXPECT_FALSE(IsTimerRunning());
75 close_handler_delegate_
.Clear();
77 close_handler_
.ActiveTabChanged();
78 EXPECT_FALSE(close_handler_delegate_
.got_destroy());
79 EXPECT_FALSE(close_handler_delegate_
.got_clone());
80 EXPECT_FALSE(IsTimerRunning());
83 // Verifies the timer is started after a close.
84 TEST_F(WebContentsCloseHandlerTest
, DontDestroyImmediatleyAfterCancel
) {
85 close_handler_
.WillCloseAllTabs();
86 close_handler_delegate_
.Clear();
87 close_handler_
.CloseAllTabsCanceled();
88 EXPECT_FALSE(close_handler_delegate_
.got_destroy());
89 EXPECT_FALSE(close_handler_delegate_
.got_clone());
90 EXPECT_TRUE(IsTimerRunning());