This source file includes following definitions.
- GetModelContent
- MakeApp
- DismissView
- GetAppListWindow
- GetWindowIcon
- IsAppPinned
- PinApp
- UnpinApp
- GetPinnable
- CanDoCreateShortcutsFlow
- DoCreateShortcutsFlow
- CanDoShowAppInfoFlow
- DoShowAppInfoFlow
- CreateNewWindow
- ActivateApp
- LaunchApp
- ShowForProfileByPath
- ShouldShowUserIcon
- SetUp
- TearDown
- CreateBuilder
- ResetBuilder
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/ui/app_list/extension_app_model_builder.h"
#include <string>
#include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_service.h"
#include "base/run_loop.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_function_test_utils.h"
#include "chrome/browser/extensions/extension_service_unittest.h"
#include "chrome/browser/extensions/install_tracker.h"
#include "chrome/browser/extensions/install_tracker_factory.h"
#include "chrome/browser/ui/app_list/app_list_controller_delegate_impl.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "extensions/browser/app_sorting.h"
#include "extensions/common/extension_set.h"
#include "extensions/common/manifest.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/app_list/app_list_item.h"
namespace {
const char kHostedAppId[] = "dceacbkfkmllgmjmbhgkpjegnodmildf";
const char kPackagedApp1Id[] = "emfkafnhnpcmabnnkckkchdilgeoekbo";
const char kPackagedApp2Id[] = "jlklkagmeajbjiobondfhiekepofmljl";
std::string GetModelContent(app_list::AppListModel* model) {
std::string content;
for (size_t i = 0; i < model->top_level_item_list()->item_count(); ++i) {
if (i > 0)
content += ',';
content += model->top_level_item_list()->item_at(i)->name();
}
return content;
}
scoped_refptr<extensions::Extension> MakeApp(const std::string& name,
const std::string& version,
const std::string& url,
const std::string& id) {
std::string err;
base::DictionaryValue value;
value.SetString("name", name);
value.SetString("version", version);
value.SetString("app.launch.web_url", url);
scoped_refptr<extensions::Extension> app =
extensions::Extension::Create(
base::FilePath(),
extensions::Manifest::INTERNAL,
value,
extensions::Extension::WAS_INSTALLED_BY_DEFAULT,
id,
&err);
EXPECT_EQ(err, "");
return app;
}
class TestAppListControllerDelegate : public AppListControllerDelegate {
public:
virtual ~TestAppListControllerDelegate() {}
virtual void DismissView() OVERRIDE {}
virtual gfx::NativeWindow GetAppListWindow() OVERRIDE { return NULL; }
virtual gfx::ImageSkia GetWindowIcon() OVERRIDE { return gfx::ImageSkia(); }
virtual bool IsAppPinned(const std::string& extension_id) OVERRIDE {
return false;
}
virtual void PinApp(const std::string& extension_id) OVERRIDE {}
virtual void UnpinApp(const std::string& extension_id) OVERRIDE {}
virtual Pinnable GetPinnable() OVERRIDE { return NO_PIN; }
virtual bool CanDoCreateShortcutsFlow() OVERRIDE { return false; }
virtual void DoCreateShortcutsFlow(Profile* profile,
const std::string& extension_id) OVERRIDE {
}
virtual bool CanDoShowAppInfoFlow() OVERRIDE { return false; }
virtual void DoShowAppInfoFlow(Profile* profile,
const std::string& extension_id) OVERRIDE {
};
virtual void CreateNewWindow(Profile* profile, bool incognito) OVERRIDE {}
virtual void ActivateApp(Profile* profile,
const extensions::Extension* extension,
AppListSource source,
int event_flags) OVERRIDE {}
virtual void LaunchApp(Profile* profile,
const extensions::Extension* extension,
AppListSource source,
int event_flags) OVERRIDE {}
virtual void ShowForProfileByPath(
const base::FilePath& profile_path) OVERRIDE {}
virtual bool ShouldShowUserIcon() OVERRIDE { return false; }
};
const char kDefaultApps[] = "Packaged App 1,Packaged App 2,Hosted App";
const size_t kDefaultAppCount = 3u;
}
class ExtensionAppModelBuilderTest : public ExtensionServiceTestBase {
public:
ExtensionAppModelBuilderTest() {}
virtual ~ExtensionAppModelBuilderTest() {}
virtual void SetUp() OVERRIDE {
ExtensionServiceTestBase::SetUp();
base::FilePath source_install_dir = data_dir_
.AppendASCII("app_list")
.AppendASCII("Extensions");
base::FilePath pref_path = source_install_dir
.DirName()
.Append(chrome::kPreferencesFilename);
InitializeInstalledExtensionService(pref_path, source_install_dir);
service_->Init();
const extensions::ExtensionSet* extensions = service_->extensions();
ASSERT_EQ(static_cast<size_t>(4), extensions->size());
CreateBuilder();
}
virtual void TearDown() OVERRIDE {
ResetBuilder();
}
protected:
void CreateBuilder() {
ResetBuilder();
model_.reset(new app_list::AppListModel);
controller_.reset(new TestAppListControllerDelegate);
builder_.reset(new ExtensionAppModelBuilder(controller_.get()));
builder_->InitializeWithProfile(profile_.get(), model_.get());
}
void ResetBuilder() {
builder_.reset();
controller_.reset();
model_.reset();
}
scoped_ptr<app_list::AppListModel> model_;
scoped_ptr<TestAppListControllerDelegate> controller_;
scoped_ptr<ExtensionAppModelBuilder> builder_;
base::ScopedTempDir second_profile_temp_dir_;
private:
DISALLOW_COPY_AND_ASSIGN(ExtensionAppModelBuilderTest);
};
TEST_F(ExtensionAppModelBuilderTest, Build) {
EXPECT_EQ(kDefaultAppCount, model_->top_level_item_list()->item_count());
EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get()));
}
TEST_F(ExtensionAppModelBuilderTest, HideWebStore) {
scoped_refptr<extensions::Extension> store =
MakeApp("webstore",
"0.0",
"http://google.com",
std::string(extension_misc::kWebStoreAppId));
service_->AddExtension(store.get());
scoped_refptr<extensions::Extension> enterprise_store =
MakeApp("enterprise_webstore",
"0.0",
"http://google.com",
std::string(extension_misc::kEnterpriseWebStoreAppId));
service_->AddExtension(enterprise_store.get());
app_list::AppListModel model1;
ExtensionAppModelBuilder builder1(controller_.get());
builder1.InitializeWithProfile(profile_.get(), &model1);
std::string content = GetModelContent(&model1);
EXPECT_NE(std::string::npos, content.find("webstore"));
EXPECT_NE(std::string::npos, content.find("enterprise_webstore"));
profile_->GetPrefs()->SetBoolean(prefs::kHideWebStoreIcon, true);
app_list::AppListModel model2;
ExtensionAppModelBuilder builder2(controller_.get());
builder2.InitializeWithProfile(profile_.get(), &model2);
content = GetModelContent(&model2);
EXPECT_EQ(std::string::npos, content.find("webstore"));
EXPECT_EQ(std::string::npos, content.find("enterprise_webstore"));
}
TEST_F(ExtensionAppModelBuilderTest, DisableAndEnable) {
service_->DisableExtension(kHostedAppId,
extensions::Extension::DISABLE_NONE);
EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get()));
service_->EnableExtension(kHostedAppId);
EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get()));
}
TEST_F(ExtensionAppModelBuilderTest, Uninstall) {
service_->UninstallExtension(kPackagedApp2Id, false, NULL);
EXPECT_EQ(std::string("Packaged App 1,Hosted App"),
GetModelContent(model_.get()));
base::RunLoop().RunUntilIdle();
}
TEST_F(ExtensionAppModelBuilderTest, UninstallTerminatedApp) {
const extensions::Extension* app =
service_->GetInstalledExtension(kPackagedApp2Id);
ASSERT_TRUE(app != NULL);
service_->TrackTerminatedExtensionForTest(app);
service_->UninstallExtension(kPackagedApp2Id, false, NULL);
EXPECT_EQ(std::string("Packaged App 1,Hosted App"),
GetModelContent(model_.get()));
base::RunLoop().RunUntilIdle();
}
TEST_F(ExtensionAppModelBuilderTest, Reinstall) {
EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get()));
extensions::InstallTracker* tracker =
extensions::InstallTrackerFactory::GetForProfile(profile_.get());
extensions::InstallObserver::ExtensionInstallParams params(
kPackagedApp1Id, "", gfx::ImageSkia(), true, true);
tracker->OnBeginExtensionInstall(params);
EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get()));
}
TEST_F(ExtensionAppModelBuilderTest, OrdinalPrefsChange) {
extensions::AppSorting* sorting =
extensions::ExtensionPrefs::Get(profile_.get())->app_sorting();
syncer::StringOrdinal package_app_page =
sorting->GetPageOrdinal(kPackagedApp1Id);
sorting->SetPageOrdinal(kHostedAppId, package_app_page.CreateBefore());
EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get()));
syncer::StringOrdinal app1_ordinal =
sorting->GetAppLaunchOrdinal(kPackagedApp1Id);
syncer::StringOrdinal app2_ordinal =
sorting->GetAppLaunchOrdinal(kPackagedApp2Id);
sorting->SetPageOrdinal(kHostedAppId, package_app_page);
sorting->SetAppLaunchOrdinal(kHostedAppId,
app1_ordinal.CreateBetween(app2_ordinal));
EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get()));
}
TEST_F(ExtensionAppModelBuilderTest, OnExtensionMoved) {
extensions::AppSorting* sorting =
extensions::ExtensionPrefs::Get(profile_.get())->app_sorting();
sorting->SetPageOrdinal(kHostedAppId,
sorting->GetPageOrdinal(kPackagedApp1Id));
sorting->OnExtensionMoved(kHostedAppId, kPackagedApp1Id, kPackagedApp2Id);
EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get()));
sorting->OnExtensionMoved(kHostedAppId, kPackagedApp2Id, std::string());
EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get()));
sorting->OnExtensionMoved(kHostedAppId, std::string(), kPackagedApp1Id);
EXPECT_EQ(std::string(kDefaultApps), GetModelContent(model_.get()));
}
TEST_F(ExtensionAppModelBuilderTest, InvalidOrdinal) {
extensions::AppSorting* sorting =
extensions::ExtensionPrefs::Get(profile_.get())->app_sorting();
sorting->ClearOrdinals(kPackagedApp1Id);
extensions::ExtensionScopedPrefs* scoped_prefs =
extensions::ExtensionPrefs::Get(profile_.get());
scoped_prefs->UpdateExtensionPref(
kHostedAppId,
"page_ordinal",
base::Value::CreateStringValue("a corrupted ordinal"));
CreateBuilder();
}
TEST_F(ExtensionAppModelBuilderTest, OrdinalConfilicts) {
syncer::StringOrdinal conflict_ordinal =
syncer::StringOrdinal::CreateInitialOrdinal();
extensions::AppSorting* sorting =
extensions::ExtensionPrefs::Get(profile_.get())->app_sorting();
sorting->SetPageOrdinal(kHostedAppId, conflict_ordinal);
sorting->SetAppLaunchOrdinal(kHostedAppId, conflict_ordinal);
sorting->SetPageOrdinal(kPackagedApp1Id, conflict_ordinal);
sorting->SetAppLaunchOrdinal(kPackagedApp1Id, conflict_ordinal);
sorting->SetPageOrdinal(kPackagedApp2Id, conflict_ordinal);
sorting->SetAppLaunchOrdinal(kPackagedApp2Id, conflict_ordinal);
CreateBuilder();
EXPECT_EQ(std::string("Hosted App,Packaged App 1,Packaged App 2"),
GetModelContent(model_.get()));
}
TEST_F(ExtensionAppModelBuilderTest, BookmarkApp) {
const std::string kAppName = "Bookmark App";
const std::string kAppVersion = "2014.1.24.19748";
const std::string kAppUrl = "http://google.com";
const std::string kAppId = "podhdnefolignjhecmjkbimfgioanahm";
std::string err;
base::DictionaryValue value;
value.SetString("name", kAppName);
value.SetString("version", kAppVersion);
value.SetString("app.launch.web_url", kAppUrl);
scoped_refptr<extensions::Extension> bookmark_app =
extensions::Extension::Create(
base::FilePath(),
extensions::Manifest::INTERNAL,
value,
extensions::Extension::WAS_INSTALLED_BY_DEFAULT |
extensions::Extension::FROM_BOOKMARK,
kAppId,
&err);
EXPECT_TRUE(err.empty());
service_->AddExtension(bookmark_app.get());
EXPECT_EQ(kDefaultAppCount + 1, model_->top_level_item_list()->item_count());
EXPECT_NE(std::string::npos, GetModelContent(model_.get()).find(kAppName));
}