Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / storage / settings_apitest.cc
1 // Copyright (c) 2012 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/bind.h"
6 #include "base/json/json_writer.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/run_loop.h"
9 #include "base/values.h"
10 #include "chrome/browser/extensions/api/storage/settings_frontend.h"
11 #include "chrome/browser/extensions/api/storage/settings_namespace.h"
12 #include "chrome/browser/extensions/api/storage/settings_sync_util.h"
13 #include "chrome/browser/extensions/extension_apitest.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_system_factory.h"
16 #include "chrome/browser/extensions/extension_test_message_listener.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "extensions/browser/extension_system.h"
21 #include "extensions/common/value_builder.h"
22 #include "sync/api/sync_change.h"
23 #include "sync/api/sync_change_processor.h"
24 #include "sync/api/sync_error_factory.h"
25 #include "sync/api/sync_error_factory_mock.h"
26 #include "testing/gmock/include/gmock/gmock.h"
27
28 #if defined(ENABLE_CONFIGURATION_POLICY)
29 #include "chrome/browser/policy/schema_registry_service.h"
30 #include "chrome/browser/policy/schema_registry_service_factory.h"
31 #include "components/policy/core/browser/browser_policy_connector.h"
32 #include "components/policy/core/common/mock_configuration_policy_provider.h"
33 #include "components/policy/core/common/policy_bundle.h"
34 #include "components/policy/core/common/policy_map.h"
35 #include "components/policy/core/common/policy_namespace.h"
36 #include "components/policy/core/common/schema.h"
37 #include "components/policy/core/common/schema_map.h"
38 #include "components/policy/core/common/schema_registry.h"
39 #endif
40
41 namespace extensions {
42
43 using settings_namespace::LOCAL;
44 using settings_namespace::MANAGED;
45 using settings_namespace::Namespace;
46 using settings_namespace::SYNC;
47 using settings_namespace::ToString;
48 using testing::Mock;
49 using testing::Return;
50 using testing::_;
51
52 namespace {
53
54 // TODO(kalman): test both EXTENSION_SETTINGS and APP_SETTINGS.
55 const syncer::ModelType kModelType = syncer::EXTENSION_SETTINGS;
56
57 // The managed_storage extension has a key defined in its manifest, so that
58 // its extension ID is well-known and the policy system can push policies for
59 // the extension.
60 const char kManagedStorageExtensionId[] = "kjmkgkdkpedkejedfhmfcenooemhbpbo";
61
62 class NoopSyncChangeProcessor : public syncer::SyncChangeProcessor {
63  public:
64   virtual syncer::SyncError ProcessSyncChanges(
65       const tracked_objects::Location& from_here,
66       const syncer::SyncChangeList& change_list) OVERRIDE {
67     return syncer::SyncError();
68   }
69
70   virtual syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const
71       OVERRIDE {
72     return syncer::SyncDataList();
73   }
74
75   virtual ~NoopSyncChangeProcessor() {};
76 };
77
78 class SyncChangeProcessorDelegate : public syncer::SyncChangeProcessor {
79  public:
80   explicit SyncChangeProcessorDelegate(syncer::SyncChangeProcessor* recipient)
81       : recipient_(recipient) {
82     DCHECK(recipient_);
83   }
84   virtual ~SyncChangeProcessorDelegate() {}
85
86   // syncer::SyncChangeProcessor implementation.
87   virtual syncer::SyncError ProcessSyncChanges(
88       const tracked_objects::Location& from_here,
89       const syncer::SyncChangeList& change_list) OVERRIDE {
90     return recipient_->ProcessSyncChanges(from_here, change_list);
91   }
92
93   virtual syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const
94       OVERRIDE {
95     return recipient_->GetAllSyncData(type);
96   }
97
98  private:
99   // The recipient of all sync changes.
100   syncer::SyncChangeProcessor* recipient_;
101
102   DISALLOW_COPY_AND_ASSIGN(SyncChangeProcessorDelegate);
103 };
104
105 class MockSchemaRegistryObserver : public policy::SchemaRegistry::Observer {
106  public:
107   MockSchemaRegistryObserver() {}
108   virtual ~MockSchemaRegistryObserver() {}
109
110   MOCK_METHOD1(OnSchemaRegistryUpdated, void(bool));
111   MOCK_METHOD0(OnSchemaRegistryReady, void());
112 };
113
114 }  // namespace
115
116 class ExtensionSettingsApiTest : public ExtensionApiTest {
117  protected:
118   virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
119     ExtensionApiTest::SetUpInProcessBrowserTestFixture();
120
121 #if defined(ENABLE_CONFIGURATION_POLICY)
122     EXPECT_CALL(policy_provider_, IsInitializationComplete(_))
123         .WillRepeatedly(Return(true));
124     policy_provider_.SetAutoRefresh();
125     policy::BrowserPolicyConnector::SetPolicyProviderForTesting(
126         &policy_provider_);
127 #endif
128   }
129
130   void ReplyWhenSatisfied(
131       Namespace settings_namespace,
132       const std::string& normal_action,
133       const std::string& incognito_action) {
134     MaybeLoadAndReplyWhenSatisfied(
135         settings_namespace, normal_action, incognito_action, NULL, false);
136   }
137
138   const Extension* LoadAndReplyWhenSatisfied(
139       Namespace settings_namespace,
140       const std::string& normal_action,
141       const std::string& incognito_action,
142       const std::string& extension_dir) {
143     return MaybeLoadAndReplyWhenSatisfied(
144         settings_namespace,
145         normal_action,
146         incognito_action,
147         &extension_dir,
148         false);
149   }
150
151   void FinalReplyWhenSatisfied(
152       Namespace settings_namespace,
153       const std::string& normal_action,
154       const std::string& incognito_action) {
155     MaybeLoadAndReplyWhenSatisfied(
156         settings_namespace, normal_action, incognito_action, NULL, true);
157   }
158
159   void InitSync(syncer::SyncChangeProcessor* sync_processor) {
160     base::MessageLoop::current()->RunUntilIdle();
161     InitSyncWithSyncableService(
162         sync_processor,
163         browser()->profile()->GetExtensionService()->settings_frontend()->
164               GetBackendForSync(kModelType));
165   }
166
167   void SendChanges(const syncer::SyncChangeList& change_list) {
168     base::MessageLoop::current()->RunUntilIdle();
169     SendChangesToSyncableService(
170         change_list,
171         browser()->profile()->GetExtensionService()->settings_frontend()->
172               GetBackendForSync(kModelType));
173   }
174
175 #if defined(ENABLE_CONFIGURATION_POLICY)
176   void SetPolicies(const base::DictionaryValue& policies) {
177     scoped_ptr<policy::PolicyBundle> bundle(new policy::PolicyBundle());
178     policy::PolicyMap& policy_map = bundle->Get(policy::PolicyNamespace(
179         policy::POLICY_DOMAIN_EXTENSIONS, kManagedStorageExtensionId));
180     policy_map.LoadFrom(
181         &policies, policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER);
182     policy_provider_.UpdatePolicy(bundle.Pass());
183   }
184 #endif
185
186  private:
187   const Extension* MaybeLoadAndReplyWhenSatisfied(
188       Namespace settings_namespace,
189       const std::string& normal_action,
190       const std::string& incognito_action,
191       // May be NULL to imply not loading the extension.
192       const std::string* extension_dir,
193       bool is_final_action) {
194     ExtensionTestMessageListener listener("waiting", true);
195     ExtensionTestMessageListener listener_incognito("waiting_incognito", true);
196
197     // Only load the extension after the listeners have been set up, to avoid
198     // initialisation race conditions.
199     const Extension* extension = NULL;
200     if (extension_dir) {
201       extension = LoadExtensionIncognito(
202           test_data_dir_.AppendASCII("settings").AppendASCII(*extension_dir));
203       EXPECT_TRUE(extension);
204     }
205
206     EXPECT_TRUE(listener.WaitUntilSatisfied());
207     EXPECT_TRUE(listener_incognito.WaitUntilSatisfied());
208
209     listener.Reply(
210         CreateMessage(settings_namespace, normal_action, is_final_action));
211     listener_incognito.Reply(
212         CreateMessage(settings_namespace, incognito_action, is_final_action));
213     return extension;
214   }
215
216   std::string CreateMessage(
217       Namespace settings_namespace,
218       const std::string& action,
219       bool is_final_action) {
220     scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue());
221     message->SetString("namespace", ToString(settings_namespace));
222     message->SetString("action", action);
223     message->SetBoolean("isFinalAction", is_final_action);
224     std::string message_json;
225     base::JSONWriter::Write(message.get(), &message_json);
226     return message_json;
227   }
228
229   void InitSyncWithSyncableService(
230       syncer::SyncChangeProcessor* sync_processor,
231       syncer::SyncableService* settings_service) {
232     EXPECT_FALSE(settings_service->MergeDataAndStartSyncing(
233         kModelType,
234         syncer::SyncDataList(),
235         scoped_ptr<syncer::SyncChangeProcessor>(
236             new SyncChangeProcessorDelegate(sync_processor)),
237         scoped_ptr<syncer::SyncErrorFactory>(
238             new syncer::SyncErrorFactoryMock())).error().IsSet());
239   }
240
241   void SendChangesToSyncableService(
242       const syncer::SyncChangeList& change_list,
243       syncer::SyncableService* settings_service) {
244     EXPECT_FALSE(
245         settings_service->ProcessSyncChanges(FROM_HERE, change_list).IsSet());
246   }
247
248  protected:
249 #if defined(ENABLE_CONFIGURATION_POLICY)
250   policy::MockConfigurationPolicyProvider policy_provider_;
251 #endif
252 };
253
254 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest, SimpleTest) {
255   ASSERT_TRUE(RunExtensionTest("settings/simple_test")) << message_;
256 }
257
258 // Structure of this test taken from IncognitoSplitMode.
259 // Note that only split-mode incognito is tested, because spanning mode
260 // incognito looks the same as normal mode when the only API activity comes
261 // from background pages.
262 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest, SplitModeIncognito) {
263   // We need 2 ResultCatchers because we'll be running the same test in both
264   // regular and incognito mode.
265   ResultCatcher catcher, catcher_incognito;
266   catcher.RestrictToProfile(browser()->profile());
267   catcher_incognito.RestrictToProfile(
268       browser()->profile()->GetOffTheRecordProfile());
269
270   LoadAndReplyWhenSatisfied(SYNC,
271       "assertEmpty", "assertEmpty", "split_incognito");
272   ReplyWhenSatisfied(SYNC, "noop", "setFoo");
273   ReplyWhenSatisfied(SYNC, "assertFoo", "assertFoo");
274   ReplyWhenSatisfied(SYNC, "clear", "noop");
275   ReplyWhenSatisfied(SYNC, "assertEmpty", "assertEmpty");
276   ReplyWhenSatisfied(SYNC, "setFoo", "noop");
277   ReplyWhenSatisfied(SYNC, "assertFoo", "assertFoo");
278   ReplyWhenSatisfied(SYNC, "noop", "removeFoo");
279   FinalReplyWhenSatisfied(SYNC, "assertEmpty", "assertEmpty");
280
281   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
282   EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message();
283 }
284
285 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest,
286     OnChangedNotificationsBetweenBackgroundPages) {
287   // We need 2 ResultCatchers because we'll be running the same test in both
288   // regular and incognito mode.
289   ResultCatcher catcher, catcher_incognito;
290   catcher.RestrictToProfile(browser()->profile());
291   catcher_incognito.RestrictToProfile(
292       browser()->profile()->GetOffTheRecordProfile());
293
294   LoadAndReplyWhenSatisfied(SYNC,
295       "assertNoNotifications", "assertNoNotifications", "split_incognito");
296   ReplyWhenSatisfied(SYNC, "noop", "setFoo");
297   ReplyWhenSatisfied(SYNC,
298       "assertAddFooNotification", "assertAddFooNotification");
299   ReplyWhenSatisfied(SYNC, "clearNotifications", "clearNotifications");
300   ReplyWhenSatisfied(SYNC, "removeFoo", "noop");
301   FinalReplyWhenSatisfied(SYNC,
302       "assertDeleteFooNotification", "assertDeleteFooNotification");
303
304   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
305   EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message();
306 }
307
308 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest,
309     SyncAndLocalAreasAreSeparate) {
310   // We need 2 ResultCatchers because we'll be running the same test in both
311   // regular and incognito mode.
312   ResultCatcher catcher, catcher_incognito;
313   catcher.RestrictToProfile(browser()->profile());
314   catcher_incognito.RestrictToProfile(
315       browser()->profile()->GetOffTheRecordProfile());
316
317   LoadAndReplyWhenSatisfied(SYNC,
318       "assertNoNotifications", "assertNoNotifications", "split_incognito");
319
320   ReplyWhenSatisfied(SYNC, "noop", "setFoo");
321   ReplyWhenSatisfied(SYNC, "assertFoo", "assertFoo");
322   ReplyWhenSatisfied(SYNC,
323       "assertAddFooNotification", "assertAddFooNotification");
324   ReplyWhenSatisfied(LOCAL, "assertEmpty", "assertEmpty");
325   ReplyWhenSatisfied(LOCAL, "assertNoNotifications", "assertNoNotifications");
326
327   ReplyWhenSatisfied(SYNC, "clearNotifications", "clearNotifications");
328
329   ReplyWhenSatisfied(LOCAL, "setFoo", "noop");
330   ReplyWhenSatisfied(LOCAL, "assertFoo", "assertFoo");
331   ReplyWhenSatisfied(LOCAL,
332       "assertAddFooNotification", "assertAddFooNotification");
333   ReplyWhenSatisfied(SYNC, "assertFoo", "assertFoo");
334   ReplyWhenSatisfied(SYNC, "assertNoNotifications", "assertNoNotifications");
335
336   ReplyWhenSatisfied(LOCAL, "clearNotifications", "clearNotifications");
337
338   ReplyWhenSatisfied(LOCAL, "noop", "removeFoo");
339   ReplyWhenSatisfied(LOCAL, "assertEmpty", "assertEmpty");
340   ReplyWhenSatisfied(LOCAL,
341       "assertDeleteFooNotification", "assertDeleteFooNotification");
342   ReplyWhenSatisfied(SYNC, "assertFoo", "assertFoo");
343   ReplyWhenSatisfied(SYNC, "assertNoNotifications", "assertNoNotifications");
344
345   ReplyWhenSatisfied(LOCAL, "clearNotifications", "clearNotifications");
346
347   ReplyWhenSatisfied(SYNC, "removeFoo", "noop");
348   ReplyWhenSatisfied(SYNC, "assertEmpty", "assertEmpty");
349   ReplyWhenSatisfied(SYNC,
350       "assertDeleteFooNotification", "assertDeleteFooNotification");
351   ReplyWhenSatisfied(LOCAL, "assertNoNotifications", "assertNoNotifications");
352   FinalReplyWhenSatisfied(LOCAL, "assertEmpty", "assertEmpty");
353
354   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
355   EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message();
356 }
357
358 // Disabled, see crbug.com/101110
359 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest,
360     DISABLED_OnChangedNotificationsFromSync) {
361   // We need 2 ResultCatchers because we'll be running the same test in both
362   // regular and incognito mode.
363   ResultCatcher catcher, catcher_incognito;
364   catcher.RestrictToProfile(browser()->profile());
365   catcher_incognito.RestrictToProfile(
366       browser()->profile()->GetOffTheRecordProfile());
367
368   const Extension* extension =
369       LoadAndReplyWhenSatisfied(SYNC,
370           "assertNoNotifications", "assertNoNotifications", "split_incognito");
371   const std::string& extension_id = extension->id();
372
373   NoopSyncChangeProcessor sync_processor;
374   InitSync(&sync_processor);
375
376   // Set "foo" to "bar" via sync.
377   syncer::SyncChangeList sync_changes;
378   base::StringValue bar("bar");
379   sync_changes.push_back(settings_sync_util::CreateAdd(
380       extension_id, "foo", bar, kModelType));
381   SendChanges(sync_changes);
382
383   ReplyWhenSatisfied(SYNC,
384       "assertAddFooNotification", "assertAddFooNotification");
385   ReplyWhenSatisfied(SYNC, "clearNotifications", "clearNotifications");
386
387   // Remove "foo" via sync.
388   sync_changes.clear();
389   sync_changes.push_back(settings_sync_util::CreateDelete(
390       extension_id, "foo", kModelType));
391   SendChanges(sync_changes);
392
393   FinalReplyWhenSatisfied(SYNC,
394       "assertDeleteFooNotification", "assertDeleteFooNotification");
395
396   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
397   EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message();
398 }
399
400 // Disabled, see crbug.com/101110
401 //
402 // TODO: boring test, already done in the unit tests.  What we really should be
403 // be testing is that the areas don't overlap.
404 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest,
405     DISABLED_OnChangedNotificationsFromSyncNotSentToLocal) {
406   // We need 2 ResultCatchers because we'll be running the same test in both
407   // regular and incognito mode.
408   ResultCatcher catcher, catcher_incognito;
409   catcher.RestrictToProfile(browser()->profile());
410   catcher_incognito.RestrictToProfile(
411       browser()->profile()->GetOffTheRecordProfile());
412
413   const Extension* extension =
414       LoadAndReplyWhenSatisfied(LOCAL,
415           "assertNoNotifications", "assertNoNotifications", "split_incognito");
416   const std::string& extension_id = extension->id();
417
418   NoopSyncChangeProcessor sync_processor;
419   InitSync(&sync_processor);
420
421   // Set "foo" to "bar" via sync.
422   syncer::SyncChangeList sync_changes;
423   base::StringValue bar("bar");
424   sync_changes.push_back(settings_sync_util::CreateAdd(
425       extension_id, "foo", bar, kModelType));
426   SendChanges(sync_changes);
427
428   ReplyWhenSatisfied(LOCAL, "assertNoNotifications", "assertNoNotifications");
429
430   // Remove "foo" via sync.
431   sync_changes.clear();
432   sync_changes.push_back(settings_sync_util::CreateDelete(
433       extension_id, "foo", kModelType));
434   SendChanges(sync_changes);
435
436   FinalReplyWhenSatisfied(LOCAL,
437       "assertNoNotifications", "assertNoNotifications");
438
439   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
440   EXPECT_TRUE(catcher_incognito.GetNextResult()) << catcher.message();
441 }
442
443 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest, IsStorageEnabled) {
444   SettingsFrontend* frontend =
445       browser()->profile()->GetExtensionService()->settings_frontend();
446   EXPECT_TRUE(frontend->IsStorageEnabled(LOCAL));
447   EXPECT_TRUE(frontend->IsStorageEnabled(SYNC));
448
449 #if defined(ENABLE_CONFIGURATION_POLICY)
450   EXPECT_TRUE(frontend->IsStorageEnabled(MANAGED));
451 #else
452   EXPECT_FALSE(frontend->IsStorageEnabled(MANAGED));
453 #endif
454 }
455
456 #if defined(ENABLE_CONFIGURATION_POLICY)
457
458 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest, ExtensionsSchemas) {
459   // Verifies that the Schemas for the extensions domain are created on startup.
460   Profile* profile = browser()->profile();
461   ExtensionSystem* extension_system = ExtensionSystem::Get(profile);
462   if (!extension_system->ready().is_signaled()) {
463     // Wait until the extension system is ready.
464     base::RunLoop run_loop;
465     extension_system->ready().Post(FROM_HERE, run_loop.QuitClosure());
466     run_loop.Run();
467     ASSERT_TRUE(extension_system->ready().is_signaled());
468   }
469
470   // This test starts without any test extensions installed.
471   EXPECT_FALSE(GetSingleLoadedExtension());
472   message_.clear();
473
474   policy::SchemaRegistry* registry =
475       policy::SchemaRegistryServiceFactory::GetForContext(profile);
476   ASSERT_TRUE(registry);
477   EXPECT_FALSE(registry->schema_map()->GetSchema(policy::PolicyNamespace(
478       policy::POLICY_DOMAIN_EXTENSIONS, kManagedStorageExtensionId)));
479
480   MockSchemaRegistryObserver observer;
481   registry->AddObserver(&observer);
482
483   // Install a managed extension.
484   EXPECT_CALL(observer, OnSchemaRegistryUpdated(true));
485   const Extension* extension =
486       LoadExtension(test_data_dir_.AppendASCII("settings/managed_storage"));
487   ASSERT_TRUE(extension);
488   Mock::VerifyAndClearExpectations(&observer);
489   registry->RemoveObserver(&observer);
490
491   // Verify that its schema has been published, and verify its contents.
492   const policy::Schema* schema =
493       registry->schema_map()->GetSchema(policy::PolicyNamespace(
494           policy::POLICY_DOMAIN_EXTENSIONS, kManagedStorageExtensionId));
495   ASSERT_TRUE(schema);
496
497   ASSERT_TRUE(schema->valid());
498   ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema->type());
499   ASSERT_TRUE(schema->GetKnownProperty("string-policy").valid());
500   EXPECT_EQ(base::Value::TYPE_STRING,
501             schema->GetKnownProperty("string-policy").type());
502   ASSERT_TRUE(schema->GetKnownProperty("int-policy").valid());
503   EXPECT_EQ(base::Value::TYPE_INTEGER,
504             schema->GetKnownProperty("int-policy").type());
505   ASSERT_TRUE(schema->GetKnownProperty("double-policy").valid());
506   EXPECT_EQ(base::Value::TYPE_DOUBLE,
507             schema->GetKnownProperty("double-policy").type());
508   ASSERT_TRUE(schema->GetKnownProperty("boolean-policy").valid());
509   EXPECT_EQ(base::Value::TYPE_BOOLEAN,
510             schema->GetKnownProperty("boolean-policy").type());
511
512   policy::Schema list = schema->GetKnownProperty("list-policy");
513   ASSERT_TRUE(list.valid());
514   ASSERT_EQ(base::Value::TYPE_LIST, list.type());
515   ASSERT_TRUE(list.GetItems().valid());
516   EXPECT_EQ(base::Value::TYPE_STRING, list.GetItems().type());
517
518   policy::Schema dict = schema->GetKnownProperty("dict-policy");
519   ASSERT_TRUE(dict.valid());
520   ASSERT_EQ(base::Value::TYPE_DICTIONARY, dict.type());
521   list = dict.GetKnownProperty("list");
522   ASSERT_TRUE(list.valid());
523   ASSERT_EQ(base::Value::TYPE_LIST, list.type());
524   dict = list.GetItems();
525   ASSERT_TRUE(dict.valid());
526   ASSERT_EQ(base::Value::TYPE_DICTIONARY, dict.type());
527   ASSERT_TRUE(dict.GetProperty("anything").valid());
528   EXPECT_EQ(base::Value::TYPE_INTEGER, dict.GetProperty("anything").type());
529 }
530
531 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest, ManagedStorage) {
532   // Set policies for the test extension.
533   scoped_ptr<base::DictionaryValue> policy = extensions::DictionaryBuilder()
534       .Set("string-policy", "value")
535       .Set("int-policy", -123)
536       .Set("double-policy", 456e7)
537       .SetBoolean("boolean-policy", true)
538       .Set("list-policy", extensions::ListBuilder()
539           .Append("one")
540           .Append("two")
541           .Append("three"))
542       .Set("dict-policy", extensions::DictionaryBuilder()
543           .Set("list", extensions::ListBuilder()
544               .Append(extensions::DictionaryBuilder()
545                   .Set("one", 1)
546                   .Set("two", 2))
547               .Append(extensions::DictionaryBuilder()
548                   .Set("three", 3))))
549       .Build();
550   SetPolicies(*policy);
551   // Now run the extension.
552   ASSERT_TRUE(RunExtensionTest("settings/managed_storage")) << message_;
553 }
554
555 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest,
556                        DISABLED_PRE_ManagedStorageEvents) {
557   ResultCatcher catcher;
558
559   // This test starts without any test extensions installed.
560   EXPECT_FALSE(GetSingleLoadedExtension());
561   message_.clear();
562
563   // Set policies for the test extension.
564   scoped_ptr<base::DictionaryValue> policy = extensions::DictionaryBuilder()
565       .Set("constant-policy", "aaa")
566       .Set("changes-policy", "bbb")
567       .Set("deleted-policy", "ccc")
568       .Build();
569   SetPolicies(*policy);
570
571   ExtensionTestMessageListener ready_listener("ready", false);
572   // Load the extension to install the event listener.
573   const Extension* extension = LoadExtension(
574       test_data_dir_.AppendASCII("settings/managed_storage_events"));
575   ASSERT_TRUE(extension);
576   // Wait until the extension sends the "ready" message.
577   ASSERT_TRUE(ready_listener.WaitUntilSatisfied());
578
579   // Now change the policies and wait until the extension is done.
580   policy = extensions::DictionaryBuilder()
581       .Set("constant-policy", "aaa")
582       .Set("changes-policy", "ddd")
583       .Set("new-policy", "eee")
584       .Build();
585   SetPolicies(*policy);
586   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
587 }
588
589 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest,
590                        DISABLED_ManagedStorageEvents) {
591   // This test runs after PRE_ManagedStorageEvents without having deleted the
592   // profile, so the extension is still around. While the browser restarted the
593   // policy went back to the empty default, and so the extension should receive
594   // the corresponding change events.
595
596   ResultCatcher catcher;
597
598   // Verify that the test extension is still installed.
599   const Extension* extension = GetSingleLoadedExtension();
600   ASSERT_TRUE(extension);
601   EXPECT_EQ(kManagedStorageExtensionId, extension->id());
602
603   // Running the test again skips the onInstalled callback, and just triggers
604   // the onChanged notification.
605   EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
606 }
607
608 #endif  // defined(ENABLE_CONFIGURATION_POLICY)
609
610 IN_PROC_BROWSER_TEST_F(ExtensionSettingsApiTest, ManagedStorageDisabled) {
611   // Disable the 'managed' namespace. This is redundant when
612   // ENABLE_CONFIGURATION_POLICY is not defined.
613   SettingsFrontend* frontend =
614       browser()->profile()->GetExtensionService()->settings_frontend();
615   frontend->DisableStorageForTesting(MANAGED);
616   EXPECT_FALSE(frontend->IsStorageEnabled(MANAGED));
617   // Now run the extension.
618   ASSERT_TRUE(RunExtensionTest("settings/managed_storage_disabled"))
619       << message_;
620 }
621
622 }  // namespace extensions