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