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