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
3 // found in the LICENSE file.
4
5 #include <string>
6
7 #include "base/memory/scoped_ptr.h"
8 #include "chrome/test/chromedriver/chrome/status.h"
9 #include "chrome/test/chromedriver/chrome/stub_chrome.h"
10 #include "chrome/test/chromedriver/chrome/stub_web_view.h"
11 #include "chrome/test/chromedriver/session.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace {
15
16 class MockChrome : public StubChrome {
17 public:
MockChrome()18 MockChrome() : web_view_("1") {}
~MockChrome()19 virtual ~MockChrome() {}
20
GetWebViewById(const std::string & id,WebView ** web_view)21 virtual Status GetWebViewById(const std::string& id,
22 WebView** web_view) OVERRIDE {
23 if (id == web_view_.GetId()) {
24 *web_view = &web_view_;
25 return Status(kOk);
26 }
27 return Status(kUnknownError);
28 }
29
30 private:
31 StubWebView web_view_;
32 };
33
34 } // namespace
35
TEST(Session,GetTargetWindowNoChrome)36 TEST(Session, GetTargetWindowNoChrome) {
37 Session session("1");
38 WebView* web_view;
39 ASSERT_EQ(kNoSuchWindow, session.GetTargetWindow(&web_view).code());
40 }
41
TEST(Session,GetTargetWindowTargetWindowClosed)42 TEST(Session, GetTargetWindowTargetWindowClosed) {
43 scoped_ptr<Chrome> chrome(new MockChrome());
44 Session session("1", chrome.Pass());
45 session.window = "2";
46 WebView* web_view;
47 ASSERT_EQ(kNoSuchWindow, session.GetTargetWindow(&web_view).code());
48 }
49
TEST(Session,GetTargetWindowTargetWindowStillOpen)50 TEST(Session, GetTargetWindowTargetWindowStillOpen) {
51 scoped_ptr<Chrome> chrome(new MockChrome());
52 Session session("1", chrome.Pass());
53 session.window = "1";
54 WebView* web_view = NULL;
55 ASSERT_EQ(kOk, session.GetTargetWindow(&web_view).code());
56 ASSERT_TRUE(web_view);
57 }
58
TEST(Session,SwitchToParentFrame)59 TEST(Session, SwitchToParentFrame) {
60 scoped_ptr<Chrome> chrome(new MockChrome());
61 Session session("1", chrome.Pass());
62
63 // Initial frame should be top frame.
64 ASSERT_EQ(std::string(), session.GetCurrentFrameId());
65
66 // Switching to parent frame should be a no-op.
67 session.SwitchToParentFrame();
68 ASSERT_EQ(std::string(), session.GetCurrentFrameId());
69
70 session.SwitchToSubFrame("1.1", std::string());
71 ASSERT_EQ("1.1", session.GetCurrentFrameId());
72 session.SwitchToParentFrame();
73 ASSERT_EQ(std::string(), session.GetCurrentFrameId());
74
75 session.SwitchToSubFrame("2.1", std::string());
76 ASSERT_EQ("2.1", session.GetCurrentFrameId());
77 session.SwitchToSubFrame("2.2", std::string());
78 ASSERT_EQ("2.2", session.GetCurrentFrameId());
79 session.SwitchToParentFrame();
80 ASSERT_EQ("2.1", session.GetCurrentFrameId());
81 session.SwitchToParentFrame();
82 ASSERT_EQ(std::string(), session.GetCurrentFrameId());
83 }
84
TEST(Session,SwitchToTopFrame)85 TEST(Session, SwitchToTopFrame) {
86 scoped_ptr<Chrome> chrome(new MockChrome());
87 Session session("1", chrome.Pass());
88
89 // Initial frame should be top frame.
90 ASSERT_EQ(std::string(), session.GetCurrentFrameId());
91
92 // Switching to top frame should be a no-op.
93 session.SwitchToTopFrame();
94 ASSERT_EQ(std::string(), session.GetCurrentFrameId());
95
96 session.SwitchToSubFrame("3.1", std::string());
97 ASSERT_EQ("3.1", session.GetCurrentFrameId());
98 session.SwitchToSubFrame("3.2", std::string());
99 ASSERT_EQ("3.2", session.GetCurrentFrameId());
100 session.SwitchToTopFrame();
101 ASSERT_EQ(std::string(), session.GetCurrentFrameId());
102 }
103