Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / frame / web_contents_close_handler_unittest.cc
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.
4
5 #include "chrome/browser/ui/views/frame/web_contents_close_handler.h"
6
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"
10
11 class MockWebContentsCloseHandlerDelegate
12     : public WebContentsCloseHandlerDelegate {
13  public:
14   explicit MockWebContentsCloseHandlerDelegate()
15       : got_clone_(false),
16         got_destroy_(false) {
17   }
18   ~MockWebContentsCloseHandlerDelegate() override {}
19
20   void Clear() {
21     got_clone_ = got_destroy_ = false;
22   }
23
24   bool got_clone() const { return got_clone_; }
25   void clear_got_clone() { got_clone_ = false; }
26
27   bool got_destroy() const { return got_destroy_; }
28   void clear_got_destroy() { got_destroy_ = false; }
29
30   // WebContentsCloseHandlerDelegate:
31   void CloneWebContentsLayer() override { got_clone_ = true; }
32   void DestroyClonedLayer() override { got_destroy_ = true; }
33
34  private:
35   base::MessageLoopForUI message_loop_;
36   bool got_clone_;
37   bool got_destroy_;
38
39   DISALLOW_COPY_AND_ASSIGN(MockWebContentsCloseHandlerDelegate);
40 };
41
42 // -----------------------------------------------------------------------------
43
44 class WebContentsCloseHandlerTest : public testing::Test {
45  public:
46   WebContentsCloseHandlerTest() : close_handler_(&close_handler_delegate_) {}
47   ~WebContentsCloseHandlerTest() override {}
48
49  protected:
50   bool IsTimerRunning() const {
51     return close_handler_.timer_.IsRunning();
52   }
53
54   MockWebContentsCloseHandlerDelegate close_handler_delegate_;
55   WebContentsCloseHandler close_handler_;
56
57  private:
58   DISALLOW_COPY_AND_ASSIGN(WebContentsCloseHandlerTest);
59 };
60
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());
67 }
68
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();
76
77   close_handler_.ActiveTabChanged();
78   EXPECT_FALSE(close_handler_delegate_.got_destroy());
79   EXPECT_FALSE(close_handler_delegate_.got_clone());
80   EXPECT_FALSE(IsTimerRunning());
81 }
82
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());
91 }
92