This source file includes following definitions.
- SetUp
- SetUpOnMainThread
- CleanUpOnMainThread
- BrowserActionAdded
- BrowserActionRemoved
- BrowserActionMoved
- HighlightModeChanged
- ExtensionAt
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include "base/macros.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/extension_toolbar_model.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
namespace extensions {
class ExtensionToolbarModelTest : public ExtensionBrowserTest,
public ExtensionToolbarModel::Observer {
public:
virtual void SetUp() {
inserted_count_ = 0;
removed_count_ = 0;
moved_count_ = 0;
highlight_mode_count_ = 0;
ExtensionBrowserTest::SetUp();
}
virtual void SetUpOnMainThread() OVERRIDE {
model_ = ExtensionToolbarModel::Get(browser()->profile());
model_->AddObserver(this);
}
virtual void CleanUpOnMainThread() OVERRIDE {
model_->RemoveObserver(this);
}
virtual void BrowserActionAdded(const Extension* extension,
int index) OVERRIDE {
inserted_count_++;
}
virtual void BrowserActionRemoved(const Extension* extension) OVERRIDE {
removed_count_++;
}
virtual void BrowserActionMoved(const Extension* extension,
int index) OVERRIDE {
moved_count_++;
}
virtual void HighlightModeChanged(bool is_highlighting) OVERRIDE {
highlight_mode_count_ += is_highlighting ? 1 : -1;
}
const Extension* ExtensionAt(int index) {
const ExtensionList& toolbar_items = model_->toolbar_items();
for (ExtensionList::const_iterator i = toolbar_items.begin();
i < toolbar_items.end(); ++i) {
if (index-- == 0)
return i->get();
}
return NULL;
}
protected:
ExtensionToolbarModel* model_;
int inserted_count_;
int removed_count_;
int moved_count_;
int highlight_mode_count_;
};
IN_PROC_BROWSER_TEST_F(ExtensionToolbarModelTest, Basic) {
ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("none")));
EXPECT_EQ(0, inserted_count_);
EXPECT_EQ(0u, model_->toolbar_items().size());
ASSERT_EQ(NULL, ExtensionAt(0));
ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("basics")));
EXPECT_EQ(1, inserted_count_);
EXPECT_EQ(1u, model_->toolbar_items().size());
const Extension* extension = ExtensionAt(0);
ASSERT_TRUE(NULL != extension);
EXPECT_STREQ("A browser action with no icon that makes the page red",
extension->name().c_str());
model_->MoveBrowserAction(extension, 0);
EXPECT_EQ(1, moved_count_);
EXPECT_EQ(1u, model_->toolbar_items().size());
const Extension* extension2 = ExtensionAt(0);
EXPECT_EQ(extension, extension2);
UnloadExtension(extension->id());
EXPECT_EQ(1, removed_count_);
EXPECT_EQ(0u, model_->toolbar_items().size());
EXPECT_EQ(NULL, ExtensionAt(0));
}
#if defined(OS_MACOSX)
#define MAYBE_ReorderAndReinsert DISABLED_ReorderAndReinsert
#else
#define MAYBE_ReorderAndReinsert ReorderAndReinsert
#endif
IN_PROC_BROWSER_TEST_F(ExtensionToolbarModelTest, MAYBE_ReorderAndReinsert) {
base::FilePath extension_a_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("basics"));
ASSERT_TRUE(LoadExtension(extension_a_path));
EXPECT_EQ(1, inserted_count_);
EXPECT_EQ(1u, model_->toolbar_items().size());
const Extension* extensionA = ExtensionAt(0);
ASSERT_TRUE(NULL != extensionA);
EXPECT_STREQ("A browser action with no icon that makes the page red",
extensionA->name().c_str());
base::FilePath extension_b_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("popup"));
ASSERT_TRUE(LoadExtension(extension_b_path));
EXPECT_EQ(2, inserted_count_);
EXPECT_EQ(2u, model_->toolbar_items().size());
const Extension* extensionB = ExtensionAt(1);
ASSERT_TRUE(NULL != extensionB);
EXPECT_STREQ("Popup tester", extensionB->name().c_str());
base::FilePath extension_c_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("remove_popup"));
ASSERT_TRUE(LoadExtension(extension_c_path));
EXPECT_EQ(3, inserted_count_);
EXPECT_EQ(3u, model_->toolbar_items().size());
const Extension* extensionC = ExtensionAt(2);
ASSERT_TRUE(NULL != extensionC);
EXPECT_STREQ("A page action which removes a popup.",
extensionC->name().c_str());
model_->MoveBrowserAction(extensionC, 0);
EXPECT_EQ(1, moved_count_);
EXPECT_EQ(3u, model_->toolbar_items().size());
EXPECT_EQ(extensionC, ExtensionAt(0));
EXPECT_EQ(extensionA, ExtensionAt(1));
EXPECT_EQ(extensionB, ExtensionAt(2));
EXPECT_EQ(NULL, ExtensionAt(3));
model_->MoveBrowserAction(extensionA, 2);
EXPECT_EQ(2, moved_count_);
EXPECT_EQ(3u, model_->toolbar_items().size());
EXPECT_EQ(extensionC, ExtensionAt(0));
EXPECT_EQ(extensionB, ExtensionAt(1));
EXPECT_EQ(extensionA, ExtensionAt(2));
EXPECT_EQ(NULL, ExtensionAt(3));
std::string idB = extensionB->id();
UnloadExtension(idB);
EXPECT_EQ(1, removed_count_);
EXPECT_EQ(2u, model_->toolbar_items().size());
EXPECT_EQ(extensionC, ExtensionAt(0));
EXPECT_EQ(extensionA, ExtensionAt(1));
EXPECT_EQ(NULL, ExtensionAt(2));
ASSERT_TRUE(LoadExtension(extension_b_path));
EXPECT_EQ(4, inserted_count_);
EXPECT_EQ(3u, model_->toolbar_items().size());
ASSERT_STREQ(idB.c_str(), ExtensionAt(1)->id().c_str());
UnloadExtension(idB);
EXPECT_EQ(2, removed_count_);
EXPECT_EQ(2u, model_->toolbar_items().size());
EXPECT_EQ(extensionC, ExtensionAt(0));
EXPECT_EQ(extensionA, ExtensionAt(1));
EXPECT_EQ(NULL, ExtensionAt(2));
model_->MoveBrowserAction(extensionA, 0);
EXPECT_EQ(3, moved_count_);
EXPECT_EQ(2u, model_->toolbar_items().size());
EXPECT_EQ(extensionA, ExtensionAt(0));
EXPECT_EQ(extensionC, ExtensionAt(1));
EXPECT_EQ(NULL, ExtensionAt(2));
model_->MoveBrowserAction(extensionA, 0);
EXPECT_EQ(4, moved_count_);
EXPECT_EQ(2u, model_->toolbar_items().size());
EXPECT_EQ(extensionA, ExtensionAt(0));
EXPECT_EQ(extensionC, ExtensionAt(1));
EXPECT_EQ(NULL, ExtensionAt(2));
std::string idC = extensionC->id();
UnloadExtension(idC);
EXPECT_EQ(3, removed_count_);
EXPECT_EQ(1u, model_->toolbar_items().size());
EXPECT_EQ(extensionA, ExtensionAt(0));
EXPECT_EQ(NULL, ExtensionAt(1));
ASSERT_TRUE(LoadExtension(extension_c_path));
EXPECT_EQ(5, inserted_count_);
EXPECT_EQ(2u, model_->toolbar_items().size());
ASSERT_STREQ(idC.c_str(), ExtensionAt(1)->id().c_str());
}
IN_PROC_BROWSER_TEST_F(ExtensionToolbarModelTest, UnloadAndDisableMultiple) {
base::FilePath extension_a_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("basics"));
ASSERT_TRUE(LoadExtension(extension_a_path));
base::FilePath extension_b_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("popup"));
ASSERT_TRUE(LoadExtension(extension_b_path));
base::FilePath extension_c_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("remove_popup"));
ASSERT_TRUE(LoadExtension(extension_c_path));
const Extension* extensionA = ExtensionAt(0);
const Extension* extensionB = ExtensionAt(1);
const Extension* extensionC = ExtensionAt(2);
std::string idA = extensionA->id();
std::string idB = extensionB->id();
std::string idC = extensionC->id();
EXPECT_STREQ("A browser action with no icon that makes the page red",
extensionA->name().c_str());
EXPECT_STREQ("Popup tester", extensionB->name().c_str());
EXPECT_STREQ("A page action which removes a popup.",
extensionC->name().c_str());
UnloadExtension(idB);
UnloadExtension(idC);
UnloadExtension(idA);
ASSERT_TRUE(LoadExtension(extension_c_path));
ASSERT_TRUE(LoadExtension(extension_a_path));
ASSERT_TRUE(LoadExtension(extension_b_path));
EXPECT_EQ(0, moved_count_);
extensionA = ExtensionAt(0);
extensionB = ExtensionAt(1);
extensionC = ExtensionAt(2);
EXPECT_STREQ(idA.c_str(), extensionA->id().c_str());
EXPECT_STREQ(idB.c_str(), extensionB->id().c_str());
EXPECT_STREQ(idC.c_str(), extensionC->id().c_str());
model_->MoveBrowserAction(extensionC, 1);
model_->MoveBrowserAction(extensionA, 2);
EXPECT_STREQ(idC.c_str(), ExtensionAt(0)->id().c_str());
EXPECT_STREQ(idB.c_str(), ExtensionAt(1)->id().c_str());
EXPECT_STREQ(idA.c_str(), ExtensionAt(2)->id().c_str());
DisableExtension(idB);
DisableExtension(idC);
DisableExtension(idA);
EnableExtension(idA);
EnableExtension(idB);
EnableExtension(idC);
EXPECT_STREQ(idC.c_str(), ExtensionAt(0)->id().c_str());
EXPECT_STREQ(idB.c_str(), ExtensionAt(1)->id().c_str());
EXPECT_STREQ(idA.c_str(), ExtensionAt(2)->id().c_str());
}
IN_PROC_BROWSER_TEST_F(ExtensionToolbarModelTest, Uninstall) {
base::FilePath extension_a_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("basics"));
ASSERT_TRUE(LoadExtension(extension_a_path));
base::FilePath extension_b_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("popup"));
ASSERT_TRUE(LoadExtension(extension_b_path));
const Extension* extensionA = ExtensionAt(0);
const Extension* extensionB = ExtensionAt(1);
std::string idA = extensionA->id();
std::string idB = extensionB->id();
EXPECT_STREQ("A browser action with no icon that makes the page red",
extensionA->name().c_str());
EXPECT_STREQ("Popup tester", extensionB->name().c_str());
model_->MoveBrowserAction(extensionB, 0);
UninstallExtension(idB);
EXPECT_STREQ(idA.c_str(), ExtensionAt(0)->id().c_str());
EXPECT_EQ(1u, model_->toolbar_items().size());
ASSERT_TRUE(LoadExtension(extension_b_path));
EXPECT_EQ(2u, model_->toolbar_items().size());
EXPECT_STREQ(idA.c_str(), ExtensionAt(0)->id().c_str());
EXPECT_STREQ(idB.c_str(), ExtensionAt(1)->id().c_str());
}
IN_PROC_BROWSER_TEST_F(ExtensionToolbarModelTest, ReorderOnPrefChange) {
base::FilePath extension_a_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("basics"));
ASSERT_TRUE(LoadExtension(extension_a_path));
base::FilePath extension_b_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("popup"));
ASSERT_TRUE(LoadExtension(extension_b_path));
base::FilePath extension_c_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("remove_popup"));
ASSERT_TRUE(LoadExtension(extension_c_path));
std::string id_a = ExtensionAt(0)->id();
std::string id_b = ExtensionAt(1)->id();
std::string id_c = ExtensionAt(2)->id();
ExtensionIdList new_order;
new_order.push_back(id_c);
new_order.push_back(id_b);
ExtensionPrefs::Get(browser()->profile())->SetToolbarOrder(new_order);
EXPECT_EQ(id_c, ExtensionAt(0)->id());
EXPECT_EQ(id_b, ExtensionAt(1)->id());
EXPECT_EQ(id_a, ExtensionAt(2)->id());
}
IN_PROC_BROWSER_TEST_F(ExtensionToolbarModelTest, HighlightMode) {
EXPECT_FALSE(model_->HighlightExtensions(ExtensionIdList()));
EXPECT_EQ(0, highlight_mode_count_);
base::FilePath extension_a_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("basics"));
ASSERT_TRUE(LoadExtension(extension_a_path));
base::FilePath extension_b_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("popup"));
ASSERT_TRUE(LoadExtension(extension_b_path));
base::FilePath extension_c_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("remove_popup"));
ASSERT_TRUE(LoadExtension(extension_c_path));
std::string id_a = ExtensionAt(0)->id();
std::string id_b = ExtensionAt(1)->id();
std::string id_c = ExtensionAt(2)->id();
ExtensionIdList extension_ids;
extension_ids.push_back(id_b);
model_->HighlightExtensions(extension_ids);
EXPECT_EQ(1, highlight_mode_count_);
EXPECT_TRUE(model_->is_highlighting());
EXPECT_EQ(1u, model_->toolbar_items().size());
EXPECT_EQ(id_b, ExtensionAt(0)->id());
model_->StopHighlighting();
EXPECT_EQ(0, highlight_mode_count_);
EXPECT_FALSE(model_->is_highlighting());
EXPECT_EQ(3u, model_->toolbar_items().size());
EXPECT_EQ(id_a, ExtensionAt(0)->id());
EXPECT_EQ(id_b, ExtensionAt(1)->id());
EXPECT_EQ(id_c, ExtensionAt(2)->id());
model_->StopHighlighting();
EXPECT_EQ(0, highlight_mode_count_);
EXPECT_FALSE(model_->is_highlighting());
extension_ids.clear();
extension_ids.push_back(id_a);
extension_ids.push_back(id_b);
extension_ids.push_back(id_c);
model_->HighlightExtensions(extension_ids);
EXPECT_EQ(1, highlight_mode_count_);
EXPECT_EQ(3u, model_->toolbar_items().size());
EXPECT_EQ(id_a, ExtensionAt(0)->id());
EXPECT_EQ(id_b, ExtensionAt(1)->id());
EXPECT_EQ(id_c, ExtensionAt(2)->id());
extension_ids.clear();
extension_ids.push_back(id_b);
model_->HighlightExtensions(extension_ids);
EXPECT_EQ(2, highlight_mode_count_);
EXPECT_EQ(1u, model_->toolbar_items().size());
EXPECT_EQ(id_b, ExtensionAt(0)->id());
extension_ids.clear();
extension_ids.push_back(id_a);
extension_ids.push_back(id_b);
model_->HighlightExtensions(extension_ids);
EXPECT_EQ(3, highlight_mode_count_);
EXPECT_EQ(2u, model_->toolbar_items().size());
EXPECT_EQ(id_a, ExtensionAt(0)->id());
EXPECT_EQ(id_b, ExtensionAt(1)->id());
extension_ids.clear();
model_->HighlightExtensions(extension_ids);
EXPECT_EQ(2, highlight_mode_count_);
EXPECT_FALSE(model_->is_highlighting());
EXPECT_EQ(id_a, ExtensionAt(0)->id());
EXPECT_EQ(id_b, ExtensionAt(1)->id());
EXPECT_EQ(id_c, ExtensionAt(2)->id());
}
#if defined(OS_MACOSX)
#define MAYBE_HighlightModeRemove DISABLED_HighlightModeRemove
#else
#define MAYBE_HighlightModeRemove HighlightModeRemove
#endif
IN_PROC_BROWSER_TEST_F(ExtensionToolbarModelTest, MAYBE_HighlightModeRemove) {
base::FilePath extension_a_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("basics"));
ASSERT_TRUE(LoadExtension(extension_a_path));
base::FilePath extension_b_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("popup"));
ASSERT_TRUE(LoadExtension(extension_b_path));
base::FilePath extension_c_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("remove_popup"));
ASSERT_TRUE(LoadExtension(extension_c_path));
std::string id_a = ExtensionAt(0)->id();
std::string id_b = ExtensionAt(1)->id();
std::string id_c = ExtensionAt(2)->id();
ExtensionIdList extension_ids;
extension_ids.push_back(id_a);
extension_ids.push_back(id_b);
model_->HighlightExtensions(extension_ids);
EXPECT_TRUE(model_->is_highlighting());
EXPECT_EQ(1, highlight_mode_count_);
EXPECT_EQ(2u, model_->toolbar_items().size());
DisableExtension(id_a);
EXPECT_TRUE(model_->is_highlighting());
EXPECT_EQ(1u, model_->toolbar_items().size());
EXPECT_EQ(id_b, ExtensionAt(0)->id());
UninstallExtension(id_b);
EXPECT_FALSE(model_->is_highlighting());
EXPECT_EQ(0, highlight_mode_count_);
EXPECT_EQ(1u, model_->toolbar_items().size());
EXPECT_EQ(id_c, ExtensionAt(0)->id());
ASSERT_TRUE(LoadExtension(extension_b_path));
EXPECT_EQ(id_b, ExtensionAt(1)->id());
extension_ids.clear();
extension_ids.push_back(id_c);
model_->HighlightExtensions(extension_ids);
EXPECT_EQ(1, highlight_mode_count_);
EXPECT_TRUE(model_->is_highlighting());
EXPECT_EQ(1u, model_->toolbar_items().size());
EXPECT_EQ(id_c, ExtensionAt(0)->id());
UninstallExtension(id_b);
EXPECT_TRUE(model_->is_highlighting());
EXPECT_EQ(1, highlight_mode_count_);
EXPECT_EQ(1u, model_->toolbar_items().size());
EXPECT_EQ(id_c, ExtensionAt(0)->id());
model_->StopHighlighting();
EXPECT_FALSE(model_->is_highlighting());
EXPECT_EQ(0, highlight_mode_count_);
EXPECT_EQ(1u, model_->toolbar_items().size());
EXPECT_EQ(id_c, ExtensionAt(0)->id());
}
IN_PROC_BROWSER_TEST_F(ExtensionToolbarModelTest, HighlightModeAdd) {
base::FilePath extension_a_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("basics"));
ASSERT_TRUE(LoadExtension(extension_a_path));
base::FilePath extension_b_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("popup"));
ASSERT_TRUE(LoadExtension(extension_b_path));
std::string id_a = ExtensionAt(0)->id();
std::string id_b = ExtensionAt(1)->id();
ExtensionIdList extension_ids;
extension_ids.push_back(id_a);
model_->HighlightExtensions(extension_ids);
EXPECT_TRUE(model_->is_highlighting());
EXPECT_EQ(1u, model_->toolbar_items().size());
EXPECT_EQ(id_a, ExtensionAt(0)->id());
base::FilePath extension_c_path(test_data_dir_.AppendASCII("api_test")
.AppendASCII("browser_action")
.AppendASCII("remove_popup"));
const Extension* extension_c = LoadExtension(extension_c_path);
ASSERT_TRUE(extension_c);
std::string id_c = extension_c->id();
EXPECT_TRUE(model_->is_highlighting());
EXPECT_EQ(1u, model_->toolbar_items().size());
EXPECT_EQ(id_a, ExtensionAt(0)->id());
model_->StopHighlighting();
EXPECT_FALSE(model_->is_highlighting());
EXPECT_EQ(3u, model_->toolbar_items().size());
EXPECT_EQ(id_a, ExtensionAt(0)->id());
EXPECT_EQ(id_b, ExtensionAt(1)->id());
EXPECT_EQ(id_c, ExtensionAt(2)->id());
}
}