Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / preferences_private / preferences_private_apitest.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/basictypes.h"
6 #include "base/bind.h"
7 #include "base/bind_helpers.h"
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/path_service.h"
13 #include "base/values.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/extensions/api/preferences_private/preferences_private_api.h"
16 #include "chrome/browser/extensions/extension_apitest.h"
17 #include "chrome/browser/extensions/extension_function_test_utils.h"
18 #include "chrome/browser/extensions/extension_test_message_listener.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/profiles/profile_manager.h"
21 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
22 #include "chrome/browser/sync/profile_sync_service.h"
23 #include "chrome/browser/sync/profile_sync_service_factory.h"
24 #include "chrome/browser/ui/browser.h"
25 #include "chrome/common/chrome_constants.h"
26 #include "chrome/common/chrome_paths.h"
27 #include "chrome/test/base/testing_profile.h"
28 #include "components/bookmarks/core/common/bookmark_constants.h"
29 #include "components/sync_driver/sync_prefs.h"
30 #include "content/public/browser/browser_context.h"
31
32 using extensions::PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction;
33
34 namespace {
35
36 class FakeProfileSyncService : public ProfileSyncService {
37  public:
38   explicit FakeProfileSyncService(Profile* profile)
39       : ProfileSyncService(
40             NULL,
41             profile,
42             NULL,
43             ProfileOAuth2TokenServiceFactory::GetForProfile(profile),
44             browser_sync::MANUAL_START),
45         sync_initialized_(true),
46         initialized_state_violation_(false) {}
47
48   virtual ~FakeProfileSyncService() {}
49
50   static KeyedService* BuildFakeProfileSyncService(
51       content::BrowserContext* context) {
52     return new FakeProfileSyncService(static_cast<Profile*>(context));
53   }
54
55   void set_sync_initialized(bool sync_initialized) {
56     sync_initialized_ = sync_initialized;
57   }
58
59   bool initialized_state_violation() { return initialized_state_violation_; }
60
61   // ProfileSyncService:
62   virtual bool sync_initialized() const OVERRIDE {
63     return sync_initialized_;
64   }
65
66   virtual void AddObserver(
67       ProfileSyncServiceBase::Observer* observer) OVERRIDE {
68     if (sync_initialized_)
69       initialized_state_violation_ = true;
70     // Set sync initialized state to true so the function will run after
71     // OnStateChanged is called.
72     sync_initialized_ = true;
73     base::MessageLoop::current()->PostTask(
74         FROM_HERE,
75         base::Bind(&ProfileSyncServiceBase::Observer::OnStateChanged,
76                    base::Unretained(observer)));
77   }
78
79   virtual syncer::ModelTypeSet GetEncryptedDataTypes() const OVERRIDE {
80     if (!sync_initialized_)
81       initialized_state_violation_ = true;
82     syncer::ModelTypeSet type_set;
83     type_set.Put(syncer::AUTOFILL);
84     return type_set;
85   }
86
87   virtual syncer::ModelTypeSet GetPreferredDataTypes() const OVERRIDE {
88     if (!sync_initialized_)
89       initialized_state_violation_ = true;
90     syncer::ModelTypeSet preferred_types =
91         syncer::UserSelectableTypes();
92     preferred_types.Remove(syncer::TYPED_URLS);
93     return preferred_types;
94   }
95
96  private:
97   bool sync_initialized_;
98   // Set to true if a function is called when sync_initialized is in an
99   // unexpected state.
100   mutable bool initialized_state_violation_;
101
102   DISALLOW_COPY_AND_ASSIGN(FakeProfileSyncService);
103 };
104
105 class PreferencesPrivateApiTest : public ExtensionApiTest {
106  public:
107   PreferencesPrivateApiTest() : browser_(NULL), service_(NULL) {}
108   virtual ~PreferencesPrivateApiTest() {}
109
110   virtual void SetUpOnMainThread() OVERRIDE {
111     ExtensionApiTest::SetUpOnMainThread();
112
113     base::FilePath path;
114     PathService::Get(chrome::DIR_USER_DATA, &path);
115     path = path.AppendASCII("test_profile");
116     if (!base::PathExists(path))
117       CHECK(base::CreateDirectory(path));
118
119     Profile* profile =
120         Profile::CreateProfile(path, NULL, Profile::CREATE_MODE_SYNCHRONOUS);
121     sync_driver::SyncPrefs sync_prefs(profile->GetPrefs());
122     sync_prefs.SetKeepEverythingSynced(false);
123
124     ProfileManager* profile_manager = g_browser_process->profile_manager();
125     profile_manager->RegisterTestingProfile(profile, true, false);
126
127     service_ = static_cast<FakeProfileSyncService*>(
128         ProfileSyncServiceFactory::GetInstance()->SetTestingFactoryAndUse(
129         profile, &FakeProfileSyncService::BuildFakeProfileSyncService));
130
131     browser_ = new Browser(Browser::CreateParams(
132         profile, chrome::HOST_DESKTOP_TYPE_NATIVE));
133   }
134
135   // Calls GetSyncCategoriesWithoutPassphraseFunction and verifies that the
136   // results returned are the expected ones.
137   void TestGetSyncCategoriesWithoutPassphraseFunction();
138
139  protected:
140   Browser* browser_;
141   FakeProfileSyncService* service_;
142
143  private:
144   DISALLOW_COPY_AND_ASSIGN(PreferencesPrivateApiTest);
145 };
146
147 void
148 PreferencesPrivateApiTest::TestGetSyncCategoriesWithoutPassphraseFunction() {
149   scoped_refptr<PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction>
150       function(
151           new PreferencesPrivateGetSyncCategoriesWithoutPassphraseFunction);
152   ASSERT_TRUE(extension_function_test_utils::RunFunction(
153       function,
154       "[]",
155       browser_,
156       extension_function_test_utils::NONE));
157   EXPECT_FALSE(service_->initialized_state_violation());
158
159   const base::ListValue* result = function->GetResultList();
160   EXPECT_EQ(1u, result->GetSize());
161
162   const base::ListValue* categories = NULL;
163   ASSERT_TRUE(result->GetList(0, &categories));
164   EXPECT_NE(categories->end(),
165             categories->Find(base::StringValue(bookmarks::kBookmarksFileName)));
166   EXPECT_NE(categories->end(),
167             categories->Find(base::StringValue(chrome::kPreferencesFilename)));
168   EXPECT_EQ(categories->end(),
169            categories->Find(base::StringValue("Autofill"))) <<
170                "Encrypted categories should not be present";
171   EXPECT_EQ(categories->end(),
172            categories->Find(base::StringValue("Typed URLs"))) <<
173                "Unsynced categories should not be present";
174 }
175
176 IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest,
177                        GetSyncCategoriesWithoutPassphrase) {
178   TestGetSyncCategoriesWithoutPassphraseFunction();
179 }
180
181 // Verifies that we wait for the sync service to be ready before checking
182 // encryption status.
183 IN_PROC_BROWSER_TEST_F(PreferencesPrivateApiTest,
184                        GetSyncCategoriesWithoutPassphraseAsynchronous) {
185   service_->set_sync_initialized(false);
186   TestGetSyncCategoriesWithoutPassphraseFunction();
187 }
188
189 }  // namespace