Commit 284e239d authored by nancy's avatar nancy Committed by Commit Bot

Fix the unit tests when AppService context menu feature is enabled.

BUG=1038487

Change-Id: Ie93ef246c20053ee5c34c1c4dcba7fe5ecd93ed9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2026847
Commit-Queue: Nancy Wang <nancylingwang@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#737077}
parent 36eefc3e
...@@ -82,7 +82,12 @@ AppServiceShelfContextMenu::AppServiceShelfContextMenu( ...@@ -82,7 +82,12 @@ AppServiceShelfContextMenu::AppServiceShelfContextMenu(
apps::AppServiceProxy* proxy = apps::AppServiceProxy* proxy =
apps::AppServiceProxyFactory::GetForProfile(controller->profile()); apps::AppServiceProxyFactory::GetForProfile(controller->profile());
DCHECK(proxy); DCHECK(proxy);
app_type_ = proxy->AppRegistryCache().GetAppType(item->id.app_id);
// Remove the ARC shelf group Prefix.
const arc::ArcAppShelfId arc_shelf_id =
arc::ArcAppShelfId::FromString(item->id.app_id);
DCHECK(arc_shelf_id.valid());
app_type_ = proxy->AppRegistryCache().GetAppType(arc_shelf_id.app_id());
} }
AppServiceShelfContextMenu::~AppServiceShelfContextMenu() = default; AppServiceShelfContextMenu::~AppServiceShelfContextMenu() = default;
...@@ -260,6 +265,14 @@ void AppServiceShelfContextMenu::OnGetMenuModel( ...@@ -260,6 +265,14 @@ void AppServiceShelfContextMenu::OnGetMenuModel(
if (app_type_ == apps::mojom::AppType::kExtension) if (app_type_ == apps::mojom::AppType::kExtension)
BuildExtensionAppShortcutsMenu(menu_model.get()); BuildExtensionAppShortcutsMenu(menu_model.get());
// When Crostini generates shelf id with the prefix "crostini:", AppService
// can't generate the menu items, because the app_id doesn't match, so add the
// menu items at UI side, based on the app running status.
if (base::StartsWith(item().id.app_id, crostini::kCrostiniAppIdPrefix,
base::CompareCase::SENSITIVE)) {
BuildCrostiniAppMenu(menu_model.get());
}
std::move(callback).Run(std::move(menu_model)); std::move(callback).Run(std::move(menu_model));
} }
...@@ -284,14 +297,32 @@ void AppServiceShelfContextMenu::BuildArcAppShortcutsMenu( ...@@ -284,14 +297,32 @@ void AppServiceShelfContextMenu::BuildArcAppShortcutsMenu(
const ArcAppListPrefs* arc_prefs = const ArcAppListPrefs* arc_prefs =
ArcAppListPrefs::Get(controller()->profile()); ArcAppListPrefs::Get(controller()->profile());
DCHECK(arc_prefs); DCHECK(arc_prefs);
const arc::ArcAppShelfId& arc_shelf_id =
arc::ArcAppShelfId::FromString(item().id.app_id);
DCHECK(arc_shelf_id.valid());
std::unique_ptr<ArcAppListPrefs::AppInfo> app_info = std::unique_ptr<ArcAppListPrefs::AppInfo> app_info =
arc_prefs->GetApp(item().id.app_id); arc_prefs->GetApp(arc_shelf_id.app_id());
if (!app_info) { if (!app_info && !arc_shelf_id.has_shelf_group_id()) {
LOG(ERROR) << "App " << item().id.app_id << " is not available."; LOG(ERROR) << "App " << item().id.app_id << " is not available.";
std::move(callback).Run(std::move(menu_model)); std::move(callback).Run(std::move(menu_model));
return; return;
} }
if (arc_shelf_id.has_shelf_group_id()) {
const bool app_is_open = controller()->IsOpen(item().id);
if (!app_is_open && !app_info->suspended) {
DCHECK(app_info->launchable);
AddContextMenuOption(menu_model.get(), ash::MENU_OPEN_NEW,
IDS_APP_CONTEXT_MENU_ACTIVATE_ARC);
}
if (app_is_open) {
AddContextMenuOption(menu_model.get(), ash::MENU_CLOSE,
IDS_SHELF_CONTEXT_MENU_CLOSE);
}
}
arc_shortcuts_menu_builder_ = arc_shortcuts_menu_builder_ =
std::make_unique<arc::ArcAppShortcutsMenuBuilder>( std::make_unique<arc::ArcAppShortcutsMenuBuilder>(
controller()->profile(), item().id.app_id, display_id(), controller()->profile(), item().id.app_id, display_id(),
...@@ -300,6 +331,17 @@ void AppServiceShelfContextMenu::BuildArcAppShortcutsMenu( ...@@ -300,6 +331,17 @@ void AppServiceShelfContextMenu::BuildArcAppShortcutsMenu(
app_info->package_name, std::move(menu_model), std::move(callback)); app_info->package_name, std::move(menu_model), std::move(callback));
} }
void AppServiceShelfContextMenu::BuildCrostiniAppMenu(
ui::SimpleMenuModel* menu_model) {
if (controller()->IsOpen(item().id)) {
AddContextMenuOption(menu_model, ash::MENU_CLOSE,
IDS_SHELF_CONTEXT_MENU_CLOSE);
} else {
AddContextMenuOption(menu_model, ash::MENU_OPEN_NEW,
IDS_APP_CONTEXT_MENU_ACTIVATE_ARC);
}
}
void AppServiceShelfContextMenu::ShowAppInfo() { void AppServiceShelfContextMenu::ShowAppInfo() {
if (app_type_ == apps::mojom::AppType::kArc) { if (app_type_ == apps::mojom::AppType::kArc) {
chrome::ShowAppManagementPage(controller()->profile(), item().id.app_id); chrome::ShowAppManagementPage(controller()->profile(), item().id.app_id);
...@@ -391,14 +433,15 @@ extensions::LaunchType AppServiceShelfContextMenu::GetExtensionLaunchType() ...@@ -391,14 +433,15 @@ extensions::LaunchType AppServiceShelfContextMenu::GetExtensionLaunchType()
bool AppServiceShelfContextMenu::ShouldAddPinMenu() { bool AppServiceShelfContextMenu::ShouldAddPinMenu() {
switch (app_type_) { switch (app_type_) {
case apps::mojom::AppType::kArc: { case apps::mojom::AppType::kArc: {
const arc::ArcAppShelfId& app_id = const arc::ArcAppShelfId& arc_shelf_id =
arc::ArcAppShelfId::FromString(item().id.app_id); arc::ArcAppShelfId::FromString(item().id.app_id);
DCHECK(arc_shelf_id.valid());
const ArcAppListPrefs* arc_list_prefs = const ArcAppListPrefs* arc_list_prefs =
ArcAppListPrefs::Get(controller()->profile()); ArcAppListPrefs::Get(controller()->profile());
DCHECK(arc_list_prefs); DCHECK(arc_list_prefs);
std::unique_ptr<ArcAppListPrefs::AppInfo> app_info = std::unique_ptr<ArcAppListPrefs::AppInfo> app_info =
arc_list_prefs->GetApp(app_id.app_id()); arc_list_prefs->GetApp(arc_shelf_id.app_id());
if (!app_id.has_shelf_group_id() && app_info->launchable) if (!arc_shelf_id.has_shelf_group_id() && app_info->launchable)
return true; return true;
return false; return false;
} }
...@@ -421,7 +464,6 @@ bool AppServiceShelfContextMenu::ShouldAddPinMenu() { ...@@ -421,7 +464,6 @@ bool AppServiceShelfContextMenu::ShouldAddPinMenu() {
case apps::mojom::AppType::kWeb: case apps::mojom::AppType::kWeb:
return true; return true;
default: default:
NOTREACHED();
return false; return false;
} }
} }
...@@ -53,6 +53,10 @@ class AppServiceShelfContextMenu : public ShelfContextMenu { ...@@ -53,6 +53,10 @@ class AppServiceShelfContextMenu : public ShelfContextMenu {
void BuildArcAppShortcutsMenu(std::unique_ptr<ui::SimpleMenuModel> menu_model, void BuildArcAppShortcutsMenu(std::unique_ptr<ui::SimpleMenuModel> menu_model,
GetMenuModelCallback callback); GetMenuModelCallback callback);
// Build the menu items based on the app running status for the Crostini shelf
// id with the prefix "crostini:".
void BuildCrostiniAppMenu(ui::SimpleMenuModel* menu_model);
void ShowAppInfo(); void ShowAppInfo();
// Helpers to set the launch type. // Helpers to set the launch type.
......
...@@ -35,9 +35,11 @@ ...@@ -35,9 +35,11 @@
#include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
#include "chrome/browser/ui/ash/launcher/extension_shelf_context_menu.h" #include "chrome/browser/ui/ash/launcher/extension_shelf_context_menu.h"
#include "chrome/browser/ui/ash/launcher/internal_app_shelf_context_menu.h" #include "chrome/browser/ui/ash/launcher/internal_app_shelf_context_menu.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/chrome_ash_test_base.h" #include "chrome/test/base/chrome_ash_test_base.h"
#include "chrome/test/base/testing_profile.h" #include "chrome/test/base/testing_profile.h"
#include "components/arc/metrics/arc_metrics_constants.h" #include "components/arc/metrics/arc_metrics_constants.h"
#include "components/arc/mojom/app.mojom.h"
#include "components/arc/test/fake_app_instance.h" #include "components/arc/test/fake_app_instance.h"
#include "components/exo/shell_surface_util.h" #include "components/exo/shell_surface_util.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
...@@ -74,8 +76,8 @@ class ShelfContextMenuTest : public ChromeAshTestBase { ...@@ -74,8 +76,8 @@ class ShelfContextMenuTest : public ChromeAshTestBase {
~ShelfContextMenuTest() override = default; ~ShelfContextMenuTest() override = default;
void SetUp() override { void SetUp() override {
arc_test_.SetUp(&profile_);
app_service_test_.SetUp(&profile_); app_service_test_.SetUp(&profile_);
arc_test_.SetUp(&profile_);
session_manager_ = std::make_unique<session_manager::SessionManager>(); session_manager_ = std::make_unique<session_manager::SessionManager>();
ChromeAshTestBase::SetUp(); ChromeAshTestBase::SetUp();
model_ = std::make_unique<ash::ShelfModel>(); model_ = std::make_unique<ash::ShelfModel>();
...@@ -152,6 +154,22 @@ class ShelfContextMenuTest : public ChromeAshTestBase { ...@@ -152,6 +154,22 @@ class ShelfContextMenuTest : public ChromeAshTestBase {
ash::ShelfModel* model() { return model_.get(); } ash::ShelfModel* model() { return model_.get(); }
void SendRefreshAppList(const std::vector<arc::mojom::AppInfo>& apps) {
arc_test_.app_instance()->SendRefreshAppList(apps);
app_service_test_.FlushMojoCalls();
}
void LaunchApp(const std::string& app_id,
const arc::mojom::AppInfo& info,
int32_t task_id) {
arc::LaunchApp(profile(), app_id, ui::EF_LEFT_MOUSE_BUTTON,
arc::UserInteractionType::NOT_USER_INITIATED);
// AppService checks the task id to decide whether the app is running, so
// create the task id to simulate the running app.
arc_test_.app_instance()->SendTaskCreated(task_id, info, std::string());
}
private: private:
TestingProfile profile_; TestingProfile profile_;
ArcAppTest arc_test_; ArcAppTest arc_test_;
...@@ -363,7 +381,7 @@ TEST_F(ShelfContextMenuTest, ArcLauncherMenusCheck) { ...@@ -363,7 +381,7 @@ TEST_F(ShelfContextMenuTest, ArcLauncherMenusCheck) {
TEST_F(ShelfContextMenuTest, ArcLauncherSuspendAppMenu) { TEST_F(ShelfContextMenuTest, ArcLauncherSuspendAppMenu) {
arc::mojom::AppInfo app = arc_test().fake_apps()[0]; arc::mojom::AppInfo app = arc_test().fake_apps()[0];
app.suspended = true; app.suspended = true;
arc_test().app_instance()->SendRefreshAppList({app}); SendRefreshAppList({app});
const std::string app_id = ArcAppTest::GetAppId(app); const std::string app_id = ArcAppTest::GetAppId(app);
controller()->PinAppWithID(app_id); controller()->PinAppWithID(app_id);
...@@ -389,9 +407,8 @@ TEST_F(ShelfContextMenuTest, ArcLauncherSuspendAppMenu) { ...@@ -389,9 +407,8 @@ TEST_F(ShelfContextMenuTest, ArcLauncherSuspendAppMenu) {
} }
TEST_F(ShelfContextMenuTest, ArcDeferredShelfContextMenuItemCheck) { TEST_F(ShelfContextMenuTest, ArcDeferredShelfContextMenuItemCheck) {
arc_test().app_instance()->SendRefreshAppList( SendRefreshAppList(std::vector<arc::mojom::AppInfo>(
std::vector<arc::mojom::AppInfo>(arc_test().fake_apps().begin(), arc_test().fake_apps().begin(), arc_test().fake_apps().begin() + 2));
arc_test().fake_apps().begin() + 2));
const std::string app_id1 = ArcAppTest::GetAppId(arc_test().fake_apps()[0]); const std::string app_id1 = ArcAppTest::GetAppId(arc_test().fake_apps()[0]);
const std::string app_id2 = ArcAppTest::GetAppId(arc_test().fake_apps()[1]); const std::string app_id2 = ArcAppTest::GetAppId(arc_test().fake_apps()[1]);
...@@ -405,10 +422,8 @@ TEST_F(ShelfContextMenuTest, ArcDeferredShelfContextMenuItemCheck) { ...@@ -405,10 +422,8 @@ TEST_F(ShelfContextMenuTest, ArcDeferredShelfContextMenuItemCheck) {
EXPECT_TRUE(controller()->GetItem(shelf_id1)); EXPECT_TRUE(controller()->GetItem(shelf_id1));
EXPECT_FALSE(controller()->GetItem(shelf_id2)); EXPECT_FALSE(controller()->GetItem(shelf_id2));
arc::LaunchApp(profile(), app_id1, ui::EF_LEFT_MOUSE_BUTTON, LaunchApp(app_id1, arc_test().fake_apps()[0], 1);
arc::UserInteractionType::NOT_USER_INITIATED); LaunchApp(app_id2, arc_test().fake_apps()[1], 2);
arc::LaunchApp(profile(), app_id2, ui::EF_LEFT_MOUSE_BUTTON,
arc::UserInteractionType::NOT_USER_INITIATED);
EXPECT_TRUE(controller()->GetItem(shelf_id1)); EXPECT_TRUE(controller()->GetItem(shelf_id1));
EXPECT_TRUE(controller()->GetItem(shelf_id2)); EXPECT_TRUE(controller()->GetItem(shelf_id2));
...@@ -458,9 +473,8 @@ TEST_F(ShelfContextMenuTest, ArcContextMenuOptions) { ...@@ -458,9 +473,8 @@ TEST_F(ShelfContextMenuTest, ArcContextMenuOptions) {
// you're adding a context menu option ensure that you have added the enum to // you're adding a context menu option ensure that you have added the enum to
// tools/metrics/histograms/enums.xml and that you haven't modified the order // tools/metrics/histograms/enums.xml and that you haven't modified the order
// of the existing enums. // of the existing enums.
arc_test().app_instance()->SendRefreshAppList( SendRefreshAppList(std::vector<arc::mojom::AppInfo>(
std::vector<arc::mojom::AppInfo>(arc_test().fake_apps().begin(), arc_test().fake_apps().begin(), arc_test().fake_apps().begin() + 1));
arc_test().fake_apps().begin() + 1));
const std::string app_id = ArcAppTest::GetAppId(arc_test().fake_apps()[0]); const std::string app_id = ArcAppTest::GetAppId(arc_test().fake_apps()[0]);
const ash::ShelfID shelf_id(app_id); const ash::ShelfID shelf_id(app_id);
...@@ -538,6 +552,7 @@ TEST_F(ShelfContextMenuTest, InternalAppShelfContextMenuOptionsNumber) { ...@@ -538,6 +552,7 @@ TEST_F(ShelfContextMenuTest, InternalAppShelfContextMenuOptionsNumber) {
// specifically that every menu item has an icon. // specifically that every menu item has an icon.
TEST_F(ShelfContextMenuTest, CrostiniTerminalApp) { TEST_F(ShelfContextMenuTest, CrostiniTerminalApp) {
crostini::CrostiniTestHelper crostini_helper(profile()); crostini::CrostiniTestHelper crostini_helper(profile());
crostini_helper.ReInitializeAppServiceIntegration();
const std::string app_id = crostini::kCrostiniTerminalId; const std::string app_id = crostini::kCrostiniTerminalId;
crostini::CrostiniManager::GetForProfile(profile())->AddRunningVmForTesting( crostini::CrostiniManager::GetForProfile(profile())->AddRunningVmForTesting(
crostini::kCrostiniDefaultVmName); crostini::kCrostiniDefaultVmName);
...@@ -569,9 +584,11 @@ TEST_F(ShelfContextMenuTest, CrostiniTerminalApp) { ...@@ -569,9 +584,11 @@ TEST_F(ShelfContextMenuTest, CrostiniTerminalApp) {
// Particularly, we ensure that the density changing option exists. // Particularly, we ensure that the density changing option exists.
TEST_F(ShelfContextMenuTest, CrostiniNormalApp) { TEST_F(ShelfContextMenuTest, CrostiniNormalApp) {
crostini::CrostiniTestHelper crostini_helper(profile()); crostini::CrostiniTestHelper crostini_helper(profile());
crostini_helper.ReInitializeAppServiceIntegration();
const std::string app_name = "foo"; const std::string app_name = "foo";
crostini_helper.AddApp(crostini::CrostiniTestHelper::BasicApp(app_name)); crostini_helper.AddApp(crostini::CrostiniTestHelper::BasicApp(app_name));
app_service_test().FlushMojoCalls();
const std::string app_id = const std::string app_id =
crostini::CrostiniTestHelper::GenerateAppId(app_name); crostini::CrostiniTestHelper::GenerateAppId(app_name);
crostini::CrostiniRegistryServiceFactory::GetForProfile(profile()) crostini::CrostiniRegistryServiceFactory::GetForProfile(profile())
...@@ -613,6 +630,7 @@ TEST_F(ShelfContextMenuTest, CrostiniNormalApp) { ...@@ -613,6 +630,7 @@ TEST_F(ShelfContextMenuTest, CrostiniNormalApp) {
// have an associated .desktop file, and therefore can only be closed). // have an associated .desktop file, and therefore can only be closed).
TEST_F(ShelfContextMenuTest, CrostiniUnregisteredApps) { TEST_F(ShelfContextMenuTest, CrostiniUnregisteredApps) {
crostini::CrostiniTestHelper crostini_helper(profile()); crostini::CrostiniTestHelper crostini_helper(profile());
crostini_helper.ReInitializeAppServiceIntegration();
const std::string fake_window_app_id = "foo"; const std::string fake_window_app_id = "foo";
const std::string fake_window_startup_id = "bar"; const std::string fake_window_startup_id = "bar";
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment