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 "base/compiler_specific.h"
6#include "base/synchronization/waitable_event.h"
7#include "chrome/common/chrome_paths_internal.h"
8#include "chrome_frame/crash_reporting/crash_metrics.h"
9#include "chrome_frame/test/chrome_frame_test_utils.h"
10#include "chrome_frame/test/proxy_factory_mock.h"
11#include "chrome_frame/test/test_scrubber.h"
12#include "chrome_frame/utils.h"
13
14#define GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING
15#include "testing/gmock_mutant.h"
16
17using testing::CreateFunctor;
18using testing::_;
19
20class ProxyFactoryTest : public testing::Test {
21 protected:
22  virtual void SetUp() OVERRIDE;
23
24  ChromeFrameLaunchParams* MakeLaunchParams(const wchar_t* profile_name);
25
26  ProxyFactory proxy_factory_;
27  LaunchDelegateMock launch_delegate_mock_;
28};
29
30void ProxyFactoryTest::SetUp() {
31  CrashMetricsReporter::GetInstance()->set_active(true);
32}
33
34ChromeFrameLaunchParams* ProxyFactoryTest::MakeLaunchParams(
35    const wchar_t* profile_name) {
36  GURL empty;
37  base::FilePath profile_path;
38  GetChromeFrameProfilePath(profile_name, &profile_path);
39  chrome_frame_test::OverrideDataDirectoryForThisTest(profile_path.value());
40  ChromeFrameLaunchParams* params =
41      new ChromeFrameLaunchParams(empty, empty, profile_path,
42                                  profile_path.BaseName().value(), L"", false,
43                                  false, false);
44  params->set_launch_timeout(0);
45  params->set_version_check(false);
46  return params;
47}
48
49TEST_F(ProxyFactoryTest, CreateDestroy) {
50  EXPECT_CALL(launch_delegate_mock_,
51              LaunchComplete(testing::NotNull(), testing::_)).Times(1);
52
53  scoped_refptr<ChromeFrameLaunchParams> params(
54      MakeLaunchParams(L"Adam.N.Epilinter"));
55
56  void* id = NULL;
57  proxy_factory_.GetAutomationServer(&launch_delegate_mock_, params, &id);
58  proxy_factory_.ReleaseAutomationServer(id, &launch_delegate_mock_);
59}
60
61TEST_F(ProxyFactoryTest, CreateSameProfile) {
62  LaunchDelegateMock d2;
63  EXPECT_CALL(launch_delegate_mock_,
64              LaunchComplete(testing::NotNull(), testing::_)).Times(1);
65  EXPECT_CALL(d2, LaunchComplete(testing::NotNull(), testing::_)).Times(1);
66
67  scoped_refptr<ChromeFrameLaunchParams> params(
68      MakeLaunchParams(L"Dr. Gratiano Forbeson"));
69
70  void* i1 = NULL;
71  void* i2 = NULL;
72
73  proxy_factory_.GetAutomationServer(&launch_delegate_mock_, params, &i1);
74  proxy_factory_.GetAutomationServer(&d2, params, &i2);
75
76  EXPECT_EQ(i1, i2);
77  proxy_factory_.ReleaseAutomationServer(i2, &d2);
78  proxy_factory_.ReleaseAutomationServer(i1, &launch_delegate_mock_);
79}
80
81TEST_F(ProxyFactoryTest, CreateDifferentProfiles) {
82  LaunchDelegateMock d2;
83
84  EXPECT_CALL(launch_delegate_mock_,
85              LaunchComplete(testing::NotNull(), testing::_));
86  EXPECT_CALL(d2, LaunchComplete(testing::NotNull(), testing::_));
87
88  scoped_refptr<ChromeFrameLaunchParams> params1(
89      MakeLaunchParams(L"Adam.N.Epilinter"));
90  scoped_refptr<ChromeFrameLaunchParams> params2(
91      MakeLaunchParams(L"Dr. Gratiano Forbeson"));
92
93  void* i1 = NULL;
94  void* i2 = NULL;
95
96  proxy_factory_.GetAutomationServer(&launch_delegate_mock_, params1, &i1);
97  proxy_factory_.GetAutomationServer(&d2, params2, &i2);
98
99  EXPECT_NE(i1, i2);
100  proxy_factory_.ReleaseAutomationServer(i2, &d2);
101  proxy_factory_.ReleaseAutomationServer(i1, &launch_delegate_mock_);
102}
103
104// This test has been disabled because it crashes randomly on the builders.
105// http://code.google.com/p/chromium/issues/detail?id=81039
106TEST_F(ProxyFactoryTest, DISABLED_FastCreateDestroy) {
107  LaunchDelegateMock* d1 = &launch_delegate_mock_;
108  LaunchDelegateMock* d2 = new LaunchDelegateMock();
109
110  scoped_refptr<ChromeFrameLaunchParams> params(
111      MakeLaunchParams(L"Dr. Gratiano Forbeson"));
112  params->set_launch_timeout(10000);
113
114  void* i1 = NULL;
115  base::WaitableEvent launched(true, false);
116  EXPECT_CALL(*d1, LaunchComplete(testing::NotNull(), AUTOMATION_SUCCESS))
117      .WillOnce(testing::InvokeWithoutArgs(&launched,
118                                           &base::WaitableEvent::Signal));
119  proxy_factory_.GetAutomationServer(d1, params, &i1);
120  // Wait for launch
121  ASSERT_TRUE(launched.TimedWait(base::TimeDelta::FromSeconds(10)));
122
123  // Expect second launch to succeed too
124  EXPECT_CALL(*d2, LaunchComplete(testing::NotNull(), AUTOMATION_SUCCESS))
125      .Times(1);
126
127  // Boost thread priority so we call ReleaseAutomationServer before
128  // LaunchComplete callback have a chance to be executed.
129  ::SetThreadPriority(::GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
130  void* i2 = NULL;
131  proxy_factory_.GetAutomationServer(d2, params, &i2);
132  EXPECT_EQ(i1, i2);
133  proxy_factory_.ReleaseAutomationServer(i2, d2);
134  delete d2;
135
136  ::SetThreadPriority(::GetCurrentThread(), THREAD_PRIORITY_NORMAL);
137  proxy_factory_.ReleaseAutomationServer(i1, d1);
138}
139