File: browser_list_router_helper_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (167 lines) | stat: -rw-r--r-- 6,315 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/sync/sessions/browser_list_router_helper.h"

#include <memory>
#include <vector>

#include "base/containers/contains.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/resource_coordinator/tab_manager.h"
#include "chrome/browser/sync/sessions/sync_sessions_web_contents_router.h"
#include "chrome/browser/sync/sessions/sync_sessions_web_contents_router_factory.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/sync_sessions/synced_tab_delegate.h"

namespace sync_sessions {

class MockLocalSessionEventHandler : public LocalSessionEventHandler {
 public:
  void OnSessionRestoreComplete() override {}

  void OnLocalTabModified(SyncedTabDelegate* modified_tab) override {
    seen_urls_.push_back(modified_tab->GetVirtualURLAtIndex(
        modified_tab->GetCurrentEntryIndex()));
    seen_ids_.push_back(modified_tab->GetSessionId());
  }

  void OnLocalTabClosed() override {}

  std::vector<GURL>* seen_urls() { return &seen_urls_; }
  std::vector<SessionID>* seen_ids() { return &seen_ids_; }

 private:
  std::vector<GURL> seen_urls_;
  std::vector<SessionID> seen_ids_;
};

class BrowserListRouterHelperTest : public BrowserWithTestWindowTest {
 protected:
  ~BrowserListRouterHelperTest() override = default;

  MockLocalSessionEventHandler handler_1;
  MockLocalSessionEventHandler handler_2;
};

TEST_F(BrowserListRouterHelperTest, ObservationScopedToSingleProfile) {
  TestingProfile* profile_1 = profile();
  TestingProfile* profile_2 =
      profile_manager()->CreateTestingProfile("testing_profile2");

  std::unique_ptr<BrowserWindow> window_2(CreateBrowserWindow());
  std::unique_ptr<Browser> browser_2(
      CreateBrowser(profile_2, browser()->type(), false, window_2.get()));

  SyncSessionsWebContentsRouterFactory::GetInstance()
      ->GetForProfile(profile_1)
      ->StartRoutingTo(&handler_1);
  SyncSessionsWebContentsRouterFactory::GetInstance()
      ->GetForProfile(profile_2)
      ->StartRoutingTo(&handler_2);

  GURL gurl_1("http://foo1.com");
  GURL gurl_2("http://foo2.com");
  AddTab(browser(), gurl_1);
  AddTab(browser_2.get(), gurl_2);

  std::vector<GURL>* handler_1_urls = handler_1.seen_urls();
  EXPECT_TRUE(base::Contains(*handler_1_urls, gurl_1));
  EXPECT_FALSE(base::Contains(*handler_1_urls, gurl_2));

  std::vector<GURL>* handler_2_urls = handler_2.seen_urls();
  EXPECT_TRUE(base::Contains(*handler_2_urls, gurl_2));
  EXPECT_FALSE(base::Contains(*handler_2_urls, gurl_1));

  // Add a browser for each profile.
  std::unique_ptr<BrowserWindow> window_3(CreateBrowserWindow());
  std::unique_ptr<BrowserWindow> window_4(CreateBrowserWindow());

  std::unique_ptr<Browser> new_browser_in_first_profile(
      CreateBrowser(profile_1, browser()->type(), false, window_3.get()));
  std::unique_ptr<Browser> new_browser_in_second_profile(
      CreateBrowser(profile_2, browser()->type(), false, window_4.get()));

  GURL gurl_3("http://foo3.com");
  GURL gurl_4("http://foo4.com");
  AddTab(new_browser_in_first_profile.get(), gurl_3);
  AddTab(new_browser_in_second_profile.get(), gurl_4);

  handler_1_urls = handler_1.seen_urls();
  EXPECT_TRUE(base::Contains(*handler_1_urls, gurl_3));
  EXPECT_FALSE(base::Contains(*handler_1_urls, gurl_4));

  handler_2_urls = handler_2.seen_urls();
  EXPECT_TRUE(base::Contains(*handler_2_urls, gurl_4));
  EXPECT_FALSE(base::Contains(*handler_2_urls, gurl_3));

  // Cleanup needed for manually created browsers so they don't complain about
  // having open tabs when destructing.
  browser_2->tab_strip_model()->CloseAllTabs();
  new_browser_in_first_profile->tab_strip_model()->CloseAllTabs();
  new_browser_in_second_profile->tab_strip_model()->CloseAllTabs();
}

// Added when fixing https://crbug.com/777745, ensure tab discards are observed.
TEST_F(BrowserListRouterHelperTest, NotifyOnDiscardTab) {
  TestingProfile* profile_1 = profile();
  TestingProfile* profile_2 =
      profile_manager()->CreateTestingProfile("testing_profile2");

  std::unique_ptr<BrowserWindow> window_2(CreateBrowserWindow());
  std::unique_ptr<Browser> browser_2(
      CreateBrowser(profile_2, browser()->type(), false, window_2.get()));

  SyncSessionsWebContentsRouterFactory::GetInstance()
      ->GetForProfile(profile_1)
      ->StartRoutingTo(&handler_1);
  SyncSessionsWebContentsRouterFactory::GetInstance()
      ->GetForProfile(profile_2)
      ->StartRoutingTo(&handler_2);

  GURL gurl_1("http://foo1.com");
  AddTab(browser(), gurl_1);

  // Tab needs to have been active to be found when discarding.
  BrowserList::GetInstance()->SetLastActive(browser());

  EXPECT_EQ(gurl_1, *handler_1.seen_urls()->rbegin());
  SessionID old_id = *handler_1.seen_ids()->rbegin();

  // Remove previous any observations from setup to make checking expectations
  // easier below.
  handler_1.seen_urls()->clear();
  handler_1.seen_ids()->clear();

  g_browser_process->GetTabManager()->DiscardTabByExtension(
      browser()->tab_strip_model()->GetWebContentsAt(0));

  // We're typically notified twice while discarding tabs. Once for the
  // destruction of the old web contents, and once for the new. This test case
  // is really trying to make sure the TabReplacedAt() method is called, which
  // is going to be invoked for the new web contents. We can tell it is the new
  // one by finding |gurl_1| for an id that is not |old_id|.
  bool found_new_id = false;
  for (size_t i = 0; i < handler_1.seen_ids()->size(); ++i) {
    if (handler_1.seen_ids()->at(i) != old_id &&
        handler_1.seen_urls()->at(i) == gurl_1) {
      found_new_id = true;
      break;
    }
  }
  EXPECT_TRUE(found_new_id);

  // And of course |profile_2| shouldn't have seen anything.
  EXPECT_EQ(0U, handler_2.seen_urls()->size());

  // Cleanup needed for manually created browsers so they don't complain about
  // having open tabs when destructing.
  browser_2->tab_strip_model()->CloseAllTabs();
}

}  // namespace sync_sessions