| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/browser.h" | 5 #include "chrome/browser/browser.h" |
| 6 #include "chrome/browser/extensions/extension_browsertest.h" |
| 6 #include "chrome/browser/extensions/extension_host.h" | 7 #include "chrome/browser/extensions/extension_host.h" |
| 7 #include "chrome/browser/extensions/extension_shelf_model.h" | 8 #include "chrome/browser/extensions/extension_shelf_model.h" |
| 8 #include "chrome/browser/extensions/extensions_service.h" | 9 #include "chrome/browser/extensions/extensions_service.h" |
| 9 #include "chrome/browser/extensions/test_extension_loader.h" | |
| 10 #include "chrome/browser/profile.h" | 10 #include "chrome/browser/profile.h" |
| 11 #include "chrome/browser/views/extensions/extension_shelf.h" |
| 12 #include "chrome/browser/views/frame/browser_view.h" |
| 11 #include "chrome/common/chrome_paths.h" | 13 #include "chrome/common/chrome_paths.h" |
| 12 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/common/extensions/extension_error_reporter.h" | 15 #include "chrome/common/extensions/extension_error_reporter.h" |
| 14 #include "chrome/test/in_process_browser_test.h" | 16 #include "chrome/test/in_process_browser_test.h" |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| 18 // The extension we're using as our test case. | 20 // The extension we're using as our test case. |
| 19 const char* kExtensionId = "behllobkkfkfnphdnhnkndlbkcpglgmj"; | 21 const char* kExtensionId = "behllobkkfkfnphdnhnkndlbkcpglgmj"; |
| 20 | 22 |
| 21 }; // namespace | 23 }; // namespace |
| 22 | 24 |
| 23 | 25 |
| 24 // An InProcessBrowserTest for testing the ExtensionShelfModel. | 26 // An InProcessBrowserTest for testing the ExtensionShelfModel. |
| 25 // TODO(erikkay) It's unfortunate that this needs to be an in-proc browser test. | 27 // TODO(erikkay) It's unfortunate that this needs to be an in-proc browser test. |
| 26 // It would be nice to refactor things so that ExtensionShelfModel, | 28 // It would be nice to refactor things so that ExtensionShelfModel, |
| 27 // ExtensionHost and ExtensionsService could run without so much of the browser | 29 // ExtensionHost and ExtensionsService could run without so much of the browser |
| 28 // in place. | 30 // in place. |
| 29 class ExtensionShelfModelTest : public InProcessBrowserTest, | 31 class ExtensionShelfModelTest : public ExtensionBrowserTest, |
| 30 public ExtensionShelfModelObserver { | 32 public ExtensionShelfModelObserver { |
| 31 public: | 33 public: |
| 32 virtual void SetUp() { | 34 virtual void SetUp() { |
| 33 // Initialize the error reporter here, or BrowserMain will create it with | |
| 34 // the wrong MessageLoop. | |
| 35 ExtensionErrorReporter::Init(false); | |
| 36 inserted_count_ = 0; | 35 inserted_count_ = 0; |
| 37 removed_count_ = 0; | 36 removed_count_ = 0; |
| 38 moved_count_ = 0; | 37 moved_count_ = 0; |
| 39 | |
| 40 InProcessBrowserTest::SetUp(); | 38 InProcessBrowserTest::SetUp(); |
| 41 } | 39 } |
| 42 | 40 |
| 43 virtual void SetUpCommandLine(CommandLine* command_line) { | |
| 44 command_line->AppendSwitch(switches::kEnableExtensions); | |
| 45 } | |
| 46 | |
| 47 virtual Browser* CreateBrowser(Profile* profile) { | 41 virtual Browser* CreateBrowser(Profile* profile) { |
| 48 Browser* b = InProcessBrowserTest::CreateBrowser(profile); | 42 Browser* b = InProcessBrowserTest::CreateBrowser(profile); |
| 49 model_ = new ExtensionShelfModel(b); | 43 BrowserView* browser_view = static_cast<BrowserView*>(b->window()); |
| 44 model_ = browser_view->extension_shelf()->model(); |
| 50 model_->AddObserver(this); | 45 model_->AddObserver(this); |
| 51 return b; | 46 return b; |
| 52 } | 47 } |
| 53 | 48 |
| 49 virtual void CleanUpOnMainThread() { |
| 50 model_->RemoveObserver(this); |
| 51 } |
| 52 |
| 54 virtual void ToolstripInsertedAt(ExtensionHost* toolstrip, int index) { | 53 virtual void ToolstripInsertedAt(ExtensionHost* toolstrip, int index) { |
| 55 inserted_count_++; | 54 inserted_count_++; |
| 56 } | 55 } |
| 57 | 56 |
| 58 virtual void ToolstripRemovingAt(ExtensionHost* toolstrip, int index) { | 57 virtual void ToolstripRemovingAt(ExtensionHost* toolstrip, int index) { |
| 59 removed_count_++; | 58 removed_count_++; |
| 60 } | 59 } |
| 61 | 60 |
| 62 virtual void ToolstripMoved(ExtensionHost* toolstrip, | 61 virtual void ToolstripMoved(ExtensionHost* toolstrip, |
| 63 int from_index, | 62 int from_index, |
| 64 int to_index) { | 63 int to_index) { |
| 65 moved_count_++; | 64 moved_count_++; |
| 66 } | 65 } |
| 67 | 66 |
| 68 protected: | 67 protected: |
| 69 ExtensionShelfModel* model_; | 68 ExtensionShelfModel* model_; |
| 70 | 69 |
| 71 int inserted_count_; | 70 int inserted_count_; |
| 72 int removed_count_; | 71 int removed_count_; |
| 73 int moved_count_; | 72 int moved_count_; |
| 74 }; | 73 }; |
| 75 | 74 |
| 76 // TODO(erikkay): http://crbug.com/15291 disabled because fails on build-bot. | 75 IN_PROC_BROWSER_TEST_F(ExtensionShelfModelTest, Basic) { |
| 77 IN_PROC_BROWSER_TEST_F(ExtensionShelfModelTest, DISABLED_Basic) { | 76 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("good") |
| 78 // Get the path to our extension. | 77 .AppendASCII("Extensions") |
| 79 FilePath path; | 78 .AppendASCII(kExtensionId) |
| 80 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); | 79 .AppendASCII("1.0.0.0"))); |
| 81 path = path.AppendASCII("extensions") | |
| 82 .AppendASCII("good") | |
| 83 .AppendASCII("Extensions") | |
| 84 .AppendASCII(kExtensionId).AppendASCII("1.0.0.0"); | |
| 85 ASSERT_TRUE(file_util::DirectoryExists(path)); // sanity check | |
| 86 | |
| 87 // Wait for the extension to load and grab a pointer to it. | |
| 88 TestExtensionLoader loader(browser()->profile()); | |
| 89 Extension* extension = loader.Load(kExtensionId, path); | |
| 90 ASSERT_TRUE(extension); | |
| 91 | 80 |
| 92 // extension1 has two toolstrips | 81 // extension1 has two toolstrips |
| 93 EXPECT_EQ(inserted_count_, 2); | 82 EXPECT_EQ(inserted_count_, 2); |
| 94 ExtensionHost* one = model_->ToolstripAt(0); | 83 ExtensionHost* one = model_->ToolstripAt(0); |
| 95 ExtensionHost* two = model_->ToolstripAt(1); | 84 ExtensionHost* two = model_->ToolstripAt(1); |
| 96 EXPECT_EQ(one->GetURL().path(), "/toolstrip1.html"); | 85 EXPECT_EQ(one->GetURL().path(), "/toolstrip1.html"); |
| 97 EXPECT_EQ(two->GetURL().path(), "/toolstrip2.html"); | 86 EXPECT_EQ(two->GetURL().path(), "/toolstrip2.html"); |
| 98 | 87 |
| 99 model_->MoveToolstripAt(0, 1); | 88 model_->MoveToolstripAt(0, 1); |
| 100 EXPECT_EQ(two, model_->ToolstripAt(0)); | 89 EXPECT_EQ(two, model_->ToolstripAt(0)); |
| 101 EXPECT_EQ(one, model_->ToolstripAt(1)); | 90 EXPECT_EQ(one, model_->ToolstripAt(1)); |
| 102 EXPECT_EQ(moved_count_, 1); | 91 EXPECT_EQ(moved_count_, 1); |
| 103 | 92 |
| 104 model_->RemoveToolstripAt(0); | 93 model_->RemoveToolstripAt(0); |
| 105 EXPECT_EQ(one, model_->ToolstripAt(0)); | 94 EXPECT_EQ(one, model_->ToolstripAt(0)); |
| 106 EXPECT_EQ(1, model_->count()); | 95 EXPECT_EQ(1, model_->count()); |
| 107 EXPECT_EQ(removed_count_, 1); | 96 EXPECT_EQ(removed_count_, 1); |
| 108 | |
| 109 // Tear down |model_| manually here rather than in the destructor or with | |
| 110 // a scoped_ptr. InProcessBrowserTest doesn't give us a chance to clean | |
| 111 // up before the browser and all of its services have been shut down, | |
| 112 // and |model_| depends on these existing. | |
| 113 model_->RemoveObserver(this); | |
| 114 delete model_; | |
| 115 model_ = NULL; | |
| 116 } | 97 } |
| OLD | NEW |