Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / omnibox / omnibox_edit_unittest.cc
index 828df59..dc71213 100644 (file)
@@ -9,12 +9,11 @@
 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
 #include "chrome/browser/ui/omnibox/omnibox_view.h"
 #include "chrome/browser/ui/toolbar/test_toolbar_model.h"
-#include "chrome/test/base/testing_browser_process.h"
 #include "chrome/test/base/testing_profile.h"
 #include "testing/gtest/include/gtest/gtest.h"
-#include "ui/gfx/font.h"
-#include "ui/gfx/image/image.h"
 
+using base::ASCIIToUTF16;
+using base::UTF8ToUTF16;
 using content::WebContents;
 
 namespace {
@@ -24,66 +23,75 @@ class TestingOmniboxView : public OmniboxView {
   explicit TestingOmniboxView(OmniboxEditController* controller)
       : OmniboxView(NULL, controller, NULL) {}
 
-  virtual void SaveStateToTab(WebContents* tab) OVERRIDE {}
-  virtual void OnTabChanged(const WebContents* web_contents) OVERRIDE {}
-  virtual void Update() OVERRIDE {}
-  virtual void OpenMatch(const AutocompleteMatch& match,
-                         WindowOpenDisposition disposition,
-                         const GURL& alternate_nav_url,
-                         size_t selected_line) OVERRIDE {}
-  virtual string16 GetText() const OVERRIDE { return string16(); }
-  virtual void SetUserText(const string16& text,
-                           const string16& display_text,
-                           bool update_popup) OVERRIDE {}
-  virtual void SetWindowTextAndCaretPos(const string16& text,
-                                        size_t caret_pos,
-                                        bool update_popup,
-                                        bool notify_text_changed) OVERRIDE {}
-  virtual void SetForcedQuery() OVERRIDE {}
-  virtual bool IsSelectAll() const OVERRIDE { return false; }
-  virtual bool DeleteAtEndPressed() OVERRIDE { return false; }
-  virtual void GetSelectionBounds(size_t* start, size_t* end) const OVERRIDE {}
-  virtual void SelectAll(bool reversed) OVERRIDE {}
-  virtual void RevertAll() OVERRIDE {}
-  virtual void UpdatePopup() OVERRIDE {}
-  virtual void SetFocus() OVERRIDE {}
-  virtual void ApplyCaretVisibility() OVERRIDE {}
-  virtual void OnTemporaryTextMaybeChanged(
-      const string16& display_text,
-      bool save_original_selection,
-      bool notify_text_changed) OVERRIDE {}
-  virtual bool OnInlineAutocompleteTextMaybeChanged(
-      const string16& display_text, size_t user_text_length) OVERRIDE {
-    return false;
+  // OmniboxView:
+  void SaveStateToTab(WebContents* tab) override {}
+  void OnTabChanged(const WebContents* web_contents) override {}
+  void Update() override {}
+  void UpdatePlaceholderText() override {}
+  void OpenMatch(const AutocompleteMatch& match,
+                 WindowOpenDisposition disposition,
+                 const GURL& alternate_nav_url,
+                 const base::string16& pasted_text,
+                 size_t selected_line) override {}
+  base::string16 GetText() const override { return text_; }
+  void SetUserText(const base::string16& text,
+                   const base::string16& display_text,
+                   bool update_popup) override {
+    text_ = display_text;
   }
-  virtual void OnRevertTemporaryText() OVERRIDE {}
-  virtual void OnBeforePossibleChange() OVERRIDE {}
-  virtual bool OnAfterPossibleChange() OVERRIDE { return false; }
-  virtual gfx::NativeView GetNativeView() const OVERRIDE { return NULL; }
-  virtual gfx::NativeView GetRelativeWindowForPopup() const OVERRIDE {
-    return NULL;
+  void SetWindowTextAndCaretPos(const base::string16& text,
+                                size_t caret_pos,
+                                bool update_popup,
+                                bool notify_text_changed) override {
+    text_ = text;
   }
-  virtual void SetGrayTextAutocompletion(const string16& input) OVERRIDE {}
-  virtual string16 GetGrayTextAutocompletion() const OVERRIDE {
-    return string16();
+  void SetForcedQuery() override {}
+  bool IsSelectAll() const override { return false; }
+  bool DeleteAtEndPressed() override { return false; }
+  void GetSelectionBounds(size_t* start, size_t* end) const override {}
+  void SelectAll(bool reversed) override {}
+  void RevertAll() override {}
+  void UpdatePopup() override {}
+  void SetFocus() override {}
+  void ApplyCaretVisibility() override {}
+  void OnTemporaryTextMaybeChanged(const base::string16& display_text,
+                                   bool save_original_selection,
+                                   bool notify_text_changed) override {
+    text_ = display_text;
   }
-  virtual int TextWidth() const OVERRIDE { return 0; }
-  virtual bool IsImeComposing() const OVERRIDE { return false; }
-
-#if defined(TOOLKIT_VIEWS)
-  virtual int GetMaxEditWidth(int entry_width) const OVERRIDE {
-    return entry_width;
+  bool OnInlineAutocompleteTextMaybeChanged(const base::string16& display_text,
+                                            size_t user_text_length) override {
+    const bool text_changed = text_ != display_text;
+    text_ = display_text;
+    inline_autocomplete_text_ = display_text.substr(user_text_length);
+    return text_changed;
+  }
+  void OnInlineAutocompleteTextCleared() override {
+    inline_autocomplete_text_.clear();
   }
-  virtual views::View* AddToView(views::View* parent) OVERRIDE { return NULL; }
-  virtual int OnPerformDrop(const ui::DropTargetEvent& event) OVERRIDE {
-    return 0;
+  void OnRevertTemporaryText() override {}
+  void OnBeforePossibleChange() override {}
+  bool OnAfterPossibleChange() override { return false; }
+  gfx::NativeView GetNativeView() const override { return NULL; }
+  gfx::NativeView GetRelativeWindowForPopup() const override { return NULL; }
+  void SetGrayTextAutocompletion(const base::string16& input) override {}
+  base::string16 GetGrayTextAutocompletion() const override {
+    return base::string16();
   }
-#endif
+  int GetTextWidth() const override { return 0; }
+  int GetWidth() const override { return 0; }
+  bool IsImeComposing() const override { return false; }
+  int GetOmniboxTextLength() const override { return 0; }
+  void EmphasizeURLComponents() override {}
 
-  virtual int GetOmniboxTextLength() const OVERRIDE { return 0; }
-  virtual void EmphasizeURLComponents() OVERRIDE { }
+  const base::string16& inline_autocomplete_text() const {
+    return inline_autocomplete_text_;
+  }
 
  private:
+  base::string16 text_;
+  base::string16 inline_autocomplete_text_;
+
   DISALLOW_COPY_AND_ASSIGN(TestingOmniboxView);
 };
 
@@ -93,13 +101,19 @@ class TestingOmniboxEditController : public OmniboxEditController {
       : OmniboxEditController(NULL),
         toolbar_model_(toolbar_model) {
   }
-  virtual void Update(const content::WebContents* contents) OVERRIDE {}
-  virtual void OnChanged() OVERRIDE {}
-  virtual void OnSetFocus() OVERRIDE {}
-  virtual InstantController* GetInstant() OVERRIDE { return NULL; }
-  virtual WebContents* GetWebContents() OVERRIDE { return NULL; }
-  virtual ToolbarModel* GetToolbarModel() OVERRIDE { return toolbar_model_; }
-  virtual const ToolbarModel* GetToolbarModel() const OVERRIDE {
+
+ protected:
+  // OmniboxEditController:
+  void Update(const content::WebContents* contents) override {}
+  void OnChanged() override {}
+  void OnSetFocus() override {}
+  void ShowURL() override {}
+  void HideURL() override {}
+  void EndOriginChipAnimations(bool cancel_fade) override {}
+  InstantController* GetInstant() override { return NULL; }
+  WebContents* GetWebContents() override { return NULL; }
+  ToolbarModel* GetToolbarModel() override { return toolbar_model_; }
+  const ToolbarModel* GetToolbarModel() const override {
     return toolbar_model_;
   }
 
@@ -113,10 +127,10 @@ class TestingOmniboxEditController : public OmniboxEditController {
 
 class AutocompleteEditTest : public ::testing::Test {
  public:
-   TestToolbarModel* toolbar_model() { return &toolbar_model_; }
+  TestToolbarModel* toolbar_model() { return &toolbar_model_; }
 
 private:
-   TestToolbarModel toolbar_model_;
+ private:
+  TestToolbarModel toolbar_model_;
 };
 
 // Tests various permutations of AutocompleteModel::AdjustTextForCopy.
@@ -179,20 +193,20 @@ TEST_F(AutocompleteEditTest, AdjustTextForCopy) {
   // NOTE: The TemplateURLService must be created before the
   // AutocompleteClassifier so that the SearchProvider gets a non-NULL
   // TemplateURLService at construction time.
-  TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
+  TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
       &profile, &TemplateURLServiceFactory::BuildInstanceFor);
-  AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse(
+  AutocompleteClassifierFactory::GetInstance()->SetTestingFactory(
       &profile, &AutocompleteClassifierFactory::BuildInstanceFor);
   OmniboxEditModel model(&view, &controller, &profile);
 
-  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input); ++i) {
+  for (size_t i = 0; i < arraysize(input); ++i) {
     toolbar_model()->set_text(ASCIIToUTF16(input[i].perm_text));
     model.UpdatePermanentText();
 
-    toolbar_model()->set_replace_search_url_with_search_terms(
+    toolbar_model()->set_perform_search_term_replacement(
         input[i].extracted_search_terms);
 
-    string16 result = ASCIIToUTF16(input[i].input);
+    base::string16 result = ASCIIToUTF16(input[i].input);
     GURL url;
     bool write_url;
     model.AdjustTextForCopy(input[i].sel_start, input[i].is_all_selected,
@@ -203,3 +217,44 @@ TEST_F(AutocompleteEditTest, AdjustTextForCopy) {
       EXPECT_EQ(input[i].expected_url, url.spec()) << " @" << i;
   }
 }
+
+TEST_F(AutocompleteEditTest, InlineAutocompleteText) {
+  TestingOmniboxEditController controller(toolbar_model());
+  TestingOmniboxView view(&controller);
+  TestingProfile profile;
+  // NOTE: The TemplateURLService must be created before the
+  // AutocompleteClassifier so that the SearchProvider gets a non-NULL
+  // TemplateURLService at construction time.
+  TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
+      &profile, &TemplateURLServiceFactory::BuildInstanceFor);
+  AutocompleteClassifierFactory::GetInstance()->SetTestingFactory(
+      &profile, &AutocompleteClassifierFactory::BuildInstanceFor);
+  OmniboxEditModel model(&view, &controller, &profile);
+
+  // Test if the model updates the inline autocomplete text in the view.
+  EXPECT_EQ(base::string16(), view.inline_autocomplete_text());
+  model.SetUserText(UTF8ToUTF16("he"));
+  model.OnPopupDataChanged(UTF8ToUTF16("llo"), NULL, base::string16(), false);
+  EXPECT_EQ(UTF8ToUTF16("hello"), view.GetText());
+  EXPECT_EQ(UTF8ToUTF16("llo"), view.inline_autocomplete_text());
+
+  model.OnAfterPossibleChange(UTF8ToUTF16("he"), UTF8ToUTF16("hel"), 3, 3,
+                              false, true, false, true);
+  EXPECT_EQ(base::string16(), view.inline_autocomplete_text());
+  model.OnPopupDataChanged(UTF8ToUTF16("lo"), NULL, base::string16(), false);
+  EXPECT_EQ(UTF8ToUTF16("hello"), view.GetText());
+  EXPECT_EQ(UTF8ToUTF16("lo"), view.inline_autocomplete_text());
+
+  model.Revert();
+  EXPECT_EQ(base::string16(), view.GetText());
+  EXPECT_EQ(base::string16(), view.inline_autocomplete_text());
+
+  model.SetUserText(UTF8ToUTF16("he"));
+  model.OnPopupDataChanged(UTF8ToUTF16("llo"), NULL, base::string16(), false);
+  EXPECT_EQ(UTF8ToUTF16("hello"), view.GetText());
+  EXPECT_EQ(UTF8ToUTF16("llo"), view.inline_autocomplete_text());
+
+  model.AcceptTemporaryTextAsUserText();
+  EXPECT_EQ(UTF8ToUTF16("hello"), view.GetText());
+  EXPECT_EQ(base::string16(), view.inline_autocomplete_text());
+}