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.
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"
16 class MockChrome
: public StubChrome
{
18 MockChrome() : web_view_("1") {}
19 ~MockChrome() override
{}
21 Status
GetWebViewById(const std::string
& id
, WebView
** web_view
) override
{
22 if (id
== web_view_
.GetId()) {
23 *web_view
= &web_view_
;
26 return Status(kUnknownError
);
30 StubWebView web_view_
;
35 TEST(Session
, GetTargetWindowNoChrome
) {
38 ASSERT_EQ(kNoSuchWindow
, session
.GetTargetWindow(&web_view
).code());
41 TEST(Session
, GetTargetWindowTargetWindowClosed
) {
42 scoped_ptr
<Chrome
> chrome(new MockChrome());
43 Session
session("1", chrome
.Pass());
46 ASSERT_EQ(kNoSuchWindow
, session
.GetTargetWindow(&web_view
).code());
49 TEST(Session
, GetTargetWindowTargetWindowStillOpen
) {
50 scoped_ptr
<Chrome
> chrome(new MockChrome());
51 Session
session("1", chrome
.Pass());
53 WebView
* web_view
= NULL
;
54 ASSERT_EQ(kOk
, session
.GetTargetWindow(&web_view
).code());
55 ASSERT_TRUE(web_view
);
58 TEST(Session
, SwitchToParentFrame
) {
59 scoped_ptr
<Chrome
> chrome(new MockChrome());
60 Session
session("1", chrome
.Pass());
62 // Initial frame should be top frame.
63 ASSERT_EQ(std::string(), session
.GetCurrentFrameId());
65 // Switching to parent frame should be a no-op.
66 session
.SwitchToParentFrame();
67 ASSERT_EQ(std::string(), session
.GetCurrentFrameId());
69 session
.SwitchToSubFrame("1.1", std::string());
70 ASSERT_EQ("1.1", session
.GetCurrentFrameId());
71 session
.SwitchToParentFrame();
72 ASSERT_EQ(std::string(), session
.GetCurrentFrameId());
74 session
.SwitchToSubFrame("2.1", std::string());
75 ASSERT_EQ("2.1", session
.GetCurrentFrameId());
76 session
.SwitchToSubFrame("2.2", std::string());
77 ASSERT_EQ("2.2", session
.GetCurrentFrameId());
78 session
.SwitchToParentFrame();
79 ASSERT_EQ("2.1", session
.GetCurrentFrameId());
80 session
.SwitchToParentFrame();
81 ASSERT_EQ(std::string(), session
.GetCurrentFrameId());
84 TEST(Session
, SwitchToTopFrame
) {
85 scoped_ptr
<Chrome
> chrome(new MockChrome());
86 Session
session("1", chrome
.Pass());
88 // Initial frame should be top frame.
89 ASSERT_EQ(std::string(), session
.GetCurrentFrameId());
91 // Switching to top frame should be a no-op.
92 session
.SwitchToTopFrame();
93 ASSERT_EQ(std::string(), session
.GetCurrentFrameId());
95 session
.SwitchToSubFrame("3.1", std::string());
96 ASSERT_EQ("3.1", session
.GetCurrentFrameId());
97 session
.SwitchToSubFrame("3.2", std::string());
98 ASSERT_EQ("3.2", session
.GetCurrentFrameId());
99 session
.SwitchToTopFrame();
100 ASSERT_EQ(std::string(), session
.GetCurrentFrameId());