Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(804)

Side by Side Diff: chrome/browser/memory/tab_manager_observer_browsertest.cc

Issue 2898033002: [TabManager] Move TabManager into chrome/browser/resource_coordinator. (Closed)
Patch Set: rebase Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/macros.h"
6 #include "chrome/browser/browser_process.h"
7 #include "chrome/browser/memory/tab_manager.h"
8 #include "chrome/browser/memory/tab_manager_observer.h"
9 #include "chrome/browser/memory/tab_manager_web_contents_data.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/common/url_constants.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "content/public/test/test_utils.h"
15 #include "url/gurl.h"
16
17 using content::OpenURLParams;
18 using content::WebContents;
19
20 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_LINUX)
21
22 namespace memory {
23
24 class TabManagerObserverTest : public InProcessBrowserTest {
25 public:
26 TabManagerObserverTest() {}
27
28 // Helper functions.
29 void set_tab_strip_model(TabStripModel* tsm) { tab_strip_model_ = tsm; }
30
31 int GetIndex(WebContents* contents) {
32 return tab_strip_model_->GetIndexOfWebContents(contents);
33 }
34
35 WebContents* GetContents(int index) {
36 return tab_strip_model_->GetWebContentsAt(index);
37 }
38
39 int64_t ContentsId(WebContents* contents) {
40 return TabManager::IdFromWebContents(contents);
41 }
42
43 private:
44 TabStripModel* tab_strip_model_;
45 };
46
47 class MockTabManagerObserver : public TabManagerObserver {
48 public:
49 MockTabManagerObserver()
50 : nb_events_(0),
51 contents_(nullptr),
52 is_discarded_(false),
53 is_auto_discardable_(true) {}
54
55 // TabManagerObserver implementation:
56 void OnDiscardedStateChange(content::WebContents* contents,
57 bool is_discarded) override {
58 nb_events_++;
59 contents_ = contents;
60 is_discarded_ = is_discarded;
61 }
62
63 void OnAutoDiscardableStateChange(content::WebContents* contents,
64 bool is_auto_discardable) override {
65 nb_events_++;
66 contents_ = contents;
67 is_auto_discardable_ = is_auto_discardable;
68 }
69
70 int nb_events() const { return nb_events_; }
71 WebContents* content() const { return contents_; }
72 bool is_discarded() const { return is_discarded_; }
73 bool is_auto_discardable() const { return is_auto_discardable_; }
74
75 private:
76 int nb_events_;
77 WebContents* contents_;
78 bool is_discarded_;
79 bool is_auto_discardable_;
80
81 DISALLOW_COPY_AND_ASSIGN(MockTabManagerObserver);
82 };
83
84 IN_PROC_BROWSER_TEST_F(TabManagerObserverTest, OnDiscardStateChange) {
85 TabManager* tab_manager = g_browser_process->GetTabManager();
86 auto* tsm = browser()->tab_strip_model();
87 set_tab_strip_model(tsm);
88
89 // Open two tabs.
90 OpenURLParams open1(GURL(chrome::kChromeUIAboutURL), content::Referrer(),
91 WindowOpenDisposition::NEW_BACKGROUND_TAB,
92 ui::PAGE_TRANSITION_TYPED, false);
93 int index_1 = GetIndex(browser()->OpenURL(open1));
94
95 OpenURLParams open2(GURL(chrome::kChromeUICreditsURL), content::Referrer(),
96 WindowOpenDisposition::NEW_BACKGROUND_TAB,
97 ui::PAGE_TRANSITION_TYPED, false);
98 int index_2 = GetIndex(browser()->OpenURL(open2));
99
100 // Subscribe observer to TabManager's observer list.
101 MockTabManagerObserver tabmanager_observer;
102 tab_manager->AddObserver(&tabmanager_observer);
103
104 // Discards both tabs and make sure the events were observed properly.
105 EXPECT_TRUE(tab_manager->DiscardTabById(ContentsId(GetContents(index_1))));
106 EXPECT_EQ(1, tabmanager_observer.nb_events());
107 EXPECT_EQ(ContentsId(GetContents(index_1)),
108 ContentsId(tabmanager_observer.content()));
109 EXPECT_TRUE(tabmanager_observer.is_discarded());
110
111 EXPECT_TRUE(tab_manager->DiscardTabById(ContentsId(GetContents(index_2))));
112 EXPECT_EQ(2, tabmanager_observer.nb_events());
113 EXPECT_EQ(ContentsId(GetContents(index_2)),
114 ContentsId(tabmanager_observer.content()));
115 EXPECT_TRUE(tabmanager_observer.is_discarded());
116
117 // Discarding an already discarded tab shouldn't fire the observers.
118 EXPECT_FALSE(tab_manager->DiscardTabById(ContentsId(GetContents(index_1))));
119 EXPECT_EQ(2, tabmanager_observer.nb_events());
120
121 // Reload tab 1.
122 tsm->ActivateTabAt(index_1, false);
123 EXPECT_EQ(index_1, tsm->active_index());
124 EXPECT_EQ(3, tabmanager_observer.nb_events());
125 EXPECT_EQ(ContentsId(GetContents(index_1)),
126 ContentsId(tabmanager_observer.content()));
127 EXPECT_FALSE(tabmanager_observer.is_discarded());
128
129 // Reloading a tab that's not discarded shouldn't fire the observers.
130 tsm->ActivateTabAt(index_1, false);
131 EXPECT_EQ(3, tabmanager_observer.nb_events());
132
133 // Reload tab 2.
134 tsm->ActivateTabAt(index_2, false);
135 EXPECT_EQ(index_2, tsm->active_index());
136 EXPECT_EQ(4, tabmanager_observer.nb_events());
137 EXPECT_EQ(ContentsId(GetContents(index_2)),
138 ContentsId(tabmanager_observer.content()));
139 EXPECT_FALSE(tabmanager_observer.is_discarded());
140
141 // After removing the observer from the TabManager's list, it shouldn't
142 // receive events anymore.
143 tab_manager->RemoveObserver(&tabmanager_observer);
144 EXPECT_TRUE(tab_manager->DiscardTabById(ContentsId(GetContents(index_1))));
145 EXPECT_EQ(4, tabmanager_observer.nb_events());
146 }
147
148 IN_PROC_BROWSER_TEST_F(TabManagerObserverTest, OnAutoDiscardableStateChange) {
149 TabManager* tab_manager = g_browser_process->GetTabManager();
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 WindowOpenDisposition::NEW_BACKGROUND_TAB,
156 ui::PAGE_TRANSITION_TYPED, false);
157 WebContents* contents = browser()->OpenURL(open);
158
159 // Subscribe observer to TabManager's observer list.
160 MockTabManagerObserver observer;
161 tab_manager->AddObserver(&observer);
162
163 // No events initially.
164 EXPECT_EQ(0, observer.nb_events());
165
166 // Should maintain at zero since the default value of the state is true.
167 tab_manager->SetTabAutoDiscardableState(contents, true);
168 EXPECT_EQ(0, observer.nb_events());
169
170 // Now it has to change.
171 tab_manager->SetTabAutoDiscardableState(contents, false);
172 EXPECT_EQ(1, observer.nb_events());
173 EXPECT_FALSE(observer.is_auto_discardable());
174 EXPECT_EQ(ContentsId(contents), ContentsId(observer.content()));
175
176 // No changes since it's not a new state.
177 tab_manager->SetTabAutoDiscardableState(contents, false);
178 EXPECT_EQ(1, observer.nb_events());
179
180 // Change it back and we should have another event.
181 tab_manager->SetTabAutoDiscardableState(contents, true);
182 EXPECT_EQ(2, observer.nb_events());
183 EXPECT_TRUE(observer.is_auto_discardable());
184 EXPECT_EQ(ContentsId(contents), ContentsId(observer.content()));
185 }
186
187 } // namespace memory
188
189 #endif // OS_WIN || OS_MAXOSX || OS_LINUX
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698