app_shim_host_mac_unittest.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright 2013 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 "apps/app_shim/app_shim_host_mac.h"
6
7#include "apps/app_shim/app_shim_messages.h"
8#include "base/memory/scoped_vector.h"
9#include "chrome/test/base/testing_profile.h"
10#include "ipc/ipc_message.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13class TestingAppShimHost : public AppShimHost {
14 public:
15  explicit TestingAppShimHost(Profile* profile);
16  virtual ~TestingAppShimHost() {}
17
18  bool ReceiveMessage(IPC::Message* message);
19
20  const std::vector<IPC::Message*>& sent_messages() {
21    return sent_messages_.get();
22  }
23
24  void set_fails_profile(bool fails_profile) {
25    fails_profile_ = fails_profile;
26  }
27
28  void set_fails_launch(bool fails_launch) {
29    fails_launch_ = fails_launch;
30  }
31
32 protected:
33  virtual Profile* FetchProfileForDirectory(const std::string& profile_dir)
34      OVERRIDE;
35  virtual bool LaunchApp(Profile* profile, const std::string& app_id) OVERRIDE;
36
37  virtual bool Send(IPC::Message* message) OVERRIDE;
38
39  Profile* test_profile_;
40  bool fails_profile_;
41  bool fails_launch_;
42
43  ScopedVector<IPC::Message> sent_messages_;
44
45  DISALLOW_COPY_AND_ASSIGN(TestingAppShimHost);
46};
47
48TestingAppShimHost::TestingAppShimHost(Profile* profile)
49    : test_profile_(profile),
50      fails_profile_(false),
51      fails_launch_(false) {
52}
53
54bool TestingAppShimHost::ReceiveMessage(IPC::Message* message) {
55  bool handled = OnMessageReceived(*message);
56  delete message;
57  return handled;
58}
59
60bool TestingAppShimHost::Send(IPC::Message* message) {
61  sent_messages_.push_back(message);
62  return true;
63}
64
65Profile* TestingAppShimHost::FetchProfileForDirectory(
66    const std::string& profile_dir) {
67  return fails_profile_ ? NULL : test_profile_;
68}
69
70bool TestingAppShimHost::LaunchApp(
71    Profile* profile, const std::string& app_id) {
72  return !fails_launch_;
73}
74
75class AppShimHostTest : public testing::Test {
76 public:
77  TestingAppShimHost* host() { return host_.get(); }
78  TestingProfile* profile() { return profile_.get(); }
79
80  bool LaunchWasSuccessful() {
81    EXPECT_EQ(1u, host()->sent_messages().size());
82    IPC::Message* message = host()->sent_messages()[0];
83    EXPECT_EQ(AppShimMsg_LaunchApp_Done::ID, message->type());
84    AppShimMsg_LaunchApp_Done::Param param;
85    AppShimMsg_LaunchApp_Done::Read(message, &param);
86    return param.a;
87  }
88
89 private:
90  virtual void SetUp() OVERRIDE {
91    profile_.reset(new TestingProfile);
92    host_.reset(new TestingAppShimHost(profile()));
93  }
94
95  scoped_ptr<TestingAppShimHost> host_;
96  scoped_ptr<TestingProfile> profile_;
97};
98
99static const std::string kTestAppId = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
100static const std::string kTestProfileDir = "Default";
101
102TEST_F(AppShimHostTest, TestLaunchApp) {
103  host()->ReceiveMessage(
104      new AppShimHostMsg_LaunchApp(kTestProfileDir, kTestAppId));
105  ASSERT_EQ(kTestAppId, host()->app_id());
106  ASSERT_EQ(profile(), host()->profile());
107  ASSERT_TRUE(LaunchWasSuccessful());
108}
109
110TEST_F(AppShimHostTest, TestFailProfile) {
111  host()->set_fails_profile(true);
112  host()->ReceiveMessage(
113      new AppShimHostMsg_LaunchApp(kTestProfileDir, kTestAppId));
114  ASSERT_TRUE(host()->app_id().empty());
115  ASSERT_FALSE(LaunchWasSuccessful());
116}
117
118TEST_F(AppShimHostTest, TestFailLaunch) {
119  host()->set_fails_launch(true);
120  host()->ReceiveMessage(
121      new AppShimHostMsg_LaunchApp(kTestProfileDir, kTestAppId));
122  ASSERT_TRUE(host()->app_id().empty());
123  ASSERT_FALSE(LaunchWasSuccessful());
124}
125