OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "chrome/browser/ui/app_list/drive/drive_app_converter.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/path_service.h" | |
12 #include "base/version.h" | |
13 #include "chrome/browser/extensions/extension_browsertest.h" | |
14 #include "chrome/browser/extensions/extension_service.h" | |
15 #include "chrome/common/chrome_paths.h" | |
16 #include "chrome/common/extensions/extension_constants.h" | |
17 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" | |
18 #include "content/public/test/test_utils.h" | |
19 #include "extensions/browser/extension_system.h" | |
20 #include "extensions/common/extension.h" | |
21 #include "extensions/common/manifest_handlers/icons_handler.h" | |
22 #include "extensions/common/permissions/permission_set.h" | |
23 #include "extensions/common/permissions/permissions_data.h" | |
24 #include "net/test/embedded_test_server/embedded_test_server.h" | |
25 #include "testing/gtest/include/gtest/gtest.h" | |
26 | |
27 namespace { | |
28 | |
29 const char kAppName[] = "Test drive app"; | |
30 const char kAppUrl[] = "http://foobar.com/drive_app"; | |
31 | |
32 } // namespace | |
33 | |
34 class DriveAppConverterTest : public ExtensionBrowserTest { | |
35 public: | |
36 DriveAppConverterTest() {} | |
37 virtual ~DriveAppConverterTest() {} | |
38 | |
39 // ExtensionBrowserTest: | |
40 virtual void SetUpOnMainThread() OVERRIDE { | |
41 ExtensionBrowserTest::SetUpOnMainThread(); | |
42 | |
43 base::FilePath test_data_dir; | |
44 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir); | |
45 embedded_test_server()->ServeFilesFromDirectory(test_data_dir); | |
46 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
47 } | |
48 | |
49 void InstallAndWaitFinish(const drive::DriveAppInfo& drive_app) { | |
50 runner_ = new content::MessageLoopRunner; | |
51 | |
52 converter_.reset(new DriveAppConverter( | |
53 profile(), | |
54 drive_app, | |
55 base::Bind(&DriveAppConverterTest::ConverterFinished, | |
56 base::Unretained(this)))); | |
57 converter_->Start(); | |
58 | |
59 runner_->Run(); | |
60 } | |
61 | |
62 GURL GetTestUrl(const std::string& path) { | |
63 return embedded_test_server()->base_url().Resolve(path); | |
64 } | |
65 | |
66 drive::DriveAppInfo GetTestDriveApp() { | |
67 // Define four icons. icon1.png is 16x16 and good to use. icon2.png is | |
68 // 16x16 but claims to be 32x32 and should be dropped. icon3.png is 66x66 | |
69 // and not a valid extension icon size and should be dropped too. The forth | |
70 // one is icon2.png with 16x16 but should be ignored because 16x16 already | |
71 // has icon1.png as its resource. | |
72 drive::DriveAppInfo::IconList app_icons; | |
73 app_icons.push_back(std::make_pair(16, GetTestUrl("extensions/icon1.png"))); | |
74 app_icons.push_back(std::make_pair(32, GetTestUrl("extensions/icon2.png"))); | |
75 app_icons.push_back(std::make_pair(66, GetTestUrl("extensions/icon3.png"))); | |
76 app_icons.push_back(std::make_pair(16, GetTestUrl("extensions/icon2.png"))); | |
77 | |
78 drive::DriveAppInfo::IconList document_icons; | |
79 | |
80 return drive::DriveAppInfo("fake_drive_app_id", | |
81 "fake_product_id", | |
82 app_icons, | |
83 document_icons, | |
84 kAppName, | |
85 GURL(kAppUrl), | |
86 true); | |
87 } | |
88 | |
89 const DriveAppConverter* converter() const { return converter_.get(); } | |
90 | |
91 private: | |
92 void ConverterFinished(const DriveAppConverter* converter, bool success) { | |
93 if (runner_) | |
94 runner_->Quit(); | |
95 } | |
96 | |
97 scoped_ptr<DriveAppConverter> converter_; | |
98 scoped_refptr<content::MessageLoopRunner> runner_; | |
99 | |
100 DISALLOW_COPY_AND_ASSIGN(DriveAppConverterTest); | |
101 }; | |
102 | |
103 IN_PROC_BROWSER_TEST_F(DriveAppConverterTest, GoodApp) { | |
104 InstallAndWaitFinish(GetTestDriveApp()); | |
105 | |
106 const extensions::Extension* app = converter()->app(); | |
107 ASSERT_TRUE(app != NULL); | |
108 EXPECT_EQ(kAppName, app->name()); | |
109 EXPECT_TRUE(app->is_hosted_app()); | |
110 EXPECT_TRUE(app->from_bookmark()); | |
111 EXPECT_EQ(GURL(kAppUrl), extensions::AppLaunchInfo::GetLaunchWebURL(app)); | |
112 EXPECT_EQ(extensions::LAUNCH_CONTAINER_TAB, | |
113 extensions::AppLaunchInfo::GetLaunchContainer(app)); | |
114 EXPECT_EQ(0u, app->permissions_data()->active_permissions()->apis().size()); | |
115 EXPECT_EQ(1u, extensions::IconsInfo::GetIcons(app).map().size()); | |
116 | |
117 const extensions::Extension* installed = | |
118 extensions::ExtensionSystem::Get(profile()) | |
119 ->extension_service() | |
120 ->GetInstalledExtension(app->id()); | |
121 EXPECT_EQ(app, installed); | |
122 } | |
123 | |
124 IN_PROC_BROWSER_TEST_F(DriveAppConverterTest, BadApp) { | |
125 drive::DriveAppInfo no_name = GetTestDriveApp(); | |
126 no_name.app_name.clear(); | |
127 InstallAndWaitFinish(no_name); | |
128 EXPECT_TRUE(converter()->app() == NULL); | |
129 | |
130 drive::DriveAppInfo no_url = GetTestDriveApp(); | |
131 no_url.create_url = GURL(); | |
132 InstallAndWaitFinish(no_url); | |
133 EXPECT_TRUE(converter()->app() == NULL); | |
134 } | |
135 | |
136 IN_PROC_BROWSER_TEST_F(DriveAppConverterTest, InstallTwice) { | |
137 InstallAndWaitFinish(GetTestDriveApp()); | |
138 const extensions::Extension* first_install = converter()->app(); | |
139 ASSERT_TRUE(first_install != NULL); | |
140 const std::string first_install_id = first_install->id(); | |
141 const base::Version first_install_version(first_install->VersionString()); | |
142 ASSERT_TRUE(first_install_version.IsValid()); | |
143 | |
144 InstallAndWaitFinish(GetTestDriveApp()); | |
145 const extensions::Extension* second_install = converter()->app(); | |
146 ASSERT_TRUE(second_install != NULL); | |
147 | |
148 // Two different app instances. | |
149 ASSERT_NE(first_install, second_install); | |
150 EXPECT_EQ(first_install_id, second_install->id()); | |
151 EXPECT_GE(second_install->version()->CompareTo(first_install_version), 0); | |
152 } | |
OLD | NEW |