Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / extension_action / browser_action_browsertest.cc
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/extension_test_message_listener.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/browser/state_store.h"
15 #include "extensions/common/extension.h"
16 #include "third_party/skia/include/core/SkColor.h"
17
18 namespace extensions {
19
20 namespace {
21
22 // A key into the StateStore; we don't use any results, but need to know when
23 // it's initialized.
24 const char kBrowserActionStorageKey[] = "browser_action";
25 // The name of the extension we add.
26 const char kExtensionName[] = "Default Persistence Test Extension";
27
28 void QuitMessageLoop(content::MessageLoopRunner* runner,
29                      scoped_ptr<base::Value> value) {
30   runner->Quit();
31 }
32
33 // We need to wait for the state store to initialize and respond to requests
34 // so we can see if the preferences persist. Do this by posting our own request
35 // to the state store, which should be handled after all others.
36 void WaitForStateStore(Profile* profile, const std::string& extension_id) {
37   scoped_refptr<content::MessageLoopRunner> runner =
38       new content::MessageLoopRunner;
39   ExtensionSystem::Get(profile)->state_store()->GetExtensionValue(
40       extension_id,
41       kBrowserActionStorageKey,
42       base::Bind(&QuitMessageLoop, runner));
43   runner->Run();
44 }
45
46 }  // namespace
47
48 // Setup for the test by loading an extension, which should set the browser
49 // action background to blue.
50 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest,
51                        PRE_BrowserActionDefaultPersistence) {
52   ExtensionTestMessageListener listener("Background Color Set",
53                                         false /* won't send custom reply */);
54
55   const Extension* extension =
56       LoadExtension(test_data_dir_.AppendASCII("api_test")
57                         .AppendASCII("browser_action")
58                         .AppendASCII("default_persistence"));
59   ASSERT_TRUE(extension);
60   ASSERT_EQ(kExtensionName, extension->name());
61   WaitForStateStore(profile(), extension->id());
62
63   // Make sure we've given the extension enough time to set the background color
64   // in chrome.runtime.onInstalled.
65   ASSERT_TRUE(listener.WaitUntilSatisfied());
66
67   ExtensionAction* extension_action =
68       ExtensionActionManager::Get(profile())->GetBrowserAction(*extension);
69   ASSERT_TRUE(extension_action);
70   EXPECT_EQ(SK_ColorBLUE, extension_action->GetBadgeBackgroundColor(0));
71 }
72
73 // When Chrome restarts, the Extension will immediately update the browser
74 // action, but will not modify the badge background color. Thus, the background
75 // should remain blue (persisting the default set in onInstalled()).
76 IN_PROC_BROWSER_TEST_F(ExtensionBrowserTest, BrowserActionDefaultPersistence) {
77   // Find the extension (it's a shame we don't have an ID for this, but it
78   // was generated in the last test).
79   const Extension* extension = NULL;
80   const ExtensionSet& extension_set =
81       ExtensionRegistry::Get(profile())->enabled_extensions();
82   for (ExtensionSet::const_iterator iter = extension_set.begin();
83        iter != extension_set.end();
84        ++iter) {
85     if ((*iter)->name() == kExtensionName) {
86       extension = *iter;
87       break;
88     }
89   }
90   ASSERT_TRUE(extension) << "Could not find extension in registry.";
91
92   ExtensionAction* extension_action =
93       ExtensionActionManager::Get(profile())->GetBrowserAction(*extension);
94   ASSERT_TRUE(extension_action);
95
96   // If the extension hasn't already set the badge text, then we should wait for
97   // it to do so.
98   if (extension_action->GetBadgeText(0) != "Hello") {
99     ExtensionTestMessageListener listener("Badge Text Set",
100                                           false /* won't send custom reply */);
101     ASSERT_TRUE(listener.WaitUntilSatisfied());
102   }
103
104   // If this log becomes frequent, this test is losing its effectiveness, and
105   // we need to find a more invasive way of ensuring the test's StateStore
106   // initializes after extensions get their onStartup event.
107   if (ExtensionSystem::Get(profile())->state_store()->IsInitialized())
108     LOG(WARNING) << "State store already initialized; test guaranteed to pass.";
109
110   // Wait for the StateStore to load, and fetch the defaults.
111   WaitForStateStore(profile(), extension->id());
112
113   // Ensure the BrowserAction's badge background is still blue.
114   EXPECT_EQ(SK_ColorBLUE, extension_action->GetBadgeBackgroundColor(0));
115 }
116
117 }  // namespace extensions