| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/logging.h" | 5 #include "base/logging.h" |
| 6 #include "base/metrics/histogram.h" | 6 #include "base/metrics/histogram.h" |
| 7 #include "base/metrics/histogram_samples.h" | 7 #include "base/metrics/histogram_samples.h" |
| 8 #include "base/metrics/statistics_recorder.h" | 8 #include "base/metrics/statistics_recorder.h" |
| 9 #include "base/test/histogram_tester.h" |
| 9 #include "chrome/browser/chromeos/input_method/input_method_configuration.h" | 10 #include "chrome/browser/chromeos/input_method/input_method_configuration.h" |
| 10 #include "chrome/browser/chromeos/input_method/input_method_engine.h" | 11 #include "chrome/browser/chromeos/input_method/input_method_engine.h" |
| 11 #include "chrome/browser/chromeos/input_method/input_method_engine_interface.h" | 12 #include "chrome/browser/chromeos/input_method/input_method_engine_interface.h" |
| 12 #include "chrome/browser/chromeos/input_method/mock_input_method_manager.h" | 13 #include "chrome/browser/chromeos/input_method/mock_input_method_manager.h" |
| 13 #include "chromeos/ime/extension_ime_util.h" | 14 #include "chromeos/ime/extension_ime_util.h" |
| 14 #include "chromeos/ime/mock_component_extension_ime_manager_delegate.h" | 15 #include "chromeos/ime/mock_component_extension_ime_manager_delegate.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "ui/base/ime/chromeos/mock_ime_input_context_handler.h" | 17 #include "ui/base/ime/chromeos/mock_ime_input_context_handler.h" |
| 17 | 18 |
| 18 namespace chromeos { | 19 namespace chromeos { |
| 19 | 20 |
| 20 namespace input_method { | 21 namespace input_method { |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 const char kTestExtensionId[] = "mppnpdlheglhdfmldimlhpnegondlapf"; | 24 const char kTestExtensionId[] = "mppnpdlheglhdfmldimlhpnegondlapf"; |
| 24 const char kTestExtensionId2[] = "dmpipdbjkoajgdeppkffbjhngfckdloi"; | 25 const char kTestExtensionId2[] = "dmpipdbjkoajgdeppkffbjhngfckdloi"; |
| 25 const char kTestImeComponentId[] = "test_engine_id"; | 26 const char kTestImeComponentId[] = "test_engine_id"; |
| 26 | 27 |
| 27 const char* kHistogramNames[] = { | |
| 28 "InputMethod.Enable.test_engine_id", "InputMethod.Commit.test_engine_id", | |
| 29 "InputMethod.CommitCharacter.test_engine_id", | |
| 30 }; | |
| 31 | |
| 32 scoped_ptr<base::HistogramSamples> GetHistogramSamples( | |
| 33 const char* histogram_name) { | |
| 34 base::HistogramBase* histogram = | |
| 35 base::StatisticsRecorder::FindHistogram(histogram_name); | |
| 36 EXPECT_NE(static_cast<base::HistogramBase*>(NULL), histogram); | |
| 37 return histogram->SnapshotSamples().Pass(); | |
| 38 } | |
| 39 | |
| 40 enum CallsBitmap { | 28 enum CallsBitmap { |
| 41 NONE = 0U, | 29 NONE = 0U, |
| 42 ACTIVATE = 1U, | 30 ACTIVATE = 1U, |
| 43 DEACTIVATED = 2U, | 31 DEACTIVATED = 2U, |
| 44 ONFOCUS = 4U, | 32 ONFOCUS = 4U, |
| 45 ONBLUR = 8U | 33 ONBLUR = 8U |
| 46 }; | 34 }; |
| 47 | 35 |
| 48 void InitInputMethod() { | 36 void InitInputMethod() { |
| 49 ComponentExtensionIMEManager* comp_ime_manager = | 37 ComponentExtensionIMEManager* comp_ime_manager = |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 class InputMethodEngineTest : public testing::Test { | 113 class InputMethodEngineTest : public testing::Test { |
| 126 public: | 114 public: |
| 127 InputMethodEngineTest() : observer_(NULL), input_view_("inputview.html") { | 115 InputMethodEngineTest() : observer_(NULL), input_view_("inputview.html") { |
| 128 languages_.push_back("en-US"); | 116 languages_.push_back("en-US"); |
| 129 layouts_.push_back("us"); | 117 layouts_.push_back("us"); |
| 130 InitInputMethod(); | 118 InitInputMethod(); |
| 131 IMEBridge::Initialize(); | 119 IMEBridge::Initialize(); |
| 132 mock_ime_input_context_handler_.reset(new MockIMEInputContextHandler()); | 120 mock_ime_input_context_handler_.reset(new MockIMEInputContextHandler()); |
| 133 IMEBridge::Get()->SetInputContextHandler( | 121 IMEBridge::Get()->SetInputContextHandler( |
| 134 mock_ime_input_context_handler_.get()); | 122 mock_ime_input_context_handler_.get()); |
| 135 | |
| 136 base::StatisticsRecorder::Initialize(); | |
| 137 | |
| 138 for (size_t i = 0; i < arraysize(kHistogramNames); i++) { | |
| 139 base::Histogram::FactoryGet( | |
| 140 kHistogramNames[i], 0, 1000000, 50, base::HistogramBase::kNoFlags) | |
| 141 ->Add(0); | |
| 142 initial_histogram_samples_[i] = | |
| 143 GetHistogramSamples(kHistogramNames[i]).Pass(); | |
| 144 initial_histogram_samples_map_[kHistogramNames[i]] = | |
| 145 initial_histogram_samples_[i].get(); | |
| 146 } | |
| 147 } | 123 } |
| 148 virtual ~InputMethodEngineTest() { | 124 virtual ~InputMethodEngineTest() { |
| 149 IMEBridge::Get()->SetInputContextHandler(NULL); | 125 IMEBridge::Get()->SetInputContextHandler(NULL); |
| 150 engine_.reset(); | 126 engine_.reset(); |
| 151 Shutdown(); | 127 Shutdown(); |
| 152 } | 128 } |
| 153 | 129 |
| 154 protected: | 130 protected: |
| 155 scoped_ptr<base::HistogramSamples> GetHistogramSamplesDelta( | |
| 156 const char* histogram_name) { | |
| 157 scoped_ptr<base::HistogramSamples> delta_samples( | |
| 158 GetHistogramSamples(histogram_name)); | |
| 159 delta_samples->Subtract(*initial_histogram_samples_map_[histogram_name]); | |
| 160 | |
| 161 return delta_samples.Pass(); | |
| 162 } | |
| 163 | |
| 164 void ExpectNewSample(const char* histogram_name, | |
| 165 base::HistogramBase::Sample sample, | |
| 166 int total_count, | |
| 167 int sample_count) { | |
| 168 scoped_ptr<base::HistogramSamples> delta_samples( | |
| 169 GetHistogramSamplesDelta(histogram_name)); | |
| 170 EXPECT_EQ(total_count, delta_samples->TotalCount()); | |
| 171 EXPECT_EQ(sample_count, delta_samples->GetCount(sample)); | |
| 172 } | |
| 173 | |
| 174 void CreateEngine(bool whitelisted) { | 131 void CreateEngine(bool whitelisted) { |
| 175 engine_.reset(new InputMethodEngine()); | 132 engine_.reset(new InputMethodEngine()); |
| 176 observer_ = new TestObserver(); | 133 observer_ = new TestObserver(); |
| 177 scoped_ptr<InputMethodEngineInterface::Observer> observer_ptr(observer_); | 134 scoped_ptr<InputMethodEngineInterface::Observer> observer_ptr(observer_); |
| 178 engine_->Initialize(observer_ptr.Pass(), | 135 engine_->Initialize(observer_ptr.Pass(), |
| 179 whitelisted ? kTestExtensionId : kTestExtensionId2); | 136 whitelisted ? kTestExtensionId : kTestExtensionId2); |
| 180 } | 137 } |
| 181 | 138 |
| 182 void FocusIn(ui::TextInputType input_type) { | 139 void FocusIn(ui::TextInputType input_type) { |
| 183 IMEEngineHandlerInterface::InputContext input_context( | 140 IMEEngineHandlerInterface::InputContext input_context( |
| 184 input_type, ui::TEXT_INPUT_MODE_DEFAULT); | 141 input_type, ui::TEXT_INPUT_MODE_DEFAULT); |
| 185 engine_->FocusIn(input_context); | 142 engine_->FocusIn(input_context); |
| 186 IMEBridge::Get()->SetCurrentTextInputType(input_type); | 143 IMEBridge::Get()->SetCurrentTextInputType(input_type); |
| 187 } | 144 } |
| 188 | 145 |
| 189 scoped_ptr<InputMethodEngine> engine_; | 146 scoped_ptr<InputMethodEngine> engine_; |
| 190 | 147 |
| 191 TestObserver* observer_; | 148 TestObserver* observer_; |
| 192 std::vector<std::string> languages_; | 149 std::vector<std::string> languages_; |
| 193 std::vector<std::string> layouts_; | 150 std::vector<std::string> layouts_; |
| 194 GURL options_page_; | 151 GURL options_page_; |
| 195 GURL input_view_; | 152 GURL input_view_; |
| 196 | 153 |
| 197 scoped_ptr<base::HistogramSamples> | |
| 198 initial_histogram_samples_[arraysize(kHistogramNames)]; | |
| 199 std::map<std::string, base::HistogramSamples*> initial_histogram_samples_map_; | |
| 200 | |
| 201 scoped_ptr<MockIMEInputContextHandler> mock_ime_input_context_handler_; | 154 scoped_ptr<MockIMEInputContextHandler> mock_ime_input_context_handler_; |
| 202 | 155 |
| 203 private: | 156 private: |
| 204 DISALLOW_COPY_AND_ASSIGN(InputMethodEngineTest); | 157 DISALLOW_COPY_AND_ASSIGN(InputMethodEngineTest); |
| 205 }; | 158 }; |
| 206 | 159 |
| 207 } // namespace | 160 } // namespace |
| 208 | 161 |
| 209 TEST_F(InputMethodEngineTest, TestSwitching) { | 162 TEST_F(InputMethodEngineTest, TestSwitching) { |
| 210 CreateEngine(false); | 163 CreateEngine(false); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 FocusIn(ui::TEXT_INPUT_TYPE_PASSWORD); | 226 FocusIn(ui::TEXT_INPUT_TYPE_PASSWORD); |
| 274 EXPECT_EQ(ONFOCUS, observer_->GetCallsBitmapAndReset()); | 227 EXPECT_EQ(ONFOCUS, observer_->GetCallsBitmapAndReset()); |
| 275 engine_->Disable(); | 228 engine_->Disable(); |
| 276 EXPECT_EQ(DEACTIVATED, observer_->GetCallsBitmapAndReset()); | 229 EXPECT_EQ(DEACTIVATED, observer_->GetCallsBitmapAndReset()); |
| 277 } | 230 } |
| 278 | 231 |
| 279 TEST_F(InputMethodEngineTest, TestHistograms) { | 232 TEST_F(InputMethodEngineTest, TestHistograms) { |
| 280 CreateEngine(true); | 233 CreateEngine(true); |
| 281 FocusIn(ui::TEXT_INPUT_TYPE_TEXT); | 234 FocusIn(ui::TEXT_INPUT_TYPE_TEXT); |
| 282 engine_->Enable(kTestImeComponentId); | 235 engine_->Enable(kTestImeComponentId); |
| 236 std::vector<InputMethodEngineInterface::SegmentInfo> segments; |
| 237 engine_->SetComposition( |
| 238 engine_->GetCotextIdForTesting(), "test", 0, 0, 0, segments, NULL); |
| 283 std::string error; | 239 std::string error; |
| 284 ExpectNewSample("InputMethod.Enable.test_engine_id", 1, 1, 1); | 240 base::HistogramTester histograms; |
| 285 engine_->CommitText(1, "input", &error); | 241 engine_->CommitText(1, "input", &error); |
| 286 engine_->CommitText(1, "入力", &error); | 242 engine_->CommitText(1, |
| 287 engine_->CommitText(1, "input入力", &error); | 243 "\xE5\x85\xA5\xE5\x8A\x9B", // 2 UTF-8 characters |
| 288 ExpectNewSample("InputMethod.Commit.test_engine_id", 1, 3, 3); | 244 &error); |
| 289 ExpectNewSample("InputMethod.CommitCharacter.test_engine_id", 5, 3, 1); | 245 engine_->CommitText(1, "input\xE5\x85\xA5\xE5\x8A\x9B", &error); |
| 290 ExpectNewSample("InputMethod.CommitCharacter.test_engine_id", 2, 3, 1); | 246 histograms.ExpectTotalCount("InputMethod.CommitLength", 3); |
| 291 ExpectNewSample("InputMethod.CommitCharacter.test_engine_id", 7, 3, 1); | 247 histograms.ExpectUniqueSample("InputMethod.CommitLength", 5, 1); |
| 248 histograms.ExpectUniqueSample("InputMethod.CommitLength", 2, 1); |
| 249 histograms.ExpectUniqueSample("InputMethod.CommitLength", 7, 1); |
| 292 } | 250 } |
| 293 | 251 |
| 294 } // namespace input_method | 252 } // namespace input_method |
| 295 } // namespace chromeos | 253 } // namespace chromeos |
| OLD | NEW |