summaryrefslogtreecommitdiffstats
path: root/chrome_frame/test/proxy_factory_mock.cc
blob: 4932b0152d765d7ec203da26e3a6f6ee05e8e696 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/compiler_specific.h"
#include "base/synchronization/waitable_event.h"
#include "chrome/common/chrome_paths_internal.h"
#include "chrome_frame/crash_reporting/crash_metrics.h"
#include "chrome_frame/test/chrome_frame_test_utils.h"
#include "chrome_frame/test/proxy_factory_mock.h"
#include "chrome_frame/test/test_scrubber.h"

#define GMOCK_MUTANT_INCLUDE_LATE_OBJECT_BINDING
#include "testing/gmock_mutant.h"

using testing::CreateFunctor;
using testing::_;

class ProxyFactoryTest : public testing::Test {
 protected:
  virtual void SetUp() OVERRIDE;

  ChromeFrameLaunchParams* MakeLaunchParams(const wchar_t* profile_name);

  ProxyFactory proxy_factory_;
  LaunchDelegateMock launch_delegate_mock_;
};

void ProxyFactoryTest::SetUp() {
  CrashMetricsReporter::GetInstance()->set_active(true);
}

ChromeFrameLaunchParams* ProxyFactoryTest::MakeLaunchParams(
    const wchar_t* profile_name) {
  GURL empty;
  FilePath profile_path(chrome_frame_test::GetProfilePath(profile_name));
  chrome_frame_test::OverrideDataDirectoryForThisTest(profile_path.value());
  ChromeFrameLaunchParams* params =
      new ChromeFrameLaunchParams(empty, empty, profile_path,
                                  profile_path.BaseName().value(), L"", false,
                                  false, false, false);
  params->set_launch_timeout(0);
  params->set_version_check(false);
  return params;
}

TEST_F(ProxyFactoryTest, CreateDestroy) {
  EXPECT_CALL(launch_delegate_mock_,
              LaunchComplete(testing::NotNull(), testing::_)).Times(1);

  scoped_refptr<ChromeFrameLaunchParams> params(
      MakeLaunchParams(L"Adam.N.Epilinter"));

  void* id = NULL;
  proxy_factory_.GetAutomationServer(&launch_delegate_mock_, params, &id);
  proxy_factory_.ReleaseAutomationServer(id, &launch_delegate_mock_);
}

TEST_F(ProxyFactoryTest, CreateSameProfile) {
  LaunchDelegateMock d2;
  EXPECT_CALL(launch_delegate_mock_,
              LaunchComplete(testing::NotNull(), testing::_)).Times(1);
  EXPECT_CALL(d2, LaunchComplete(testing::NotNull(), testing::_)).Times(1);

  scoped_refptr<ChromeFrameLaunchParams> params(
      MakeLaunchParams(L"Dr. Gratiano Forbeson"));

  void* i1 = NULL;
  void* i2 = NULL;

  proxy_factory_.GetAutomationServer(&launch_delegate_mock_, params, &i1);
  proxy_factory_.GetAutomationServer(&d2, params, &i2);

  EXPECT_EQ(i1, i2);
  proxy_factory_.ReleaseAutomationServer(i2, &d2);
  proxy_factory_.ReleaseAutomationServer(i1, &launch_delegate_mock_);
}

TEST_F(ProxyFactoryTest, CreateDifferentProfiles) {
  EXPECT_CALL(launch_delegate_mock_,
              LaunchComplete(testing::NotNull(), testing::_)).Times(2);

  scoped_refptr<ChromeFrameLaunchParams> params1(
      MakeLaunchParams(L"Adam.N.Epilinter"));
  scoped_refptr<ChromeFrameLaunchParams> params2(
      MakeLaunchParams(L"Dr. Gratiano Forbeson"));

  void* i1 = NULL;
  void* i2 = NULL;

  proxy_factory_.GetAutomationServer(&launch_delegate_mock_, params1, &i1);
  proxy_factory_.GetAutomationServer(&launch_delegate_mock_, params2, &i2);

  EXPECT_NE(i1, i2);
  proxy_factory_.ReleaseAutomationServer(i2, &launch_delegate_mock_);
  proxy_factory_.ReleaseAutomationServer(i1, &launch_delegate_mock_);
}

// This test has been disabled because it crashes randomly on the builders.
// http://code.google.com/p/chromium/issues/detail?id=81039
TEST_F(ProxyFactoryTest, DISABLED_FastCreateDestroy) {
  LaunchDelegateMock* d1 = &launch_delegate_mock_;
  LaunchDelegateMock* d2 = new LaunchDelegateMock();

  scoped_refptr<ChromeFrameLaunchParams> params(
      MakeLaunchParams(L"Dr. Gratiano Forbeson"));
  params->set_launch_timeout(10000);

  void* i1 = NULL;
  base::WaitableEvent launched(true, false);
  EXPECT_CALL(*d1, LaunchComplete(testing::NotNull(), AUTOMATION_SUCCESS))
      .WillOnce(testing::InvokeWithoutArgs(&launched,
                                           &base::WaitableEvent::Signal));
  proxy_factory_.GetAutomationServer(d1, params, &i1);
  // Wait for launch
  ASSERT_TRUE(launched.TimedWait(base::TimeDelta::FromSeconds(10)));

  // Expect second launch to succeed too
  EXPECT_CALL(*d2, LaunchComplete(testing::NotNull(), AUTOMATION_SUCCESS))
      .Times(1);

  // Boost thread priority so we call ReleaseAutomationServer before
  // LaunchComplete callback have a chance to be executed.
  ::SetThreadPriority(::GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
  void* i2 = NULL;
  proxy_factory_.GetAutomationServer(d2, params, &i2);
  EXPECT_EQ(i1, i2);
  proxy_factory_.ReleaseAutomationServer(i2, d2);
  delete d2;

  ::SetThreadPriority(::GetCurrentThread(), THREAD_PRIORITY_NORMAL);
  proxy_factory_.ReleaseAutomationServer(i1, d1);
}