app_search_provider_unittest.cc revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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 <string>
6
7#include "base/basictypes.h"
8#include "base/memory/scoped_ptr.h"
9#include "base/run_loop.h"
10#include "base/strings/utf_string_conversions.h"
11#include "chrome/browser/extensions/extension_service.h"
12#include "chrome/browser/ui/app_list/app_list_test_util.h"
13#include "chrome/browser/ui/app_list/search/app_search_provider.h"
14#include "chrome/browser/ui/app_list/search/chrome_search_result.h"
15#include "chrome/common/chrome_constants.h"
16#include "chrome/test/base/testing_profile.h"
17#include "extensions/common/extension_set.h"
18#include "testing/gtest/include/gtest/gtest.h"
19
20namespace app_list {
21namespace test {
22
23class AppSearchProviderTest : public AppListTestBase {
24 public:
25  AppSearchProviderTest() {}
26  virtual ~AppSearchProviderTest() {}
27
28  // AppListTestBase overrides:
29  virtual void SetUp() OVERRIDE {
30    AppListTestBase::SetUp();
31
32    app_search_.reset(new AppSearchProvider(profile_.get(), NULL));
33  }
34
35  std::string RunQuery(const std::string& query) {
36    app_search_->Start(base::UTF8ToUTF16(query));
37    app_search_->Stop();
38
39    std::string result_str;
40    const SearchProvider::Results& results = app_search_->results();
41    for (size_t i = 0; i < results.size(); ++i) {
42      if (!result_str.empty())
43        result_str += ',';
44
45      result_str += base::UTF16ToUTF8(results[i]->title());
46    }
47    return result_str;
48  }
49
50 private:
51  scoped_ptr<AppSearchProvider> app_search_;
52
53  DISALLOW_COPY_AND_ASSIGN(AppSearchProviderTest);
54};
55
56TEST_F(AppSearchProviderTest, Basic) {
57  EXPECT_EQ("", RunQuery(""));
58  EXPECT_EQ("", RunQuery("!@#$-,-_"));
59  EXPECT_EQ("", RunQuery("unmatched query"));
60
61  // Search for "pa" should return both packaged app. The order is undefined
62  // because the test only considers textual relevance and the two apps end
63  // up having the same score.
64  const std::string result = RunQuery("pa");
65  EXPECT_TRUE(result == "Packaged App 1,Packaged App 2" ||
66              result == "Packaged App 2,Packaged App 1");
67
68  EXPECT_EQ("Packaged App 1", RunQuery("pa1"));
69  EXPECT_EQ("Packaged App 2", RunQuery("pa2"));
70  EXPECT_EQ("Packaged App 1", RunQuery("app1"));
71  EXPECT_EQ("Hosted App", RunQuery("host"));
72}
73
74TEST_F(AppSearchProviderTest, DisableAndEnable) {
75  EXPECT_EQ("Hosted App", RunQuery("host"));
76
77  service_->DisableExtension(kHostedAppId,
78                             extensions::Extension::DISABLE_NONE);
79  EXPECT_EQ("Hosted App", RunQuery("host"));
80
81  service_->EnableExtension(kHostedAppId);
82  EXPECT_EQ("Hosted App", RunQuery("host"));
83}
84
85TEST_F(AppSearchProviderTest, Uninstall) {
86  EXPECT_EQ("Packaged App 1", RunQuery("pa1"));
87  service_->UninstallExtension(kPackagedApp1Id, false, NULL);
88  EXPECT_EQ("", RunQuery("pa1"));
89
90  // Let uninstall code to clean up.
91  base::RunLoop().RunUntilIdle();
92}
93
94}  // namespace test
95}  // namespace app_list
96