| 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 "chrome/browser/ui/tabs/tab_strip_model.h" | 5 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 | 177 |
| 178 WebContents* CreateWebContentsWithSharedRPH(WebContents* web_contents) { | 178 WebContents* CreateWebContentsWithSharedRPH(WebContents* web_contents) { |
| 179 WebContents::CreateParams create_params( | 179 WebContents::CreateParams create_params( |
| 180 profile(), web_contents->GetRenderViewHost()->GetSiteInstance()); | 180 profile(), web_contents->GetRenderViewHost()->GetSiteInstance()); |
| 181 WebContents* retval = WebContents::Create(create_params); | 181 WebContents* retval = WebContents::Create(create_params); |
| 182 EXPECT_EQ(retval->GetRenderProcessHost(), | 182 EXPECT_EQ(retval->GetRenderProcessHost(), |
| 183 web_contents->GetRenderProcessHost()); | 183 web_contents->GetRenderProcessHost()); |
| 184 return retval; | 184 return retval; |
| 185 } | 185 } |
| 186 | 186 |
| 187 WebContents* CreateWebContentsWithID(int id) { |
| 188 WebContents* contents = CreateWebContents(); |
| 189 SetID(contents, id); |
| 190 return contents; |
| 191 } |
| 192 |
| 187 // Sets the id of the specified contents. | 193 // Sets the id of the specified contents. |
| 188 void SetID(WebContents* contents, int id) { | 194 void SetID(WebContents* contents, int id) { |
| 189 contents->SetUserData(&kTabStripModelTestIDUserDataKey, | 195 contents->SetUserData(&kTabStripModelTestIDUserDataKey, |
| 190 new TabStripModelTestIDUserData(id)); | 196 new TabStripModelTestIDUserData(id)); |
| 191 } | 197 } |
| 192 | 198 |
| 193 // Returns the id of the specified contents. | 199 // Returns the id of the specified contents. |
| 194 int GetID(WebContents* contents) { | 200 int GetID(WebContents* contents) { |
| 195 TabStripModelTestIDUserData* user_data = | 201 TabStripModelTestIDUserData* user_data = |
| 196 static_cast<TabStripModelTestIDUserData*>( | 202 static_cast<TabStripModelTestIDUserData*>( |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 result += " "; | 237 result += " "; |
| 232 result += base::IntToString(indices[i]); | 238 result += base::IntToString(indices[i]); |
| 233 } | 239 } |
| 234 return result; | 240 return result; |
| 235 } | 241 } |
| 236 | 242 |
| 237 void PrepareTabstripForSelectionTest(TabStripModel* model, | 243 void PrepareTabstripForSelectionTest(TabStripModel* model, |
| 238 int tab_count, | 244 int tab_count, |
| 239 int pinned_count, | 245 int pinned_count, |
| 240 const std::string& selected_tabs) { | 246 const std::string& selected_tabs) { |
| 241 for (int i = 0; i < tab_count; ++i) { | 247 for (int i = 0; i < tab_count; ++i) |
| 242 WebContents* contents = CreateWebContents(); | 248 model->AppendWebContents(CreateWebContentsWithID(i), true); |
| 243 SetID(contents, i); | |
| 244 model->AppendWebContents(contents, true); | |
| 245 } | |
| 246 for (int i = 0; i < pinned_count; ++i) | 249 for (int i = 0; i < pinned_count; ++i) |
| 247 model->SetTabPinned(i, true); | 250 model->SetTabPinned(i, true); |
| 248 | 251 |
| 249 ui::ListSelectionModel selection_model; | 252 ui::ListSelectionModel selection_model; |
| 250 std::vector<std::string> selection; | 253 std::vector<std::string> selection; |
| 251 base::SplitStringAlongWhitespace(selected_tabs, &selection); | 254 base::SplitStringAlongWhitespace(selected_tabs, &selection); |
| 252 for (size_t i = 0; i < selection.size(); ++i) { | 255 for (size_t i = 0; i < selection.size(); ++i) { |
| 253 int value; | 256 int value; |
| 254 ASSERT_TRUE(base::StringToInt(selection[i], &value)); | 257 ASSERT_TRUE(base::StringToInt(selection[i], &value)); |
| 255 selection_model.AddIndexToSelection(value); | 258 selection_model.AddIndexToSelection(value); |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 TEST_F(TabStripModelTest, TestBasicAPI) { | 434 TEST_F(TabStripModelTest, TestBasicAPI) { |
| 432 TabStripDummyDelegate delegate; | 435 TabStripDummyDelegate delegate; |
| 433 TabStripModel tabstrip(&delegate, profile()); | 436 TabStripModel tabstrip(&delegate, profile()); |
| 434 MockTabStripModelObserver observer(&tabstrip); | 437 MockTabStripModelObserver observer(&tabstrip); |
| 435 tabstrip.AddObserver(&observer); | 438 tabstrip.AddObserver(&observer); |
| 436 | 439 |
| 437 EXPECT_TRUE(tabstrip.empty()); | 440 EXPECT_TRUE(tabstrip.empty()); |
| 438 | 441 |
| 439 typedef MockTabStripModelObserver::State State; | 442 typedef MockTabStripModelObserver::State State; |
| 440 | 443 |
| 441 WebContents* contents1 = CreateWebContents(); | 444 WebContents* contents1 = CreateWebContentsWithID(1); |
| 442 SetID(contents1, 1); | |
| 443 | 445 |
| 444 // Note! The ordering of these tests is important, each subsequent test | 446 // Note! The ordering of these tests is important, each subsequent test |
| 445 // builds on the state established in the previous. This is important if you | 447 // builds on the state established in the previous. This is important if you |
| 446 // ever insert tests rather than append. | 448 // ever insert tests rather than append. |
| 447 | 449 |
| 448 // Test AppendWebContents, ContainsIndex | 450 // Test AppendWebContents, ContainsIndex |
| 449 { | 451 { |
| 450 EXPECT_FALSE(tabstrip.ContainsIndex(0)); | 452 EXPECT_FALSE(tabstrip.ContainsIndex(0)); |
| 451 tabstrip.AppendWebContents(contents1, true); | 453 tabstrip.AppendWebContents(contents1, true); |
| 452 EXPECT_TRUE(tabstrip.ContainsIndex(0)); | 454 EXPECT_TRUE(tabstrip.ContainsIndex(0)); |
| 453 EXPECT_EQ(1, tabstrip.count()); | 455 EXPECT_EQ(1, tabstrip.count()); |
| 454 EXPECT_EQ(3, observer.GetStateCount()); | 456 EXPECT_EQ(3, observer.GetStateCount()); |
| 455 State s1(contents1, 0, MockTabStripModelObserver::INSERT); | 457 State s1(contents1, 0, MockTabStripModelObserver::INSERT); |
| 456 s1.foreground = true; | 458 s1.foreground = true; |
| 457 EXPECT_TRUE(observer.StateEquals(0, s1)); | 459 EXPECT_TRUE(observer.StateEquals(0, s1)); |
| 458 State s2(contents1, 0, MockTabStripModelObserver::ACTIVATE); | 460 State s2(contents1, 0, MockTabStripModelObserver::ACTIVATE); |
| 459 EXPECT_TRUE(observer.StateEquals(1, s2)); | 461 EXPECT_TRUE(observer.StateEquals(1, s2)); |
| 460 State s3(contents1, 0, MockTabStripModelObserver::SELECT); | 462 State s3(contents1, 0, MockTabStripModelObserver::SELECT); |
| 461 s3.src_contents = NULL; | 463 s3.src_contents = NULL; |
| 462 s3.src_index = ui::ListSelectionModel::kUnselectedIndex; | 464 s3.src_index = ui::ListSelectionModel::kUnselectedIndex; |
| 463 EXPECT_TRUE(observer.StateEquals(2, s3)); | 465 EXPECT_TRUE(observer.StateEquals(2, s3)); |
| 464 observer.ClearStates(); | 466 observer.ClearStates(); |
| 465 } | 467 } |
| 466 EXPECT_EQ("1", GetTabStripStateString(tabstrip)); | 468 EXPECT_EQ("1", GetTabStripStateString(tabstrip)); |
| 467 | 469 |
| 468 // Test InsertWebContentsAt, foreground tab. | 470 // Test InsertWebContentsAt, foreground tab. |
| 469 WebContents* contents2 = CreateWebContents(); | 471 WebContents* contents2 = CreateWebContentsWithID(2); |
| 470 SetID(contents2, 2); | |
| 471 { | 472 { |
| 472 tabstrip.InsertWebContentsAt(1, contents2, TabStripModel::ADD_ACTIVE); | 473 tabstrip.InsertWebContentsAt(1, contents2, TabStripModel::ADD_ACTIVE); |
| 473 | 474 |
| 474 EXPECT_EQ(2, tabstrip.count()); | 475 EXPECT_EQ(2, tabstrip.count()); |
| 475 EXPECT_EQ(4, observer.GetStateCount()); | 476 EXPECT_EQ(4, observer.GetStateCount()); |
| 476 State s1(contents2, 1, MockTabStripModelObserver::INSERT); | 477 State s1(contents2, 1, MockTabStripModelObserver::INSERT); |
| 477 s1.foreground = true; | 478 s1.foreground = true; |
| 478 EXPECT_TRUE(observer.StateEquals(0, s1)); | 479 EXPECT_TRUE(observer.StateEquals(0, s1)); |
| 479 State s2(contents1, 0, MockTabStripModelObserver::DEACTIVATE); | 480 State s2(contents1, 0, MockTabStripModelObserver::DEACTIVATE); |
| 480 EXPECT_TRUE(observer.StateEquals(1, s2)); | 481 EXPECT_TRUE(observer.StateEquals(1, s2)); |
| 481 State s3(contents2, 1, MockTabStripModelObserver::ACTIVATE); | 482 State s3(contents2, 1, MockTabStripModelObserver::ACTIVATE); |
| 482 s3.src_contents = contents1; | 483 s3.src_contents = contents1; |
| 483 EXPECT_TRUE(observer.StateEquals(2, s3)); | 484 EXPECT_TRUE(observer.StateEquals(2, s3)); |
| 484 State s4(contents2, 1, MockTabStripModelObserver::SELECT); | 485 State s4(contents2, 1, MockTabStripModelObserver::SELECT); |
| 485 s4.src_contents = contents1; | 486 s4.src_contents = contents1; |
| 486 s4.src_index = 0; | 487 s4.src_index = 0; |
| 487 EXPECT_TRUE(observer.StateEquals(3, s4)); | 488 EXPECT_TRUE(observer.StateEquals(3, s4)); |
| 488 observer.ClearStates(); | 489 observer.ClearStates(); |
| 489 } | 490 } |
| 490 EXPECT_EQ("1 2", GetTabStripStateString(tabstrip)); | 491 EXPECT_EQ("1 2", GetTabStripStateString(tabstrip)); |
| 491 | 492 |
| 492 // Test InsertWebContentsAt, background tab. | 493 // Test InsertWebContentsAt, background tab. |
| 493 WebContents* contents3 = CreateWebContents(); | 494 WebContents* contents3 = CreateWebContentsWithID(3); |
| 494 SetID(contents3, 3); | |
| 495 { | 495 { |
| 496 tabstrip.InsertWebContentsAt(2, contents3, TabStripModel::ADD_NONE); | 496 tabstrip.InsertWebContentsAt(2, contents3, TabStripModel::ADD_NONE); |
| 497 | 497 |
| 498 EXPECT_EQ(3, tabstrip.count()); | 498 EXPECT_EQ(3, tabstrip.count()); |
| 499 EXPECT_EQ(1, observer.GetStateCount()); | 499 EXPECT_EQ(1, observer.GetStateCount()); |
| 500 State s1(contents3, 2, MockTabStripModelObserver::INSERT); | 500 State s1(contents3, 2, MockTabStripModelObserver::INSERT); |
| 501 s1.foreground = false; | 501 s1.foreground = false; |
| 502 EXPECT_TRUE(observer.StateEquals(0, s1)); | 502 EXPECT_TRUE(observer.StateEquals(0, s1)); |
| 503 observer.ClearStates(); | 503 observer.ClearStates(); |
| 504 } | 504 } |
| (...skipping 1305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1810 #endif | 1810 #endif |
| 1811 | 1811 |
| 1812 base::DictionaryValue manifest; | 1812 base::DictionaryValue manifest; |
| 1813 manifest.SetString("name", "hi!"); | 1813 manifest.SetString("name", "hi!"); |
| 1814 manifest.SetString("version", "1"); | 1814 manifest.SetString("version", "1"); |
| 1815 manifest.SetString("app.launch.web_url", "http://www.google.com"); | 1815 manifest.SetString("app.launch.web_url", "http://www.google.com"); |
| 1816 std::string error; | 1816 std::string error; |
| 1817 scoped_refptr<Extension> extension_app( | 1817 scoped_refptr<Extension> extension_app( |
| 1818 Extension::Create(path, extensions::Manifest::INVALID_LOCATION, | 1818 Extension::Create(path, extensions::Manifest::INVALID_LOCATION, |
| 1819 manifest, Extension::NO_FLAGS, &error)); | 1819 manifest, Extension::NO_FLAGS, &error)); |
| 1820 WebContents* contents1 = CreateWebContents(); | 1820 WebContents* contents1 = CreateWebContentsWithID(1); |
| 1821 extensions::TabHelper::CreateForWebContents(contents1); | 1821 extensions::TabHelper::CreateForWebContents(contents1); |
| 1822 extensions::TabHelper::FromWebContents(contents1) | 1822 extensions::TabHelper::FromWebContents(contents1) |
| 1823 ->SetExtensionApp(extension_app.get()); | 1823 ->SetExtensionApp(extension_app.get()); |
| 1824 WebContents* contents2 = CreateWebContents(); | 1824 WebContents* contents2 = CreateWebContentsWithID(2); |
| 1825 extensions::TabHelper::CreateForWebContents(contents2); | 1825 extensions::TabHelper::CreateForWebContents(contents2); |
| 1826 extensions::TabHelper::FromWebContents(contents2) | 1826 extensions::TabHelper::FromWebContents(contents2) |
| 1827 ->SetExtensionApp(extension_app.get()); | 1827 ->SetExtensionApp(extension_app.get()); |
| 1828 WebContents* contents3 = CreateWebContents(); | 1828 WebContents* contents3 = CreateWebContentsWithID(3); |
| 1829 | |
| 1830 SetID(contents1, 1); | |
| 1831 SetID(contents2, 2); | |
| 1832 SetID(contents3, 3); | |
| 1833 | 1829 |
| 1834 // Note! The ordering of these tests is important, each subsequent test | 1830 // Note! The ordering of these tests is important, each subsequent test |
| 1835 // builds on the state established in the previous. This is important if you | 1831 // builds on the state established in the previous. This is important if you |
| 1836 // ever insert tests rather than append. | 1832 // ever insert tests rather than append. |
| 1837 | 1833 |
| 1838 // Initial state, tab3 only and selected. | 1834 // Initial state, tab3 only and selected. |
| 1839 tabstrip.AppendWebContents(contents3, true); | 1835 tabstrip.AppendWebContents(contents3, true); |
| 1840 | 1836 |
| 1841 observer.ClearStates(); | 1837 observer.ClearStates(); |
| 1842 | 1838 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1932 TEST_F(TabStripModelTest, Pinning) { | 1928 TEST_F(TabStripModelTest, Pinning) { |
| 1933 TabStripDummyDelegate delegate; | 1929 TabStripDummyDelegate delegate; |
| 1934 TabStripModel tabstrip(&delegate, profile()); | 1930 TabStripModel tabstrip(&delegate, profile()); |
| 1935 MockTabStripModelObserver observer(&tabstrip); | 1931 MockTabStripModelObserver observer(&tabstrip); |
| 1936 tabstrip.AddObserver(&observer); | 1932 tabstrip.AddObserver(&observer); |
| 1937 | 1933 |
| 1938 EXPECT_TRUE(tabstrip.empty()); | 1934 EXPECT_TRUE(tabstrip.empty()); |
| 1939 | 1935 |
| 1940 typedef MockTabStripModelObserver::State State; | 1936 typedef MockTabStripModelObserver::State State; |
| 1941 | 1937 |
| 1942 WebContents* contents1 = CreateWebContents(); | 1938 WebContents* contents1 = CreateWebContentsWithID(1); |
| 1943 WebContents* contents2 = CreateWebContents(); | 1939 WebContents* contents2 = CreateWebContentsWithID(2); |
| 1944 WebContents* contents3 = CreateWebContents(); | 1940 WebContents* contents3 = CreateWebContentsWithID(3); |
| 1945 | |
| 1946 SetID(contents1, 1); | |
| 1947 SetID(contents2, 2); | |
| 1948 SetID(contents3, 3); | |
| 1949 | 1941 |
| 1950 // Note! The ordering of these tests is important, each subsequent test | 1942 // Note! The ordering of these tests is important, each subsequent test |
| 1951 // builds on the state established in the previous. This is important if you | 1943 // builds on the state established in the previous. This is important if you |
| 1952 // ever insert tests rather than append. | 1944 // ever insert tests rather than append. |
| 1953 | 1945 |
| 1954 // Initial state, three tabs, first selected. | 1946 // Initial state, three tabs, first selected. |
| 1955 tabstrip.AppendWebContents(contents1, true); | 1947 tabstrip.AppendWebContents(contents1, true); |
| 1956 tabstrip.AppendWebContents(contents2, false); | 1948 tabstrip.AppendWebContents(contents2, false); |
| 1957 tabstrip.AppendWebContents(contents3, false); | 1949 tabstrip.AppendWebContents(contents3, false); |
| 1958 | 1950 |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2067 // Pin "3" and "1". | 2059 // Pin "3" and "1". |
| 2068 { | 2060 { |
| 2069 tabstrip.SetTabPinned(0, true); | 2061 tabstrip.SetTabPinned(0, true); |
| 2070 tabstrip.SetTabPinned(1, true); | 2062 tabstrip.SetTabPinned(1, true); |
| 2071 | 2063 |
| 2072 EXPECT_EQ("1p 3p 2", GetTabStripStateString(tabstrip)); | 2064 EXPECT_EQ("1p 3p 2", GetTabStripStateString(tabstrip)); |
| 2073 | 2065 |
| 2074 observer.ClearStates(); | 2066 observer.ClearStates(); |
| 2075 } | 2067 } |
| 2076 | 2068 |
| 2077 WebContents* contents4 = CreateWebContents(); | 2069 WebContents* contents4 = CreateWebContentsWithID(4); |
| 2078 SetID(contents4, 4); | |
| 2079 | 2070 |
| 2080 // Insert "4" between "1" and "3". As "1" and "4" are pinned, "4" should end | 2071 // Insert "4" between "1" and "3". As "1" and "4" are pinned, "4" should end |
| 2081 // up after them. | 2072 // up after them. |
| 2082 { | 2073 { |
| 2083 tabstrip.InsertWebContentsAt(1, contents4, TabStripModel::ADD_NONE); | 2074 tabstrip.InsertWebContentsAt(1, contents4, TabStripModel::ADD_NONE); |
| 2084 | 2075 |
| 2085 ASSERT_EQ(1, observer.GetStateCount()); | 2076 ASSERT_EQ(1, observer.GetStateCount()); |
| 2086 State state(contents4, 2, MockTabStripModelObserver::INSERT); | 2077 State state(contents4, 2, MockTabStripModelObserver::INSERT); |
| 2087 EXPECT_TRUE(observer.StateEquals(0, state)); | 2078 EXPECT_TRUE(observer.StateEquals(0, state)); |
| 2088 | 2079 |
| (...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2572 WebContents* moved_contents = strip_src.DetachWebContentsAt(1); | 2563 WebContents* moved_contents = strip_src.DetachWebContentsAt(1); |
| 2573 EXPECT_EQ(contents2, moved_contents); | 2564 EXPECT_EQ(contents2, moved_contents); |
| 2574 | 2565 |
| 2575 // Attach the tab to the destination tab strip. | 2566 // Attach the tab to the destination tab strip. |
| 2576 strip_dst.AppendWebContents(moved_contents, true); | 2567 strip_dst.AppendWebContents(moved_contents, true); |
| 2577 EXPECT_TRUE(strip_dst.IsTabBlocked(0)); | 2568 EXPECT_TRUE(strip_dst.IsTabBlocked(0)); |
| 2578 | 2569 |
| 2579 strip_dst.CloseAllTabs(); | 2570 strip_dst.CloseAllTabs(); |
| 2580 strip_src.CloseAllTabs(); | 2571 strip_src.CloseAllTabs(); |
| 2581 } | 2572 } |
| OLD | NEW |