Home | History | Annotate | Download | only in frame
      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   virtual ~MockWebContentsCloseHandlerDelegate() {}
     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   virtual void CloneWebContentsLayer() OVERRIDE {
     32     got_clone_ = true;
     33   }
     34   virtual void DestroyClonedLayer() OVERRIDE {
     35     got_destroy_ = true;
     36   }
     37 
     38  private:
     39   base::MessageLoopForUI message_loop_;
     40   bool got_clone_;
     41   bool got_destroy_;
     42 
     43   DISALLOW_COPY_AND_ASSIGN(MockWebContentsCloseHandlerDelegate);
     44 };
     45 
     46 // -----------------------------------------------------------------------------
     47 
     48 class WebContentsCloseHandlerTest : public testing::Test {
     49  public:
     50   WebContentsCloseHandlerTest() : close_handler_(&close_handler_delegate_) {}
     51   virtual ~WebContentsCloseHandlerTest() {}
     52 
     53  protected:
     54   bool IsTimerRunning() const {
     55     return close_handler_.timer_.IsRunning();
     56   }
     57 
     58   MockWebContentsCloseHandlerDelegate close_handler_delegate_;
     59   WebContentsCloseHandler close_handler_;
     60 
     61  private:
     62   DISALLOW_COPY_AND_ASSIGN(WebContentsCloseHandlerTest);
     63 };
     64 
     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());
     71 }
     72 
     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();
     80 
     81   close_handler_.ActiveTabChanged();
     82   EXPECT_FALSE(close_handler_delegate_.got_destroy());
     83   EXPECT_FALSE(close_handler_delegate_.got_clone());
     84   EXPECT_FALSE(IsTimerRunning());
     85 }
     86 
     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());
     95 }
     96 
     97