| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/files/file_path.h" | |
| 6 #include "base/message_loop/message_loop.h" | |
| 7 #include "chrome/browser/extensions/extension_action.h" | |
| 8 #include "chrome/browser/extensions/extension_action_manager.h" | |
| 9 #include "chrome/browser/extensions/extension_browsertest.h" | |
| 10 #include "chrome/browser/extensions/state_store.h" | |
| 11 #include "content/public/test/test_utils.h" | |
| 12 #include "extensions/browser/extension_registry.h" | |
| 13 #include "extensions/browser/extension_system.h" | |
| 14 #include "extensions/common/extension.h" | |
| 15 #include "third_party/skia/include/core/SkColor.h" | |
| 16 | |
| 17 namespace extensions { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // A key into the StateStore; we don't use any results, but need to know when | |
| 22 // it's initialized. | |
| 23 const char kBrowserActionStorageKey[] = "browser_action"; | |
| 24 // The name of the extension we add. | |
| 25 const char kExtensionName[] = "Default Persistence Test Extension"; | |
| 26 | |
| 27 void QuitMessageLoop(content::MessageLoopRunner* runner, | |
| 28 scoped_ptr<base::Value> value) { | |
| 29 runner->Quit(); | |
| 30 } | |
| 31 | |
| 32 // We need to wait for the state store to initialize and respond to requests | |
| 33 // so we can see if the preferences persist. Do this by posting our own request | |
| 34 // to the state store, which should be handled after all others. | |
| 35 void WaitForStateStore(Profile* profile, const std::string& extension_id) { | |
| 36 scoped_refptr<content::MessageLoopRunner> runner = | |
| 37 new content::MessageLoopRunner; | |
| 38 ExtensionSystem::Get(profile)->state_store()->GetExtensionValue( | |
| 39 extension_id, | |
| 40 kBrowserActionStorageKey, | |
| 41 base::Bind(&QuitMessageLoop, runner)); | |
| 42 runner->Run(); | |
| 43 } | |
| 44 | |
| 45 } // namespace | |
| 46 | |
| 47 // Setup for the test by loading an extension, which should set the browser | |
| 48 // action background to blue. | |
| 49 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, | |
| 50 PRE_BrowserActionDefaultPersistence) { | |
| 51 const Extension* extension = | |
| 52 LoadExtension(test_data_dir_.AppendASCII("api_test") | |
| 53 .AppendASCII("browser_action") | |
| 54 .AppendASCII("default_persistence")); | |
| 55 ASSERT_TRUE(extension); | |
| 56 ASSERT_EQ(kExtensionName, extension->name()); | |
| 57 WaitForStateStore(profile(), extension->id()); | |
| 58 | |
| 59 ExtensionAction* extension_action = | |
| 60 ExtensionActionManager::Get(profile())->GetBrowserAction(*extension); | |
| 61 ASSERT_TRUE(extension_action); | |
| 62 EXPECT_EQ(SK_ColorBLUE, extension_action->GetBadgeBackgroundColor(0)); | |
| 63 } | |
| 64 | |
| 65 // When Chrome restarts, the Extension will immediately update the browser | |
| 66 // action, but will not modify the badge background color. Thus, the background | |
| 67 // should remain blue (persisting the default set in onInstalled()). | |
| 68 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, BrowserActionDefaultPersistence) { | |
| 69 // Find the extension (it's a shame we don't have an ID for this, but it | |
| 70 // was generated in the last test). | |
| 71 const Extension* extension = NULL; | |
| 72 const ExtensionSet& extension_set = | |
| 73 ExtensionRegistry::Get(profile())->enabled_extensions(); | |
| 74 for (ExtensionSet::const_iterator iter = extension_set.begin(); | |
| 75 iter != extension_set.end(); | |
| 76 ++iter) { | |
| 77 if ((*iter)->name() == kExtensionName) { | |
| 78 extension = *iter; | |
| 79 break; | |
| 80 } | |
| 81 } | |
| 82 ASSERT_TRUE(extension) << "Could not find extension in registry."; | |
| 83 | |
| 84 // If this log becomes frequent, this test is losing its effectiveness, and | |
| 85 // we need to find a more invasive way of ensuring the test's StateStore | |
| 86 // initializes after extensions get their onStartup event. | |
| 87 if (ExtensionSystem::Get(profile())->state_store()->IsInitialized()) | |
| 88 LOG(WARNING) << "State store already initialized; test guaranteed to pass."; | |
| 89 | |
| 90 // Wait for the StateStore to load, and fetch the defaults. | |
| 91 WaitForStateStore(profile(), extension->id()); | |
| 92 | |
| 93 // Ensure the BrowserAction's badge background is still blue. | |
| 94 ExtensionAction* extension_action = | |
| 95 ExtensionActionManager::Get(profile())->GetBrowserAction(*extension); | |
| 96 ASSERT_TRUE(extension_action); | |
| 97 EXPECT_EQ(SK_ColorBLUE, extension_action->GetBadgeBackgroundColor(0)); | |
| 98 } | |
| 99 | |
| 100 } // namespace extensions | |
| OLD | NEW |