| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 // Tests common functionality used by the Chrome Extensions webNavigation API | 5 // Tests common functionality used by the Chrome Extensions webNavigation API |
| 6 // implementation. | 6 // implementation. |
| 7 | 7 |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/browser_thread.h" | 11 #include "chrome/browser/browser_thread.h" |
| 12 #include "chrome/browser/extensions/extension_webnavigation_api.h" | 12 #include "chrome/browser/extensions/extension_webnavigation_api.h" |
| 13 #include "chrome/browser/renderer_host/test/test_render_view_host.h" | 13 #include "chrome/browser/renderer_host/test/test_render_view_host.h" |
| 14 #include "chrome/browser/tab_contents/test_tab_contents.h" | 14 #include "chrome/browser/tab_contents/test_tab_contents.h" |
| 15 #include "chrome/common/url_constants.h" | |
| 16 #include "chrome/test/testing_profile.h" | 15 #include "chrome/test/testing_profile.h" |
| 17 | 16 |
| 18 | 17 |
| 19 class FrameNavigationStateTest : public RenderViewHostTestHarness { | 18 class FrameNavigationStateTest : public RenderViewHostTestHarness { |
| 20 }; | 19 }; |
| 21 | 20 |
| 22 // Test that a frame is correctly tracked, and removed once the tab contents | 21 // Test that a frame is correctly tracked, and removed once the tab contents |
| 23 // goes away. | 22 // goes away. |
| 24 TEST_F(FrameNavigationStateTest, TrackFrame) { | 23 TEST_F(FrameNavigationStateTest, TrackFrame) { |
| 25 FrameNavigationState navigation_state; | 24 FrameNavigationState navigation_state; |
| 26 const int64 frame_id1 = 23; | 25 const int64 frame_id1 = 23; |
| 27 const int64 frame_id2 = 42; | 26 const int64 frame_id2 = 42; |
| 28 const GURL url1("http://www.google.com/"); | 27 const GURL url1("http://www.google.com/"); |
| 29 const GURL url2("http://mail.google.com/"); | 28 const GURL url2("http://mail.google.com/"); |
| 30 | 29 |
| 31 // Create a main frame. | 30 // Create a main frame. |
| 32 EXPECT_FALSE(navigation_state.CanSendEvents(frame_id1)); | 31 EXPECT_FALSE(navigation_state.CanSendEvents(frame_id1)); |
| 33 navigation_state.TrackFrame(frame_id1, url1, true, contents()); | 32 navigation_state.TrackFrame(frame_id1, url1, true, false, contents()); |
| 34 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id1)); | 33 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id1)); |
| 35 | 34 |
| 36 // Add a sub frame. | 35 // Add a sub frame. |
| 37 EXPECT_FALSE(navigation_state.CanSendEvents(frame_id2)); | 36 EXPECT_FALSE(navigation_state.CanSendEvents(frame_id2)); |
| 38 navigation_state.TrackFrame(frame_id2, url2, false, contents()); | 37 navigation_state.TrackFrame(frame_id2, url2, false, false, contents()); |
| 39 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id2)); | 38 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id2)); |
| 40 | 39 |
| 41 // Check frame state. | 40 // Check frame state. |
| 42 EXPECT_TRUE(navigation_state.IsMainFrame(frame_id1)); | 41 EXPECT_TRUE(navigation_state.IsMainFrame(frame_id1)); |
| 43 EXPECT_EQ(url1, navigation_state.GetUrl(frame_id1)); | 42 EXPECT_EQ(url1, navigation_state.GetUrl(frame_id1)); |
| 44 EXPECT_FALSE(navigation_state.IsMainFrame(frame_id2)); | 43 EXPECT_FALSE(navigation_state.IsMainFrame(frame_id2)); |
| 45 EXPECT_EQ(url2, navigation_state.GetUrl(frame_id2)); | 44 EXPECT_EQ(url2, navigation_state.GetUrl(frame_id2)); |
| 46 | 45 |
| 47 | 46 |
| 48 // Removing the tab contents should also remove all state of its frames. | 47 // Removing the tab contents should also remove all state of its frames. |
| 49 navigation_state.RemoveTabContentsState(contents()); | 48 navigation_state.RemoveTabContentsState(contents()); |
| 50 EXPECT_FALSE(navigation_state.CanSendEvents(frame_id1)); | 49 EXPECT_FALSE(navigation_state.CanSendEvents(frame_id1)); |
| 51 EXPECT_FALSE(navigation_state.CanSendEvents(frame_id2)); | 50 EXPECT_FALSE(navigation_state.CanSendEvents(frame_id2)); |
| 52 } | 51 } |
| 53 | 52 |
| 54 // Test that no events can be sent for a frame after an error occurred, but | 53 // Test that no events can be sent for a frame after an error occurred, but |
| 55 // before a new navigation happened in this frame. | 54 // before a new navigation happened in this frame. |
| 56 TEST_F(FrameNavigationStateTest, ErrorState) { | 55 TEST_F(FrameNavigationStateTest, ErrorState) { |
| 57 FrameNavigationState navigation_state; | 56 FrameNavigationState navigation_state; |
| 58 const int64 frame_id = 42; | 57 const int64 frame_id = 42; |
| 59 const GURL url("http://www.google.com/"); | 58 const GURL url("http://www.google.com/"); |
| 60 | 59 |
| 61 navigation_state.TrackFrame(frame_id, url, true, contents()); | 60 navigation_state.TrackFrame(frame_id, url, true, false, contents()); |
| 62 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id)); | 61 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id)); |
| 63 | 62 |
| 64 // After an error occurred, no further events should be sent. | 63 // After an error occurred, no further events should be sent. |
| 65 navigation_state.ErrorOccurredInFrame(frame_id); | 64 navigation_state.ErrorOccurredInFrame(frame_id); |
| 66 EXPECT_FALSE(navigation_state.CanSendEvents(frame_id)); | 65 EXPECT_FALSE(navigation_state.CanSendEvents(frame_id)); |
| 67 | 66 |
| 68 // Navigations to the "unreachable web data" URL should be ignored. | 67 // Navigations to a network error page should be ignored. |
| 69 navigation_state.TrackFrame( | 68 navigation_state.TrackFrame(frame_id, GURL(), true, true, contents()); |
| 70 frame_id, GURL(chrome::kUnreachableWebDataURL), true, contents()); | |
| 71 EXPECT_FALSE(navigation_state.CanSendEvents(frame_id)); | 69 EXPECT_FALSE(navigation_state.CanSendEvents(frame_id)); |
| 72 | 70 |
| 73 // However, when the frame navigates again, it should send events again. | 71 // However, when the frame navigates again, it should send events again. |
| 74 navigation_state.TrackFrame(frame_id, url, true, contents()); | 72 navigation_state.TrackFrame(frame_id, url, true, false, contents()); |
| 75 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id)); | 73 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id)); |
| 76 } | 74 } |
| 77 | 75 |
| 78 // Tests that for a sub frame, no events are send after an error occurred, but | 76 // Tests that for a sub frame, no events are send after an error occurred, but |
| 79 // before a new navigation happened in this frame. | 77 // before a new navigation happened in this frame. |
| 80 TEST_F(FrameNavigationStateTest, ErrorStateFrame) { | 78 TEST_F(FrameNavigationStateTest, ErrorStateFrame) { |
| 81 FrameNavigationState navigation_state; | 79 FrameNavigationState navigation_state; |
| 82 const int64 frame_id1 = 23; | 80 const int64 frame_id1 = 23; |
| 83 const int64 frame_id2 = 42; | 81 const int64 frame_id2 = 42; |
| 84 const GURL url("http://www.google.com/"); | 82 const GURL url("http://www.google.com/"); |
| 85 | 83 |
| 86 navigation_state.TrackFrame(frame_id1, url, true, contents()); | 84 navigation_state.TrackFrame(frame_id1, url, true, false, contents()); |
| 87 navigation_state.TrackFrame(frame_id2, url, false, contents()); | 85 navigation_state.TrackFrame(frame_id2, url, false, false, contents()); |
| 88 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id1)); | 86 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id1)); |
| 89 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id2)); | 87 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id2)); |
| 90 | 88 |
| 91 // After an error occurred, no further events should be sent. | 89 // After an error occurred, no further events should be sent. |
| 92 navigation_state.ErrorOccurredInFrame(frame_id2); | 90 navigation_state.ErrorOccurredInFrame(frame_id2); |
| 93 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id1)); | 91 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id1)); |
| 94 EXPECT_FALSE(navigation_state.CanSendEvents(frame_id2)); | 92 EXPECT_FALSE(navigation_state.CanSendEvents(frame_id2)); |
| 95 | 93 |
| 96 // Navigations to the "unreachable web data" URL should be ignored. | 94 // Navigations to a network error page should be ignored. |
| 97 navigation_state.TrackFrame( | 95 navigation_state.TrackFrame(frame_id2, GURL(), false, true, contents()); |
| 98 frame_id2, GURL(chrome::kUnreachableWebDataURL), false, contents()); | |
| 99 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id1)); | 96 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id1)); |
| 100 EXPECT_FALSE(navigation_state.CanSendEvents(frame_id2)); | 97 EXPECT_FALSE(navigation_state.CanSendEvents(frame_id2)); |
| 101 | 98 |
| 102 // However, when the frame navigates again, it should send events again. | 99 // However, when the frame navigates again, it should send events again. |
| 103 navigation_state.TrackFrame(frame_id2, url, false, contents()); | 100 navigation_state.TrackFrame(frame_id2, url, false, false, contents()); |
| 104 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id1)); | 101 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id1)); |
| 105 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id2)); | 102 EXPECT_TRUE(navigation_state.CanSendEvents(frame_id2)); |
| 106 } | 103 } |
| OLD | NEW |