OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stdio.h> | 5 #include <stdio.h> |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/scoped_observer.h" | 8 #include "base/scoped_observer.h" |
9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 1848 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1859 browser()->toolbar_model()->set_url_replacement_enabled(false); | 1859 browser()->toolbar_model()->set_url_replacement_enabled(false); |
1860 test_toolbar_model->set_text(url_b); | 1860 test_toolbar_model->set_text(url_b); |
1861 omnibox_view->Update(); | 1861 omnibox_view->Update(); |
1862 EXPECT_EQ(url_a, omnibox_view->GetText()); | 1862 EXPECT_EQ(url_a, omnibox_view->GetText()); |
1863 omnibox_view->RevertAll(); | 1863 omnibox_view->RevertAll(); |
1864 EXPECT_EQ(url_b, omnibox_view->GetText()); | 1864 EXPECT_EQ(url_b, omnibox_view->GetText()); |
1865 test_toolbar_model->set_text(url_c); | 1865 test_toolbar_model->set_text(url_c); |
1866 omnibox_view->Update(); | 1866 omnibox_view->Update(); |
1867 EXPECT_EQ(url_c, omnibox_view->GetText()); | 1867 EXPECT_EQ(url_c, omnibox_view->GetText()); |
1868 } | 1868 } |
| 1869 |
| 1870 namespace { |
| 1871 |
| 1872 // Returns the number of characters currently selected in |omnibox_view|. |
| 1873 size_t GetSelectionSize(OmniboxView* omnibox_view) { |
| 1874 size_t start, end; |
| 1875 omnibox_view->GetSelectionBounds(&start, &end); |
| 1876 if (end >= start) |
| 1877 return end - start; |
| 1878 return start - end; |
| 1879 } |
| 1880 |
| 1881 } // namespace |
| 1882 |
| 1883 // Test that if the Omnibox has focus, and had everything selected before a |
| 1884 // non-user-initiated update, then it retains the selection after the update. |
| 1885 IN_PROC_BROWSER_TEST_F(OmniboxViewTest, SelectAllStaysAfterUpdate) { |
| 1886 OmniboxView* omnibox_view = nullptr; |
| 1887 ASSERT_NO_FATAL_FAILURE(GetOmniboxView(&omnibox_view)); |
| 1888 TestToolbarModel* test_toolbar_model = new TestToolbarModel; |
| 1889 scoped_ptr<ToolbarModel> toolbar_model(test_toolbar_model); |
| 1890 browser()->swap_toolbar_models(&toolbar_model); |
| 1891 |
| 1892 base::string16 url_a(ASCIIToUTF16("http://www.a.com/")); |
| 1893 base::string16 url_b(ASCIIToUTF16("http://www.b.com/")); |
| 1894 chrome::FocusLocationBar(browser()); |
| 1895 |
| 1896 test_toolbar_model->set_text(url_a); |
| 1897 omnibox_view->Update(); |
| 1898 EXPECT_EQ(url_a, omnibox_view->GetText()); |
| 1899 EXPECT_TRUE(omnibox_view->IsSelectAll()); |
| 1900 |
| 1901 // Updating while selected should retain SelectAll(). |
| 1902 test_toolbar_model->set_text(url_b); |
| 1903 omnibox_view->Update(); |
| 1904 EXPECT_EQ(url_b, omnibox_view->GetText()); |
| 1905 EXPECT_TRUE(omnibox_view->IsSelectAll()); |
| 1906 |
| 1907 // Select nothing, then switch back. Shouldn't gain a selection. |
| 1908 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT, 0)); |
| 1909 test_toolbar_model->set_text(url_a); |
| 1910 omnibox_view->Update(); |
| 1911 EXPECT_EQ(url_a, omnibox_view->GetText()); |
| 1912 EXPECT_FALSE(omnibox_view->IsSelectAll()); |
| 1913 |
| 1914 // Test behavior of the "reversed" attribute of OmniboxView::SelectAll(). |
| 1915 test_toolbar_model->set_text(ASCIIToUTF16("AB")); |
| 1916 omnibox_view->Update(); |
| 1917 // Should be at the end already. Shift+Left to select "reversed". |
| 1918 EXPECT_EQ(0u, GetSelectionSize(omnibox_view)); |
| 1919 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, ui::EF_SHIFT_DOWN)); |
| 1920 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, ui::EF_SHIFT_DOWN)); |
| 1921 EXPECT_EQ(2u, GetSelectionSize(omnibox_view)); |
| 1922 EXPECT_TRUE(omnibox_view->IsSelectAll()); |
| 1923 |
| 1924 test_toolbar_model->set_text(ASCIIToUTF16("CD")); |
| 1925 omnibox_view->Update(); |
| 1926 EXPECT_EQ(2u, GetSelectionSize(omnibox_view)); |
| 1927 |
| 1928 // At the start, so Shift+Left should do nothing. |
| 1929 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, ui::EF_SHIFT_DOWN)); |
| 1930 EXPECT_EQ(2u, GetSelectionSize(omnibox_view)); |
| 1931 |
| 1932 // And Shift+Right should reduce by one character. |
| 1933 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT, ui::EF_SHIFT_DOWN)); |
| 1934 EXPECT_EQ(1u, GetSelectionSize(omnibox_view)); |
| 1935 |
| 1936 // No go to start and select all to the right (not reversed). |
| 1937 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0)); |
| 1938 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, 0)); |
| 1939 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT, ui::EF_SHIFT_DOWN)); |
| 1940 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT, ui::EF_SHIFT_DOWN)); |
| 1941 test_toolbar_model->set_text(ASCIIToUTF16("AB")); |
| 1942 omnibox_view->Update(); |
| 1943 EXPECT_EQ(2u, GetSelectionSize(omnibox_view)); |
| 1944 |
| 1945 // Now Shift+Right should do nothing, and Shift+Left should reduce. |
| 1946 // At the end, so Shift+Right should do nothing. |
| 1947 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_RIGHT, ui::EF_SHIFT_DOWN)); |
| 1948 EXPECT_EQ(2u, GetSelectionSize(omnibox_view)); |
| 1949 |
| 1950 // And Left should reduce by one character. |
| 1951 ASSERT_NO_FATAL_FAILURE(SendKey(ui::VKEY_LEFT, ui::EF_SHIFT_DOWN)); |
| 1952 EXPECT_EQ(1u, GetSelectionSize(omnibox_view)); |
| 1953 } |
OLD | NEW |