Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: apps/app_shim/app_shim_host_mac_unittest.cc

Issue 14579006: Start app shim when app launched. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comment Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « apps/app_shim/app_shim_host_mac.cc ('k') | apps/app_shim/app_shim_launch.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "apps/app_shim/app_shim_host_mac.h" 5 #include "apps/app_shim/app_shim_host_mac.h"
6 6
7 #include "apps/app_shim/app_shim_messages.h" 7 #include "apps/app_shim/app_shim_messages.h"
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/memory/scoped_vector.h" 9 #include "base/memory/scoped_vector.h"
10 #include "chrome/test/base/testing_profile.h" 10 #include "chrome/test/base/testing_profile.h"
(...skipping 10 matching lines...) Expand all
21 bool ReceiveMessage(IPC::Message* message); 21 bool ReceiveMessage(IPC::Message* message);
22 22
23 const std::vector<IPC::Message*>& sent_messages() { 23 const std::vector<IPC::Message*>& sent_messages() {
24 return sent_messages_.get(); 24 return sent_messages_.get();
25 } 25 }
26 26
27 void set_fails_profile(bool fails_profile) { 27 void set_fails_profile(bool fails_profile) {
28 fails_profile_ = fails_profile; 28 fails_profile_ = fails_profile;
29 } 29 }
30 30
31 void set_fails_launch(bool fails_launch) {
32 fails_launch_ = fails_launch;
33 }
34
35 protected: 31 protected:
36 virtual Profile* FetchProfileForDirectory(const std::string& profile_dir) 32 virtual Profile* FetchProfileForDirectory(const base::FilePath& profile_dir)
37 OVERRIDE; 33 OVERRIDE;
38 virtual bool Send(IPC::Message* message) OVERRIDE; 34 virtual bool Send(IPC::Message* message) OVERRIDE;
39 35
40 private: 36 private:
41 Profile* test_profile_; 37 Profile* test_profile_;
42 bool fails_profile_; 38 bool fails_profile_;
43 bool fails_launch_;
44 39
45 ScopedVector<IPC::Message> sent_messages_; 40 ScopedVector<IPC::Message> sent_messages_;
46 41
47 DISALLOW_COPY_AND_ASSIGN(TestingAppShimHost); 42 DISALLOW_COPY_AND_ASSIGN(TestingAppShimHost);
48 }; 43 };
49 44
50 TestingAppShimHost::TestingAppShimHost(Profile* profile) 45 TestingAppShimHost::TestingAppShimHost(Profile* profile)
51 : test_profile_(profile), 46 : test_profile_(profile),
52 fails_profile_(false), 47 fails_profile_(false) {
53 fails_launch_(false) {
54 } 48 }
55 49
56 bool TestingAppShimHost::ReceiveMessage(IPC::Message* message) { 50 bool TestingAppShimHost::ReceiveMessage(IPC::Message* message) {
57 bool handled = OnMessageReceived(*message); 51 bool handled = OnMessageReceived(*message);
58 delete message; 52 delete message;
59 return handled; 53 return handled;
60 } 54 }
61 55
62 bool TestingAppShimHost::Send(IPC::Message* message) { 56 bool TestingAppShimHost::Send(IPC::Message* message) {
63 sent_messages_.push_back(message); 57 sent_messages_.push_back(message);
64 return true; 58 return true;
65 } 59 }
66 60
67 Profile* TestingAppShimHost::FetchProfileForDirectory( 61 Profile* TestingAppShimHost::FetchProfileForDirectory(
68 const std::string& profile_dir) { 62 const base::FilePath& profile_dir) {
69 return fails_profile_ ? NULL : test_profile_; 63 return fails_profile_ ? NULL : test_profile_;
70 } 64 }
71 65
66 const char kTestAppId[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
67 const char kTestProfileDir[] = "Default";
68
72 class AppShimHostTest : public testing::Test, 69 class AppShimHostTest : public testing::Test,
73 public apps::AppShimHandler { 70 public apps::AppShimHandler {
74 public: 71 public:
75 AppShimHostTest() : launch_count_(0), 72 AppShimHostTest() : fail_launch_(false),
73 launch_count_(0),
74 launch_now_count_(0),
76 close_count_(0), 75 close_count_(0),
77 focus_count_(0), 76 focus_count_(0),
78 quit_count_(0) {} 77 quit_count_(0) {}
79 78
80 TestingAppShimHost* host() { return host_.get(); } 79 TestingAppShimHost* host() { return host_.get(); }
81 TestingProfile* profile() { return profile_.get(); } 80 TestingProfile* profile() { return profile_.get(); }
82 81
82 void LaunchApp(bool launch_now) {
83 EXPECT_TRUE(host()->ReceiveMessage(new AppShimHostMsg_LaunchApp(
84 base::FilePath(kTestProfileDir), kTestAppId,
85 launch_now ? apps::APP_SHIM_LAUNCH_NORMAL :
86 apps::APP_SHIM_LAUNCH_REGISTER_ONLY)));
87 }
88
83 bool LaunchWasSuccessful() { 89 bool LaunchWasSuccessful() {
84 EXPECT_EQ(1u, host()->sent_messages().size()); 90 EXPECT_EQ(1u, host()->sent_messages().size());
85 IPC::Message* message = host()->sent_messages()[0]; 91 IPC::Message* message = host()->sent_messages()[0];
86 EXPECT_EQ(AppShimMsg_LaunchApp_Done::ID, message->type()); 92 EXPECT_EQ(AppShimMsg_LaunchApp_Done::ID, message->type());
87 AppShimMsg_LaunchApp_Done::Param param; 93 AppShimMsg_LaunchApp_Done::Param param;
88 AppShimMsg_LaunchApp_Done::Read(message, &param); 94 AppShimMsg_LaunchApp_Done::Read(message, &param);
89 return param.a; 95 return param.a;
90 } 96 }
91 97
92 void SimulateDisconnect() { 98 void SimulateDisconnect() {
93 implicit_cast<IPC::Listener*>(host_.release())->OnChannelError(); 99 implicit_cast<IPC::Listener*>(host_.release())->OnChannelError();
94 } 100 }
95 101
96 protected: 102 protected:
97 virtual bool OnShimLaunch(Host* host) OVERRIDE { 103 virtual bool OnShimLaunch(Host* host,
104 apps::AppShimLaunchType launch_type) OVERRIDE {
98 ++launch_count_; 105 ++launch_count_;
99 return true; 106 if (launch_type == apps::APP_SHIM_LAUNCH_NORMAL)
107 ++launch_now_count_;
108 return !fail_launch_;
100 } 109 }
101 110
102 virtual void OnShimClose(Host* host) OVERRIDE { ++close_count_; } 111 virtual void OnShimClose(Host* host) OVERRIDE { ++close_count_; }
103 virtual void OnShimFocus(Host* host) OVERRIDE { ++focus_count_; } 112 virtual void OnShimFocus(Host* host) OVERRIDE { ++focus_count_; }
104 virtual void OnShimQuit(Host* host) OVERRIDE { ++quit_count_; } 113 virtual void OnShimQuit(Host* host) OVERRIDE { ++quit_count_; }
105 114
115 bool fail_launch_;
106 int launch_count_; 116 int launch_count_;
117 int launch_now_count_;
107 int close_count_; 118 int close_count_;
108 int focus_count_; 119 int focus_count_;
109 int quit_count_; 120 int quit_count_;
110 121
111 private: 122 private:
112 virtual void SetUp() OVERRIDE { 123 virtual void SetUp() OVERRIDE {
113 testing::Test::SetUp(); 124 testing::Test::SetUp();
114 profile_.reset(new TestingProfile); 125 profile_.reset(new TestingProfile);
115 host_.reset(new TestingAppShimHost(profile())); 126 host_.reset(new TestingAppShimHost(profile()));
116 } 127 }
117 128
118 scoped_ptr<TestingAppShimHost> host_; 129 scoped_ptr<TestingAppShimHost> host_;
119 scoped_ptr<TestingProfile> profile_; 130 scoped_ptr<TestingProfile> profile_;
120 131
121 DISALLOW_COPY_AND_ASSIGN(AppShimHostTest); 132 DISALLOW_COPY_AND_ASSIGN(AppShimHostTest);
122 }; 133 };
123 134
124 const char kTestAppId[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
125 const char kTestProfileDir[] = "Default";
126 135
127 } // namespace 136 } // namespace
128 137
129 TEST_F(AppShimHostTest, TestLaunchAppWithHandler) { 138 TEST_F(AppShimHostTest, TestLaunchAppWithHandler) {
130 apps::AppShimHandler::RegisterHandler(kTestAppId, this); 139 apps::AppShimHandler::RegisterHandler(kTestAppId, this);
131 EXPECT_TRUE(host()->ReceiveMessage( 140 LaunchApp(true);
132 new AppShimHostMsg_LaunchApp(kTestProfileDir, kTestAppId)));
133 EXPECT_EQ(kTestAppId, 141 EXPECT_EQ(kTestAppId,
134 implicit_cast<apps::AppShimHandler::Host*>(host())->GetAppId()); 142 implicit_cast<apps::AppShimHandler::Host*>(host())->GetAppId());
135 EXPECT_TRUE(LaunchWasSuccessful()); 143 EXPECT_TRUE(LaunchWasSuccessful());
136 EXPECT_EQ(1, launch_count_); 144 EXPECT_EQ(1, launch_count_);
145 EXPECT_EQ(1, launch_now_count_);
137 EXPECT_EQ(0, focus_count_); 146 EXPECT_EQ(0, focus_count_);
138 EXPECT_EQ(0, close_count_); 147 EXPECT_EQ(0, close_count_);
139 148
140 EXPECT_TRUE(host()->ReceiveMessage(new AppShimHostMsg_FocusApp())); 149 EXPECT_TRUE(host()->ReceiveMessage(new AppShimHostMsg_FocusApp()));
141 EXPECT_EQ(1, focus_count_); 150 EXPECT_EQ(1, focus_count_);
142 151
143 EXPECT_TRUE(host()->ReceiveMessage(new AppShimHostMsg_QuitApp())); 152 EXPECT_TRUE(host()->ReceiveMessage(new AppShimHostMsg_QuitApp()));
144 EXPECT_EQ(1, quit_count_); 153 EXPECT_EQ(1, quit_count_);
145 154
146 SimulateDisconnect(); 155 SimulateDisconnect();
147 EXPECT_EQ(1, close_count_); 156 EXPECT_EQ(1, close_count_);
148 apps::AppShimHandler::RemoveHandler(kTestAppId); 157 apps::AppShimHandler::RemoveHandler(kTestAppId);
149 } 158 }
150 159
160 TEST_F(AppShimHostTest, TestNoLaunchNow) {
161 apps::AppShimHandler::RegisterHandler(kTestAppId, this);
162 LaunchApp(false);
163 EXPECT_EQ(kTestAppId,
164 implicit_cast<apps::AppShimHandler::Host*>(host())->GetAppId());
165 EXPECT_TRUE(LaunchWasSuccessful());
166 EXPECT_EQ(1, launch_count_);
167 EXPECT_EQ(0, launch_now_count_);
168 EXPECT_EQ(0, focus_count_);
169 EXPECT_EQ(0, close_count_);
170 apps::AppShimHandler::RemoveHandler(kTestAppId);
171 }
172
151 TEST_F(AppShimHostTest, TestFailProfile) { 173 TEST_F(AppShimHostTest, TestFailProfile) {
152 host()->set_fails_profile(true); 174 host()->set_fails_profile(true);
153 host()->ReceiveMessage( 175 LaunchApp(true);
154 new AppShimHostMsg_LaunchApp(kTestProfileDir, kTestAppId));
155 ASSERT_FALSE(LaunchWasSuccessful()); 176 ASSERT_FALSE(LaunchWasSuccessful());
156 } 177 }
157 178
158 TEST_F(AppShimHostTest, TestFailLaunch) { 179 TEST_F(AppShimHostTest, TestFailLaunch) {
159 host()->set_fails_launch(true); 180 apps::AppShimHandler::RegisterHandler(kTestAppId, this);
160 host()->ReceiveMessage( 181 fail_launch_ = true;
161 new AppShimHostMsg_LaunchApp(kTestProfileDir, kTestAppId)); 182 LaunchApp(true);
162 ASSERT_FALSE(LaunchWasSuccessful()); 183 ASSERT_FALSE(LaunchWasSuccessful());
184 apps::AppShimHandler::RemoveHandler(kTestAppId);
163 } 185 }
OLDNEW
« no previous file with comments | « apps/app_shim/app_shim_host_mac.cc ('k') | apps/app_shim/app_shim_launch.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698