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

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: Missed a file 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
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), close_count_(0), focus_count_(0) {} 72 AppShimHostTest() : fail_launch_(false),
73 launch_count_(0),
74 launch_now_count_(0),
75 close_count_(0),
76 focus_count_(0) {}
76 77
77 TestingAppShimHost* host() { return host_.get(); } 78 TestingAppShimHost* host() { return host_.get(); }
78 TestingProfile* profile() { return profile_.get(); } 79 TestingProfile* profile() { return profile_.get(); }
79 80
81 void LaunchApp(bool launch_now) {
82 EXPECT_TRUE(host()->ReceiveMessage(new AppShimHostMsg_LaunchApp(
83 base::FilePath(kTestProfileDir), kTestAppId,
84 launch_now ? apps::LAUNCH_NOW : apps::REGISTER_WITHOUT_LAUNCH)));
85 }
86
80 bool LaunchWasSuccessful() { 87 bool LaunchWasSuccessful() {
81 EXPECT_EQ(1u, host()->sent_messages().size()); 88 EXPECT_EQ(1u, host()->sent_messages().size());
82 IPC::Message* message = host()->sent_messages()[0]; 89 IPC::Message* message = host()->sent_messages()[0];
83 EXPECT_EQ(AppShimMsg_LaunchApp_Done::ID, message->type()); 90 EXPECT_EQ(AppShimMsg_LaunchApp_Done::ID, message->type());
84 AppShimMsg_LaunchApp_Done::Param param; 91 AppShimMsg_LaunchApp_Done::Param param;
85 AppShimMsg_LaunchApp_Done::Read(message, &param); 92 AppShimMsg_LaunchApp_Done::Read(message, &param);
86 return param.a; 93 return param.a;
87 } 94 }
88 95
89 void SimulateDisconnect() { 96 void SimulateDisconnect() {
90 implicit_cast<IPC::Listener*>(host_.release())->OnChannelError(); 97 implicit_cast<IPC::Listener*>(host_.release())->OnChannelError();
91 } 98 }
92 99
93 protected: 100 protected:
94 virtual bool OnShimLaunch(Host* host) OVERRIDE { 101 virtual bool OnShimLaunch(Host* host,
102 apps::AppShimLaunchType launch_type) OVERRIDE {
95 ++launch_count_; 103 ++launch_count_;
96 return true; 104 if (launch_type == apps::LAUNCH_NOW)
105 ++launch_now_count_;
106 return !fail_launch_;
97 } 107 }
98 108
99 virtual void OnShimClose(Host* host) OVERRIDE { ++close_count_; } 109 virtual void OnShimClose(Host* host) OVERRIDE { ++close_count_; }
100 virtual void OnShimFocus(Host* host) OVERRIDE { ++focus_count_; } 110 virtual void OnShimFocus(Host* host) OVERRIDE { ++focus_count_; }
101 111
112 bool fail_launch_;
102 int launch_count_; 113 int launch_count_;
114 int launch_now_count_;
103 int close_count_; 115 int close_count_;
104 int focus_count_; 116 int focus_count_;
105 117
106 private: 118 private:
107 virtual void SetUp() OVERRIDE { 119 virtual void SetUp() OVERRIDE {
108 testing::Test::SetUp(); 120 testing::Test::SetUp();
109 profile_.reset(new TestingProfile); 121 profile_.reset(new TestingProfile);
110 host_.reset(new TestingAppShimHost(profile())); 122 host_.reset(new TestingAppShimHost(profile()));
111 } 123 }
112 124
113 scoped_ptr<TestingAppShimHost> host_; 125 scoped_ptr<TestingAppShimHost> host_;
114 scoped_ptr<TestingProfile> profile_; 126 scoped_ptr<TestingProfile> profile_;
115 127
116 DISALLOW_COPY_AND_ASSIGN(AppShimHostTest); 128 DISALLOW_COPY_AND_ASSIGN(AppShimHostTest);
117 }; 129 };
118 130
119 const char kTestAppId[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
120 const char kTestProfileDir[] = "Default";
121 131
122 } // namespace 132 } // namespace
123 133
124 TEST_F(AppShimHostTest, TestLaunchAppWithHandler) { 134 TEST_F(AppShimHostTest, TestLaunchAppWithHandler) {
125 apps::AppShimHandler::RegisterHandler(kTestAppId, this); 135 apps::AppShimHandler::RegisterHandler(kTestAppId, this);
126 EXPECT_TRUE(host()->ReceiveMessage( 136 LaunchApp(true);
127 new AppShimHostMsg_LaunchApp(kTestProfileDir, kTestAppId)));
128 EXPECT_EQ(kTestAppId, 137 EXPECT_EQ(kTestAppId,
129 implicit_cast<apps::AppShimHandler::Host*>(host())->GetAppId()); 138 implicit_cast<apps::AppShimHandler::Host*>(host())->GetAppId());
130 EXPECT_TRUE(LaunchWasSuccessful()); 139 EXPECT_TRUE(LaunchWasSuccessful());
131 EXPECT_EQ(1, launch_count_); 140 EXPECT_EQ(1, launch_count_);
141 EXPECT_EQ(1, launch_now_count_);
132 EXPECT_EQ(0, focus_count_); 142 EXPECT_EQ(0, focus_count_);
133 EXPECT_EQ(0, close_count_); 143 EXPECT_EQ(0, close_count_);
134 144
135 EXPECT_TRUE(host()->ReceiveMessage(new AppShimHostMsg_FocusApp())); 145 EXPECT_TRUE(host()->ReceiveMessage(new AppShimHostMsg_FocusApp()));
136 EXPECT_EQ(1, focus_count_); 146 EXPECT_EQ(1, focus_count_);
137 147
138 SimulateDisconnect(); 148 SimulateDisconnect();
139 EXPECT_EQ(1, close_count_); 149 EXPECT_EQ(1, close_count_);
140 apps::AppShimHandler::RemoveHandler(kTestAppId); 150 apps::AppShimHandler::RemoveHandler(kTestAppId);
141 } 151 }
142 152
153 TEST_F(AppShimHostTest, TestNoLaunchNow) {
154 apps::AppShimHandler::RegisterHandler(kTestAppId, this);
155 LaunchApp(false);
156 EXPECT_EQ(kTestAppId,
157 implicit_cast<apps::AppShimHandler::Host*>(host())->GetAppId());
158 EXPECT_TRUE(LaunchWasSuccessful());
159 EXPECT_EQ(1, launch_count_);
160 EXPECT_EQ(0, launch_now_count_);
161 EXPECT_EQ(0, focus_count_);
162 EXPECT_EQ(0, close_count_);
163 apps::AppShimHandler::RemoveHandler(kTestAppId);
164 }
165
143 TEST_F(AppShimHostTest, TestFailProfile) { 166 TEST_F(AppShimHostTest, TestFailProfile) {
144 host()->set_fails_profile(true); 167 host()->set_fails_profile(true);
145 host()->ReceiveMessage( 168 LaunchApp(true);
146 new AppShimHostMsg_LaunchApp(kTestProfileDir, kTestAppId));
147 ASSERT_FALSE(LaunchWasSuccessful()); 169 ASSERT_FALSE(LaunchWasSuccessful());
148 } 170 }
149 171
150 TEST_F(AppShimHostTest, TestFailLaunch) { 172 TEST_F(AppShimHostTest, TestFailLaunch) {
151 host()->set_fails_launch(true); 173 apps::AppShimHandler::RegisterHandler(kTestAppId, this);
152 host()->ReceiveMessage( 174 fail_launch_ = true;
153 new AppShimHostMsg_LaunchApp(kTestProfileDir, kTestAppId)); 175 LaunchApp(true);
154 ASSERT_FALSE(LaunchWasSuccessful()); 176 ASSERT_FALSE(LaunchWasSuccessful());
177 apps::AppShimHandler::RemoveHandler(kTestAppId);
155 } 178 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698