5da13fe9e67d9db002207a4fa6b84a74766640c4
[platform/framework/web/crosswalk.git] / src / chrome / browser / search / hotword_service_unittest.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/metrics/field_trial.h"
6 #include "base/prefs/pref_service.h"
7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/search/hotword_service.h"
9 #include "chrome/browser/search/hotword_service_factory.h"
10 #include "chrome/common/pref_names.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "content/public/test/test_browser_thread_bundle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 class HotwordServiceTest : public testing::Test {
16  protected:
17   HotwordServiceTest() : field_trial_list_(NULL) {}
18   virtual ~HotwordServiceTest() {}
19
20   void SetApplicationLocale(Profile* profile, const std::string& new_locale) {
21 #if defined(OS_CHROMEOS)
22         // On ChromeOS locale is per-profile.
23     profile->GetPrefs()->SetString(prefs::kApplicationLocale, new_locale);
24 #else
25     g_browser_process->SetApplicationLocale(new_locale);
26 #endif
27   }
28
29  private:
30   base::FieldTrialList field_trial_list_;
31   content::TestBrowserThreadBundle thread_bundle_;
32 };
33
34 TEST_F(HotwordServiceTest, ShouldShowOptInPopup) {
35   TestingProfile::Builder profile_builder;
36   TestingProfile::Builder otr_profile_builder;
37   otr_profile_builder.SetIncognito();
38   scoped_ptr<TestingProfile> profile = profile_builder.Build();
39   scoped_ptr<TestingProfile> otr_profile = otr_profile_builder.Build();
40
41   // Popup should not be shown for incognito profiles.
42   EXPECT_TRUE(otr_profile.get() != NULL);
43   EXPECT_FALSE(HotwordServiceFactory::ShouldShowOptInPopup(otr_profile.get()));
44
45   HotwordServiceFactory* hotword_service_factory =
46       HotwordServiceFactory::GetInstance();
47   HotwordService* hotword_service =
48       hotword_service_factory->GetForProfile(profile.get());
49   EXPECT_TRUE(hotword_service != NULL);
50
51   // Popup should not be shown if hotword search has been enabled or turned off.
52   // Test both paths for accessing whether to do the popup. Just in case.
53   profile->GetPrefs()->SetBoolean(prefs::kHotwordSearchEnabled, true);
54   EXPECT_FALSE(HotwordServiceFactory::ShouldShowOptInPopup(profile.get()));
55   EXPECT_FALSE(hotword_service->ShouldShowOptInPopup());
56
57   profile->GetPrefs()->SetBoolean(prefs::kHotwordSearchEnabled, false);
58   EXPECT_FALSE(HotwordServiceFactory::ShouldShowOptInPopup(profile.get()));
59   EXPECT_FALSE(hotword_service->ShouldShowOptInPopup());
60
61   // Rest the enabled pref for the next part of the test.
62   profile->GetPrefs()->ClearPref(prefs::kHotwordSearchEnabled);
63
64   // Popup should be shown until max number of times are reached.
65   for (int i = 0; i < hotword_service->MaxNumberTimesToShowOptInPopup(); i++) {
66     EXPECT_TRUE(HotwordServiceFactory::ShouldShowOptInPopup(profile.get()));
67     EXPECT_TRUE(hotword_service->ShouldShowOptInPopup());
68     hotword_service->ShowOptInPopup();
69   }
70
71   // The opt in popup should not be shown if it has already been shown the
72   // maximum number of times.
73   EXPECT_FALSE(HotwordServiceFactory::ShouldShowOptInPopup(profile.get()));
74   EXPECT_FALSE(hotword_service->ShouldShowOptInPopup());
75
76 }
77
78 TEST_F(HotwordServiceTest, IsHotwordAllowedBadFieldTrial) {
79   TestingProfile::Builder profile_builder;
80   TestingProfile::Builder otr_profile_builder;
81   otr_profile_builder.SetIncognito();
82   scoped_ptr<TestingProfile> profile = profile_builder.Build();
83   scoped_ptr<TestingProfile> otr_profile = otr_profile_builder.Build();
84
85   HotwordServiceFactory* hotword_service_factory =
86       HotwordServiceFactory::GetInstance();
87
88   // Check that the service exists so that a NULL service be ruled out in
89   // following tests.
90   HotwordService* hotword_service =
91       hotword_service_factory->GetForProfile(profile.get());
92   EXPECT_TRUE(hotword_service != NULL);
93
94   // When the field trial is empty or Disabled, it should not be allowed.
95   std::string group = base::FieldTrialList::FindFullName(
96       hotword_internal::kHotwordFieldTrialName);
97   EXPECT_TRUE(group.empty());
98   EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile.get()));
99
100   ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
101      hotword_internal::kHotwordFieldTrialName,
102      hotword_internal::kHotwordFieldTrialDisabledGroupName));
103   group = base::FieldTrialList::FindFullName(
104       hotword_internal::kHotwordFieldTrialName);
105   EXPECT_TRUE(group ==hotword_internal::kHotwordFieldTrialDisabledGroupName);
106   EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile.get()));
107
108   // Set a valid locale with invalid field trial to be sure it is
109   // still false.
110   SetApplicationLocale(static_cast<Profile*>(profile.get()), "en");
111   EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile.get()));
112
113   // Test that incognito returns false as well.
114   EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(otr_profile.get()));
115 }
116
117 TEST_F(HotwordServiceTest, IsHotwordAllowedLocale) {
118   TestingProfile::Builder profile_builder;
119   TestingProfile::Builder otr_profile_builder;
120   otr_profile_builder.SetIncognito();
121   scoped_ptr<TestingProfile> profile = profile_builder.Build();
122   scoped_ptr<TestingProfile> otr_profile = otr_profile_builder.Build();
123
124   HotwordServiceFactory* hotword_service_factory =
125       HotwordServiceFactory::GetInstance();
126
127   // Check that the service exists so that a NULL service be ruled out in
128   // following tests.
129   HotwordService* hotword_service =
130       hotword_service_factory->GetForProfile(profile.get());
131   EXPECT_TRUE(hotword_service != NULL);
132
133   // Set the field trial to a valid one.
134   ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
135       hotword_internal::kHotwordFieldTrialName, "Good"));
136
137   // Set the language to an invalid one.
138   SetApplicationLocale(static_cast<Profile*>(profile.get()), "non-valid");
139   EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(profile.get()));
140
141   // Now with valid locales it should be fine.
142   SetApplicationLocale(static_cast<Profile*>(profile.get()), "en");
143   EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile.get()));
144   SetApplicationLocale(static_cast<Profile*>(profile.get()), "en-US");
145   EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile.get()));
146   SetApplicationLocale(static_cast<Profile*>(profile.get()), "en_us");
147   EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile.get()));
148   SetApplicationLocale(static_cast<Profile*>(profile.get()), "de_DE");
149   EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile.get()));
150   SetApplicationLocale(static_cast<Profile*>(profile.get()), "fr_fr");
151   EXPECT_TRUE(HotwordServiceFactory::IsHotwordAllowed(profile.get()));
152
153   // Test that incognito even with a valid locale and valid field trial
154   // still returns false.
155   SetApplicationLocale(static_cast<Profile*>(otr_profile.get()), "en");
156   EXPECT_FALSE(HotwordServiceFactory::IsHotwordAllowed(otr_profile.get()));
157 }
158
159 TEST_F(HotwordServiceTest, AudioLoggingPrefSetCorrectly) {
160   TestingProfile::Builder profile_builder;
161   scoped_ptr<TestingProfile> profile = profile_builder.Build();
162
163   HotwordServiceFactory* hotword_service_factory =
164       HotwordServiceFactory::GetInstance();
165   HotwordService* hotword_service =
166       hotword_service_factory->GetForProfile(profile.get());
167   EXPECT_TRUE(hotword_service != NULL);
168
169   // If it's a fresh profile, although the default value is true,
170   // it should return false if the preference has never been set.
171   EXPECT_FALSE(hotword_service->IsOptedIntoAudioLogging());
172 }