| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/macros.h" | 5 #include "base/macros.h" |
| 6 #include "chrome/browser/browser_process.h" | 6 #include "chrome/browser/browser_process.h" |
| 7 #include "chrome/browser/memory/tab_manager.h" | 7 #include "chrome/browser/memory/tab_manager.h" |
| 8 #include "chrome/browser/memory/tab_manager_observer.h" | 8 #include "chrome/browser/memory/tab_manager_observer.h" |
| 9 #include "chrome/browser/memory/tab_manager_web_contents_data.h" |
| 9 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 11 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
| 12 #include "chrome/test/base/in_process_browser_test.h" | 13 #include "chrome/test/base/in_process_browser_test.h" |
| 13 #include "content/public/test/test_utils.h" | 14 #include "content/public/test/test_utils.h" |
| 14 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 15 | 16 |
| 16 using content::OpenURLParams; | 17 using content::OpenURLParams; |
| 17 using content::WebContents; | 18 using content::WebContents; |
| 18 | 19 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 39 return reinterpret_cast<int64_t>(contents); | 40 return reinterpret_cast<int64_t>(contents); |
| 40 } | 41 } |
| 41 | 42 |
| 42 private: | 43 private: |
| 43 TabStripModel* tab_strip_model_; | 44 TabStripModel* tab_strip_model_; |
| 44 }; | 45 }; |
| 45 | 46 |
| 46 class MockTabManagerObserver : public TabManagerObserver { | 47 class MockTabManagerObserver : public TabManagerObserver { |
| 47 public: | 48 public: |
| 48 MockTabManagerObserver() | 49 MockTabManagerObserver() |
| 49 : nb_events_(0), contents_(nullptr), is_discarded_(false) {} | 50 : nb_events_(0), |
| 51 contents_(nullptr), |
| 52 is_discarded_(false), |
| 53 is_discardable_(true) {} |
| 50 | 54 |
| 51 // TabManagerObserver implementation: | 55 // TabManagerObserver implementation: |
| 52 void OnDiscardedStateChange(content::WebContents* contents, | 56 void OnDiscardedStateChange(content::WebContents* contents, |
| 53 bool is_discarded) override { | 57 bool is_discarded) override { |
| 54 nb_events_++; | 58 nb_events_++; |
| 55 contents_ = contents; | 59 contents_ = contents; |
| 56 is_discarded_ = is_discarded; | 60 is_discarded_ = is_discarded; |
| 57 } | 61 } |
| 58 | 62 |
| 63 void OnDiscardableStateChange(content::WebContents* contents, |
| 64 bool is_discardable) override { |
| 65 nb_events_++; |
| 66 contents_ = contents; |
| 67 is_discardable_ = is_discardable; |
| 68 } |
| 69 |
| 59 int nb_events() const { return nb_events_; } | 70 int nb_events() const { return nb_events_; } |
| 60 WebContents* content() const { return contents_; } | 71 WebContents* content() const { return contents_; } |
| 61 bool is_discarded() const { return is_discarded_; } | 72 bool is_discarded() const { return is_discarded_; } |
| 73 bool is_discardable() const { return is_discardable_; } |
| 62 | 74 |
| 63 private: | 75 private: |
| 64 int nb_events_; | 76 int nb_events_; |
| 65 WebContents* contents_; | 77 WebContents* contents_; |
| 66 bool is_discarded_; | 78 bool is_discarded_; |
| 79 bool is_discardable_; |
| 67 | 80 |
| 68 DISALLOW_COPY_AND_ASSIGN(MockTabManagerObserver); | 81 DISALLOW_COPY_AND_ASSIGN(MockTabManagerObserver); |
| 69 }; | 82 }; |
| 70 | 83 |
| 71 IN_PROC_BROWSER_TEST_F(TabManagerObserverTest, OnDiscardStateChange) { | 84 IN_PROC_BROWSER_TEST_F(TabManagerObserverTest, OnDiscardStateChange) { |
| 72 TabManager* tab_manager = g_browser_process->GetTabManager(); | 85 TabManager* tab_manager = g_browser_process->GetTabManager(); |
| 73 ASSERT_TRUE(tab_manager); | 86 ASSERT_TRUE(tab_manager); |
| 74 auto tsm = browser()->tab_strip_model(); | 87 auto tsm = browser()->tab_strip_model(); |
| 75 set_tab_strip_model(tsm); | 88 set_tab_strip_model(tsm); |
| 76 | 89 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 ContentsId(tabmanager_observer.content())); | 137 ContentsId(tabmanager_observer.content())); |
| 125 EXPECT_FALSE(tabmanager_observer.is_discarded()); | 138 EXPECT_FALSE(tabmanager_observer.is_discarded()); |
| 126 | 139 |
| 127 // After removing the observer from the TabManager's list, it shouldn't | 140 // After removing the observer from the TabManager's list, it shouldn't |
| 128 // receive events anymore. | 141 // receive events anymore. |
| 129 tab_manager->RemoveObserver(&tabmanager_observer); | 142 tab_manager->RemoveObserver(&tabmanager_observer); |
| 130 EXPECT_TRUE(tab_manager->DiscardTabById(ContentsId(GetContents(index_1)))); | 143 EXPECT_TRUE(tab_manager->DiscardTabById(ContentsId(GetContents(index_1)))); |
| 131 EXPECT_EQ(4, tabmanager_observer.nb_events()); | 144 EXPECT_EQ(4, tabmanager_observer.nb_events()); |
| 132 } | 145 } |
| 133 | 146 |
| 147 IN_PROC_BROWSER_TEST_F(TabManagerObserverTest, OnDiscardableStateChange) { |
| 148 TabManager* tab_manager = g_browser_process->GetTabManager(); |
| 149 ASSERT_TRUE(tab_manager); |
| 150 auto tsm = browser()->tab_strip_model(); |
| 151 set_tab_strip_model(tsm); |
| 152 |
| 153 // Open two tabs. |
| 154 OpenURLParams open(GURL(chrome::kChromeUIAboutURL), content::Referrer(), |
| 155 NEW_BACKGROUND_TAB, ui::PAGE_TRANSITION_TYPED, false); |
| 156 WebContents* contents = browser()->OpenURL(open); |
| 157 |
| 158 // Subscribe observer to TabManager's observer list. |
| 159 MockTabManagerObserver observer; |
| 160 tab_manager->AddObserver(&observer); |
| 161 |
| 162 // No events initially. |
| 163 EXPECT_EQ(0, observer.nb_events()); |
| 164 |
| 165 // Should maintain at zero since the default value of the state is true. |
| 166 tab_manager->GetWebContentsData(contents)->SetDiscardableState(true); |
| 167 EXPECT_EQ(0, observer.nb_events()); |
| 168 |
| 169 // Now it has to change. |
| 170 tab_manager->GetWebContentsData(contents)->SetDiscardableState(false); |
| 171 EXPECT_EQ(1, observer.nb_events()); |
| 172 EXPECT_FALSE(observer.is_discardable()); |
| 173 EXPECT_EQ(ContentsId(contents), ContentsId(observer.content())); |
| 174 |
| 175 // No changes since it's not a new state. |
| 176 tab_manager->GetWebContentsData(contents)->SetDiscardableState(false); |
| 177 EXPECT_EQ(1, observer.nb_events()); |
| 178 |
| 179 // Change it back and we should have another event. |
| 180 tab_manager->GetWebContentsData(contents)->SetDiscardableState(true); |
| 181 EXPECT_EQ(2, observer.nb_events()); |
| 182 EXPECT_TRUE(observer.is_discardable()); |
| 183 EXPECT_EQ(ContentsId(contents), ContentsId(observer.content())); |
| 184 } |
| 185 |
| 134 } // namespace memory | 186 } // namespace memory |
| 135 | 187 |
| 136 #endif // OS_WIN || OS_MAXOSX || OS_LINUX | 188 #endif // OS_WIN || OS_MAXOSX || OS_LINUX |
| OLD | NEW |