Skip to content

Commit

Permalink
Add BrowserWindowInterface mock for split view tests
Browse files Browse the repository at this point in the history
Upstream code chains calls to GetBrowserWindowInterface() in several places now,
preventing the use of TestTabStripModelDelegate as-is in our SplitView unit
tests. This subclass provides a mock implementation of BrowserWindowInterface
that can be returned instead.
  • Loading branch information
emerick committed Sep 8, 2024
1 parent b731a96 commit 8dcf7da
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 7 deletions.
3 changes: 3 additions & 0 deletions browser/ui/tabs/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ source_set("unit_tests") {

if (!is_android) {
sources = [
"browser_window_interface_test_tab_strip_delegate.cc",
"browser_window_interface_test_tab_strip_delegate.h",
"split_view_browser_data_unittest.cc",
"split_view_tab_strip_model_adapter_unittest.cc",
]
Expand All @@ -48,6 +50,7 @@ source_set("unit_tests") {
":test_support",
"//chrome/browser",
"//chrome/browser/ui",
"//chrome/browser/ui/browser_window:browser_window",
"//chrome/test:test_support",
"//chrome/test:test_support_ui",
"//components/tab_groups",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/* Copyright (c) 2024 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "brave/browser/ui/tabs/test/browser_window_interface_test_tab_strip_delegate.h"

#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "url/gurl.h"

BrowserWindowInterfaceTestTabStripModelDelegate::TestBrowserWindowInterface::
TestBrowserWindowInterface(Profile* profile)
: profile_(profile),
session_id_(SessionID::InvalidValue()),
features_(BrowserWindowFeatures::CreateBrowserWindowFeatures()) {}

BrowserWindowInterfaceTestTabStripModelDelegate::TestBrowserWindowInterface::
~TestBrowserWindowInterface() = default;

views::WebView* BrowserWindowInterfaceTestTabStripModelDelegate::
TestBrowserWindowInterface::GetWebView() {
return nullptr;
}

Profile* BrowserWindowInterfaceTestTabStripModelDelegate::
TestBrowserWindowInterface::GetProfile() {
return profile_;
}

void BrowserWindowInterfaceTestTabStripModelDelegate::
TestBrowserWindowInterface::OpenGURL(const GURL& gurl,
WindowOpenDisposition disposition) {}

content::WebContents* BrowserWindowInterfaceTestTabStripModelDelegate::
TestBrowserWindowInterface::OpenURL(
const content::OpenURLParams& params,
base::OnceCallback<void(content::NavigationHandle&)>
navigation_handle_callback) {
return nullptr;
}

const SessionID& BrowserWindowInterfaceTestTabStripModelDelegate::
TestBrowserWindowInterface::GetSessionID() {
return session_id_;
}

bool BrowserWindowInterfaceTestTabStripModelDelegate::
TestBrowserWindowInterface::IsTabStripVisible() {
return false;
}

views::View* BrowserWindowInterfaceTestTabStripModelDelegate::
TestBrowserWindowInterface::TopContainer() {
return nullptr;
}

tabs::TabInterface* BrowserWindowInterfaceTestTabStripModelDelegate::
TestBrowserWindowInterface::GetActiveTabInterface() {
return nullptr;
}

BrowserWindowFeatures& BrowserWindowInterfaceTestTabStripModelDelegate::
TestBrowserWindowInterface::GetFeatures() {
return *features_;
}

web_modal::WebContentsModalDialogHost*
BrowserWindowInterfaceTestTabStripModelDelegate::TestBrowserWindowInterface::
GetWebContentsModalDialogHostForWindow() {
return nullptr;
}

bool BrowserWindowInterfaceTestTabStripModelDelegate::
TestBrowserWindowInterface::IsActive() {
return false;
}

base::CallbackListSubscription BrowserWindowInterfaceTestTabStripModelDelegate::
TestBrowserWindowInterface::RegisterDidBecomeActive(
DidBecomeActiveCallback callback) {
return did_become_active_callback_list_.Add(std::move(callback));
}

base::CallbackListSubscription BrowserWindowInterfaceTestTabStripModelDelegate::
TestBrowserWindowInterface::RegisterDidBecomeInactive(
DidBecomeInactiveCallback callback) {
return did_become_inactive_callback_list_.Add(std::move(callback));
}

ExclusiveAccessManager* BrowserWindowInterfaceTestTabStripModelDelegate::
TestBrowserWindowInterface::GetExclusiveAccessManager() {
return nullptr;
}

BrowserActions* BrowserWindowInterfaceTestTabStripModelDelegate::
TestBrowserWindowInterface::GetActions() {
return nullptr;
}

BrowserWindowInterface::Type BrowserWindowInterfaceTestTabStripModelDelegate::
TestBrowserWindowInterface::GetType() const {
return TYPE_NORMAL;
}

user_education::FeaturePromoController*
BrowserWindowInterfaceTestTabStripModelDelegate::TestBrowserWindowInterface::
GetFeaturePromoController() {
return nullptr;
}

BrowserWindowInterfaceTestTabStripModelDelegate::
BrowserWindowInterfaceTestTabStripModelDelegate(Profile* profile)
: interface_(std::make_unique<TestBrowserWindowInterface>(profile)) {}

BrowserWindowInterfaceTestTabStripModelDelegate::
~BrowserWindowInterfaceTestTabStripModelDelegate() = default;

BrowserWindowInterface*
BrowserWindowInterfaceTestTabStripModelDelegate::GetBrowserWindowInterface() {
return interface_.get();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* Copyright (c) 2024 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include <memory>

#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/tabs/test_tab_strip_model_delegate.h"
#include "components/sessions/core/session_id.h"
#include "url/gurl.h"

class BrowserActions;
class ExclusiveAccessManager;
class Profile;

namespace content {
class WebContents;
}

namespace tabs {
class TabInterface;
}

namespace user_education {
class FeaturePromoController;
}

namespace views {
class WebView;
}

// Implements TestTabStripModelDelegate with a mocked BrowserWindowInterface
class BrowserWindowInterfaceTestTabStripModelDelegate
: public TestTabStripModelDelegate {
public:
class TestBrowserWindowInterface : public BrowserWindowInterface {
public:
explicit TestBrowserWindowInterface(Profile* profile);
~TestBrowserWindowInterface() override;
TestBrowserWindowInterface(const TestBrowserWindowInterface&) = delete;
TestBrowserWindowInterface& operator=(const TestBrowserWindowInterface&) =
delete;

// BrowserWindowInterface:
views::WebView* GetWebView() override;
Profile* GetProfile() override;
void OpenGURL(const GURL& gurl, WindowOpenDisposition disposition) override;
content::WebContents* OpenURL(
const content::OpenURLParams& params,
base::OnceCallback<void(content::NavigationHandle&)>
navigation_handle_callback) override;
const SessionID& GetSessionID() override;
bool IsTabStripVisible() override;
views::View* TopContainer() override;
tabs::TabInterface* GetActiveTabInterface() override;
BrowserWindowFeatures& GetFeatures() override;
web_modal::WebContentsModalDialogHost*
GetWebContentsModalDialogHostForWindow() override;
bool IsActive() override;
base::CallbackListSubscription RegisterDidBecomeActive(
DidBecomeActiveCallback callback) override;
base::CallbackListSubscription RegisterDidBecomeInactive(
DidBecomeInactiveCallback callback) override;
ExclusiveAccessManager* GetExclusiveAccessManager() override;
BrowserActions* GetActions() override;
BrowserWindowInterface::Type GetType() const override;
user_education::FeaturePromoController* GetFeaturePromoController()
override;

private:
raw_ptr<Profile> profile_;
SessionID session_id_;
std::unique_ptr<BrowserWindowFeatures> features_;

using DidBecomeActiveCallbackList =
base::RepeatingCallbackList<void(BrowserWindowInterface*)>;
DidBecomeActiveCallbackList did_become_active_callback_list_;

using DidBecomeInactiveCallbackList =
base::RepeatingCallbackList<void(BrowserWindowInterface*)>;
DidBecomeInactiveCallbackList did_become_inactive_callback_list_;
};

explicit BrowserWindowInterfaceTestTabStripModelDelegate(Profile* profile);
~BrowserWindowInterfaceTestTabStripModelDelegate() override;
BrowserWindowInterfaceTestTabStripModelDelegate(
const BrowserWindowInterfaceTestTabStripModelDelegate&) = delete;
BrowserWindowInterfaceTestTabStripModelDelegate& operator=(
const BrowserWindowInterfaceTestTabStripModelDelegate&) = delete;

// TestTabStripModelDelegate:
BrowserWindowInterface* GetBrowserWindowInterface() override;

private:
std::unique_ptr<BrowserWindowInterface> interface_;
};
24 changes: 20 additions & 4 deletions browser/ui/tabs/test/split_view_browser_data_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@
#include "base/test/gtest_util.h"
#include "base/test/scoped_feature_list.h"
#include "brave/browser/ui/tabs/features.h"
#include "brave/browser/ui/tabs/test/browser_window_interface_test_tab_strip_delegate.h"
#include "brave/chromium_src/chrome/test/base/testing_browser_process.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/tabs/tab_model.h"
#include "chrome/browser/ui/tabs/test_tab_strip_model_delegate.h"
#include "chrome/browser/universal_web_contents_observers.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/common/content_client.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gtest/include/gtest/gtest.h"

class SplitViewBrowserDataUnitTest : public ::testing::Test {
class SplitViewBrowserDataUnitTest : public ::testing::Test,
public content::ContentBrowserClient {
public:
SplitViewBrowserDataUnitTest()
: feature_list_(tabs::features::kBraveSplitView) {}
Expand All @@ -37,7 +42,10 @@ class SplitViewBrowserDataUnitTest : public ::testing::Test {

// ::testing::Test:
void SetUp() override {
delegate_ = std::make_unique<TestTabStripModelDelegate>();
client_ = content::SetBrowserClientForTesting(this);
delegate_ =
std::make_unique<BrowserWindowInterfaceTestTabStripModelDelegate>(
&profile_);
tab_strip_model_ =
std::make_unique<TabStripModel>(delegate_.get(), &profile_);
data_.reset(new SplitViewBrowserData(nullptr));
Expand All @@ -48,6 +56,12 @@ class SplitViewBrowserDataUnitTest : public ::testing::Test {
data_.reset();
tab_strip_model_.reset();
delegate_.reset();
content::SetBrowserClientForTesting(client_);
}

void OnWebContentsCreated(content::WebContents* web_contents) override {
content::ContentBrowserClient::OnWebContentsCreated(web_contents);
AttachUniversalWebContentsObservers(web_contents);
}

private:
Expand All @@ -57,7 +71,9 @@ class SplitViewBrowserDataUnitTest : public ::testing::Test {
content::RenderViewHostTestEnabler render_view_host_test_enabler_;
TestingProfile profile_;

std::unique_ptr<TestTabStripModelDelegate> delegate_;
raw_ptr<content::ContentBrowserClient> client_;

std::unique_ptr<BrowserWindowInterfaceTestTabStripModelDelegate> delegate_;
std::unique_ptr<TabStripModel> tab_strip_model_;

std::unique_ptr<SplitViewBrowserData> data_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include "base/run_loop.h"
#include "brave/browser/ui/tabs/features.h"
#include "brave/browser/ui/tabs/split_view_browser_data.h"
#include "brave/browser/ui/tabs/test/browser_window_interface_test_tab_strip_delegate.h"
#include "chrome/browser/ui/tabs/tab_group_model.h"
#include "chrome/browser/ui/tabs/test_tab_strip_model_delegate.h"
#include "chrome/browser/universal_web_contents_observers.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
Expand Down Expand Up @@ -47,7 +47,9 @@ class SplitViewTabStripModelAdapterUnitTest
void SetUp() override {
client_ = content::SetBrowserClientForTesting(this);

delegate_ = std::make_unique<TestTabStripModelDelegate>();
delegate_ =
std::make_unique<BrowserWindowInterfaceTestTabStripModelDelegate>(
&profile_);
model_ = std::make_unique<TabStripModel>(delegate_.get(), &profile_);

split_view_browser_data_.reset(new SplitViewBrowserData(nullptr));
Expand Down Expand Up @@ -82,7 +84,7 @@ class SplitViewTabStripModelAdapterUnitTest

raw_ptr<content::ContentBrowserClient> client_;

std::unique_ptr<TestTabStripModelDelegate> delegate_;
std::unique_ptr<BrowserWindowInterfaceTestTabStripModelDelegate> delegate_;
std::unique_ptr<TabStripModel> model_;
std::unique_ptr<SplitViewBrowserData> split_view_browser_data_;
raw_ptr<SplitViewTabStripModelAdapter> adapter_;
Expand Down

0 comments on commit 8dcf7da

Please sign in to comment.