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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
// Copyright 2022 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/ui/views/web_apps/deprecated_apps_dialog_view.h"
#include <set>
#include <string_view>
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/strings/cstring_view.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/logging_chrome.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/common/extension_id.h"
#include "extensions/test/test_extension_dir.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/views/widget/any_widget_observer.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_observer.h"
namespace {
constexpr char kMockName1[] = "Test App1";
constexpr char kMockName2[] = "Test App2";
constexpr char kMockUrl1[] = "https://www.app1.com/index.html";
constexpr char kMockUrl2[] = "https://www.app2.com/index.html";
constexpr char kMockHost1[] = "app1.com";
constexpr char kMockHost2[] = "app2.com";
} // namespace
class DeprecatedAppDialogWidgetObserver : public views::WidgetObserver {
public:
explicit DeprecatedAppDialogWidgetObserver(views::Widget* widget)
: widget_(widget) {
DCHECK(widget_);
widget_->AddObserver(this);
}
~DeprecatedAppDialogWidgetObserver() override = default;
void Wait() { run_loop_.Run(); }
// views::WidgetObserver:
void OnWidgetDestroying(views::Widget* widget) override {
widget_->RemoveObserver(this);
widget_ = nullptr;
run_loop_.Quit();
}
private:
raw_ptr<views::Widget> widget_;
base::RunLoop run_loop_;
};
class DeprecatedAppsDialogViewBrowserTest
: public extensions::ExtensionBrowserTest {
public:
DeprecatedAppsDialogViewBrowserTest() = default;
DeprecatedAppsDialogViewBrowserTest(
const DeprecatedAppsDialogViewBrowserTest&) = delete;
DeprecatedAppsDialogViewBrowserTest& operator=(
const DeprecatedAppsDialogViewBrowserTest&) = delete;
bool IsDialogShown() {
if (test_dialog_view_) {
return true;
}
return false;
}
std::string_view ClickDeprecatedDialogLinkString() {
return "document.querySelector('body > "
"deprecated-apps-link').shadowRoot.querySelector('#deprecated-apps-"
"link').click()";
}
void WaitForDialogToBeDestroyed() {
if (!test_dialog_view_) {
return;
}
DeprecatedAppDialogWidgetObserver dialog_observer(
test_dialog_view_.get()->GetWidget());
dialog_observer.Wait();
test_dialog_view_ = nullptr;
}
// A return value of -1 means that the table has not been initialized
// for the dialog.
int GetRowCountForDialog() {
views::TableView* table_for_dialog = GetTableViewForTesting();
if (table_for_dialog) {
return table_for_dialog->GetRowCount();
}
return -1;
}
views::TableView* GetTableViewForTesting() {
if (IsDialogShown()) {
return static_cast<views::TableView*>(test_dialog_view_->GetViewByID(
DeprecatedAppsDialogView::DEPRECATED_APPS_TABLE));
}
return nullptr;
}
extensions::ExtensionId InstallExtensionForTesting(base::cstring_view name,
std::string_view url,
base::cstring_view host) {
extensions::TestExtensionDir test_app_dir;
static constexpr char kMockAppManifest[] = R"({
"name": "%s",
"version": "1",
"manifest_version": 2,
"app": {
"launch": {
"web_url": "%s"
},
"urls": ["*://%s/"]
}
})";
test_app_dir.WriteManifest(
base::StringPrintf(kMockAppManifest, name.c_str(),
GURL(url).spec().c_str(), host.c_str()));
const extensions::Extension* app = InstallExtensionWithSourceAndFlags(
test_app_dir.UnpackedPath(), /*expected_change=*/1,
extensions::mojom::ManifestLocation::kInternal,
extensions::Extension::NO_FLAGS);
DCHECK(app);
deprecated_app_ids_for_testing_.insert(app->id());
return app->id();
}
protected:
std::set<extensions::ExtensionId> deprecated_app_ids_for_testing_;
base::WeakPtr<DeprecatedAppsDialogView> test_dialog_view_;
};
IN_PROC_BROWSER_TEST_F(DeprecatedAppsDialogViewBrowserTest,
VerifyTableModelForSingleApp) {
auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
InstallExtensionForTesting(kMockName1, kMockUrl1, kMockHost1);
test_dialog_view_ =
DeprecatedAppsDialogView::CreateAndShowDialog(
std::string(), deprecated_app_ids_for_testing_, web_contents)
->AsWeakPtr();
EXPECT_TRUE(IsDialogShown());
EXPECT_EQ(static_cast<int>(deprecated_app_ids_for_testing_.size()),
GetRowCountForDialog());
}
IN_PROC_BROWSER_TEST_F(DeprecatedAppsDialogViewBrowserTest,
VerifyTableModelForMultipleApps) {
auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
InstallExtensionForTesting(kMockName1, kMockUrl1, kMockHost1);
InstallExtensionForTesting(kMockName2, kMockUrl2, kMockHost2);
test_dialog_view_ =
DeprecatedAppsDialogView::CreateAndShowDialog(
std::string(), deprecated_app_ids_for_testing_, web_contents)
->AsWeakPtr();
EXPECT_TRUE(IsDialogShown());
EXPECT_EQ(static_cast<int>(deprecated_app_ids_for_testing_.size()),
GetRowCountForDialog());
}
IN_PROC_BROWSER_TEST_F(DeprecatedAppsDialogViewBrowserTest,
AcceptDialogAndVerify) {
auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
extensions::ExtensionId test_id(
InstallExtensionForTesting(kMockName1, kMockUrl1, kMockHost1));
test_dialog_view_ =
DeprecatedAppsDialogView::CreateAndShowDialog(
std::string(), deprecated_app_ids_for_testing_, web_contents)
->AsWeakPtr();
// Verify dialog is shown.
ASSERT_TRUE(IsDialogShown());
EXPECT_EQ(static_cast<int>(deprecated_app_ids_for_testing_.size()),
GetRowCountForDialog());
// Verify dialog is closed on acceptance.
ASSERT_TRUE(test_dialog_view_->Accept());
WaitForDialogToBeDestroyed();
ASSERT_FALSE(IsDialogShown());
// Verify successful uninstallation of app.
ASSERT_TRUE(
extensions::ExtensionRegistry::Get(web_contents->GetBrowserContext())
->GetInstalledExtension(test_id) == nullptr);
}
IN_PROC_BROWSER_TEST_F(DeprecatedAppsDialogViewBrowserTest,
DialogDoesNotLoadOnNavigationToChromeApps) {
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAppsURL)));
ASSERT_FALSE(IsDialogShown());
}
IN_PROC_BROWSER_TEST_F(DeprecatedAppsDialogViewBrowserTest,
DeprecatedAppsDialogShownFromLinkClick) {
auto* web_contents = browser()->tab_strip_model()->GetActiveWebContents();
InstallExtensionForTesting(kMockName1, kMockUrl1, kMockHost1);
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAppsURL)));
auto waiter = views::NamedWidgetShownWaiter(
views::test::AnyWidgetTestPasskey{}, "DeprecatedAppsDialogView");
// TODO: handle return value.
std::ignore =
content::EvalJs(web_contents, ClickDeprecatedDialogLinkString());
EXPECT_NE(waiter.WaitIfNeededAndGet(), nullptr);
}
|