|
| 1 | +// Copyright (c) 2025 The Brave Authors. All rights reserved. |
| 2 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +// License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 4 | +// You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + |
| 6 | +#include "chrome/browser/ui/webui/tab_search/tab_search_page_handler.h" |
| 7 | + |
| 8 | +#include <memory> |
| 9 | +#include <string> |
| 10 | + |
| 11 | +#include "base/memory/raw_ptr.h" |
| 12 | +#include "base/strings/string_number_conversions.h" |
| 13 | +#include "base/strings/utf_string_conversions.h" |
| 14 | +#include "base/test/bind.h" |
| 15 | +#include "base/test/gmock_callback_support.h" |
| 16 | +#include "base/test/mock_callback.h" |
| 17 | +#include "brave/components/ai_chat/core/browser/engine/mock_engine_consumer.h" |
| 18 | +#include "brave/components/ai_chat/core/browser/types.h" |
| 19 | +#include "chrome/browser/profiles/profile.h" |
| 20 | +#include "chrome/browser/ui/browser.h" |
| 21 | +#include "chrome/browser/ui/browser_list.h" |
| 22 | +#include "chrome/browser/ui/browser_tabstrip.h" |
| 23 | +#include "chrome/browser/ui/test/test_browser_closed_waiter.h" |
| 24 | +#include "chrome/browser/ui/webui/tab_search/tab_search_ui.h" |
| 25 | +#include "chrome/common/webui_url_constants.h" |
| 26 | +#include "chrome/test/base/in_process_browser_test.h" |
| 27 | +#include "chrome/test/base/ui_test_utils.h" |
| 28 | +#include "components/grit/brave_components_strings.h" |
| 29 | +#include "content/public/browser/navigation_controller.h" |
| 30 | +#include "content/public/test/browser_test.h" |
| 31 | +#include "content/public/test/browser_test_utils.h" |
| 32 | +#include "content/public/test/test_utils.h" |
| 33 | +#include "testing/gmock/include/gmock/gmock.h" |
| 34 | +#include "testing/gtest/include/gtest/gtest.h" |
| 35 | +#include "ui/base/l10n/l10n_util.h" |
| 36 | +#include "url/gurl.h" |
| 37 | +#include "url/origin.h" |
| 38 | + |
| 39 | +namespace { |
| 40 | + |
| 41 | +constexpr char kFooDotComUrl1[] = "https://foo.com/1"; |
| 42 | +constexpr char kFooDotComUrl2[] = "https://foo.com/2"; |
| 43 | +constexpr char kBarDotComUrl1[] = "https://bar.com/1"; |
| 44 | +constexpr char kBarDotComUrl2[] = "https://bar.com/2"; |
| 45 | + |
| 46 | +constexpr char kFooDotComTitle1[] = "foo.com 1"; |
| 47 | +constexpr char kFooDotComTitle2[] = "foo.com 2"; |
| 48 | +constexpr char kBarDotComTitle1[] = "bar.com 1"; |
| 49 | +constexpr char kBarDotComTitle2[] = "bar.com 2"; |
| 50 | + |
| 51 | +} // namespace |
| 52 | + |
| 53 | +using testing::_; |
| 54 | + |
| 55 | +class TabSearchPageHandlerBrowserTest : public InProcessBrowserTest { |
| 56 | + public: |
| 57 | + void SetUpOnMainThread() override { |
| 58 | + InProcessBrowserTest::SetUpOnMainThread(); |
| 59 | + webui_contents_ = content::WebContents::Create( |
| 60 | + content::WebContents::CreateParams(browser()->profile())); |
| 61 | + |
| 62 | + webui_contents_->GetController().LoadURLWithParams( |
| 63 | + content::NavigationController::LoadURLParams( |
| 64 | + GURL(chrome::kChromeUITabSearchURL))); |
| 65 | + |
| 66 | + // Finish loading after initializing. |
| 67 | + ASSERT_TRUE(content::WaitForLoadStop(webui_contents_.get())); |
| 68 | + |
| 69 | + ui_test_utils::OpenNewEmptyWindowAndWaitUntilActivated(profile1()); |
| 70 | + browser2_ = BrowserList::GetInstance()->GetLastActive(); |
| 71 | + } |
| 72 | + |
| 73 | + void TearDownOnMainThread() override { |
| 74 | + webui_contents_.reset(); |
| 75 | + InProcessBrowserTest::TearDownOnMainThread(); |
| 76 | + } |
| 77 | + |
| 78 | + TabSearchPageHandler* handler() { |
| 79 | + return webui_contents_->GetWebUI() |
| 80 | + ->GetController() |
| 81 | + ->template GetAs<TabSearchUI>() |
| 82 | + ->page_handler_for_testing(); |
| 83 | + } |
| 84 | + |
| 85 | + Profile* profile1() { return browser()->profile(); } |
| 86 | + |
| 87 | + void AppendTabWithTitle(Browser* browser, |
| 88 | + const GURL& url, |
| 89 | + const std::string& title) { |
| 90 | + chrome::AddTabAt(browser, url, -1, true); |
| 91 | + content::WebContents* web_contents = |
| 92 | + browser->tab_strip_model()->GetActiveWebContents(); |
| 93 | + web_contents->UpdateTitleForEntry( |
| 94 | + web_contents->GetController().GetLastCommittedEntry(), |
| 95 | + base::UTF8ToUTF16(title)); |
| 96 | + } |
| 97 | + |
| 98 | + // browser1 is a normal window with default profile. |
| 99 | + Browser* browser1() { return browser(); } |
| 100 | + |
| 101 | + // browser2 is a normal window with default profile. |
| 102 | + Browser* browser2() { return browser2_; } |
| 103 | + |
| 104 | + protected: |
| 105 | + std::unique_ptr<content::WebContents> webui_contents_; |
| 106 | + raw_ptr<Browser> browser2_; |
| 107 | +}; |
| 108 | + |
| 109 | +IN_PROC_BROWSER_TEST_F(TabSearchPageHandlerBrowserTest, GetFocusTabs) { |
| 110 | + // Test Engine's GetFocusTabs is called with expected tabs info and topic. |
| 111 | + handler()->SetAIChatEngineForTesting( |
| 112 | + std::make_unique<testing::NiceMock<ai_chat::MockEngineConsumer>>()); |
| 113 | + |
| 114 | + // Add tabs in windows with the default profile. |
| 115 | + AppendTabWithTitle(browser1(), GURL(kFooDotComUrl1), kFooDotComTitle1); |
| 116 | + AppendTabWithTitle(browser1(), GURL(kFooDotComUrl2), kFooDotComTitle2); |
| 117 | + AppendTabWithTitle(browser2(), GURL(kBarDotComUrl1), kBarDotComTitle1); |
| 118 | + AppendTabWithTitle(browser2(), GURL(kBarDotComUrl2), kBarDotComTitle2); |
| 119 | + |
| 120 | + ASSERT_EQ(browser1()->tab_strip_model()->count(), 3); |
| 121 | + ASSERT_EQ(browser2()->tab_strip_model()->count(), 3); |
| 122 | + |
| 123 | + const int tab_id1 = |
| 124 | + browser1()->tab_strip_model()->GetTabAtIndex(1)->GetHandle().raw_value(); |
| 125 | + const int tab_id2 = |
| 126 | + browser1()->tab_strip_model()->GetTabAtIndex(2)->GetHandle().raw_value(); |
| 127 | + const int tab_id3 = |
| 128 | + browser2()->tab_strip_model()->GetTabAtIndex(1)->GetHandle().raw_value(); |
| 129 | + const int tab_id4 = |
| 130 | + browser2()->tab_strip_model()->GetTabAtIndex(2)->GetHandle().raw_value(); |
| 131 | + |
| 132 | + std::vector<ai_chat::Tab> expected_tabs = { |
| 133 | + {base::NumberToString(tab_id1), kFooDotComTitle1, |
| 134 | + url::Origin::Create(GURL(kFooDotComUrl1))}, |
| 135 | + {base::NumberToString(tab_id2), kFooDotComTitle2, |
| 136 | + url::Origin::Create(GURL(kFooDotComUrl2))}, |
| 137 | + {base::NumberToString(tab_id3), kBarDotComTitle1, |
| 138 | + url::Origin::Create(GURL(kBarDotComUrl1))}, |
| 139 | + {base::NumberToString(tab_id4), kBarDotComTitle2, |
| 140 | + url::Origin::Create(GURL(kBarDotComUrl2))}, |
| 141 | + }; |
| 142 | + |
| 143 | + std::vector<std::string> mock_ret_tabs = {base::NumberToString(tab_id1), |
| 144 | + "100", "invalid", |
| 145 | + base::NumberToString(tab_id4)}; |
| 146 | + auto* mock_engine = static_cast<ai_chat::MockEngineConsumer*>( |
| 147 | + handler()->GetAIChatEngineForTesting()); |
| 148 | + EXPECT_CALL(*mock_engine, GetFocusTabs(expected_tabs, "topic", _)) |
| 149 | + .WillOnce(base::test::RunOnceCallback<2>(mock_ret_tabs)); |
| 150 | + |
| 151 | + handler()->GetFocusTabs("topic", base::BindLambdaForTesting( |
| 152 | + [&](bool new_window_created, |
| 153 | + tab_search::mojom::ErrorPtr error) { |
| 154 | + EXPECT_TRUE(new_window_created); |
| 155 | + EXPECT_FALSE(error); |
| 156 | + })); |
| 157 | + |
| 158 | + BrowserList* browser_list = BrowserList::GetInstance(); |
| 159 | + Browser* active_browser = browser_list->GetLastActive(); |
| 160 | + ASSERT_EQ(browser_list->size(), 3u) << "A new window should be created."; |
| 161 | + ASSERT_EQ(active_browser, browser_list->get(2)) |
| 162 | + << "The new window should be active."; |
| 163 | + EXPECT_EQ(active_browser->tab_strip_model()->count(), 2) |
| 164 | + << "The new window should have 2 tabs."; |
| 165 | + // Check the tabs are moved to the new window as expected. |
| 166 | + EXPECT_EQ(active_browser->tab_strip_model() |
| 167 | + ->GetTabAtIndex(0) |
| 168 | + ->GetHandle() |
| 169 | + .raw_value(), |
| 170 | + tab_id1); |
| 171 | + EXPECT_EQ(active_browser->tab_strip_model() |
| 172 | + ->GetTabAtIndex(1) |
| 173 | + ->GetHandle() |
| 174 | + .raw_value(), |
| 175 | + tab_id4); |
| 176 | + |
| 177 | + // Test undo. |
| 178 | + handler()->UndoFocusTabs(base::BindLambdaForTesting([&]() { |
| 179 | + // Wait for the new window to be closed. |
| 180 | + ASSERT_EQ(browser_list->size(), 3u); |
| 181 | + ASSERT_TRUE( |
| 182 | + TestBrowserClosedWaiter(browser_list->get(2)).WaitUntilClosed()); |
| 183 | + |
| 184 | + Browser* browser1 = browser_list->get(0); |
| 185 | + EXPECT_EQ(browser1->tab_strip_model()->count(), 3) |
| 186 | + << "The tabs should be moved back to the previous active window."; |
| 187 | + EXPECT_EQ( |
| 188 | + browser1->tab_strip_model()->GetTabAtIndex(1)->GetHandle().raw_value(), |
| 189 | + tab_id1); |
| 190 | + Browser* browser2 = browser_list->get(1); |
| 191 | + EXPECT_EQ(browser2->tab_strip_model()->count(), 3) |
| 192 | + << "The tabs should be moved back to the previous active window."; |
| 193 | + EXPECT_EQ( |
| 194 | + browser2->tab_strip_model()->GetTabAtIndex(2)->GetHandle().raw_value(), |
| 195 | + tab_id4); |
| 196 | + })); |
| 197 | +} |
0 commit comments