File: existing_comparison_table_sub_menu_model_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (176 lines) | stat: -rw-r--r-- 6,870 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/tabs/existing_comparison_table_sub_menu_model.h"

#include <memory>

#include "base/test/scoped_feature_list.h"
#include "chrome/browser/ui/tabs/tab_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/tabs/test_tab_strip_model_delegate.h"
#include "chrome/browser/ui/tabs/test_util.h"
#include "chrome/test/base/testing_profile.h"
#include "components/commerce/core/commerce_feature_list.h"
#include "components/commerce/core/commerce_utils.h"
#include "components/commerce/core/product_specifications/mock_product_specifications_service.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace commerce {
namespace {

const char kTestUrl1[] = "https://example1.com";
const char kTestUrl2[] = "https://example2.com";
const char kTestUrl3[] = "https://example3.com";

// One item for "New comparison table", one for the separator, and two for the
// existing tables.
const int kFullItemCount = 4;

}  // namespace

class ExistingComparisonTableSubMenuModelTest : public testing::Test {
 public:
  ExistingComparisonTableSubMenuModelTest() = default;

  void SetUp() override {
    features_.InitAndEnableFeature(commerce::kProductSpecifications);
    profile_ = std::make_unique<TestingProfile>();
    delegate_ = std::make_unique<TestTabStripModelDelegate>();
    tab_strip_model_ =
        std::make_unique<TabStripModel>(delegate_.get(), profile_.get());
    product_specifications_service_ =
        std::make_unique<MockProductSpecificationsService>();
    product_specifications_data_ = std::vector<ProductSpecificationsSet>{
        ProductSpecificationsSet(
            base::Uuid::GenerateRandomV4().AsLowercaseString(), 0, 0,
            {
                GURL(kTestUrl1),
            },
            "Set 1"),
        ProductSpecificationsSet(
            base::Uuid::GenerateRandomV4().AsLowercaseString(), 0, 0,
            {
                GURL(kTestUrl1),
                GURL(kTestUrl2),
            },
            "Set 2")};

    ON_CALL(*product_specifications_service_, GetAllProductSpecifications())
        .WillByDefault(testing::Return(product_specifications_data_));
  }

  void AddTab(GURL url) {
    std::unique_ptr<content::WebContents> web_contents =
        content::WebContentsTester::CreateTestWebContents(profile_.get(),
                                                          nullptr);
    content::WebContentsTester::For(web_contents.get())
        ->SetLastCommittedURL(url);

    tab_strip_model()->AppendTab(
        std::make_unique<tabs::TabModel>(std::move(web_contents),
                                         tab_strip_model()),
        /*foreground=*/true);
  }

  TabStripModel* tab_strip_model() { return tab_strip_model_.get(); }
  MockProductSpecificationsService* product_specifications_service() {
    return product_specifications_service_.get();
  }
  std::vector<ProductSpecificationsSet> product_specifications_data() {
    return product_specifications_data_;
  }

 private:
  content::BrowserTaskEnvironment task_environment;
  content::RenderViewHostTestEnabler rvh_test_enabler_;
  tabs::PreventTabFeatureInitialization prevent_;
  base::test::ScopedFeatureList features_;
  std::unique_ptr<TestingProfile> profile_;
  std::unique_ptr<TestTabStripModelDelegate> delegate_;
  std::unique_ptr<TabStripModel> tab_strip_model_;
  std::unique_ptr<MockProductSpecificationsService>
      product_specifications_service_;
  std::vector<ProductSpecificationsSet> product_specifications_data_;
};

TEST_F(ExistingComparisonTableSubMenuModelTest, ShouldShowSubmenu_NoSets) {
  ON_CALL(*product_specifications_service(), GetAllProductSpecifications())
      .WillByDefault(testing::Return(std::vector<ProductSpecificationsSet>()));

  ASSERT_FALSE(ExistingComparisonTableSubMenuModel::ShouldShowSubmenu(
      GURL(kTestUrl3), product_specifications_service()));
}

TEST_F(ExistingComparisonTableSubMenuModelTest,
       ShouldShowSubmenu_NoSetsContainUrl) {
  ASSERT_TRUE(ExistingComparisonTableSubMenuModel::ShouldShowSubmenu(
      GURL(kTestUrl3), product_specifications_service()));
}

TEST_F(ExistingComparisonTableSubMenuModelTest,
       ShouldShowSubmenu_SomeSetsContainUrl) {
  ASSERT_TRUE(ExistingComparisonTableSubMenuModel::ShouldShowSubmenu(
      GURL(kTestUrl2), product_specifications_service()));
}

TEST_F(ExistingComparisonTableSubMenuModelTest,
       ShouldShowSubmenu_AllSetsContainUrl) {
  ASSERT_FALSE(ExistingComparisonTableSubMenuModel::ShouldShowSubmenu(
      GURL(kTestUrl1), product_specifications_service()));
}

TEST_F(ExistingComparisonTableSubMenuModelTest, BuildMenuItems_MultipleSets) {
  AddTab(GURL(kTestUrl3));
  tab_strip_model()->SelectTabAt(0);

  ExistingComparisonTableSubMenuModel sub_menu_model(
      nullptr, nullptr, tab_strip_model(), 0, product_specifications_service());

  ASSERT_TRUE(sub_menu_model.GetItemCount() == kFullItemCount);
  ASSERT_TRUE(sub_menu_model.GetLabelAt(2) == u"Set 1");
  ASSERT_TRUE(sub_menu_model.GetLabelAt(3) == u"Set 2");
}

TEST_F(ExistingComparisonTableSubMenuModelTest,
       BuildMenuItems_SomeSetsContainUrl) {
  AddTab(GURL(kTestUrl2));
  tab_strip_model()->SelectTabAt(0);

  ExistingComparisonTableSubMenuModel sub_menu_model(
      nullptr, nullptr, tab_strip_model(), 0, product_specifications_service());

  // New item, separator, and one table item.
  ASSERT_TRUE(sub_menu_model.GetItemCount() == kFullItemCount - 1);
  ASSERT_TRUE(sub_menu_model.GetLabelAt(2) == u"Set 1");
}

TEST_F(ExistingComparisonTableSubMenuModelTest, UrlAddedToExistingSet) {
  AddTab(GURL(kTestUrl3));
  tab_strip_model()->SelectTabAt(0);

  const auto& title = tab_strip_model()->GetWebContentsAt(0)->GetTitle();

  ExistingComparisonTableSubMenuModel sub_menu_model(
      nullptr, nullptr, tab_strip_model(), 0, product_specifications_service());
  ASSERT_TRUE(sub_menu_model.GetItemCount() == kFullItemCount);

  ON_CALL(*product_specifications_service(),
          GetSetByUuid(product_specifications_data()[0].uuid()))
      .WillByDefault(testing::Return(product_specifications_data()[0]));
  EXPECT_CALL(*product_specifications_service(),
              SetUrls(product_specifications_data()[0].uuid(),
                      testing::ElementsAre(UrlInfo(GURL(kTestUrl1), u""),
                                           UrlInfo(GURL(kTestUrl3), title))))
      .Times(1);

  sub_menu_model.ExecuteCommand(sub_menu_model.GetCommandIdAt(2), 0);
}

}  // namespace commerce