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