[Mac] Log SuggestedTextTouchBarController Text Suggestion Selections

A touch bar action, TEXT_SUGGESTION, was added to the TouchBar UMA
histogram to record the number of times a user selects a text
suggestion from the touch bar.

When a user selects a text suggestion from the touch bar, the
SuggestedTextTouchBarController will now log the selection using the
new TEXT_SUGGESTION histogram category.

A unit test was added to test that the selection of a text suggestion
is logged as expected.

Change-Id: I156f6179214a7f9213b22b68b4edd3f1292b77db
Reviewed-on: https://chromium-review.googlesource.com/1141212
Reviewed-by: Avi Drissman <avi@chromium.org>
Reviewed-by: Sarah Chan <spqchan@chromium.org>
Commit-Queue: Tessa Nijssen <tnijssen@google.com>
Cr-Commit-Position: refs/heads/master@{#576576}
diff --git a/chrome/browser/ui/cocoa/touchbar/suggested_text_touch_bar_controller.mm b/chrome/browser/ui/cocoa/touchbar/suggested_text_touch_bar_controller.mm
index 57c0d93..55c7fa2 100644
--- a/chrome/browser/ui/cocoa/touchbar/suggested_text_touch_bar_controller.mm
+++ b/chrome/browser/ui/cocoa/touchbar/suggested_text_touch_bar_controller.mm
@@ -129,8 +129,12 @@
   if (index == NSNotFound)
     return;
 
-  NSTextCheckingResult* selectedResult = anItem.candidates[index];
-  [self replaceEditingWordWithSuggestion:[selectedResult replacementString]];
+  if (anItem) {
+    NSTextCheckingResult* selectedResult = anItem.candidates[index];
+    [self replaceEditingWordWithSuggestion:[selectedResult replacementString]];
+  }
+
+  ui::LogTouchBarUMA(ui::TouchBarAction::TEXT_SUGGESTION);
 }
 
 - (void)webContentsTextSelectionChanged:(const base::string16&)text
diff --git a/chrome/browser/ui/cocoa/touchbar/suggested_text_touch_bar_controller_unittest.mm b/chrome/browser/ui/cocoa/touchbar/suggested_text_touch_bar_controller_unittest.mm
index 0dcf182..0a8001b 100644
--- a/chrome/browser/ui/cocoa/touchbar/suggested_text_touch_bar_controller_unittest.mm
+++ b/chrome/browser/ui/cocoa/touchbar/suggested_text_touch_bar_controller_unittest.mm
@@ -5,9 +5,11 @@
 #import <Cocoa/Cocoa.h>
 
 #include "base/strings/utf_string_conversions.h"
+#include "base/test/metrics/histogram_tester.h"
 #import "chrome/browser/ui/cocoa/test/cocoa_test_helper.h"
 #import "chrome/browser/ui/cocoa/touchbar/suggested_text_touch_bar_controller.h"
 #include "testing/gtest/include/gtest/gtest.h"
+#import "ui/base/cocoa/touch_bar_util.h"
 #include "ui/gfx/range/range.h"
 
 const base::string16 kEmptyText(base::ASCIIToUTF16(""));
@@ -29,6 +31,8 @@
     return gfx::Range(range);
   }
 
+  SuggestedTextTouchBarController* controller() { return controller_; }
+
  private:
   base::scoped_nsobject<SuggestedTextTouchBarController> controller_;
 };
@@ -97,4 +101,13 @@
   EXPECT_EQ(gfx::Range(6, 11), GetEditingWordRange(kMultipleWords, 9));
   EXPECT_EQ(gfx::Range(6, 11), GetEditingWordRange(kMultipleWords, 10));
   EXPECT_EQ(gfx::Range(6, 11), GetEditingWordRange(kMultipleWords, 11));
+}
+
+TEST_F(SuggestedTextTouchBarControllerUnitTest, TouchBarMetricTest) {
+  if (@available(macOS 10.12.2, *)) {
+    base::HistogramTester histogram_tester;
+    [controller() candidateListTouchBarItem:nil endSelectingCandidateAtIndex:1];
+    histogram_tester.ExpectBucketCount("TouchBar.Default.Metrics",
+                                       ui::TouchBarAction::TEXT_SUGGESTION, 1);
+  }
 }
\ No newline at end of file
diff --git a/ui/base/cocoa/touch_bar_util.h b/ui/base/cocoa/touch_bar_util.h
index c97f726..f55afc2 100644
--- a/ui/base/cocoa/touch_bar_util.h
+++ b/ui/base/cocoa/touch_bar_util.h
@@ -24,6 +24,7 @@
   STAR,
   NEW_TAB,
   CREDIT_CARD_AUTOFILL,
+  TEXT_SUGGESTION,
   TOUCH_BAR_ACTION_COUNT
 };