Commit 21fd0787 authored by Melissa Zhang's avatar Melissa Zhang Committed by Commit Bot

Adding browser_tests and unit_tests for shelf pin update.

This CL adds unit_tests for ReplacePinnedItem, PinAppAtIndex,
PinnedItemIndexByAppID and a browser_test for the shelf pin update
when an Android app updates to PWA or vice-versa.

BUG=986132

Change-Id: I9b709fc1f9865854873a3f800dab56ba778fd3db
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1810167
Commit-Queue: Melissa Zhang <melzhang@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#697844}
parent 0d03612d
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include "ash/public/cpp/shelf_model.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/optional.h" #include "base/optional.h"
#include "base/run_loop.h" #include "base/run_loop.h"
...@@ -20,6 +21,7 @@ ...@@ -20,6 +21,7 @@
#include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/app_list/arc/arc_app_test.h" #include "chrome/browser/ui/app_list/arc/arc_app_test.h"
#include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/browser/web_applications/components/web_app_helpers.h" #include "chrome/browser/web_applications/components/web_app_helpers.h"
#include "components/arc/arc_util.h" #include "components/arc/arc_util.h"
...@@ -36,6 +38,10 @@ const char kPackageName[] = "com.google.maps"; ...@@ -36,6 +38,10 @@ const char kPackageName[] = "com.google.maps";
const char kAppTitle[] = "Google Maps"; const char kAppTitle[] = "Google Maps";
const char kAppUrl[] = "https://www.google.com/maps/"; const char kAppUrl[] = "https://www.google.com/maps/";
const char kAppScope[] = "https://www.google.com/"; const char kAppScope[] = "https://www.google.com/";
constexpr char kLastAppId[] = "last_app_id";
const char kAppActivity[] = "test.app.activity";
const char kAppActivity1[] = "test.app1.activity";
const char kPackageName1[] = "com.test.app";
const std::vector<uint8_t> GetFakeIconBytes() { const std::vector<uint8_t> GetFakeIconBytes() {
auto fake_app_instance = auto fake_app_instance =
...@@ -183,6 +189,23 @@ class ApkWebAppInstallerDelayedArcStartBrowserTest ...@@ -183,6 +189,23 @@ class ApkWebAppInstallerDelayedArcStartBrowserTest
void TearDownOnMainThread() override {} void TearDownOnMainThread() override {}
}; };
class ApkWebAppInstallerWithLauncherControllerBrowserTest
: public ApkWebAppInstallerBrowserTest {
public:
// ApkWebAppInstallerBrowserTest
void SetUpOnMainThread() override {
EnableArc();
launcher_controller_ = ChromeLauncherController::instance();
ASSERT_TRUE(launcher_controller_);
}
// ApkWebAppInstallerBrowserTest
void TearDownOnMainThread() override { DisableArc(); }
protected:
ChromeLauncherController* launcher_controller_;
};
// Test the full installation and uninstallation flow. // Test the full installation and uninstallation flow.
IN_PROC_BROWSER_TEST_F(ApkWebAppInstallerBrowserTest, InstallAndUninstall) { IN_PROC_BROWSER_TEST_F(ApkWebAppInstallerBrowserTest, InstallAndUninstall) {
ScopedObserver<extensions::ExtensionRegistry, ScopedObserver<extensions::ExtensionRegistry,
...@@ -371,4 +394,90 @@ IN_PROC_BROWSER_TEST_F(ApkWebAppInstallerBrowserTest, ...@@ -371,4 +394,90 @@ IN_PROC_BROWSER_TEST_F(ApkWebAppInstallerBrowserTest,
} }
} }
IN_PROC_BROWSER_TEST_F(ApkWebAppInstallerWithLauncherControllerBrowserTest,
CheckPinStateAfterUpdate) {
ScopedObserver<extensions::ExtensionRegistry,
extensions::ExtensionRegistryObserver>
observer(this);
observer.Add(extensions::ExtensionRegistry::Get(browser()->profile()));
ApkWebAppService* service = apk_web_app_service();
service->SetArcAppListPrefsForTesting(arc_app_list_prefs_);
app_instance_->SendPackageAdded(GetArcAppPackage(kPackageName, kAppTitle));
const std::string arc_app_id =
ArcAppListPrefs::GetAppId(kPackageName, kAppActivity);
/// Create an app and add to the package.
arc::mojom::AppInfo app;
app.name = kAppTitle;
app.package_name = kPackageName;
app.activity = kAppActivity;
app.sticky = true;
app_instance_->SendPackageAppListRefreshed(kPackageName, {app});
EXPECT_FALSE(installed_extension_);
EXPECT_TRUE(uninstalled_extension_id_.empty());
EXPECT_FALSE(launcher_controller_->IsAppPinned(arc_app_id));
// Pin the app to the shelf.
launcher_controller_->PinAppWithID(arc_app_id);
EXPECT_TRUE(launcher_controller_->IsAppPinned(arc_app_id));
int pin_index = launcher_controller_->PinnedItemIndexByAppID(arc_app_id);
arc_app_list_prefs_->SetPackagePrefs(kPackageName, kLastAppId,
base::Value(arc_app_id));
std::string keep_web_app_id;
// Update ARC app to web app and check that the pinned app has
// been updated.
{
base::RunLoop run_loop;
service->SetWebAppInstalledCallbackForTesting(base::BindLambdaForTesting(
[&](const std::string& package_name, const web_app::AppId& web_app_id) {
keep_web_app_id = web_app_id;
EXPECT_TRUE(installed_extension_);
EXPECT_FALSE(launcher_controller_->IsAppPinned(arc_app_id));
EXPECT_TRUE(launcher_controller_->IsAppPinned(keep_web_app_id));
int new_index =
launcher_controller_->PinnedItemIndexByAppID(keep_web_app_id);
EXPECT_EQ(pin_index, new_index);
run_loop.Quit();
}));
app_instance_->SendPackageAdded(GetWebAppPackage(kPackageName, kAppTitle));
run_loop.Run();
}
// Move the pin location of the app.
app_instance_->SendPackageAdded(GetArcAppPackage(kPackageName1, kAppTitle));
const std::string arc_app_id1 =
ArcAppListPrefs::GetAppId(kPackageName1, kAppActivity1);
launcher_controller_->PinAppAtIndex(arc_app_id1, pin_index);
EXPECT_EQ(pin_index,
launcher_controller_->PinnedItemIndexByAppID(arc_app_id1));
// The app that was previously pinned will be shifted one to the right.
pin_index += 1;
EXPECT_EQ(pin_index,
launcher_controller_->PinnedItemIndexByAppID(keep_web_app_id));
// Update to ARC app and check the pinned app has updated.
{
base::RunLoop run_loop;
service->SetWebAppUninstalledCallbackForTesting(base::BindLambdaForTesting(
[&](const std::string& package_name, const web_app::AppId& web_app_id) {
EXPECT_FALSE(uninstalled_extension_id_.empty());
EXPECT_FALSE(launcher_controller_->IsAppPinned(web_app_id));
EXPECT_TRUE(launcher_controller_->IsAppPinned(arc_app_id));
int new_index =
launcher_controller_->PinnedItemIndexByAppID(arc_app_id);
EXPECT_EQ(pin_index, new_index);
EXPECT_FALSE(launcher_controller_->IsAppPinned(keep_web_app_id));
run_loop.Quit();
}));
app_instance_->SendPackageAdded(GetArcAppPackage(kPackageName, kAppTitle));
run_loop.Run();
}
}
} // namespace chromeos } // namespace chromeos
...@@ -400,6 +400,7 @@ void ApkWebAppService::OnDidFinishInstall(const std::string& package_name, ...@@ -400,6 +400,7 @@ void ApkWebAppService::OnDidFinishInstall(const std::string& package_name,
// when the container starts up again. // when the container starts up again.
dict_update->SetPath({web_app_id, kShouldRemoveKey}, base::Value(false)); dict_update->SetPath({web_app_id, kShouldRemoveKey}, base::Value(false));
// For testing.
if (web_app_installed_callback_) if (web_app_installed_callback_)
std::move(web_app_installed_callback_).Run(package_name, web_app_id); std::move(web_app_installed_callback_).Run(package_name, web_app_id);
} }
......
...@@ -240,7 +240,7 @@ class ArcAppModelBuilderTest : public extensions::ExtensionServiceTestBase, ...@@ -240,7 +240,7 @@ class ArcAppModelBuilderTest : public extensions::ExtensionServiceTestBase,
arc_test_.SetUp(profile_.get()); arc_test_.SetUp(profile_.get());
CreateBuilder(); CreateBuilder();
CreateLauncherController()->Init(); CreateLauncherController();
// Validating decoded content does not fit well for unit tests. // Validating decoded content does not fit well for unit tests.
ArcAppIcon::DisableSafeDecodingForTesting(); ArcAppIcon::DisableSafeDecodingForTesting();
......
...@@ -792,7 +792,7 @@ void ChromeLauncherController::UnpinAppWithID(const std::string& app_id) { ...@@ -792,7 +792,7 @@ void ChromeLauncherController::UnpinAppWithID(const std::string& app_id) {
void ChromeLauncherController::ReplacePinnedItem( void ChromeLauncherController::ReplacePinnedItem(
const std::string& old_app_id, const std::string& old_app_id,
const std::string& new_app_id) { const std::string& new_app_id) {
if (!model_->IsAppPinned(old_app_id)) if (!model_->IsAppPinned(old_app_id) || model_->IsAppPinned(new_app_id))
return; return;
const int index = model_->ItemIndexByAppID(old_app_id); const int index = model_->ItemIndexByAppID(old_app_id);
...@@ -808,7 +808,7 @@ void ChromeLauncherController::ReplacePinnedItem( ...@@ -808,7 +808,7 @@ void ChromeLauncherController::ReplacePinnedItem(
void ChromeLauncherController::PinAppAtIndex(const std::string& app_id, void ChromeLauncherController::PinAppAtIndex(const std::string& app_id,
int target_index) { int target_index) {
if (target_index < 0) if (target_index < 0 || model_->IsAppPinned(app_id))
return; return;
const ash::ShelfID new_shelf_id(app_id); const ash::ShelfID new_shelf_id(app_id);
......
...@@ -4806,3 +4806,92 @@ TEST_F(ChromeLauncherControllerTest, UnpinnableComponentApps) { ...@@ -4806,3 +4806,92 @@ TEST_F(ChromeLauncherControllerTest, UnpinnableComponentApps) {
GetPinnableForAppID(id, profile())); GetPinnableForAppID(id, profile()));
} }
} }
TEST_F(ChromeLauncherControllerWithArcTest, ReplacePinnedItem) {
InitLauncherController();
SendListOfArcApps();
const std::string arc_app_id1 =
ArcAppTest::GetAppId(arc_test_.fake_apps()[0]);
const std::string arc_app_id2 =
ArcAppTest::GetAppId(arc_test_.fake_apps()[1]);
extension_service_->AddExtension(extension1_.get());
extension_service_->AddExtension(extension2_.get());
launcher_controller_->PinAppWithID(extension1_->id());
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(arc_app_id1));
// Replace pin extension to ARC app
launcher_controller_->ReplacePinnedItem(extension1_->id(), arc_app_id1);
EXPECT_TRUE(launcher_controller_->IsAppPinned(arc_app_id1));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
// Replace pin ARC app to ARC app
launcher_controller_->ReplacePinnedItem(arc_app_id1, arc_app_id2);
EXPECT_TRUE(launcher_controller_->IsAppPinned(arc_app_id2));
EXPECT_FALSE(launcher_controller_->IsAppPinned(arc_app_id1));
// Replace pin ARC app to extension app
launcher_controller_->ReplacePinnedItem(arc_app_id2, extension1_->id());
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(arc_app_id2));
// Replace pin extension app to extension app
launcher_controller_->ReplacePinnedItem(extension1_->id(), extension2_->id());
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension2_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
// Try to replace item that is not pinned.
launcher_controller_->ReplacePinnedItem(arc_app_id2, extension1_->id());
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(arc_app_id2));
// Try to replace item with item that is already pinned.
launcher_controller_->PinAppWithID(extension1_->id());
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension1_->id()));
launcher_controller_->ReplacePinnedItem(extension2_->id(), extension1_->id());
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension2_->id()));
}
TEST_F(ChromeLauncherControllerWithArcTest, PinAtIndex) {
InitLauncherController();
SendListOfArcApps();
const std::string arc_app_id1 =
ArcAppTest::GetAppId(arc_test_.fake_apps()[0]);
const std::string arc_app_id2 =
ArcAppTest::GetAppId(arc_test_.fake_apps()[1]);
extension_service_->AddExtension(extension1_.get());
extension_service_->AddExtension(extension2_.get());
int index = 0;
launcher_controller_->PinAppAtIndex(extension1_->id(), index);
EXPECT_EQ(index,
launcher_controller_->PinnedItemIndexByAppID(extension1_->id()));
launcher_controller_->PinAppAtIndex(extension2_->id(), index);
EXPECT_EQ(index,
launcher_controller_->PinnedItemIndexByAppID(extension2_->id()));
EXPECT_NE(index,
launcher_controller_->PinnedItemIndexByAppID(extension1_->id()));
index = 3;
launcher_controller_->PinAppAtIndex(arc_app_id1, index);
EXPECT_EQ(index, launcher_controller_->PinnedItemIndexByAppID(arc_app_id1));
// Test pinning at invalid index.
index = -100;
launcher_controller_->PinAppAtIndex(arc_app_id2, index);
EXPECT_NE(index, launcher_controller_->PinnedItemIndexByAppID(arc_app_id2));
EXPECT_EQ(-1, launcher_controller_->PinnedItemIndexByAppID(arc_app_id2));
// Test pinning already pinned app.
index = 0;
launcher_controller_->PinAppAtIndex(arc_app_id1, index);
EXPECT_NE(index, launcher_controller_->PinnedItemIndexByAppID(arc_app_id1));
EXPECT_EQ(3, launcher_controller_->PinnedItemIndexByAppID(arc_app_id1));
}
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